diff --git a/packages/flutter/test/widgets/widget_inspector_test.dart b/packages/flutter/test/widgets/widget_inspector_test.dart index d17615af447..209c76d9e91 100644 --- a/packages/flutter/test/widgets/widget_inspector_test.dart +++ b/packages/flutter/test/widgets/widget_inspector_test.dart @@ -1913,221 +1913,215 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { expect(column, equals(15)); }); - testWidgets('ext.flutter.inspector.getRootWidgetSummaryTree', - (WidgetTester tester) async { - const String group = 'test-group'; - await pumpWidgetTreeWithABC(tester); - final Element elementA = findElementABC('a'); - service.disposeAllGroups(); - service.resetPubRootDirectories(); - service.setSelection(elementA, 'my-group'); - final Map jsonA = (await service.testExtension( - WidgetInspectorServiceExtensions.getSelectedWidget.name, - {'objectGroup': 'my-group'}, - ))! as Map; + group('Widget Tree APIs', () { - service.resetPubRootDirectories(); - Map rootJson = (await service.testExtension( - WidgetInspectorServiceExtensions.getRootWidgetSummaryTree.name, - {'objectGroup': group}, - ))! as Map; + /// Gets the widget using [WidgetInspectorServiceExtensions.getSelectedWidget] + /// for the given [element]. + Future> selectedWidgetResponseForElement( + Element element) async { + service + ..disposeAllGroups() + ..resetPubRootDirectories() + ..setSelection(element, 'my-group'); - // We haven't yet properly specified which directories are summary tree - // directories so we get an empty tree other than the root that is always - // included. - final Object? rootWidget = - service.toObject(rootJson['valueId']! as String); - expect(rootWidget, equals(WidgetsBinding.instance.rootElement)); - List childrenJson = rootJson['children']! as List; - // There are no summary tree children. - expect(childrenJson.length, equals(0)); + return (await service.testExtension( + WidgetInspectorServiceExtensions.getSelectedWidget.name, + {'objectGroup': 'my-group'}, + ))! as Map; + } - final Map creationLocation = - jsonA['creationLocation']! as Map; - expect(creationLocation, isNotNull); - final String testFile = creationLocation['file']! as String; - expect(testFile, endsWith('widget_inspector_test.dart')); - final List segments = Uri.parse(testFile).pathSegments; - // Strip a couple subdirectories away to generate a plausible pub root - // directory. - final String pubRootTest = - '/${segments.take(segments.length - 2).join('/')}'; - service.resetPubRootDirectories(); - await service.testExtension( - WidgetInspectorServiceExtensions.addPubRootDirectories.name, - {'arg0': pubRootTest}, - ); + /// Verifies the creation location is expected for the given + /// [responseJson]. + Map verifyAndReturnCreationLocation( + Map responseJson) { + final Map creationLocation = + responseJson['creationLocation']! as Map; + expect(creationLocation, isNotNull); + return creationLocation; + } - rootJson = (await service.testExtension( - WidgetInspectorServiceExtensions.getRootWidgetSummaryTree.name, - {'objectGroup': group}, - ))! as Map; - childrenJson = rootJson['children']! as List; - // The tree of nodes returned contains all widgets created directly by the - // test. - childrenJson = rootJson['children']! as List; - expect(childrenJson.length, equals(1)); + /// Verifies the test file is expected for the given + /// [creationLocation]. + String verifyAndReturnTestFile( + Map creationLocation) { + final String testFile = creationLocation['file']! as String; + expect(testFile, endsWith('widget_inspector_test.dart')); + return testFile; + } - List alternateChildrenJson = (await service.testExtension( - WidgetInspectorServiceExtensions.getChildrenSummaryTree.name, - { - 'arg': rootJson['valueId']! as String, - 'objectGroup': group - }, - ))! as List; - expect(alternateChildrenJson.length, equals(1)); - Map childJson = - childrenJson[0]! as Map; - Map alternateChildJson = - alternateChildrenJson[0]! as Map; - expect(childJson['description'], startsWith('Directionality')); - expect( - alternateChildJson['description'], startsWith('Directionality')); - expect(alternateChildJson['valueId'], equals(childJson['valueId'])); + /// Adds a pub root directory for the given [testFile]. + void addPubRootDirectoryFor(String testFile) { + final List segments = Uri.parse(testFile).pathSegments; + // Strip a couple subdirectories away to generate a plausible pub + // root directory. + final String pubRootTest = + '/${segments.take(segments.length - 2).join('/')}'; + service + ..resetPubRootDirectories() + ..addPubRootDirectories([pubRootTest]); + } - childrenJson = childJson['children']! as List; - alternateChildrenJson = (await service.testExtension( - WidgetInspectorServiceExtensions.getChildrenSummaryTree.name, - { - 'arg': childJson['valueId']! as String, - 'objectGroup': group - }, - ))! as List; - expect(alternateChildrenJson.length, equals(1)); - expect(childrenJson.length, equals(1)); - alternateChildJson = - alternateChildrenJson[0]! as Map; - childJson = childrenJson[0]! as Map; - expect(childJson['description'], startsWith('Stack')); - expect(alternateChildJson['description'], startsWith('Stack')); - expect(alternateChildJson['valueId'], equals(childJson['valueId'])); - childrenJson = childJson['children']! as List; + /// Gets the children nodes from the JSON response. + List childrenFromJsonResponse(Map json) { + return json['children']! as List; + } - childrenJson = childJson['children']! as List; - alternateChildrenJson = (await service.testExtension( - WidgetInspectorServiceExtensions.getChildrenSummaryTree.name, - { - 'arg': childJson['valueId']! as String, - 'objectGroup': group - }, - ))! as List; - expect(alternateChildrenJson.length, equals(3)); - expect(childrenJson.length, equals(3)); - alternateChildJson = - alternateChildrenJson[2]! as Map; - childJson = childrenJson[2]! as Map; - expect(childJson['description'], startsWith('Text')); - expect(alternateChildJson['description'], startsWith('Text')); - expect(alternateChildJson['valueId'], equals(childJson['valueId'])); - alternateChildrenJson = (await service.testExtension( - WidgetInspectorServiceExtensions.getChildrenSummaryTree.name, - { - 'arg': childJson['valueId']! as String, - 'objectGroup': group - }, - ))! as List; - childrenJson = childJson['children']! as List; - expect(alternateChildrenJson.length, equals(0)); - expect(childrenJson.length, equals(0)); - }); + /// Gets the children nodes using a call to + /// [WidgetInspectorServiceExtensions.getChildrenSummaryTree]. + Future> childrenFromGetChildrenSummaryTree( + String valueId, String group) async { + return (await service.testExtension( + WidgetInspectorServiceExtensions.getChildrenSummaryTree.name, + {'arg': valueId, 'objectGroup': group}, + ))! as List; + } - testWidgets( - 'ext.flutter.inspector.getRootWidgetSummaryTreeWithPreviews', - (WidgetTester tester) async { - const String group = 'test-group'; + /// Verifies that the children from the JSON response are identical to + /// those from [WidgetInspectorServiceExtensions.getChildrenSummaryTree]. + Future verifyChildrenMatchOtherApi(Map jsonResponse, + {required String group, bool checkForPreviews = false}) async { + List children = childrenFromJsonResponse(jsonResponse); + List childrenFromOtherApi = + await childrenFromGetChildrenSummaryTree( + jsonResponse['valueId']! as String, group); - await pumpWidgetTreeWithABC(tester); - final Element elementA = findElementABC('a'); + // Verify that the number of children are the same, + expect(children.length, equals(1)); + expect(children.length, equals(childrenFromOtherApi.length)); - service - ..disposeAllGroups() - ..resetPubRootDirectories() - ..setSelection(elementA, 'my-group'); + // Get the first child. + Map child = + children[0]! as Map; + Map childFromOtherApi = + childrenFromOtherApi[0]! as Map; - final Map jsonA = (await service.testExtension( - WidgetInspectorServiceExtensions.getSelectedWidget.name, - {'objectGroup': 'my-group'}, - ))! as Map; + // Verify the first child is the same. + expect(child['description'], startsWith('Directionality')); + expect(child['description'], equals(childFromOtherApi['description'])); + expect(child['valueId'], equals(childFromOtherApi['valueId'])); - final Map creationLocation = - jsonA['creationLocation']! as Map; - expect(creationLocation, isNotNull); - final String testFile = creationLocation['file']! as String; - expect(testFile, endsWith('widget_inspector_test.dart')); - final List segments = Uri.parse(testFile).pathSegments; - // Strip a couple subdirectories away to generate a plausible pub root - // directory. - final String pubRootTest = - '/${segments.take(segments.length - 2).join('/')}'; - service - ..resetPubRootDirectories() - ..addPubRootDirectories([pubRootTest]); + // Get the first child's children. + children = childrenFromJsonResponse(child); + childrenFromOtherApi = await childrenFromGetChildrenSummaryTree( + childFromOtherApi['valueId']! as String, group); - final Map rootJson = (await service.testExtension( - WidgetInspectorServiceExtensions - .getRootWidgetSummaryTreeWithPreviews.name, - {'groupName': group}, - ))! as Map; - List childrenJson = rootJson['children']! as List; - // The tree of nodes returned contains all widgets created directly by the - // test. - childrenJson = rootJson['children']! as List; - expect(childrenJson.length, equals(1)); + // Verify the first child's children are the same length. + expect(children.length, equals(1)); + expect(children.length, equals(childrenFromOtherApi.length)); - List alternateChildrenJson = (await service.testExtension( - WidgetInspectorServiceExtensions.getChildrenSummaryTree.name, - { - 'arg': rootJson['valueId']! as String, - 'objectGroup': group - }, - ))! as List; - expect(alternateChildrenJson.length, equals(1)); - Map childJson = - childrenJson[0]! as Map; - Map alternateChildJson = - alternateChildrenJson[0]! as Map; - expect(childJson['description'], startsWith('Directionality')); - expect( - alternateChildJson['description'], startsWith('Directionality')); - expect(alternateChildJson['valueId'], equals(childJson['valueId'])); + // Get the first child's first child. + child = children[0]! as Map; + childFromOtherApi = + childrenFromOtherApi[0]! as Map; - childrenJson = childJson['children']! as List; - alternateChildrenJson = (await service.testExtension( - WidgetInspectorServiceExtensions.getChildrenSummaryTree.name, - { - 'arg': childJson['valueId']! as String, - 'objectGroup': group - }, - ))! as List; - expect(alternateChildrenJson.length, equals(1)); - expect(childrenJson.length, equals(1)); - alternateChildJson = - alternateChildrenJson[0]! as Map; - childJson = childrenJson[0]! as Map; - expect(childJson['description'], startsWith('Stack')); - expect(alternateChildJson['description'], startsWith('Stack')); - expect(alternateChildJson['valueId'], equals(childJson['valueId'])); - childrenJson = childJson['children']! as List; + // Verify the first child's first child is the same. + expect(child['description'], startsWith('Stack')); + expect(child['description'], + equals(childFromOtherApi['description'])); + expect(child['valueId'], equals(childFromOtherApi['valueId'])); - childrenJson = childJson['children']! as List; - alternateChildrenJson = (await service.testExtension( - WidgetInspectorServiceExtensions.getChildrenSummaryTree.name, - { - 'arg': childJson['valueId']! as String, - 'objectGroup': group - }, - ))! as List; - expect(alternateChildrenJson.length, equals(3)); - expect(childrenJson.length, equals(3)); - alternateChildJson = - alternateChildrenJson[2]! as Map; - childJson = childrenJson[2]! as Map; - expect(childJson['description'], startsWith('Text')); + // Get the first child's first child's children. + children = childrenFromJsonResponse(child); + childrenFromOtherApi = await childrenFromGetChildrenSummaryTree( + childFromOtherApi['valueId']! as String, group); - // [childJson] contains the 'textPreview' key since the tree was requested - // with previews [getRootWidgetSummaryTreeWithPreviews]. - expect(childJson['textPreview'], equals('c')); + // Verify the first child's first child's children are the same + // length. + expect(children.length, equals(3)); + expect(children.length, equals(childrenFromOtherApi.length)); + + // Get the first child's first child's third child. + child = children[2]! as Map; + childFromOtherApi = + childrenFromOtherApi[2]! as Map; + + // Verify the first child's first child's third child are the same. + expect(child['description'], startsWith('Text')); + expect(child['description'], childFromOtherApi['description']); + expect(child['valueId'], equals(childFromOtherApi['valueId'])); + + // If the tree was requested with previews, then check that the + // child has the `textPreview` key: + if (checkForPreviews) { + expect(child['textPreview'], equals('c')); + } + + // Get the first child's first child's third child's children. + children = childrenFromJsonResponse(child); + childrenFromOtherApi = await childrenFromGetChildrenSummaryTree( + childFromOtherApi['valueId']! as String, group); + + // Verify first child's first child's third child's has no children. + expect(children.length, equals(0)); + expect(childrenFromOtherApi.length, equals(children.length)); + } + + testWidgets('ext.flutter.inspector.getRootWidgetSummaryTree', + (WidgetTester tester) async { + const String group = 'test-group'; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + final Map jsonA = + await selectedWidgetResponseForElement(elementA); + + service.resetPubRootDirectories(); + Map rootJson = (await service.testExtension( + WidgetInspectorServiceExtensions.getRootWidgetSummaryTree.name, + {'objectGroup': group}, + ))! as Map; + + // We haven't yet properly specified which directories are summary tree + // directories so we get an empty tree other than the root that is always + // included. + final Object? rootWidget = + service.toObject(rootJson['valueId']! as String); + expect(rootWidget, equals(WidgetsBinding.instance.rootElement)); + final List childrenJson = + rootJson['children']! as List; + // There are no summary tree children. + expect(childrenJson.length, equals(0)); + + final Map creationLocation = + verifyAndReturnCreationLocation(jsonA); + final String testFile = verifyAndReturnTestFile(creationLocation); + addPubRootDirectoryFor(testFile); + + rootJson = (await service.testExtension( + WidgetInspectorServiceExtensions.getRootWidgetSummaryTree.name, + {'objectGroup': group}, + ))! as Map; + + await verifyChildrenMatchOtherApi(rootJson, group: group); + }); + + testWidgets( + 'ext.flutter.inspector.getRootWidgetSummaryTreeWithPreviews', + (WidgetTester tester) async { + const String group = 'test-group'; + + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + final Map jsonA = + await selectedWidgetResponseForElement(elementA); + + final Map creationLocation = + verifyAndReturnCreationLocation(jsonA); + final String testFile = verifyAndReturnTestFile(creationLocation); + addPubRootDirectoryFor(testFile); + + final Map rootJson = (await service.testExtension( + WidgetInspectorServiceExtensions + .getRootWidgetSummaryTreeWithPreviews.name, + {'groupName': group}, + ))! as Map; + + await verifyChildrenMatchOtherApi( + rootJson, + group: group, + checkForPreviews: true, + ); + }); }); testWidgets('ext.flutter.inspector.getSelectedSummaryWidget',