mirror of
https://github.com/flutter/flutter.git
synced 2025-06-03 00:51:18 +00:00
Refactor getRootWidgetSummaryTree
tests in widget_inspector_test.dart
(#149930)
This commit is contained in:
parent
47560fac5c
commit
034c0d03f8
@ -1913,19 +1913,157 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService {
|
|||||||
expect(column, equals(15));
|
expect(column, equals(15));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
group('Widget Tree APIs', () {
|
||||||
|
|
||||||
|
/// Gets the widget using [WidgetInspectorServiceExtensions.getSelectedWidget]
|
||||||
|
/// for the given [element].
|
||||||
|
Future<Map<String, dynamic>> selectedWidgetResponseForElement(
|
||||||
|
Element element) async {
|
||||||
|
service
|
||||||
|
..disposeAllGroups()
|
||||||
|
..resetPubRootDirectories()
|
||||||
|
..setSelection(element, 'my-group');
|
||||||
|
|
||||||
|
return (await service.testExtension(
|
||||||
|
WidgetInspectorServiceExtensions.getSelectedWidget.name,
|
||||||
|
<String, String>{'objectGroup': 'my-group'},
|
||||||
|
))! as Map<String, dynamic>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Verifies the creation location is expected for the given
|
||||||
|
/// [responseJson].
|
||||||
|
Map<String, Object?> verifyAndReturnCreationLocation(
|
||||||
|
Map<String, dynamic> responseJson) {
|
||||||
|
final Map<String, Object?> creationLocation =
|
||||||
|
responseJson['creationLocation']! as Map<String, Object?>;
|
||||||
|
expect(creationLocation, isNotNull);
|
||||||
|
return creationLocation;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Verifies the test file is expected for the given
|
||||||
|
/// [creationLocation].
|
||||||
|
String verifyAndReturnTestFile(
|
||||||
|
Map<String, Object?> creationLocation) {
|
||||||
|
final String testFile = creationLocation['file']! as String;
|
||||||
|
expect(testFile, endsWith('widget_inspector_test.dart'));
|
||||||
|
return testFile;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Adds a pub root directory for the given [testFile].
|
||||||
|
void addPubRootDirectoryFor(String testFile) {
|
||||||
|
final List<String> 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(<String>[pubRootTest]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the children nodes from the JSON response.
|
||||||
|
List<Object?> childrenFromJsonResponse(Map<String, Object?> json) {
|
||||||
|
return json['children']! as List<Object?>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Gets the children nodes using a call to
|
||||||
|
/// [WidgetInspectorServiceExtensions.getChildrenSummaryTree].
|
||||||
|
Future<List<Object?>> childrenFromGetChildrenSummaryTree(
|
||||||
|
String valueId, String group) async {
|
||||||
|
return (await service.testExtension(
|
||||||
|
WidgetInspectorServiceExtensions.getChildrenSummaryTree.name,
|
||||||
|
<String, String>{'arg': valueId, 'objectGroup': group},
|
||||||
|
))! as List<Object?>;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Verifies that the children from the JSON response are identical to
|
||||||
|
/// those from [WidgetInspectorServiceExtensions.getChildrenSummaryTree].
|
||||||
|
Future<void> verifyChildrenMatchOtherApi(Map<String, Object?> jsonResponse,
|
||||||
|
{required String group, bool checkForPreviews = false}) async {
|
||||||
|
List<Object?> children = childrenFromJsonResponse(jsonResponse);
|
||||||
|
List<Object?> childrenFromOtherApi =
|
||||||
|
await childrenFromGetChildrenSummaryTree(
|
||||||
|
jsonResponse['valueId']! as String, group);
|
||||||
|
|
||||||
|
// Verify that the number of children are the same,
|
||||||
|
expect(children.length, equals(1));
|
||||||
|
expect(children.length, equals(childrenFromOtherApi.length));
|
||||||
|
|
||||||
|
// Get the first child.
|
||||||
|
Map<String, Object?> child =
|
||||||
|
children[0]! as Map<String, Object?>;
|
||||||
|
Map<String, Object?> childFromOtherApi =
|
||||||
|
childrenFromOtherApi[0]! as Map<String, Object?>;
|
||||||
|
|
||||||
|
// Verify the first child is the same.
|
||||||
|
expect(child['description'], startsWith('Directionality'));
|
||||||
|
expect(child['description'], equals(childFromOtherApi['description']));
|
||||||
|
expect(child['valueId'], equals(childFromOtherApi['valueId']));
|
||||||
|
|
||||||
|
// Get the first child's children.
|
||||||
|
children = childrenFromJsonResponse(child);
|
||||||
|
childrenFromOtherApi = await childrenFromGetChildrenSummaryTree(
|
||||||
|
childFromOtherApi['valueId']! as String, group);
|
||||||
|
|
||||||
|
// Verify the first child's children are the same length.
|
||||||
|
expect(children.length, equals(1));
|
||||||
|
expect(children.length, equals(childrenFromOtherApi.length));
|
||||||
|
|
||||||
|
// Get the first child's first child.
|
||||||
|
child = children[0]! as Map<String, Object?>;
|
||||||
|
childFromOtherApi =
|
||||||
|
childrenFromOtherApi[0]! as Map<String, Object?>;
|
||||||
|
|
||||||
|
// Verify the first child's first child is the same.
|
||||||
|
expect(child['description'], startsWith('Stack'));
|
||||||
|
expect(child['description'],
|
||||||
|
equals(childFromOtherApi['description']));
|
||||||
|
expect(child['valueId'], equals(childFromOtherApi['valueId']));
|
||||||
|
|
||||||
|
// Get the first child's first child's children.
|
||||||
|
children = childrenFromJsonResponse(child);
|
||||||
|
childrenFromOtherApi = await childrenFromGetChildrenSummaryTree(
|
||||||
|
childFromOtherApi['valueId']! as String, group);
|
||||||
|
|
||||||
|
// Verify the first child's first child's children are the same
|
||||||
|
// length.
|
||||||
|
expect(children.length, equals(3));
|
||||||
|
expect(children.length, equals(childrenFromOtherApi.length));
|
||||||
|
|
||||||
|
// Get the first child's first child's third child.
|
||||||
|
child = children[2]! as Map<String, Object?>;
|
||||||
|
childFromOtherApi =
|
||||||
|
childrenFromOtherApi[2]! as Map<String, Object?>;
|
||||||
|
|
||||||
|
// Verify the first child's first child's third child are the same.
|
||||||
|
expect(child['description'], startsWith('Text'));
|
||||||
|
expect(child['description'], childFromOtherApi['description']);
|
||||||
|
expect(child['valueId'], equals(childFromOtherApi['valueId']));
|
||||||
|
|
||||||
|
// If the tree was requested with previews, then check that the
|
||||||
|
// child has the `textPreview` key:
|
||||||
|
if (checkForPreviews) {
|
||||||
|
expect(child['textPreview'], equals('c'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get the first child's first child's third child's children.
|
||||||
|
children = childrenFromJsonResponse(child);
|
||||||
|
childrenFromOtherApi = await childrenFromGetChildrenSummaryTree(
|
||||||
|
childFromOtherApi['valueId']! as String, group);
|
||||||
|
|
||||||
|
// Verify first child's first child's third child's has no children.
|
||||||
|
expect(children.length, equals(0));
|
||||||
|
expect(childrenFromOtherApi.length, equals(children.length));
|
||||||
|
}
|
||||||
|
|
||||||
testWidgets('ext.flutter.inspector.getRootWidgetSummaryTree',
|
testWidgets('ext.flutter.inspector.getRootWidgetSummaryTree',
|
||||||
(WidgetTester tester) async {
|
(WidgetTester tester) async {
|
||||||
const String group = 'test-group';
|
const String group = 'test-group';
|
||||||
await pumpWidgetTreeWithABC(tester);
|
await pumpWidgetTreeWithABC(tester);
|
||||||
final Element elementA = findElementABC('a');
|
final Element elementA = findElementABC('a');
|
||||||
|
final Map<String, dynamic> jsonA =
|
||||||
service.disposeAllGroups();
|
await selectedWidgetResponseForElement(elementA);
|
||||||
service.resetPubRootDirectories();
|
|
||||||
service.setSelection(elementA, 'my-group');
|
|
||||||
final Map<String, dynamic> jsonA = (await service.testExtension(
|
|
||||||
WidgetInspectorServiceExtensions.getSelectedWidget.name,
|
|
||||||
<String, String>{'objectGroup': 'my-group'},
|
|
||||||
))! as Map<String, dynamic>;
|
|
||||||
|
|
||||||
service.resetPubRootDirectories();
|
service.resetPubRootDirectories();
|
||||||
Map<String, Object?> rootJson = (await service.testExtension(
|
Map<String, Object?> rootJson = (await service.testExtension(
|
||||||
@ -1939,97 +2077,22 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService {
|
|||||||
final Object? rootWidget =
|
final Object? rootWidget =
|
||||||
service.toObject(rootJson['valueId']! as String);
|
service.toObject(rootJson['valueId']! as String);
|
||||||
expect(rootWidget, equals(WidgetsBinding.instance.rootElement));
|
expect(rootWidget, equals(WidgetsBinding.instance.rootElement));
|
||||||
List<Object?> childrenJson = rootJson['children']! as List<Object?>;
|
final List<Object?> childrenJson =
|
||||||
|
rootJson['children']! as List<Object?>;
|
||||||
// There are no summary tree children.
|
// There are no summary tree children.
|
||||||
expect(childrenJson.length, equals(0));
|
expect(childrenJson.length, equals(0));
|
||||||
|
|
||||||
final Map<String, Object?> creationLocation =
|
final Map<String, Object?> creationLocation =
|
||||||
jsonA['creationLocation']! as Map<String, Object?>;
|
verifyAndReturnCreationLocation(jsonA);
|
||||||
expect(creationLocation, isNotNull);
|
final String testFile = verifyAndReturnTestFile(creationLocation);
|
||||||
final String testFile = creationLocation['file']! as String;
|
addPubRootDirectoryFor(testFile);
|
||||||
expect(testFile, endsWith('widget_inspector_test.dart'));
|
|
||||||
final List<String> 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,
|
|
||||||
<String, String>{'arg0': pubRootTest},
|
|
||||||
);
|
|
||||||
|
|
||||||
rootJson = (await service.testExtension(
|
rootJson = (await service.testExtension(
|
||||||
WidgetInspectorServiceExtensions.getRootWidgetSummaryTree.name,
|
WidgetInspectorServiceExtensions.getRootWidgetSummaryTree.name,
|
||||||
<String, String>{'objectGroup': group},
|
<String, String>{'objectGroup': group},
|
||||||
))! as Map<String, Object?>;
|
))! as Map<String, Object?>;
|
||||||
childrenJson = rootJson['children']! as List<Object?>;
|
|
||||||
// The tree of nodes returned contains all widgets created directly by the
|
|
||||||
// test.
|
|
||||||
childrenJson = rootJson['children']! as List<Object?>;
|
|
||||||
expect(childrenJson.length, equals(1));
|
|
||||||
|
|
||||||
List<Object?> alternateChildrenJson = (await service.testExtension(
|
await verifyChildrenMatchOtherApi(rootJson, group: group);
|
||||||
WidgetInspectorServiceExtensions.getChildrenSummaryTree.name,
|
|
||||||
<String, String>{
|
|
||||||
'arg': rootJson['valueId']! as String,
|
|
||||||
'objectGroup': group
|
|
||||||
},
|
|
||||||
))! as List<Object?>;
|
|
||||||
expect(alternateChildrenJson.length, equals(1));
|
|
||||||
Map<String, Object?> childJson =
|
|
||||||
childrenJson[0]! as Map<String, Object?>;
|
|
||||||
Map<String, Object?> alternateChildJson =
|
|
||||||
alternateChildrenJson[0]! as Map<String, Object?>;
|
|
||||||
expect(childJson['description'], startsWith('Directionality'));
|
|
||||||
expect(
|
|
||||||
alternateChildJson['description'], startsWith('Directionality'));
|
|
||||||
expect(alternateChildJson['valueId'], equals(childJson['valueId']));
|
|
||||||
|
|
||||||
childrenJson = childJson['children']! as List<Object?>;
|
|
||||||
alternateChildrenJson = (await service.testExtension(
|
|
||||||
WidgetInspectorServiceExtensions.getChildrenSummaryTree.name,
|
|
||||||
<String, String>{
|
|
||||||
'arg': childJson['valueId']! as String,
|
|
||||||
'objectGroup': group
|
|
||||||
},
|
|
||||||
))! as List<Object?>;
|
|
||||||
expect(alternateChildrenJson.length, equals(1));
|
|
||||||
expect(childrenJson.length, equals(1));
|
|
||||||
alternateChildJson =
|
|
||||||
alternateChildrenJson[0]! as Map<String, Object?>;
|
|
||||||
childJson = childrenJson[0]! as Map<String, Object?>;
|
|
||||||
expect(childJson['description'], startsWith('Stack'));
|
|
||||||
expect(alternateChildJson['description'], startsWith('Stack'));
|
|
||||||
expect(alternateChildJson['valueId'], equals(childJson['valueId']));
|
|
||||||
childrenJson = childJson['children']! as List<Object?>;
|
|
||||||
|
|
||||||
childrenJson = childJson['children']! as List<Object?>;
|
|
||||||
alternateChildrenJson = (await service.testExtension(
|
|
||||||
WidgetInspectorServiceExtensions.getChildrenSummaryTree.name,
|
|
||||||
<String, String>{
|
|
||||||
'arg': childJson['valueId']! as String,
|
|
||||||
'objectGroup': group
|
|
||||||
},
|
|
||||||
))! as List<Object?>;
|
|
||||||
expect(alternateChildrenJson.length, equals(3));
|
|
||||||
expect(childrenJson.length, equals(3));
|
|
||||||
alternateChildJson =
|
|
||||||
alternateChildrenJson[2]! as Map<String, Object?>;
|
|
||||||
childJson = childrenJson[2]! as Map<String, Object?>;
|
|
||||||
expect(childJson['description'], startsWith('Text'));
|
|
||||||
expect(alternateChildJson['description'], startsWith('Text'));
|
|
||||||
expect(alternateChildJson['valueId'], equals(childJson['valueId']));
|
|
||||||
alternateChildrenJson = (await service.testExtension(
|
|
||||||
WidgetInspectorServiceExtensions.getChildrenSummaryTree.name,
|
|
||||||
<String, String>{
|
|
||||||
'arg': childJson['valueId']! as String,
|
|
||||||
'objectGroup': group
|
|
||||||
},
|
|
||||||
))! as List<Object?>;
|
|
||||||
childrenJson = childJson['children']! as List<Object?>;
|
|
||||||
expect(alternateChildrenJson.length, equals(0));
|
|
||||||
expect(childrenJson.length, equals(0));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets(
|
testWidgets(
|
||||||
@ -2039,95 +2102,26 @@ class _TestWidgetInspectorService extends TestWidgetInspectorService {
|
|||||||
|
|
||||||
await pumpWidgetTreeWithABC(tester);
|
await pumpWidgetTreeWithABC(tester);
|
||||||
final Element elementA = findElementABC('a');
|
final Element elementA = findElementABC('a');
|
||||||
|
final Map<String, dynamic> jsonA =
|
||||||
service
|
await selectedWidgetResponseForElement(elementA);
|
||||||
..disposeAllGroups()
|
|
||||||
..resetPubRootDirectories()
|
|
||||||
..setSelection(elementA, 'my-group');
|
|
||||||
|
|
||||||
final Map<String, dynamic> jsonA = (await service.testExtension(
|
|
||||||
WidgetInspectorServiceExtensions.getSelectedWidget.name,
|
|
||||||
<String, String>{'objectGroup': 'my-group'},
|
|
||||||
))! as Map<String, dynamic>;
|
|
||||||
|
|
||||||
final Map<String, Object?> creationLocation =
|
final Map<String, Object?> creationLocation =
|
||||||
jsonA['creationLocation']! as Map<String, Object?>;
|
verifyAndReturnCreationLocation(jsonA);
|
||||||
expect(creationLocation, isNotNull);
|
final String testFile = verifyAndReturnTestFile(creationLocation);
|
||||||
final String testFile = creationLocation['file']! as String;
|
addPubRootDirectoryFor(testFile);
|
||||||
expect(testFile, endsWith('widget_inspector_test.dart'));
|
|
||||||
final List<String> 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(<String>[pubRootTest]);
|
|
||||||
|
|
||||||
final Map<String, Object?> rootJson = (await service.testExtension(
|
final Map<String, Object?> rootJson = (await service.testExtension(
|
||||||
WidgetInspectorServiceExtensions
|
WidgetInspectorServiceExtensions
|
||||||
.getRootWidgetSummaryTreeWithPreviews.name,
|
.getRootWidgetSummaryTreeWithPreviews.name,
|
||||||
<String, String>{'groupName': group},
|
<String, String>{'groupName': group},
|
||||||
))! as Map<String, Object?>;
|
))! as Map<String, Object?>;
|
||||||
List<Object?> childrenJson = rootJson['children']! as List<Object?>;
|
|
||||||
// The tree of nodes returned contains all widgets created directly by the
|
|
||||||
// test.
|
|
||||||
childrenJson = rootJson['children']! as List<Object?>;
|
|
||||||
expect(childrenJson.length, equals(1));
|
|
||||||
|
|
||||||
List<Object?> alternateChildrenJson = (await service.testExtension(
|
await verifyChildrenMatchOtherApi(
|
||||||
WidgetInspectorServiceExtensions.getChildrenSummaryTree.name,
|
rootJson,
|
||||||
<String, String>{
|
group: group,
|
||||||
'arg': rootJson['valueId']! as String,
|
checkForPreviews: true,
|
||||||
'objectGroup': group
|
);
|
||||||
},
|
});
|
||||||
))! as List<Object?>;
|
|
||||||
expect(alternateChildrenJson.length, equals(1));
|
|
||||||
Map<String, Object?> childJson =
|
|
||||||
childrenJson[0]! as Map<String, Object?>;
|
|
||||||
Map<String, Object?> alternateChildJson =
|
|
||||||
alternateChildrenJson[0]! as Map<String, Object?>;
|
|
||||||
expect(childJson['description'], startsWith('Directionality'));
|
|
||||||
expect(
|
|
||||||
alternateChildJson['description'], startsWith('Directionality'));
|
|
||||||
expect(alternateChildJson['valueId'], equals(childJson['valueId']));
|
|
||||||
|
|
||||||
childrenJson = childJson['children']! as List<Object?>;
|
|
||||||
alternateChildrenJson = (await service.testExtension(
|
|
||||||
WidgetInspectorServiceExtensions.getChildrenSummaryTree.name,
|
|
||||||
<String, String>{
|
|
||||||
'arg': childJson['valueId']! as String,
|
|
||||||
'objectGroup': group
|
|
||||||
},
|
|
||||||
))! as List<Object?>;
|
|
||||||
expect(alternateChildrenJson.length, equals(1));
|
|
||||||
expect(childrenJson.length, equals(1));
|
|
||||||
alternateChildJson =
|
|
||||||
alternateChildrenJson[0]! as Map<String, Object?>;
|
|
||||||
childJson = childrenJson[0]! as Map<String, Object?>;
|
|
||||||
expect(childJson['description'], startsWith('Stack'));
|
|
||||||
expect(alternateChildJson['description'], startsWith('Stack'));
|
|
||||||
expect(alternateChildJson['valueId'], equals(childJson['valueId']));
|
|
||||||
childrenJson = childJson['children']! as List<Object?>;
|
|
||||||
|
|
||||||
childrenJson = childJson['children']! as List<Object?>;
|
|
||||||
alternateChildrenJson = (await service.testExtension(
|
|
||||||
WidgetInspectorServiceExtensions.getChildrenSummaryTree.name,
|
|
||||||
<String, String>{
|
|
||||||
'arg': childJson['valueId']! as String,
|
|
||||||
'objectGroup': group
|
|
||||||
},
|
|
||||||
))! as List<Object?>;
|
|
||||||
expect(alternateChildrenJson.length, equals(3));
|
|
||||||
expect(childrenJson.length, equals(3));
|
|
||||||
alternateChildJson =
|
|
||||||
alternateChildrenJson[2]! as Map<String, Object?>;
|
|
||||||
childJson = childrenJson[2]! as Map<String, Object?>;
|
|
||||||
expect(childJson['description'], startsWith('Text'));
|
|
||||||
|
|
||||||
// [childJson] contains the 'textPreview' key since the tree was requested
|
|
||||||
// with previews [getRootWidgetSummaryTreeWithPreviews].
|
|
||||||
expect(childJson['textPreview'], equals('c'));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
testWidgets('ext.flutter.inspector.getSelectedSummaryWidget',
|
testWidgets('ext.flutter.inspector.getSelectedSummaryWidget',
|
||||||
|
Loading…
Reference in New Issue
Block a user