1 | // Copyright 2014 The Flutter Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. |
4 | |
5 | import 'package:flutter/foundation.dart'; |
6 | import 'package:flutter/rendering.dart'; |
7 | import 'package:flutter_test/flutter_test.dart'; |
8 | import 'package:vector_math/vector_math_64.dart' ; |
9 | |
10 | import 'rendering_tester.dart'; |
11 | |
12 | void main() { |
13 | TestRenderingFlutterBinding.ensureInitialized(); |
14 | |
15 | test('Describe transform control test' , () { |
16 | final Matrix4 identity = Matrix4.identity(); |
17 | final List<String> description = debugDescribeTransform(identity); |
18 | expect(description, <String>[ |
19 | '[0] 1.0,0.0,0.0,0.0' , |
20 | '[1] 0.0,1.0,0.0,0.0' , |
21 | '[2] 0.0,0.0,1.0,0.0' , |
22 | '[3] 0.0,0.0,0.0,1.0' , |
23 | ]); |
24 | }); |
25 | |
26 | test('transform property test' , () { |
27 | final Matrix4 transform = Matrix4.diagonal3(Vector3.all(2.0)); |
28 | final TransformProperty simple = TransformProperty('transform' , transform); |
29 | expect(simple.name, equals('transform' )); |
30 | expect(simple.value, same(transform)); |
31 | expect( |
32 | simple.toString(parentConfiguration: sparseTextConfiguration), |
33 | equals( |
34 | 'transform:\n' |
35 | ' [0] 2.0,0.0,0.0,0.0\n' |
36 | ' [1] 0.0,2.0,0.0,0.0\n' |
37 | ' [2] 0.0,0.0,2.0,0.0\n' |
38 | ' [3] 0.0,0.0,0.0,1.0' , |
39 | ), |
40 | ); |
41 | expect( |
42 | simple.toString(parentConfiguration: singleLineTextConfiguration), |
43 | equals('transform: [2.0,0.0,0.0,0.0; 0.0,2.0,0.0,0.0; 0.0,0.0,2.0,0.0; 0.0,0.0,0.0,1.0]' ), |
44 | ); |
45 | |
46 | final TransformProperty nullProperty = TransformProperty('transform' , null); |
47 | expect(nullProperty.name, equals('transform' )); |
48 | expect(nullProperty.value, isNull); |
49 | expect(nullProperty.toString(), equals('transform: null' )); |
50 | |
51 | final TransformProperty hideNull = TransformProperty('transform' , null, defaultValue: null); |
52 | expect(hideNull.value, isNull); |
53 | expect(hideNull.toString(), equals('transform: null' )); |
54 | }); |
55 | |
56 | test('debugPaintPadding' , () { |
57 | expect((Canvas canvas) { |
58 | debugPaintPadding(canvas, const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), null); |
59 | }, paints..rect(color: const Color(0x90909090))); |
60 | expect( |
61 | (Canvas canvas) { |
62 | debugPaintPadding( |
63 | canvas, |
64 | const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), |
65 | const Rect.fromLTRB(11.0, 11.0, 19.0, 19.0), |
66 | ); |
67 | }, |
68 | paints |
69 | ..path(color: const Color(0x900090FF)) |
70 | ..path(color: const Color(0xFF0090FF)), |
71 | ); |
72 | expect( |
73 | (Canvas canvas) { |
74 | debugPaintPadding( |
75 | canvas, |
76 | const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), |
77 | const Rect.fromLTRB(15.0, 15.0, 15.0, 15.0), |
78 | ); |
79 | }, |
80 | paints |
81 | ..rect(rect: const Rect.fromLTRB(10.0, 10.0, 20.0, 20.0), color: const Color(0x90909090)), |
82 | ); |
83 | }); |
84 | |
85 | test('debugPaintPadding from render objects' , () { |
86 | debugPaintSizeEnabled = true; |
87 | RenderSliver s; |
88 | RenderBox b; |
89 | final RenderViewport root = RenderViewport( |
90 | crossAxisDirection: AxisDirection.right, |
91 | offset: ViewportOffset.zero(), |
92 | children: <RenderSliver>[ |
93 | s = RenderSliverPadding( |
94 | padding: const EdgeInsets.all(10.0), |
95 | child: RenderSliverToBoxAdapter( |
96 | child: b = RenderPadding(padding: const EdgeInsets.all(10.0)), |
97 | ), |
98 | ), |
99 | ], |
100 | ); |
101 | layout(root); |
102 | expect( |
103 | b.debugPaint, |
104 | paints |
105 | ..rect(color: const Color(0xFF00FFFF)) |
106 | ..rect(color: const Color(0x90909090)), |
107 | ); |
108 | expect(b.debugPaint, isNot(paints..path())); |
109 | expect( |
110 | s.debugPaint, |
111 | paints |
112 | ..circle(hasMaskFilter: true) |
113 | ..line(hasMaskFilter: true) |
114 | ..path(hasMaskFilter: true) |
115 | ..path(hasMaskFilter: true) |
116 | ..path(color: const Color(0x900090FF)) |
117 | ..path(color: const Color(0xFF0090FF)), |
118 | ); |
119 | expect(s.debugPaint, isNot(paints..rect())); |
120 | debugPaintSizeEnabled = false; |
121 | }); |
122 | |
123 | test('debugPaintPadding from render objects' , () { |
124 | debugPaintSizeEnabled = true; |
125 | RenderSliver s; |
126 | final RenderBox b = RenderPadding( |
127 | padding: const EdgeInsets.all(10.0), |
128 | child: RenderViewport( |
129 | crossAxisDirection: AxisDirection.right, |
130 | offset: ViewportOffset.zero(), |
131 | children: <RenderSliver>[s = RenderSliverPadding(padding: const EdgeInsets.all(10.0))], |
132 | ), |
133 | ); |
134 | layout(b); |
135 | expect(s.debugPaint, paints..rect(color: const Color(0x90909090))); |
136 | expect( |
137 | s.debugPaint, |
138 | isNot( |
139 | paints |
140 | ..circle(hasMaskFilter: true) |
141 | ..line(hasMaskFilter: true) |
142 | ..path(hasMaskFilter: true) |
143 | ..path(hasMaskFilter: true) |
144 | ..path(color: const Color(0x900090FF)) |
145 | ..path(color: const Color(0xFF0090FF)), |
146 | ), |
147 | ); |
148 | expect( |
149 | b.debugPaint, |
150 | paints |
151 | ..rect(color: const Color(0xFF00FFFF)) |
152 | ..path(color: const Color(0x900090FF)) |
153 | ..path(color: const Color(0xFF0090FF)), |
154 | ); |
155 | expect(b.debugPaint, isNot(paints..rect(color: const Color(0x90909090)))); |
156 | debugPaintSizeEnabled = false; |
157 | }); |
158 | |
159 | test('debugPaintPadding from render objects with inverted direction vertical' , () { |
160 | debugPaintSizeEnabled = true; |
161 | RenderSliver s; |
162 | final RenderViewport root = RenderViewport( |
163 | axisDirection: AxisDirection.up, |
164 | crossAxisDirection: AxisDirection.right, |
165 | offset: ViewportOffset.zero(), |
166 | children: <RenderSliver>[ |
167 | s = RenderSliverPadding( |
168 | padding: const EdgeInsets.all(10.0), |
169 | child: RenderSliverToBoxAdapter( |
170 | child: RenderPadding(padding: const EdgeInsets.all(10.0)), |
171 | ), |
172 | ), |
173 | ], |
174 | ); |
175 | layout(root); |
176 | dynamic error; |
177 | final PaintingContext context = PaintingContext( |
178 | ContainerLayer(), |
179 | const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0), |
180 | ); |
181 | try { |
182 | s.debugPaint(context, const Offset(0.0, 500)); |
183 | } catch (e) { |
184 | error = e; |
185 | } |
186 | expect(error, isNull); |
187 | debugPaintSizeEnabled = false; |
188 | }); |
189 | |
190 | test('debugPaintPadding from render objects with inverted direction horizontal' , () { |
191 | debugPaintSizeEnabled = true; |
192 | RenderSliver s; |
193 | final RenderViewport root = RenderViewport( |
194 | axisDirection: AxisDirection.left, |
195 | crossAxisDirection: AxisDirection.down, |
196 | offset: ViewportOffset.zero(), |
197 | children: <RenderSliver>[ |
198 | s = RenderSliverPadding( |
199 | padding: const EdgeInsets.all(10.0), |
200 | child: RenderSliverToBoxAdapter( |
201 | child: RenderPadding(padding: const EdgeInsets.all(10.0)), |
202 | ), |
203 | ), |
204 | ], |
205 | ); |
206 | layout(root); |
207 | dynamic error; |
208 | final PaintingContext context = PaintingContext( |
209 | ContainerLayer(), |
210 | const Rect.fromLTRB(0.0, 0.0, 800.0, 600.0), |
211 | ); |
212 | try { |
213 | s.debugPaint(context, const Offset(0.0, 500)); |
214 | } catch (e) { |
215 | error = e; |
216 | } |
217 | expect(error, isNull); |
218 | debugPaintSizeEnabled = false; |
219 | }); |
220 | |
221 | test('debugDisableOpacity keeps things in the right spot' , () { |
222 | debugDisableOpacityLayers = true; |
223 | |
224 | final RenderDecoratedBox blackBox = RenderDecoratedBox( |
225 | decoration: const BoxDecoration(color: Color(0xff000000)), |
226 | child: RenderConstrainedBox( |
227 | additionalConstraints: BoxConstraints.tight(const Size.square(20.0)), |
228 | ), |
229 | ); |
230 | final RenderOpacity root = RenderOpacity( |
231 | opacity: .5, |
232 | child: RenderRepaintBoundary(child: blackBox), |
233 | ); |
234 | layout(root, phase: EnginePhase.compositingBits); |
235 | |
236 | final OffsetLayer rootLayer = OffsetLayer(); |
237 | final PaintingContext context = PaintingContext(rootLayer, const Rect.fromLTWH(0, 0, 500, 500)); |
238 | context.paintChild(root, const Offset(40, 40)); |
239 | |
240 | final OpacityLayer opacityLayer = rootLayer.firstChild! as OpacityLayer; |
241 | expect(opacityLayer.offset, const Offset(40, 40)); |
242 | debugDisableOpacityLayers = false; |
243 | }); |
244 | |
245 | test('debugAssertAllRenderVarsUnset warns when debugProfileLayoutsEnabled set' , () { |
246 | debugProfileLayoutsEnabled = true; |
247 | expect(() => debugAssertAllRenderVarsUnset('ERROR' ), throwsFlutterError); |
248 | debugProfileLayoutsEnabled = false; |
249 | }); |
250 | |
251 | test('debugAssertAllRenderVarsUnset warns when debugDisableClipLayers set' , () { |
252 | debugDisableClipLayers = true; |
253 | expect(() => debugAssertAllRenderVarsUnset('ERROR' ), throwsFlutterError); |
254 | debugDisableClipLayers = false; |
255 | }); |
256 | |
257 | test('debugAssertAllRenderVarsUnset warns when debugDisablePhysicalShapeLayers set' , () { |
258 | debugDisablePhysicalShapeLayers = true; |
259 | expect(() => debugAssertAllRenderVarsUnset('ERROR' ), throwsFlutterError); |
260 | debugDisablePhysicalShapeLayers = false; |
261 | }); |
262 | |
263 | test('debugAssertAllRenderVarsUnset warns when debugDisableOpacityLayers set' , () { |
264 | debugDisableOpacityLayers = true; |
265 | expect(() => debugAssertAllRenderVarsUnset('ERROR' ), throwsFlutterError); |
266 | debugDisableOpacityLayers = false; |
267 | }); |
268 | |
269 | test('debugCheckHasBoundedAxis warns for vertical and horizontal axis' , () { |
270 | expect( |
271 | () => debugCheckHasBoundedAxis(Axis.vertical, const BoxConstraints()), |
272 | throwsFlutterError, |
273 | ); |
274 | expect( |
275 | () => debugCheckHasBoundedAxis(Axis.horizontal, const BoxConstraints()), |
276 | throwsFlutterError, |
277 | ); |
278 | }); |
279 | } |
280 | |