utils.dart (3247B)
1 import 'package:flutter/material.dart'; 2 import 'package:flutter_test/flutter_test.dart'; 3 4 class Utils { 5 static printWidgets(WidgetTester tester) { 6 debugPrint(tester.allWidgets.toList().join("\n").toString()); 7 } 8 9 static bool isWidgetInWidgets(WidgetTester tester, Type widget) { 10 debugPrint(tester.allWidgets 11 .toList() 12 .map((e) => e.runtimeType) 13 .join("\n") 14 .toString()); 15 return tester.allWidgets 16 .toList() 17 .map((e) => e.runtimeType) 18 .contains(widget); 19 } 20 21 static Widget getWidgetWrapper(Widget child, TestObserver routeObserver) { 22 return MaterialApp( 23 title: "Fluffychat", 24 theme: ThemeData( 25 primarySwatch: Colors.blue, 26 ), 27 navigatorObservers: <NavigatorObserver>[routeObserver], 28 home: child, 29 ); 30 } 31 32 static double getOpacity(WidgetTester tester, String textValue) { 33 final FadeTransition opacityWidget = tester.widget<FadeTransition>(find 34 .ancestor( 35 of: find.text(textValue), 36 matching: find.byType(FadeTransition), 37 ) 38 .first); 39 return opacityWidget.opacity.value; 40 } 41 42 static Future<Null> tapItem(WidgetTester tester, Key key) async { 43 /// Tap the button which should open the PopupMenu. 44 /// By calling tester.pumpAndSettle(), we ensure that all animations 45 /// have completed before we continue further. 46 await tester.tap(find.byKey(key)); 47 await tester.pumpAndSettle(); 48 } 49 } 50 51 typedef OnObservation = void Function( 52 Route<dynamic> route, Route<dynamic> previousRoute); 53 54 /// Example Usage: 55 /// 56 /// ``` 57 /// bool isPushed = false; 58 // bool isPopped = false; 59 // 60 // final TestObserver observer = TestObserver() 61 // ..onPushed = (Route<dynamic> route, Route<dynamic> previousRoute) { 62 // // Pushes the initial route. 63 // expect(route is PageRoute && route.settings.name == '/', isTrue); 64 // expect(previousRoute, isNull); 65 // isPushed = true; 66 // } 67 // ..onPopped = (Route<dynamic> route, Route<dynamic> previousRoute) { 68 // isPopped = true; 69 // }; 70 /// ``` 71 /// 72 class TestObserver extends NavigatorObserver { 73 OnObservation onPushed; 74 OnObservation onPopped; 75 OnObservation onRemoved; 76 OnObservation onReplaced; 77 78 @override 79 void didPush(Route<dynamic> route, Route<dynamic> previousRoute) { 80 if (onPushed != null) { 81 onPushed(route, previousRoute); 82 } 83 } 84 85 @override 86 void didPop(Route<dynamic> route, Route<dynamic> previousRoute) { 87 if (onPopped != null) { 88 onPopped(route, previousRoute); 89 } 90 } 91 92 @override 93 void didRemove(Route<dynamic> route, Route<dynamic> previousRoute) { 94 if (onRemoved != null) onRemoved(route, previousRoute); 95 } 96 97 @override 98 void didReplace({Route<dynamic> oldRoute, Route<dynamic> newRoute}) { 99 if (onReplaced != null) onReplaced(newRoute, oldRoute); 100 } 101 } 102 103 class RouteMatcher extends Matcher { 104 final String expected; 105 bool contains = false; 106 107 RouteMatcher(this.expected, {this.contains}); 108 109 @override 110 Description describe(Description description) { 111 return null; 112 } 113 114 @override 115 bool matches(covariant String item, Map matchState) { 116 if (contains) { 117 return item.toString().contains(expected); 118 } 119 return item == expected; 120 } 121 }