Unverified Commit dd4a8150 authored by Polina Cherkasova's avatar Polina Cherkasova Committed by GitHub

Update getProperties to handle Diagnosticable as input. (#128897)

parent e87b2021
......@@ -1722,9 +1722,12 @@ mixin WidgetInspectorService {
return _safeJsonEncode(_getProperties(diagnosticsNodeId, groupName));
}
List<Object> _getProperties(String? diagnosticsNodeId, String groupName) {
final DiagnosticsNode? node = toObject(diagnosticsNodeId) as DiagnosticsNode?;
return _nodesToJson(node == null ? const <DiagnosticsNode>[] : node.getProperties(), InspectorSerializationDelegate(groupName: groupName, service: this), parent: node);
List<Object> _getProperties(String? diagnosticsOrDiagnosticableId, String groupName) {
final DiagnosticsNode? node = _idToDiagnosticsNode(diagnosticsOrDiagnosticableId);
if (node == null) {
return const <Object>[];
}
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<Object> _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<Object> _getChildrenSummaryTree(String? diagnosticsOrDiagnosticableId, String groupName) {
final DiagnosticsNode? node = _idToDiagnosticsNode(diagnosticsOrDiagnosticableId);
if (node == null) {
return <Object>[];
}
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]
......
......@@ -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<Object?> propertiesJson = json.decode(service.getProperties(id, group)) as List<Object?>;
final List<DiagnosticsNode> properties = diagnosticable.toDiagnosticsNode().getProperties();
expect(properties, isNotEmpty);
expect(propertiesJson.length, equals(properties.length));
for (int i = 0; i < propertiesJson.length; ++i) {
final Map<String, Object?> propertyJson = propertiesJson[i]! as Map<String, Object?>;
expect(service.toObject(propertyJson['valueId'] as String?), equals(properties[i].value));
expect(service.toObject(propertyJson['objectId']! as String), isA<DiagnosticsNode>());
}
});
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<Object?> propertiesJson = (await service.testExtension(
WidgetInspectorServiceExtensions.getProperties.name,
<String, String>{'arg': id, 'objectGroup': group},
))! as List<Object?>;
final List<DiagnosticsNode> properties = diagnosticable.toDiagnosticsNode().getProperties();
expect(properties, isNotEmpty);
expect(propertiesJson.length, equals(properties.length));
for (int i = 0; i < propertiesJson.length; ++i) {
final Map<String, Object?> propertyJson = propertiesJson[i]! as Map<String, Object?>;
expect(service.toObject(propertyJson['valueId'] as String?), equals(properties[i].value));
expect(service.toObject(propertyJson['objectId']! as String), isA<DiagnosticsNode>());
}
});
testWidgets('ext.flutter.inspector.getChildren', (WidgetTester tester) async {
const String group = 'test-group';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment