diff --git a/packages/flutter/test/widgets/widget_inspector_test.dart b/packages/flutter/test/widgets/widget_inspector_test.dart index 32808e3034..38137c6658 100644 --- a/packages/flutter/test/widgets/widget_inspector_test.dart +++ b/packages/flutter/test/widgets/widget_inspector_test.dart @@ -299,6 +299,26 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { } }); + Future pumpWidgetTreeWithABC(WidgetTester tester) async { + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + Text('a', textDirection: TextDirection.ltr), + Text('b', textDirection: TextDirection.ltr), + Text('c', textDirection: TextDirection.ltr), + ], + ), + ), + ); + } + + Element findElementABC(String letter) { + assert(['a', 'b', 'c'].contains(letter)); + return find.text(letter).evaluate().first; + } + test ('objectToDiagnosticsNode returns null for non-diagnosticable', () { expect(WidgetInspectorService.objectToDiagnosticsNode(Alignment.bottomCenter), isNull); }); @@ -745,440 +765,6 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { ); }); - test('WidgetInspectorService null id', () { - service.disposeAllGroups(); - expect(service.toObject(null), isNull); - expect(service.toId(null, 'test-group'), isNull); - }); - - test('WidgetInspectorService dispose group', () { - service.disposeAllGroups(); - final Object a = Object(); - const String group1 = 'group-1'; - const String group2 = 'group-2'; - const String group3 = 'group-3'; - final String? aId = service.toId(a, group1); - expect(service.toId(a, group2), equals(aId)); - expect(service.toId(a, group3), equals(aId)); - service.disposeGroup(group1); - service.disposeGroup(group2); - expect(service.toObject(aId), equals(a)); - service.disposeGroup(group3); - expect(() => service.toObject(aId), throwsFlutterError); - }); - - test('WidgetInspectorService dispose id', () { - service.disposeAllGroups(); - final Object a = Object(); - final Object b = Object(); - const String group1 = 'group-1'; - const String group2 = 'group-2'; - final String? aId = service.toId(a, group1); - final String? bId = service.toId(b, group1); - expect(service.toId(a, group2), equals(aId)); - service.disposeId(bId, group1); - expect(() => service.toObject(bId), throwsFlutterError); - service.disposeId(aId, group1); - expect(service.toObject(aId), equals(a)); - service.disposeId(aId, group2); - expect(() => service.toObject(aId), throwsFlutterError); - }); - - test('WidgetInspectorService toObjectForSourceLocation', () { - const String group = 'test-group'; - const Text widget = Text('a', textDirection: TextDirection.ltr); - service.disposeAllGroups(); - final String id = service.toId(widget, group)!; - expect(service.toObjectForSourceLocation(id), equals(widget)); - final Element element = widget.createElement(); - final String elementId = service.toId(element, group)!; - expect(service.toObjectForSourceLocation(elementId), equals(widget)); - expect(element, isNot(equals(widget))); - service.disposeGroup(group); - expect(() => service.toObjectForSourceLocation(elementId), throwsFlutterError); - }); - - test('WidgetInspectorService object id test', () { - const Text a = Text('a', textDirection: TextDirection.ltr); - const Text b = Text('b', textDirection: TextDirection.ltr); - const Text c = Text('c', textDirection: TextDirection.ltr); - const Text d = Text('d', textDirection: TextDirection.ltr); - - const String group1 = 'group-1'; - const String group2 = 'group-2'; - const String group3 = 'group-3'; - service.disposeAllGroups(); - - final String? aId = service.toId(a, group1); - final String? bId = service.toId(b, group2); - final String? cId = service.toId(c, group3); - final String? dId = service.toId(d, group1); - // Make sure we get a consistent id if we add the object to a group multiple - // times. - expect(aId, equals(service.toId(a, group1))); - expect(service.toObject(aId), equals(a)); - expect(service.toObject(aId), isNot(equals(b))); - expect(service.toObject(bId), equals(b)); - expect(service.toObject(cId), equals(c)); - expect(service.toObject(dId), equals(d)); - // Make sure we get a consistent id even if we add the object to a different - // group. - expect(aId, equals(service.toId(a, group3))); - expect(aId, isNot(equals(bId))); - expect(aId, isNot(equals(cId))); - - service.disposeGroup(group3); - }); - - testWidgets('WidgetInspectorService maybeSetSelection', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; - final Element elementB = find.text('b').evaluate().first; - - service.disposeAllGroups(); - service.selection.clear(); - int selectionChangedCount = 0; - service.selectionChangedCallback = () => selectionChangedCount++; - service.setSelection('invalid selection'); - expect(selectionChangedCount, equals(0)); - expect(service.selection.currentElement, isNull); - service.setSelection(elementA); - expect(selectionChangedCount, equals(1)); - expect(service.selection.currentElement, equals(elementA)); - expect(service.selection.current, equals(elementA.renderObject)); - - service.setSelection(elementB.renderObject); - expect(selectionChangedCount, equals(2)); - expect(service.selection.current, equals(elementB.renderObject)); - expect(service.selection.currentElement, equals((elementB.renderObject!.debugCreator! as DebugCreator).element)); - - service.setSelection('invalid selection'); - expect(selectionChangedCount, equals(2)); - expect(service.selection.current, equals(elementB.renderObject)); - - service.setSelectionById(service.toId(elementA, 'my-group')); - expect(selectionChangedCount, equals(3)); - expect(service.selection.currentElement, equals(elementA)); - expect(service.selection.current, equals(elementA.renderObject)); - - service.setSelectionById(service.toId(elementA, 'my-group')); - expect(selectionChangedCount, equals(3)); - expect(service.selection.currentElement, equals(elementA)); - }); - - testWidgets('WidgetInspectorService defunct selection regression test', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; - - service.setSelection(elementA); - expect(service.selection.currentElement, equals(elementA)); - expect(service.selection.current, equals(elementA.renderObject)); - - await tester.pumpWidget( - const SizedBox( - child: Text('b', textDirection: TextDirection.ltr), - ), - ); - // Selection is now empty as the element is defunct. - expect(service.selection.currentElement, equals(null)); - expect(service.selection.current, equals(null)); - - // Verify that getting the debug creation location of the defunct element - // does not crash. - expect(debugIsLocalCreationLocation(elementA), isFalse); - - // Verify that generating json for a defunct element does not crash. - expect( - elementA.toDiagnosticsNode().toJsonMap( - InspectorSerializationDelegate( - service: service, - includeProperties: true, - ), - ), - isNotNull, - ); - - final Element elementB = find.text('b').evaluate().first; - service.setSelection(elementB); - expect(service.selection.currentElement, equals(elementB)); - expect(service.selection.current, equals(elementB.renderObject)); - - // Set selection back to a defunct element. - service.setSelection(elementA); - - expect(service.selection.currentElement, equals(null)); - expect(service.selection.current, equals(null)); - }); - - testWidgets('WidgetInspectorService getParentChain', (WidgetTester tester) async { - const String group = 'test-group'; - - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - - service.disposeAllGroups(); - final Element elementB = find.text('b').evaluate().first; - final String bId = service.toId(elementB, group)!; - final Object? jsonList = json.decode(service.getParentChain(bId, group)); - expect(jsonList, isList); - final List chainElements = jsonList! as List; - final List expectedChain = elementB.debugGetDiagnosticChain().reversed.toList(); - // Sanity check that the chain goes back to the root. - expect(expectedChain.first, tester.binding.rootElement); - - expect(chainElements.length, equals(expectedChain.length)); - for (int i = 0; i < expectedChain.length; i += 1) { - expect(chainElements[i], isMap); - final Map chainNode = chainElements[i]! as Map; - final Element element = expectedChain[i]; - expect(chainNode['node'], isMap); - final Map jsonNode = chainNode['node']! as Map; - expect(service.toObject(jsonNode['valueId']! as String), equals(element)); - - expect(chainNode['children'], isList); - final List jsonChildren = chainNode['children']! as List; - final List childrenElements = []; - element.visitChildren(childrenElements.add); - expect(jsonChildren.length, equals(childrenElements.length)); - if (i + 1 == expectedChain.length) { - expect(chainNode['childIndex'], isNull); - } else { - expect(chainNode['childIndex'], equals(childrenElements.indexOf(expectedChain[i+1]))); - } - for (int j = 0; j < childrenElements.length; j += 1) { - expect(jsonChildren[j], isMap); - final Map childJson = jsonChildren[j]! as Map; - expect(service.toObject(childJson['valueId']! as String), equals(childrenElements[j])); - } - } - }); - - test('WidgetInspectorService getProperties', () { - const Diagnosticable diagnosticable = Text('a', textDirection: TextDirection.ltr); - const String group = 'group'; - service.disposeAllGroups(); - final String id = service.toId(diagnosticable, group)!; - final List propertiesJson = json.decode(service.getProperties(id, group)) as List; - final List properties = diagnosticable.toDiagnosticsNode().getProperties(); - expect(properties, isNotEmpty); - expect(propertiesJson.length, equals(properties.length)); - for (int i = 0; i < propertiesJson.length; ++i) { - final Map propertyJson = propertiesJson[i]! as Map; - expect(service.toObject(propertyJson['valueId'] as String?), equals(properties[i].value)); - } - }); - - testWidgets('WidgetInspectorService getChildren', (WidgetTester tester) async { - const String group = 'test-group'; - - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final DiagnosticsNode diagnostic = find.byType(Stack).evaluate().first.toDiagnosticsNode(); - service.disposeAllGroups(); - final String id = service.toId(diagnostic, group)!; - final List propertiesJson = json.decode(service.getChildren(id, group)) as List; - final List children = diagnostic.getChildren(); - expect(children.length, equals(3)); - expect(propertiesJson.length, equals(children.length)); - for (int i = 0; i < propertiesJson.length; ++i) { - final Map propertyJson = propertiesJson[i]! as Map; - expect(service.toObject(propertyJson['valueId']! as String), equals(children[i].value)); - } - }); - - testWidgets('WidgetInspectorService creationLocation', (WidgetTester tester) async { - await tester.pumpWidget( - Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - const Text('a'), - const Text('b', textDirection: TextDirection.ltr), - 'c'.text(), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; - final Element elementB = find.text('b').evaluate().first; - final Element elementC = find.text('c').evaluate().first; - - service.disposeAllGroups(); - service.resetPubRootDirectories(); - service.setSelection(elementA, 'my-group'); - final Map jsonA = json.decode(service.getSelectedWidget(null, 'my-group')) as Map; - final Map creationLocationA = jsonA['creationLocation']! as Map; - expect(creationLocationA, isNotNull); - final String fileA = creationLocationA['file']! as String; - final int lineA = creationLocationA['line']! as int; - final int columnA = creationLocationA['column']! as int; - final String nameA = creationLocationA['name']! as String; - expect(nameA, equals('Text')); - - service.setSelection(elementB, 'my-group'); - final Map jsonB = json.decode(service.getSelectedWidget(null, 'my-group')) as Map; - final Map creationLocationB = jsonB['creationLocation']! as Map; - expect(creationLocationB, isNotNull); - final String fileB = creationLocationB['file']! as String; - final int lineB = creationLocationB['line']! as int; - final int columnB = creationLocationB['column']! as int; - final String? nameB = creationLocationB['name'] as String?; - expect(nameB, equals('Text')); - - service.setSelection(elementC, 'my-group'); - final Map jsonC = json.decode(service.getSelectedWidget(null, 'my-group')) as Map; - final Map creationLocationC = jsonC['creationLocation']! as Map; - expect(creationLocationC, isNotNull); - final String fileC = creationLocationC['file']! as String; - final int lineC = creationLocationC['line']! as int; - final int columnC = creationLocationC['column']! as int; - final String? nameC = creationLocationC['name'] as String?; - expect(nameC, equals('TextFromString|text')); - - expect(fileA, endsWith('widget_inspector_test.dart')); - expect(fileA, equals(fileB)); - expect(fileA, equals(fileC)); - // We don't hardcode the actual lines the widgets are created on as that - // would make this test fragile. - expect(lineA + 1, equals(lineB)); - expect(lineB + 1, equals(lineC)); - // Column numbers are more stable than line numbers. - expect(columnA, equals(21)); - expect(columnA, equals(columnB)); - expect(columnC, equals(19)); - }, skip: !WidgetInspectorService.instance.isWidgetCreationTracked()); // [intended] Test requires --track-widget-creation flag. - - testWidgets('WidgetInspectorService setSelection notifiers for an Element', - (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; - - service.disposeAllGroups(); - - setupDefaultPubRootDirectory(service); - - // Select the widget - service.setSelection(elementA, 'my-group'); - - // ensure that developer.inspect was called on the widget - final List objectsInspected = service.inspectedObjects(); - expect(objectsInspected, equals([elementA])); - - // ensure that a navigate event was sent for the element - final List> navigateEventsPosted - = service.dispatchedEvents('navigate', stream: 'ToolEvent',); - expect(navigateEventsPosted.length, equals(1)); - final Map event = navigateEventsPosted[0]; - final String file = event['fileUri']! as String; - final int line = event['line']! as int; - final int column = event['column']! as int; - expect(file, endsWith('widget_inspector_test.dart')); - // We don't hardcode the actual lines the widgets are created on as that - // would make this test fragile. - expect(line, isNotNull); - // Column numbers are more stable than line numbers. - expect(column, equals(15)); - }, - skip: !WidgetInspectorService.instance.isWidgetCreationTracked(), // [intended] Test requires --track-widget-creation flag. - ); - - testWidgets( - 'WidgetInspectorService setSelection notifiers for a RenderObject', - (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; - - service.disposeAllGroups(); - - setupDefaultPubRootDirectory(service); - - // Select the render object for the widget. - service.setSelection(elementA.renderObject, 'my-group'); - - // ensure that developer.inspect was called on the widget - final List objectsInspected = service.inspectedObjects(); - expect(objectsInspected, equals([elementA.renderObject])); - - // ensure that a navigate event was sent for the renderObject - final List> navigateEventsPosted - = service.dispatchedEvents('navigate', stream: 'ToolEvent',); - expect(navigateEventsPosted.length, equals(1)); - final Map event = navigateEventsPosted[0]; - final String file = event['fileUri']! as String; - final int line = event['line']! as int; - final int column = event['column']! as int; - expect(file, endsWith('widget_inspector_test.dart')); - // We don't hardcode the actual lines the widgets are created on as that - // would make this test fragile. - expect(line, isNotNull); - // Column numbers are more stable than line numbers. - expect(column, equals(17)); - }, - skip: !WidgetInspectorService.instance.isWidgetCreationTracked(), // [intended] Test requires --track-widget-creation flag. - ); - testWidgets( 'WidgetInspector selectButton inspection for tap', (WidgetTester tester) async { @@ -1550,36 +1136,300 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { }); }); - group( - 'WidgetInspectorService', - () { - late final String pubRootTest; + group('Widget Inspector Service API', () { + group('Does not require flag --track-widget-location', () { + test('null id', () { + service.disposeAllGroups(); + expect(service.toObject(null), isNull); + expect(service.toId(null, 'test-group'), isNull); + }); - setUpAll(() { - pubRootTest = generateTestPubRootDirectory(service); + test('dispose group', () { + service.disposeAllGroups(); + final Object a = Object(); + const String group1 = 'group-1'; + const String group2 = 'group-2'; + const String group3 = 'group-3'; + final String? aId = service.toId(a, group1); + expect(service.toId(a, group2), equals(aId)); + expect(service.toId(a, group3), equals(aId)); + service.disposeGroup(group1); + service.disposeGroup(group2); + expect(service.toObject(aId), equals(a)); + service.disposeGroup(group3); + expect(() => service.toObject(aId), throwsFlutterError); + }); + + test('dispose id', () { + service.disposeAllGroups(); + final Object a = Object(); + final Object b = Object(); + const String group1 = 'group-1'; + const String group2 = 'group-2'; + final String? aId = service.toId(a, group1); + final String? bId = service.toId(b, group1); + expect(service.toId(a, group2), equals(aId)); + service.disposeId(bId, group1); + expect(() => service.toObject(bId), throwsFlutterError); + service.disposeId(aId, group1); + expect(service.toObject(aId), equals(a)); + service.disposeId(aId, group2); + expect(() => service.toObject(aId), throwsFlutterError); + }); + + test('toObjectForSourceLocation', () { + const String group = 'test-group'; + const Text widget = Text('a', textDirection: TextDirection.ltr); + service.disposeAllGroups(); + final String id = service.toId(widget, group)!; + expect(service.toObjectForSourceLocation(id), equals(widget)); + final Element element = widget.createElement(); + final String elementId = service.toId(element, group)!; + expect(service.toObjectForSourceLocation(elementId), equals(widget)); + expect(element, isNot(equals(widget))); + service.disposeGroup(group); + expect(() => service.toObjectForSourceLocation(elementId), + throwsFlutterError); + }); + + test('object id test', () { + const Text a = Text('a', textDirection: TextDirection.ltr); + const Text b = Text('b', textDirection: TextDirection.ltr); + const Text c = Text('c', textDirection: TextDirection.ltr); + const Text d = Text('d', textDirection: TextDirection.ltr); + + const String group1 = 'group-1'; + const String group2 = 'group-2'; + const String group3 = 'group-3'; + service.disposeAllGroups(); + + final String? aId = service.toId(a, group1); + final String? bId = service.toId(b, group2); + final String? cId = service.toId(c, group3); + final String? dId = service.toId(d, group1); + // Make sure we get a consistent id if we add the object to a group multiple + // times. + expect(aId, equals(service.toId(a, group1))); + expect(service.toObject(aId), equals(a)); + expect(service.toObject(aId), isNot(equals(b))); + expect(service.toObject(bId), equals(b)); + expect(service.toObject(cId), equals(c)); + expect(service.toObject(dId), equals(d)); + // Make sure we get a consistent id even if we add the object to a different + // group. + expect(aId, equals(service.toId(a, group3))); + expect(aId, isNot(equals(bId))); + expect(aId, isNot(equals(cId))); + + service.disposeGroup(group3); + }); + + testWidgets('maybeSetSelection', (WidgetTester tester) async { + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + final Element elementB = findElementABC('b'); + + service.disposeAllGroups(); + service.selection.clear(); + int selectionChangedCount = 0; + service.selectionChangedCallback = () => selectionChangedCount++; + service.setSelection('invalid selection'); + expect(selectionChangedCount, equals(0)); + expect(service.selection.currentElement, isNull); + service.setSelection(elementA); + expect(selectionChangedCount, equals(1)); + expect(service.selection.currentElement, equals(elementA)); + expect(service.selection.current, equals(elementA.renderObject)); + + service.setSelection(elementB.renderObject); + expect(selectionChangedCount, equals(2)); + expect(service.selection.current, equals(elementB.renderObject)); + expect( + service.selection.currentElement, + equals((elementB.renderObject!.debugCreator! as DebugCreator) + .element)); + + service.setSelection('invalid selection'); + expect(selectionChangedCount, equals(2)); + expect(service.selection.current, equals(elementB.renderObject)); + + service.setSelectionById(service.toId(elementA, 'my-group')); + expect(selectionChangedCount, equals(3)); + expect(service.selection.currentElement, equals(elementA)); + expect(service.selection.current, equals(elementA.renderObject)); + + service.setSelectionById(service.toId(elementA, 'my-group')); + expect(selectionChangedCount, equals(3)); + expect(service.selection.currentElement, equals(elementA)); + }); + + testWidgets('defunct selection regression test', + (WidgetTester tester) async { + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + Text('a', textDirection: TextDirection.ltr), + ], + ), + ), + ); + final Element elementA = find.text('a').evaluate().first; + + service.setSelection(elementA); + expect(service.selection.currentElement, equals(elementA)); + expect(service.selection.current, equals(elementA.renderObject)); + + await tester.pumpWidget( + const SizedBox( + child: Text('b', textDirection: TextDirection.ltr), + ), + ); + // Selection is now empty as the element is defunct. + expect(service.selection.currentElement, equals(null)); + expect(service.selection.current, equals(null)); + + // Verify that getting the debug creation location of the defunct element + // does not crash. + expect(debugIsLocalCreationLocation(elementA), isFalse); + + // Verify that generating json for a defunct element does not crash. + expect( + elementA.toDiagnosticsNode().toJsonMap( + InspectorSerializationDelegate( + service: service, + includeProperties: true, + ), + ), + isNotNull, + ); + + final Element elementB = find.text('b').evaluate().first; + service.setSelection(elementB); + expect(service.selection.currentElement, equals(elementB)); + expect(service.selection.current, equals(elementB.renderObject)); + + // Set selection back to a defunct element. + service.setSelection(elementA); + + expect(service.selection.currentElement, equals(null)); + expect(service.selection.current, equals(null)); + }); + + testWidgets('getParentChain', (WidgetTester tester) async { + const String group = 'test-group'; + + await pumpWidgetTreeWithABC(tester); + + service.disposeAllGroups(); + final Element elementB = findElementABC('b'); + final String bId = service.toId(elementB, group)!; + final Object? jsonList = + json.decode(service.getParentChain(bId, group)); + expect(jsonList, isList); + final List chainElements = jsonList! as List; + final List expectedChain = + elementB.debugGetDiagnosticChain().reversed.toList(); + // Sanity check that the chain goes back to the root. + expect(expectedChain.first, tester.binding.rootElement); + + expect(chainElements.length, equals(expectedChain.length)); + for (int i = 0; i < expectedChain.length; i += 1) { + expect(chainElements[i], isMap); + final Map chainNode = + chainElements[i]! as Map; + final Element element = expectedChain[i]; + expect(chainNode['node'], isMap); + final Map jsonNode = + chainNode['node']! as Map; + expect(service.toObject(jsonNode['valueId']! as String), + equals(element)); + + expect(chainNode['children'], isList); + final List jsonChildren = + chainNode['children']! as List; + final List childrenElements = []; + element.visitChildren(childrenElements.add); + expect(jsonChildren.length, equals(childrenElements.length)); + if (i + 1 == expectedChain.length) { + expect(chainNode['childIndex'], isNull); + } else { + expect(chainNode['childIndex'], + equals(childrenElements.indexOf(expectedChain[i + 1]))); + } + for (int j = 0; j < childrenElements.length; j += 1) { + expect(jsonChildren[j], isMap); + final Map childJson = + jsonChildren[j]! as Map; + expect(service.toObject(childJson['valueId']! as String), + equals(childrenElements[j])); + } + } + }); + + test('getProperties', () { + const Diagnosticable diagnosticable = + Text('a', textDirection: TextDirection.ltr); + const String group = 'group'; + service.disposeAllGroups(); + final String id = service.toId(diagnosticable, group)!; + final List propertiesJson = + json.decode(service.getProperties(id, group)) as List; + final List properties = + diagnosticable.toDiagnosticsNode().getProperties(); + expect(properties, isNotEmpty); + expect(propertiesJson.length, equals(properties.length)); + for (int i = 0; i < propertiesJson.length; ++i) { + final Map propertyJson = + propertiesJson[i]! as Map; + expect(service.toObject(propertyJson['valueId'] as String?), + equals(properties[i].value)); + } + }); + + testWidgets('getChildren', (WidgetTester tester) async { + const String group = 'test-group'; + + await pumpWidgetTreeWithABC(tester); + + final DiagnosticsNode diagnostic = + find.byType(Stack).evaluate().first.toDiagnosticsNode(); + service.disposeAllGroups(); + final String id = service.toId(diagnostic, group)!; + final List propertiesJson = + json.decode(service.getChildren(id, group)) as List; + final List children = diagnostic.getChildren(); + expect(children.length, equals(3)); + expect(propertiesJson.length, equals(children.length)); + for (int i = 0; i < propertiesJson.length; ++i) { + final Map propertyJson = + propertiesJson[i]! as Map; + expect(service.toObject(propertyJson['valueId']! as String), + equals(children[i].value)); + } + }); }); - setUp(() { - service.disposeAllGroups(); - service.resetPubRootDirectories(); - }); + group('Requires flag --track-widget-creation', () { + + late final String pubRootTest; + + setUpAll(() { + pubRootTest = generateTestPubRootDirectory(service); + }); + + setUp(() { + service.disposeAllGroups(); + service.resetPubRootDirectories(); + }); group('addPubRootDirectories', () { testWidgets( 'does not have createdByLocalProject when there are no pubRootDirectories', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); final Map jsonObject = @@ -1598,18 +1448,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject when the element is part of the pubRootDirectory', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.addPubRootDirectories([pubRootTest]); @@ -1624,18 +1464,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'does not have createdByLocalProject when widget package directory is a suffix of a pubRootDirectory', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.addPubRootDirectories(['/invalid/$pubRootTest']); @@ -1649,18 +1479,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject when the pubRootDirectory is prefixed with file://', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.addPubRootDirectories(['file://$pubRootTest']); @@ -1674,18 +1494,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'does not have createdByLocalProject when thePubRootDirectory has a different suffix', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.addPubRootDirectories(['$pubRootTest/different']); @@ -1699,18 +1509,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject even if another pubRootDirectory does not match', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.addPubRootDirectories([ @@ -1727,18 +1527,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'widget is part of core framework and is the child of a widget in the package pubRootDirectories', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); final Element richText = find .descendant( of: find.text('a'), @@ -1796,257 +1586,664 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { ); }); - group('createdByLocalProject', () { - setUp(() { + group('createdByLocalProject', () { + setUp(() { + service.resetPubRootDirectories(); + }); + + testWidgets( + 'reacts to add and removing pubRootDirectories', + (WidgetTester tester) async { + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + + service.addPubRootDirectories([ + pubRootTest, + 'file://$pubRootTest', + '/unrelated/$pubRootTest', + ]); + + service.setSelection(elementA, 'my-group'); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + contains('createdByLocalProject'), + ); + + service.removePubRootDirectories([pubRootTest]); + + service.setSelection(elementA, 'my-group'); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + isNot(contains('createdByLocalProject')), + ); + }, + ); + + testWidgets( + 'does not match when the package directory does not match', + (WidgetTester tester) async { + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + service.setSelection(elementA, 'my-group'); + + service.addPubRootDirectories([ + '$pubRootTest/different', + '/unrelated/$pubRootTest', + ]); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + isNot(contains('createdByLocalProject')), + ); + }, + ); + + testWidgets( + 'has createdByLocalProject when the pubRootDirectory is prefixed with file://', + (WidgetTester tester) async { + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + service.setSelection(elementA, 'my-group'); + + service.addPubRootDirectories(['file://$pubRootTest']); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + contains('createdByLocalProject'), + ); + }, + ); + + testWidgets( + 'can handle consecutive calls to add', + (WidgetTester tester) async { + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + service.setSelection(elementA, 'my-group'); + + service.addPubRootDirectories([ + pubRootTest, + ]); + service.addPubRootDirectories([ + '/invalid/$pubRootTest', + ]); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + contains('createdByLocalProject'), + ); + }, + ); + testWidgets( + 'can handle removing an unrelated pubRootDirectory', + (WidgetTester tester) async { + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + service.setSelection(elementA, 'my-group'); + + service.addPubRootDirectories([ + pubRootTest, + '/invalid/$pubRootTest', + ]); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + contains('createdByLocalProject'), + ); + + service.removePubRootDirectories([ + '/invalid/$pubRootTest', + ]); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + contains('createdByLocalProject'), + ); + }, + ); + + testWidgets( + 'can handle parent widget being part of a separate package', + (WidgetTester tester) async { + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + final Element richText = find + .descendant( + of: find.text('a'), + matching: find.byType(RichText), + ) + .evaluate() + .first; + service.setSelection(richText, 'my-group'); + service.addPubRootDirectories([pubRootTest]); + + final Map jsonObject = + json.decode(service.getSelectedWidget(null, 'my-group')) + as Map; + expect(jsonObject, isNot(contains('createdByLocalProject'))); + final Map creationLocation = + jsonObject['creationLocation']! as Map; + expect(creationLocation, isNotNull); + // This RichText widget is created by the build method of the Text widget + // thus the creation location is in text.dart not basic.dart + final List pathSegmentsFramework = + Uri.parse(creationLocation['file']! as String).pathSegments; + expect( + pathSegmentsFramework.join('/'), + endsWith('/flutter/lib/src/widgets/text.dart'), + ); + + // Strip off /src/widgets/text.dart. + final String pubRootFramework = + '/${pathSegmentsFramework.take(pathSegmentsFramework.length - 3).join('/')}'; + service.resetPubRootDirectories(); + service.addPubRootDirectories([pubRootFramework]); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + contains('createdByLocalProject'), + ); + service.setSelection(elementA, 'my-group'); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + isNot(contains('createdByLocalProject')), + ); + + service.resetPubRootDirectories(); + service.addPubRootDirectories( + [pubRootFramework, pubRootTest]); + service.setSelection(elementA, 'my-group'); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + contains('createdByLocalProject'), + ); + service.setSelection(richText, 'my-group'); + expect( + json.decode(service.getSelectedWidget(null, 'my-group')), + contains('createdByLocalProject'), + ); + }, + ); + }); + + testWidgets('creationLocation', (WidgetTester tester) async { + await tester.pumpWidget( + Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + const Text('a'), + const Text('b', textDirection: TextDirection.ltr), + 'c'.text(), + ], + ), + ), + ); + final Element elementA = find.text('a').evaluate().first; + final Element elementB = find.text('b').evaluate().first; + final Element elementC = find.text('c').evaluate().first; + + service.disposeAllGroups(); service.resetPubRootDirectories(); + service.setSelection(elementA, 'my-group'); + final Map jsonA = + json.decode(service.getSelectedWidget(null, 'my-group')) + as Map; + final Map creationLocationA = + jsonA['creationLocation']! as Map; + expect(creationLocationA, isNotNull); + final String fileA = creationLocationA['file']! as String; + final int lineA = creationLocationA['line']! as int; + final int columnA = creationLocationA['column']! as int; + final String nameA = creationLocationA['name']! as String; + expect(nameA, equals('Text')); + + service.setSelection(elementB, 'my-group'); + final Map jsonB = + json.decode(service.getSelectedWidget(null, 'my-group')) + as Map; + final Map creationLocationB = + jsonB['creationLocation']! as Map; + expect(creationLocationB, isNotNull); + final String fileB = creationLocationB['file']! as String; + final int lineB = creationLocationB['line']! as int; + final int columnB = creationLocationB['column']! as int; + final String? nameB = creationLocationB['name'] as String?; + expect(nameB, equals('Text')); + + service.setSelection(elementC, 'my-group'); + final Map jsonC = + json.decode(service.getSelectedWidget(null, 'my-group')) + as Map; + final Map creationLocationC = + jsonC['creationLocation']! as Map; + expect(creationLocationC, isNotNull); + final String fileC = creationLocationC['file']! as String; + final int lineC = creationLocationC['line']! as int; + final int columnC = creationLocationC['column']! as int; + final String? nameC = creationLocationC['name'] as String?; + expect(nameC, equals('TextFromString|text')); + + expect(fileA, endsWith('widget_inspector_test.dart')); + expect(fileA, equals(fileB)); + expect(fileA, equals(fileC)); + // We don't hardcode the actual lines the widgets are created on as that + // would make this test fragile. + expect(lineA + 1, equals(lineB)); + expect(lineB + 1, equals(lineC)); + // Column numbers are more stable than line numbers. + expect(columnA, equals(25)); + expect(columnA, equals(columnB)); + expect(columnC, equals(23)); + }); + + testWidgets('setSelection notifiers for an Element', + (WidgetTester tester) async { + await tester.pumpWidget( + const Directionality( + textDirection: TextDirection.ltr, + child: Stack( + children: [ + Text('a'), + Text('b', textDirection: TextDirection.ltr), + Text('c', textDirection: TextDirection.ltr), + ], + ), + ), + ); + final Element elementA = find.text('a').evaluate().first; + + service.disposeAllGroups(); + + setupDefaultPubRootDirectory(service); + + // Select the widget + service.setSelection(elementA, 'my-group'); + + // ensure that developer.inspect was called on the widget + final List objectsInspected = service.inspectedObjects(); + expect(objectsInspected, equals([elementA])); + + // ensure that a navigate event was sent for the element + final List> navigateEventsPosted = + service.dispatchedEvents( + 'navigate', + stream: 'ToolEvent', + ); + expect(navigateEventsPosted.length, equals(1)); + final Map event = navigateEventsPosted[0]; + final String file = event['fileUri']! as String; + final int line = event['line']! as int; + final int column = event['column']! as int; + expect(file, endsWith('widget_inspector_test.dart')); + // We don't hardcode the actual lines the widgets are created on as that + // would make this test fragile. + expect(line, isNotNull); + // Column numbers are more stable than line numbers. + expect(column, equals(19)); + }); + + testWidgets('setSelection notifiers for a RenderObject', + (WidgetTester tester) async { + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + + service.disposeAllGroups(); + + setupDefaultPubRootDirectory(service); + + // Select the render object for the widget. + service.setSelection(elementA.renderObject, 'my-group'); + + // ensure that developer.inspect was called on the widget + final List objectsInspected = service.inspectedObjects(); + expect( + objectsInspected, equals([elementA.renderObject])); + + // ensure that a navigate event was sent for the renderObject + final List> navigateEventsPosted = + service.dispatchedEvents( + 'navigate', + stream: 'ToolEvent', + ); + expect(navigateEventsPosted.length, equals(1)); + final Map event = navigateEventsPosted[0]; + final String file = event['fileUri']! as String; + final int line = event['line']! as int; + final int column = event['column']! as int; + expect(file, endsWith('widget_inspector_test.dart')); + // We don't hardcode the actual lines the widgets are created on as that + // would make this test fragile. + expect(line, isNotNull); + // Column numbers are more stable than line numbers. + 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; + + 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)); + List childrenJson = rootJson['children']! as List; + // There are no summary tree children. + expect(childrenJson.length, equals(0)); + + 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}, + ); + + 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)); + + 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'])); + + 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; + + 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; + expect(alternateChildrenJson.length, equals(0)); + // Tests are failing when this typo is fixed. + expect(childJson['chidlren'], isNull); }); testWidgets( - 'reacts to add and removing pubRootDirectories', - (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + 'ext.flutter.inspector.getRootWidgetSummaryTreeWithPreviews', + (WidgetTester tester) async { + const String group = 'test-group'; - service.addPubRootDirectories([ - pubRootTest, - 'file://$pubRootTest', - '/unrelated/$pubRootTest', - ]); + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); - service.setSelection(elementA, 'my-group'); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - contains('createdByLocalProject'), - ); + service + ..disposeAllGroups() + ..resetPubRootDirectories() + ..setSelection(elementA, 'my-group'); - service.removePubRootDirectories([pubRootTest]); + final Map jsonA = (await service.testExtension( + WidgetInspectorServiceExtensions.getSelectedWidget.name, + {'objectGroup': 'my-group'}, + ))! as Map; - service.setSelection(elementA, 'my-group'); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - isNot(contains('createdByLocalProject')), - ); - }, - ); + 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]); - testWidgets( - 'does not match when the package directory does not match', - (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; - service.setSelection(elementA, 'my-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)); - service.addPubRootDirectories([ - '$pubRootTest/different', - '/unrelated/$pubRootTest', - ]); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - isNot(contains('createdByLocalProject')), - ); - }, - ); + 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'])); - testWidgets( - 'has createdByLocalProject when the pubRootDirectory is prefixed with file://', - (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; - service.setSelection(elementA, 'my-group'); + 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; - service.addPubRootDirectories(['file://$pubRootTest']); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - contains('createdByLocalProject'), - ); - }, - ); + 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')); - testWidgets( - 'can handle consecutive calls to add', - (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; - service.setSelection(elementA, 'my-group'); + // [childJson] contains the 'textPreview' key since the tree was requested + // with previews [getRootWidgetSummaryTreeWithPreviews]. + expect(childJson['textPreview'], equals('c')); + }); - service.addPubRootDirectories([ - pubRootTest, - ]); - service.addPubRootDirectories([ - '/invalid/$pubRootTest', - ]); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - contains('createdByLocalProject'), - ); - }, - ); - testWidgets( - 'can handle removing an unrelated pubRootDirectory', - (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; - service.setSelection(elementA, 'my-group'); + testWidgets('ext.flutter.inspector.getSelectedSummaryWidget', + (WidgetTester tester) async { + const String group = 'test-group'; - service.addPubRootDirectories([ - pubRootTest, - '/invalid/$pubRootTest', - ]); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - contains('createdByLocalProject'), - ); + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); - service.removePubRootDirectories([ - '/invalid/$pubRootTest', - ]); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - contains('createdByLocalProject'), - ); - }, - ); + final List children = + elementA.debugDescribeChildren(); + expect(children.length, equals(1)); + final DiagnosticsNode richTextDiagnostic = children.first; - testWidgets( - 'can handle parent widget being part of a separate package', - (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; - final Element richText = find - .descendant( - of: find.text('a'), - matching: find.byType(RichText), - ) - .evaluate() - .first; - service.setSelection(richText, 'my-group'); - service.addPubRootDirectories([pubRootTest]); + service.disposeAllGroups(); + service.resetPubRootDirectories(); + service.setSelection(elementA, 'my-group'); + final Map jsonA = (await service.testExtension( + WidgetInspectorServiceExtensions.getSelectedWidget.name, + {'objectGroup': 'my-group'}, + ))! as Map; + service.setSelection(richTextDiagnostic.value, 'my-group'); - final Map jsonObject = - json.decode(service.getSelectedWidget(null, 'my-group')) - as Map; - expect(jsonObject, isNot(contains('createdByLocalProject'))); - final Map creationLocation = - jsonObject['creationLocation']! as Map; - expect(creationLocation, isNotNull); - // This RichText widget is created by the build method of the Text widget - // thus the creation location is in text.dart not basic.dart - final List pathSegmentsFramework = - Uri.parse(creationLocation['file']! as String).pathSegments; - expect( - pathSegmentsFramework.join('/'), - endsWith('/flutter/lib/src/widgets/text.dart'), - ); + service.resetPubRootDirectories(); + Map? summarySelection = await service.testExtension( + WidgetInspectorServiceExtensions.getSelectedSummaryWidget.name, + {'objectGroup': group}, + ) as Map?; + // No summary selection because we haven't set the pub root directories + // yet to indicate what directories are in the summary tree. + expect(summarySelection, isNull); - // Strip off /src/widgets/text.dart. - final String pubRootFramework = - '/${pathSegmentsFramework.take(pathSegmentsFramework.length - 3).join('/')}'; - service.resetPubRootDirectories(); - service.addPubRootDirectories([pubRootFramework]); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - contains('createdByLocalProject'), - ); - service.setSelection(elementA, 'my-group'); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - isNot(contains('createdByLocalProject')), - ); + 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}, + ); - service.resetPubRootDirectories(); - service - .addPubRootDirectories([pubRootFramework, pubRootTest]); - service.setSelection(elementA, 'my-group'); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - contains('createdByLocalProject'), - ); - service.setSelection(richText, 'my-group'); - expect( - json.decode(service.getSelectedWidget(null, 'my-group')), - contains('createdByLocalProject'), - ); - }, - ); - }); - }, - skip: !WidgetInspectorService.instance.isWidgetCreationTracked(), // [intended] Test requires --track-widget-creation flag. - ); + summarySelection = (await service.testExtension( + WidgetInspectorServiceExtensions.getSelectedSummaryWidget.name, + {'objectGroup': group}, + ))! as Map; + expect(summarySelection['valueId'], isNotNull); + // We got the Text element instead of the selected RichText element + // because only the RichText element is part of the summary tree. + expect(service.toObject(summarySelection['valueId']! as String), + elementA); + + // Verify tha the regular getSelectedWidget method still returns + // the RichText object not the Text element. + final Map regularSelection = + (await service.testExtension( + WidgetInspectorServiceExtensions.getSelectedWidget.name, + {'objectGroup': 'my-group'}, + ))! as Map; + expect(service.toObject(regularSelection['valueId']! as String), + richTextDiagnostic.value); + }); + + testWidgets('ext.flutter.inspector creationLocation', + (WidgetTester tester) async { + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + final Element elementB = findElementABC('b'); + + service.disposeAllGroups(); + service.resetPubRootDirectories(); + service.setSelection(elementA, 'my-group'); + final Map jsonA = (await service.testExtension( + WidgetInspectorServiceExtensions.getSelectedWidget.name, + {'objectGroup': 'my-group'}, + ))! as Map; + final Map creationLocationA = + jsonA['creationLocation']! as Map; + expect(creationLocationA, isNotNull); + final String fileA = creationLocationA['file']! as String; + final int lineA = creationLocationA['line']! as int; + final int columnA = creationLocationA['column']! as int; + + service.setSelection(elementB, 'my-group'); + final Map jsonB = (await service.testExtension( + WidgetInspectorServiceExtensions.getSelectedWidget.name, + {'objectGroup': 'my-group'}, + ))! as Map; + final Map creationLocationB = + jsonB['creationLocation']! as Map; + expect(creationLocationB, isNotNull); + final String fileB = creationLocationB['file']! as String; + final int lineB = creationLocationB['line']! as int; + final int columnB = creationLocationB['column']! as int; + expect(fileA, endsWith('widget_inspector_test.dart')); + expect(fileA, equals(fileB)); + // We don't hardcode the actual lines the widgets are created on as that + // would make this test fragile. + expect(lineA + 1, equals(lineB)); + // Column numbers are more stable than line numbers. + expect(columnA, equals(15)); + expect(columnA, equals(columnB)); + }); + }, skip: !WidgetInspectorService.instance.isWidgetCreationTracked()); // [intended] Test requires --track-widget-creation flag. + }); group('InspectorSelection', () { testWidgets('receives notifications when selection changes', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b'), - ], - ), - ), - ); + await pumpWidgetTreeWithABC(tester); final InspectorSelection selection = InspectorSelection(); addTearDown(selection.dispose); int count = 0; @@ -2130,20 +2327,9 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { }); testWidgets('ext.flutter.inspector.setSelection', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; - final Element elementB = find.text('b').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); + final Element elementB = findElementABC('b'); service.disposeAllGroups(); service.selection.clear(); @@ -2182,20 +2368,9 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets('ext.flutter.inspector.getParentChain', (WidgetTester tester) async { const String group = 'test-group'; - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); + await pumpWidgetTreeWithABC(tester); + final Element elementB = findElementABC('b'); - final Element elementB = find.text('b').evaluate().first; final String bId = service.toId(elementB, group)!; final Object? jsonList = await service.testExtension( WidgetInspectorServiceExtensions.getParentChain.name, @@ -2253,19 +2428,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets('ext.flutter.inspector.getChildren', (WidgetTester tester) async { const String group = 'test-group'; - - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); + await pumpWidgetTreeWithABC(tester); final DiagnosticsNode diagnostic = find.byType(Stack).evaluate().first.toDiagnosticsNode(); final String id = service.toId(diagnostic, group)!; final List propertiesJson = (await service.testExtension( @@ -2283,19 +2446,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets('ext.flutter.inspector.getChildrenDetailsSubtree', (WidgetTester tester) async { const String group = 'test-group'; - - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); + await pumpWidgetTreeWithABC(tester); final Diagnosticable diagnosticable = find.byType(Stack).evaluate().first; final String id = service.toId(diagnosticable, group)!; final List childrenJson = (await service.testExtension( @@ -2323,18 +2474,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets('WidgetInspectorService getDetailsSubtree', (WidgetTester tester) async { const String group = 'test-group'; - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); + await pumpWidgetTreeWithABC(tester); final Diagnosticable diagnosticable = find.byType(Stack).evaluate().first; final String id = service.toId(diagnosticable, group)!; final Map subtreeJson = (await service.testExtension( @@ -2412,327 +2552,6 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { expect(nestedRelatedProperty, isNot(contains('children'))); }); - testWidgets('ext.flutter.inspector.getRootWidgetSummaryTree', (WidgetTester tester) async { - const String group = 'test-group'; - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - - final Element elementA = find.text('a').evaluate().first; - - service.disposeAllGroups(); - service.resetPubRootDirectories(); - service.setSelection(elementA, 'my-group'); - final Map jsonA = (await service.testExtension( - WidgetInspectorServiceExtensions.getSelectedWidget.name, - {'objectGroup': 'my-group'}, - ))! as Map; - - 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)); - List childrenJson = rootJson['children']! as List; - // There are no summary tree children. - expect(childrenJson.length, equals(0)); - - 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}, - ); - - 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)); - - 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'])); - - 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; - - 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; - expect(alternateChildrenJson.length , equals(0)); - // Tests are failing when this typo is fixed. - expect(childJson['chidlren'], isNull); - }, skip: !WidgetInspectorService.instance.isWidgetCreationTracked()); // [intended] Test requires --track-widget-creation flag. - - testWidgets('ext.flutter.inspector.getRootWidgetSummaryTreeWithPreviews', (WidgetTester tester) async { - const String group = 'test-group'; - - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; - - service - ..disposeAllGroups() - ..resetPubRootDirectories() - ..setSelection(elementA, 'my-group'); - - final Map jsonA = (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() - ..addPubRootDirectories([pubRootTest]); - - 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)); - - 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'])); - - 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; - - 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')); - - // [childJson] contains the 'textPreview' key since the tree was requested - // with previews [getRootWidgetSummaryTreeWithPreviews]. - expect(childJson['textPreview'], equals('c')); - }, skip: !WidgetInspectorService.instance.isWidgetCreationTracked()); // [intended] Test requires --track-widget-creation flag. - - testWidgets('ext.flutter.inspector.getSelectedSummaryWidget', (WidgetTester tester) async { - const String group = 'test-group'; - - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a', textDirection: TextDirection.ltr), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; - - final List children = elementA.debugDescribeChildren(); - expect(children.length, equals(1)); - final DiagnosticsNode richTextDiagnostic = children.first; - - service.disposeAllGroups(); - service.resetPubRootDirectories(); - service.setSelection(elementA, 'my-group'); - final Map jsonA = (await service.testExtension( - WidgetInspectorServiceExtensions.getSelectedWidget.name, - {'objectGroup': 'my-group'}, - ))! as Map; - service.setSelection(richTextDiagnostic.value, 'my-group'); - - service.resetPubRootDirectories(); - Map? summarySelection = await service.testExtension( - WidgetInspectorServiceExtensions.getSelectedSummaryWidget.name, - {'objectGroup': group}, - ) as Map?; - // No summary selection because we haven't set the pub root directories - // yet to indicate what directories are in the summary tree. - expect(summarySelection, isNull); - - 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}, - ); - - summarySelection = (await service.testExtension( - WidgetInspectorServiceExtensions.getSelectedSummaryWidget.name, - {'objectGroup': group}, - ))! as Map; - expect(summarySelection['valueId'], isNotNull); - // We got the Text element instead of the selected RichText element - // because only the RichText element is part of the summary tree. - expect(service.toObject(summarySelection['valueId']! as String), elementA); - - // Verify tha the regular getSelectedWidget method still returns - // the RichText object not the Text element. - final Map regularSelection = (await service.testExtension( - WidgetInspectorServiceExtensions.getSelectedWidget.name, - {'objectGroup': 'my-group'}, - ))! as Map; - expect(service.toObject(regularSelection['valueId']! as String), richTextDiagnostic.value); - }, skip: !WidgetInspectorService.instance.isWidgetCreationTracked()); // [intended] Test requires --track-widget-creation flag. - - testWidgets('ext.flutter.inspector creationLocation', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; - final Element elementB = find.text('b').evaluate().first; - - service.disposeAllGroups(); - service.resetPubRootDirectories(); - service.setSelection(elementA, 'my-group'); - final Map jsonA = (await service.testExtension( - WidgetInspectorServiceExtensions.getSelectedWidget.name, - {'objectGroup': 'my-group'}, - ))! as Map; - final Map creationLocationA = jsonA['creationLocation']! as Map; - expect(creationLocationA, isNotNull); - final String fileA = creationLocationA['file']! as String; - final int lineA = creationLocationA['line']! as int; - final int columnA = creationLocationA['column']! as int; - - service.setSelection(elementB, 'my-group'); - final Map jsonB = (await service.testExtension( - WidgetInspectorServiceExtensions.getSelectedWidget.name, - {'objectGroup': 'my-group'}, - ))! as Map; - final Map creationLocationB = jsonB['creationLocation']! as Map; - expect(creationLocationB, isNotNull); - final String fileB = creationLocationB['file']! as String; - final int lineB = creationLocationB['line']! as int; - final int columnB = creationLocationB['column']! as int; - expect(fileA, endsWith('widget_inspector_test.dart')); - expect(fileA, equals(fileB)); - // We don't hardcode the actual lines the widgets are created on as that - // would make this test fragile. - expect(lineA + 1, equals(lineB)); - // Column numbers are more stable than line numbers. - expect(columnA, equals(15)); - expect(columnA, equals(columnB)); - }, skip: !WidgetInspectorService.instance.isWidgetCreationTracked()); // [intended] Test requires --track-widget-creation flag. - group( 'ext.flutter.inspector.addPubRootDirectories group', () { @@ -2749,20 +2568,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject when the widget is in the pubRootDirectory', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -2782,20 +2589,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'does not have createdByLocalProject if the prefix of the pubRootDirectory is different', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -2815,20 +2610,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject if the pubRootDirectory is prefixed with file://', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -2848,20 +2631,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'does not have createdByLocalProject if the pubRootDirectory has a different suffix', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -2881,20 +2652,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject if at least one of the pubRootDirectories matches', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -2918,19 +2677,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'widget is part of core framework and is the child of a widget in the package pubRootDirectories', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); // The RichText child of the Text widget is created by the core framework // not the current package. @@ -3029,19 +2777,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject when the widget is in the pubRootDirectory', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -3061,19 +2798,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'does not have createdByLocalProject if the prefix of the pubRootDirectory is different', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -3096,19 +2822,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject if the pubRootDirectory is prefixed with file://', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -3128,19 +2843,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'does not have createdByLocalProject if the pubRootDirectory has a different suffix', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -3163,19 +2867,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject if at least one of the pubRootDirectories matches', (WidgetTester tester) async { - await tester.pumpWidget( - const Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ), - ); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); await service.testExtension( @@ -3221,18 +2914,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'reacts to add and removing pubRootDirectories', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); await service.testExtension( WidgetInspectorServiceExtensions.addPubRootDirectories.name, @@ -3271,18 +2954,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'does not match when the package directory does not match', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.testExtension( @@ -3305,18 +2978,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject when the pubRootDirectory is prefixed with file://', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.testExtension( @@ -3336,18 +2999,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'can handle consecutive calls to add', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.testExtension( @@ -3370,18 +3023,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'can handle removing an unrelated pubRootDirectory', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.testExtension( @@ -3416,18 +3059,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'can handle parent widget being part of a separate package', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); final Element richText = find .descendant( of: find.text('a'), @@ -3527,18 +3160,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'reacts to add and removing pubRootDirectories', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); await service.testExtension( WidgetInspectorServiceExtensions.addPubRootDirectories.name, @@ -3579,18 +3202,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'does not match when the package directory does not match', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.testExtension( @@ -3613,18 +3226,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'has createdByLocalProject when the pubRootDirectory is prefixed with file://', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.testExtension( @@ -3647,18 +3250,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'can handle consecutive calls to add', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.testExtension( @@ -3687,18 +3280,8 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { testWidgets( 'can handle removing an unrelated pubRootDirectory', (WidgetTester tester) async { - const Widget widget = Directionality( - textDirection: TextDirection.ltr, - child: Stack( - children: [ - Text('a'), - Text('b', textDirection: TextDirection.ltr), - Text('c', textDirection: TextDirection.ltr), - ], - ), - ); - await tester.pumpWidget(widget); - final Element elementA = find.text('a').evaluate().first; + await pumpWidgetTreeWithABC(tester); + final Element elementA = findElementABC('a'); service.setSelection(elementA, 'my-group'); service.testExtension(