@@ -7,6 +7,7 @@ import 'package:flutter_test/flutter_test.dart';
77import 'package:flutter/material.dart' ;
88import 'package:flutter/widgets.dart' ;
99import 'package:flutter/rendering.dart' ;
10+ import 'package:mockito/mockito.dart' ;
1011
1112void main () {
1213 group ('PhysicalShape' , () {
@@ -278,6 +279,90 @@ void main() {
278279 equals ('UnconstrainedBox(alignment: topRight, constrainedAxis: horizontal, textDirection: rtl)' ),
279280 );
280281 });
282+
283+ group ('ColoredBox' , () {
284+ _MockCanvas mockCanvas;
285+ _MockPaintingContext mockContext;
286+ const Color colorToPaint = Color (0xFFABCDEF );
287+
288+ setUp (() {
289+ mockContext = _MockPaintingContext ();
290+ mockCanvas = _MockCanvas ();
291+ when (mockContext.canvas).thenReturn (mockCanvas);
292+ });
293+
294+ testWidgets ('ColoredBox - no size, no child' , (WidgetTester tester) async {
295+ await tester.pumpWidget (Flex (
296+ direction: Axis .horizontal,
297+ textDirection: TextDirection .ltr,
298+ children: const < Widget > [
299+ SizedBox .shrink (
300+ child: ColoredBox (color: colorToPaint),
301+ ),
302+ ],
303+ ));
304+ expect (find.byType (ColoredBox ), findsOneWidget);
305+ final RenderObject renderColoredBox = tester.renderObject (find.byType (ColoredBox ));
306+
307+ renderColoredBox.paint (mockContext, Offset .zero);
308+
309+ verifyNever (mockCanvas.drawRect (any, any));
310+ verifyNever (mockContext.paintChild (any, any));
311+ });
312+
313+ testWidgets ('ColoredBox - no size, child' , (WidgetTester tester) async {
314+ const ValueKey <int > key = ValueKey <int >(0 );
315+ const Widget child = SizedBox .expand (key: key);
316+ await tester.pumpWidget (Flex (
317+ direction: Axis .horizontal,
318+ textDirection: TextDirection .ltr,
319+ children: const < Widget > [
320+ SizedBox .shrink (
321+ child: ColoredBox (color: colorToPaint, child: child),
322+ ),
323+ ],
324+ ));
325+ expect (find.byType (ColoredBox ), findsOneWidget);
326+ final RenderObject renderColoredBox = tester.renderObject (find.byType (ColoredBox ));
327+ final RenderObject renderSizedBox = tester.renderObject (find.byKey (key));
328+
329+ renderColoredBox.paint (mockContext, Offset .zero);
330+
331+ verifyNever (mockCanvas.drawRect (any, any));
332+ verify (mockContext.paintChild (renderSizedBox, Offset .zero)).called (1 );
333+ });
334+
335+ testWidgets ('ColoredBox - size, no child' , (WidgetTester tester) async {
336+ await tester.pumpWidget (const ColoredBox (color: colorToPaint));
337+ expect (find.byType (ColoredBox ), findsOneWidget);
338+ final RenderObject renderColoredBox = tester.renderObject (find.byType (ColoredBox ));
339+
340+ renderColoredBox.paint (mockContext, Offset .zero);
341+
342+ final List <dynamic > drawRect = verify (mockCanvas.drawRect (captureAny, captureAny)).captured;
343+ expect (drawRect.length, 2 );
344+ expect (drawRect[0 ], const Rect .fromLTWH (0 , 0 , 800 , 600 ));
345+ expect (drawRect[1 ].color, colorToPaint);
346+ verifyNever (mockContext.paintChild (any, any));
347+ });
348+
349+ testWidgets ('ColoredBox - size, child' , (WidgetTester tester) async {
350+ const ValueKey <int > key = ValueKey <int >(0 );
351+ const Widget child = SizedBox .expand (key: key);
352+ await tester.pumpWidget (const ColoredBox (color: colorToPaint, child: child));
353+ expect (find.byType (ColoredBox ), findsOneWidget);
354+ final RenderObject renderColoredBox = tester.renderObject (find.byType (ColoredBox ));
355+ final RenderObject renderSizedBox = tester.renderObject (find.byKey (key));
356+
357+ renderColoredBox.paint (mockContext, Offset .zero);
358+
359+ final List <dynamic > drawRect = verify (mockCanvas.drawRect (captureAny, captureAny)).captured;
360+ expect (drawRect.length, 2 );
361+ expect (drawRect[0 ], const Rect .fromLTWH (0 , 0 , 800 , 600 ));
362+ expect (drawRect[1 ].color, colorToPaint);
363+ verify (mockContext.paintChild (renderSizedBox, Offset .zero)).called (1 );
364+ });
365+ });
281366}
282367
283368HitsRenderBox hits (RenderBox renderBox) => HitsRenderBox (renderBox);
@@ -319,3 +404,6 @@ class DoesNotHitRenderBox extends Matcher {
319404 ).isEmpty;
320405 }
321406}
407+
408+ class _MockPaintingContext extends Mock implements PaintingContext {}
409+ class _MockCanvas extends Mock implements Canvas {}
0 commit comments