From dd4a8150a8406920467ba1b1168e5d327bf2ec6b Mon Sep 17 00:00:00 2001 From: Polina Cherkasova Date: Thu, 15 Jun 2023 17:04:46 -0700 Subject: [PATCH] Update getProperties to handle Diagnosticable as input. (#128897) --- .../lib/src/widgets/widget_inspector.dart | 36 +++++++++++------- .../test/widgets/widget_inspector_test.dart | 38 ++++++++++++++++++- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/packages/flutter/lib/src/widgets/widget_inspector.dart b/packages/flutter/lib/src/widgets/widget_inspector.dart index c7ff524e42..9c04f25dae 100644 --- a/packages/flutter/lib/src/widgets/widget_inspector.dart +++ b/packages/flutter/lib/src/widgets/widget_inspector.dart @@ -1722,9 +1722,12 @@ mixin WidgetInspectorService { return _safeJsonEncode(_getProperties(diagnosticsNodeId, groupName)); } - List _getProperties(String? diagnosticsNodeId, String groupName) { - final DiagnosticsNode? node = toObject(diagnosticsNodeId) as DiagnosticsNode?; - return _nodesToJson(node == null ? const [] : node.getProperties(), InspectorSerializationDelegate(groupName: groupName, service: this), parent: node); + List _getProperties(String? diagnosticsOrDiagnosticableId, String groupName) { + final DiagnosticsNode? node = _idToDiagnosticsNode(diagnosticsOrDiagnosticableId); + if (node == null) { + return const []; + } + return _nodesToJson(node.getProperties(), InspectorSerializationDelegate(groupName: groupName, service: this), parent: node); } /// Returns a JSON representation of the children of the [DiagnosticsNode] @@ -1757,26 +1760,31 @@ mixin WidgetInspectorService { return _safeJsonEncode(_getChildrenSummaryTree(diagnosticsNodeId, groupName)); } - List _getChildrenSummaryTree(String? diagnosticsNodeId, String groupName) { - final Object? theObject = toObject(diagnosticsNodeId); - final InspectorSerializationDelegate delegate = InspectorSerializationDelegate(groupName: groupName, summaryTree: true, service: this); - - // TODO(polina-c): remove this, when DevTools stops sending DiagnosticsNode. + DiagnosticsNode? _idToDiagnosticsNode(String? diagnosticsOrDiagnosticableId) { + // TODO(polina-c): start always assuming Diagnosticable, when DevTools stops sending DiagnosticsNode to + // getChildrenSummaryTree and getProperties. // https://github.com/flutter/devtools/issues/3951 + final Object? theObject = toObject(diagnosticsOrDiagnosticableId); if (theObject is DiagnosticsNode) { - return _nodesToJson(_getChildrenFiltered(theObject, delegate), delegate, parent: theObject); + return theObject; } - if (theObject is Diagnosticable) { - final DiagnosticsNode node = theObject.toDiagnosticsNode(); - return _nodesToJson(_getChildrenFiltered(node, delegate), delegate, parent: node); + return theObject.toDiagnosticsNode(); } - if (theObject == null) { + return null; + } + throw StateError('Unexpected object type ${theObject.runtimeType}.'); + } + + List _getChildrenSummaryTree(String? diagnosticsOrDiagnosticableId, String groupName) { + final DiagnosticsNode? node = _idToDiagnosticsNode(diagnosticsOrDiagnosticableId); + if (node == null) { return []; } - throw StateError('Unexpected object type ${theObject.runtimeType}'); + final InspectorSerializationDelegate delegate = InspectorSerializationDelegate(groupName: groupName, summaryTree: true, service: this); + return _nodesToJson(_getChildrenFiltered(node, delegate), delegate, parent: node); } /// Returns a JSON representation of the children of the [DiagnosticsNode] diff --git a/packages/flutter/test/widgets/widget_inspector_test.dart b/packages/flutter/test/widgets/widget_inspector_test.dart index 90dc05a8dd..a79f1ed4fa 100644 --- a/packages/flutter/test/widgets/widget_inspector_test.dart +++ b/packages/flutter/test/widgets/widget_inspector_test.dart @@ -899,7 +899,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { } }); - test('WidgetInspectorService getProperties', () { + test('WidgetInspectorService getProperties for $DiagnosticsNode', () { final DiagnosticsNode diagnostic = const Text('a', textDirection: TextDirection.ltr).toDiagnosticsNode(); const String group = 'group'; service.disposeAllGroups(); @@ -915,6 +915,22 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { } }); + test('WidgetInspectorService getProperties for $Diagnosticable', () { + 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)); + expect(service.toObject(propertyJson['objectId']! as String), isA()); + } + }); + testWidgets('WidgetInspectorService getChildren', (WidgetTester tester) async { const String group = 'test-group'; @@ -2107,7 +2123,7 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { } }); - test('ext.flutter.inspector.getProperties', () async { + test('ext.flutter.inspector.getProperties for $DiagnosticsNode', () async { final DiagnosticsNode diagnostic = const Text('a', textDirection: TextDirection.ltr).toDiagnosticsNode(); const String group = 'group'; final String id = service.toId(diagnostic, group)!; @@ -2125,6 +2141,24 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService { } }); + test('ext.flutter.inspector.getProperties for $Diagnosticable', () async { + const Diagnosticable diagnosticable = Text('a', textDirection: TextDirection.ltr); + const String group = 'group'; + final String id = service.toId(diagnosticable, group)!; + final List propertiesJson = (await service.testExtension( + WidgetInspectorServiceExtensions.getProperties.name, + {'arg': id, 'objectGroup': 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)); + expect(service.toObject(propertyJson['objectId']! as String), isA()); + } + }); + testWidgets('ext.flutter.inspector.getChildren', (WidgetTester tester) async { const String group = 'test-group';