From 37f86c3195794e5de5751b668f9828f82cb0693b Mon Sep 17 00:00:00 2001 From: Greg Spencer Date: Mon, 2 Dec 2019 17:10:09 -0800 Subject: [PATCH] Clean up some things I noticed while doing another change (#45658) This fixes some minor things I noticed while doing another change. - Uncomments an old test that wants to be run, but can't be, and marked it as "skipped", so that the code won't rot anymore. - Added the focus state to the short string version of a FocusNode - Added a missing piece of information to findAncestorWidgetOfExactType --- .../lib/src/widgets/focus_manager.dart | 7 +- .../flutter/lib/src/widgets/framework.dart | 21 +++--- .../test/widgets/focus_manager_test.dart | 14 ++-- .../test/widgets/focus_scope_test.dart | 17 ++--- .../flutter/test/widgets/navigator_test.dart | 72 +++++++++---------- 5 files changed, 70 insertions(+), 61 deletions(-) diff --git a/packages/flutter/lib/src/widgets/focus_manager.dart b/packages/flutter/lib/src/widgets/focus_manager.dart index d524cc8491..8a9b85c1a8 100644 --- a/packages/flutter/lib/src/widgets/focus_manager.dart +++ b/packages/flutter/lib/src/widgets/focus_manager.dart @@ -911,7 +911,12 @@ class FocusNode with DiagnosticableTreeMixin, ChangeNotifier { @override String toStringShort() { - return '${describeIdentity(this)}${debugLabel != null && debugLabel.isNotEmpty ? '($debugLabel)' : ''}'; + final bool hasDebugLabel = debugLabel != null && debugLabel.isNotEmpty; + final String extraData = '${hasDebugLabel ? debugLabel : ''}' + '${hasFocus && hasDebugLabel ? ' ' : ''}' + '${hasFocus && !hasPrimaryFocus ? '[IN FOCUS PATH]' : ''}' + '${hasPrimaryFocus ? '[PRIMARY FOCUS]' : ''}'; + return '${describeIdentity(this)}${extraData.isNotEmpty ? '($extraData)' : ''}'; } } diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index a3422abdf7..2d22044267 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -2044,15 +2044,15 @@ abstract class BuildContext { /// Returns the nearest ancestor widget of the given type [T], which must be the /// type of a concrete [Widget] subclass. /// - /// In general, [dependOnInheritedWidgetOfExactType] is more useful, since inherited - /// widgets will trigger consumers to rebuild when they change. This method is - /// appropriate when used in interaction event handlers (e.g. gesture - /// callbacks) or for performing one-off tasks such as asserting that you have - /// or don't have a widget of a specific type as an ancestor. The return value - /// of a Widget's build method should not depend on the value returned by this - /// method, because the build context will not rebuild if the return value of - /// this method changes. This could lead to a situation where data used in the - /// build method changes, but the widget is not rebuilt. + /// In general, [dependOnInheritedWidgetOfExactType] is more useful, since + /// inherited widgets will trigger consumers to rebuild when they change. This + /// method is appropriate when used in interaction event handlers (e.g. + /// gesture callbacks) or for performing one-off tasks such as asserting that + /// you have or don't have a widget of a specific type as an ancestor. The + /// return value of a Widget's build method should not depend on the value + /// returned by this method, because the build context will not rebuild if the + /// return value of this method changes. This could lead to a situation where + /// data used in the build method changes, but the widget is not rebuilt. /// /// Calling this method is relatively expensive (O(N) in the depth of the /// tree). Only call this method if the distance from this widget to the @@ -2062,6 +2062,9 @@ abstract class BuildContext { /// because the widget tree is no longer stable at that time. To refer to /// an ancestor from one of those methods, save a reference to the ancestor /// by calling [findAncestorWidgetOfExactType] in [State.didChangeDependencies]. + /// + /// Returns null if a widget of the requested type does not appear in the + /// ancestors of this context. T findAncestorWidgetOfExactType(); /// Returns the [State] object of the nearest ancestor [StatefulWidget] widget diff --git a/packages/flutter/test/widgets/focus_manager_test.dart b/packages/flutter/test/widgets/focus_manager_test.dart index 5d86006d4a..621085ef62 100644 --- a/packages/flutter/test/widgets/focus_manager_test.dart +++ b/packages/flutter/test/widgets/focus_manager_test.dart @@ -664,12 +664,12 @@ void main() { description, equalsIgnoringHashCodes( 'FocusManager#00000\n' - ' │ primaryFocus: FocusNode#00000(Child 4)\n' + ' │ primaryFocus: FocusNode#00000(Child 4 [PRIMARY FOCUS])\n' ' │ primaryFocusCreator: Container-[GlobalKey#00000] ← [root]\n' ' │\n' - ' └─rootScope: FocusScopeNode#00000(Root Focus Scope)\n' + ' └─rootScope: FocusScopeNode#00000(Root Focus Scope [IN FOCUS PATH])\n' ' │ IN FOCUS PATH\n' - ' │ focusedChildren: FocusScopeNode#00000\n' + ' │ focusedChildren: FocusScopeNode#00000([IN FOCUS PATH])\n' ' │\n' ' ├─Child 1: FocusScopeNode#00000(Scope 1)\n' ' │ │ context: Container-[GlobalKey#00000]\n' @@ -683,19 +683,19 @@ void main() { ' │ └─Child 2: FocusNode#00000\n' ' │ context: Container-[GlobalKey#00000]\n' ' │\n' - ' └─Child 2: FocusScopeNode#00000\n' + ' └─Child 2: FocusScopeNode#00000([IN FOCUS PATH])\n' ' │ context: Container-[GlobalKey#00000]\n' ' │ IN FOCUS PATH\n' - ' │ focusedChildren: FocusNode#00000(Child 4)\n' + ' │ focusedChildren: FocusNode#00000(Child 4 [PRIMARY FOCUS])\n' ' │\n' - ' └─Child 1: FocusNode#00000(Parent 2)\n' + ' └─Child 1: FocusNode#00000(Parent 2 [IN FOCUS PATH])\n' ' │ context: Container-[GlobalKey#00000]\n' ' │ IN FOCUS PATH\n' ' │\n' ' ├─Child 1: FocusNode#00000(Child 3)\n' ' │ context: Container-[GlobalKey#00000]\n' ' │\n' - ' └─Child 2: FocusNode#00000(Child 4)\n' + ' └─Child 2: FocusNode#00000(Child 4 [PRIMARY FOCUS])\n' ' context: Container-[GlobalKey#00000]\n' ' PRIMARY FOCUS\n' )); diff --git a/packages/flutter/test/widgets/focus_scope_test.dart b/packages/flutter/test/widgets/focus_scope_test.dart index 1dc176a03a..043da1c3c9 100644 --- a/packages/flutter/test/widgets/focus_scope_test.dart +++ b/packages/flutter/test/widgets/focus_scope_test.dart @@ -227,12 +227,12 @@ void main() { expect(parentFocusScope, hasAGoodToStringDeep); expect( parentFocusScope.toStringDeep(), - equalsIgnoringHashCodes('FocusScopeNode#00000(Parent Scope Node)\n' + equalsIgnoringHashCodes('FocusScopeNode#00000(Parent Scope Node [IN FOCUS PATH])\n' ' │ context: FocusScope\n' ' │ IN FOCUS PATH\n' - ' │ focusedChildren: FocusNode#00000(Child)\n' + ' │ focusedChildren: FocusNode#00000(Child [PRIMARY FOCUS])\n' ' │\n' - ' └─Child 1: FocusNode#00000(Child)\n' + ' └─Child 1: FocusNode#00000(Child [PRIMARY FOCUS])\n' ' context: Focus\n' ' PRIMARY FOCUS\n'), ); @@ -240,16 +240,17 @@ void main() { expect(FocusManager.instance.rootScope, hasAGoodToStringDeep); expect( FocusManager.instance.rootScope.toStringDeep(minLevel: DiagnosticLevel.info), - equalsIgnoringHashCodes('FocusScopeNode#00000(Root Focus Scope)\n' + equalsIgnoringHashCodes('FocusScopeNode#00000(Root Focus Scope [IN FOCUS PATH])\n' ' │ IN FOCUS PATH\n' - ' │ focusedChildren: FocusScopeNode#00000(Parent Scope Node)\n' + ' │ focusedChildren: FocusScopeNode#00000(Parent Scope Node [IN FOCUS\n' + ' │ PATH])\n' ' │\n' - ' └─Child 1: FocusScopeNode#00000(Parent Scope Node)\n' + ' └─Child 1: FocusScopeNode#00000(Parent Scope Node [IN FOCUS PATH])\n' ' │ context: FocusScope\n' ' │ IN FOCUS PATH\n' - ' │ focusedChildren: FocusNode#00000(Child)\n' + ' │ focusedChildren: FocusNode#00000(Child [PRIMARY FOCUS])\n' ' │\n' - ' └─Child 1: FocusNode#00000(Child)\n' + ' └─Child 1: FocusNode#00000(Child [PRIMARY FOCUS])\n' ' context: Focus\n' ' PRIMARY FOCUS\n'), ); diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index b3d9655540..a3ced73ba4 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -255,42 +255,42 @@ void main() { expect(log, equals(['left'])); }); - // This test doesn't work because the testing framework uses a fake version of - // the pointer event dispatch loop. - // - // TODO(abarth): Test more of the real code and enable this test. - // See https://github.com/flutter/flutter/issues/4771. - // - // testWidgets('Pending gestures are rejected', (WidgetTester tester) async { - // List log = []; - // final Map routes = { - // '/': (BuildContext context) { - // return new Row( - // children: [ - // new GestureDetector( - // onTap: () { - // log.add('left'); - // Navigator.pushNamed(context, '/second'); - // }, - // child: new Text('left') - // ), - // new GestureDetector( - // onTap: () { log.add('right'); }, - // child: new Text('right') - // ), - // ] - // ); - // }, - // '/second': (BuildContext context) => new Container(), - // }; - // await tester.pumpWidget(new MaterialApp(routes: routes)); - // TestGesture gesture = await tester.startGesture(tester.getCenter(find.text('right')), pointer: 23); - // expect(log, isEmpty); - // await tester.tap(find.text('left')); - // expect(log, equals(['left'])); - // await gesture.up(); - // expect(log, equals(['left'])); - // }); + testWidgets('Pending gestures are rejected', (WidgetTester tester) async { + final List log = []; + final Map routes = { + '/': (BuildContext context) { + return Row( + children: [ + GestureDetector( + onTap: () { + log.add('left'); + Navigator.pushNamed(context, '/second'); + }, + child: const Text('left') + ), + GestureDetector( + onTap: () { log.add('right'); }, + child: const Text('right'), + ), + ] + ); + }, + '/second': (BuildContext context) => Container(), + }; + await tester.pumpWidget(MaterialApp(routes: routes)); + final TestGesture gesture = await tester.startGesture(tester.getCenter(find.text('right')), pointer: 23); + expect(log, isEmpty); + await tester.tap(find.text('left')); + expect(log, equals(['left'])); + await gesture.up(); + expect(log, equals(['left'])); + + // This test doesn't work because it relies on part of the pointer event + // dispatching mechanism that is mocked out in testing. We should use the real + // mechanism even during testing and enable this test. + // TODO(abarth): Test more of the real code and enable this test. + // See https://github.com/flutter/flutter/issues/4771. + }, skip: true); testWidgets('popAndPushNamed', (WidgetTester tester) async { final Map routes = {