diff --git a/packages/flutter/test/widgets/build_scope_test.dart b/packages/flutter/test/widgets/build_scope_test.dart index de1fb30a79..b25f2d21b9 100644 --- a/packages/flutter/test/widgets/build_scope_test.dart +++ b/packages/flutter/test/widgets/build_scope_test.dart @@ -156,7 +156,7 @@ void main() { testWidgets('Setting parent state during build is forbidden', (WidgetTester tester) async { await tester.pumpWidget(new BadWidgetParent()); - expect(tester.takeException(), isNotNull); + expect(tester.takeException(), isFlutterError); await tester.pumpWidget(new Container()); }); @@ -225,4 +225,4 @@ void main() { } }); -} \ No newline at end of file +} diff --git a/packages/flutter/test/widgets/framework_test.dart b/packages/flutter/test/widgets/framework_test.dart index 559c078faf..c75bb36187 100644 --- a/packages/flutter/test/widgets/framework_test.dart +++ b/packages/flutter/test/widgets/framework_test.dart @@ -3,8 +3,14 @@ // found in the LICENSE file. import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; +class TestState extends State { + @override + Widget build(BuildContext context) => null; +} + void main() { testWidgets('UniqueKey control test', (WidgetTester tester) async { Key key = new UniqueKey(); @@ -25,6 +31,19 @@ void main() { expect(keyA, isNot(equals(keyB))); }); + testWidgets('GlobalObjectKey control test', (WidgetTester tester) async { + Object a = new Object(); + Object b = new Object(); + Key keyA = new GlobalObjectKey(a); + Key keyA2 = new GlobalObjectKey(a); + Key keyB = new GlobalObjectKey(b); + + expect(keyA, hasOneLineDescription); + expect(keyA, equals(keyA2)); + expect(keyA.hashCode, equals(keyA2.hashCode)); + expect(keyA, isNot(equals(keyB))); + }); + testWidgets('GlobalKey duplication', (WidgetTester tester) async { Key key = new GlobalKey(debugLabel: 'problematic'); @@ -55,7 +74,7 @@ void main() { ], )); - expect(tester.takeException(), isNotNull); + expect(tester.takeException(), isFlutterError); }); testWidgets('GlobalKey notification exception handling', (WidgetTester tester) async { @@ -78,4 +97,50 @@ void main() { expect(tester.takeException(), isNotNull); expect(didReceiveCallback, isTrue); }); + + testWidgets('Defunct setState throws exception', (WidgetTester tester) async { + StateSetter setState; + + await tester.pumpWidget(new StatefulBuilder( + builder: (BuildContext context, StateSetter setter) { + setState = setter; + return new Container(); + }, + )); + + // Control check that setState doesn't throw an exception. + setState(() { }); + + await tester.pumpWidget(new Container()); + + expect(() { setState(() { }); }, throwsFlutterError); + }); + + testWidgets('State toString', (WidgetTester tester) async { + TestState state = new TestState(); + expect(state.toString(), contains('no config')); + }); + + testWidgets('debugPrintGlobalKeyedWidgetLifecycle control test', (WidgetTester tester) async { + expect(debugPrintGlobalKeyedWidgetLifecycle, isFalse); + + final DebugPrintCallback oldCallback = debugPrint; + debugPrintGlobalKeyedWidgetLifecycle = true; + + List log = []; + debugPrint = (String message, { int wrapWidth }) { + log.add(message); + }; + + GlobalKey key = new GlobalKey(); + await tester.pumpWidget(new Container(key: key)); + expect(log, isEmpty); + await tester.pumpWidget(new Placeholder()); + debugPrint = oldCallback; + debugPrintGlobalKeyedWidgetLifecycle = false; + + expect(log.length, equals(2)); + expect(log[0], matches('Deactivated')); + expect(log[1], matches('Discarding .+ from inactive elements list.')); + }); } diff --git a/packages/flutter/test/widgets/parent_data_test.dart b/packages/flutter/test/widgets/parent_data_test.dart index 928a9db61c..86e003686a 100644 --- a/packages/flutter/test/widgets/parent_data_test.dart +++ b/packages/flutter/test/widgets/parent_data_test.dart @@ -257,7 +257,7 @@ void main() { ] ) ); - expect(tester.takeException(), isNotNull); + expect(tester.takeException(), isFlutterError); await tester.pumpWidget(new Stack()); @@ -276,7 +276,7 @@ void main() { ) ) ); - expect(tester.takeException(), isNotNull); + expect(tester.takeException(), isFlutterError); await tester.pumpWidget( new Stack() @@ -339,4 +339,20 @@ void main() { new TestParentData(top: 10.0, left: 10.0), ]); }); + + testWidgets('Parent data invalid ancestor', (WidgetTester tester) async { + await tester.pumpWidget(new Row( + children: [ + new Stack( + children: [ + new Expanded( + child: new Container() + ), + ], + ), + ], + )); + + expect(tester.takeException(), isFlutterError); + }); } diff --git a/packages/flutter_test/lib/src/matchers.dart b/packages/flutter_test/lib/src/matchers.dart index 17844c8bcd..1ca742f35a 100644 --- a/packages/flutter_test/lib/src/matchers.dart +++ b/packages/flutter_test/lib/src/matchers.dart @@ -66,6 +66,12 @@ const Matcher isNotInCard = const _IsNotInCard(); /// empty, and does not contain the default `Instance of ...` string. const Matcher hasOneLineDescription = const _HasOneLineDescription(); +/// A matcher for functions that throw [FlutterError]. +const Matcher throwsFlutterError = const Throws(isFlutterError); + +/// A matcher for [FlutterError]. +const Matcher isFlutterError = const isInstanceOf(); + /// Asserts that two [double]s are equal, within some tolerated error. /// /// Two values are considered equal if the difference between them is within @@ -268,4 +274,4 @@ class _MoreOrLessEquals extends Matcher { @override Description describe(Description description) => description.add('$value (±$epsilon)'); -} \ No newline at end of file +}