Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 8ea0083

Browse files
Added a test to ensure PlatformDispatcher and PlatformConfig have the same getters with exceptions.
1 parent 6a34f3f commit 8ea0083

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

testing/dart/platform_dispatcher_test.dart

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import 'dart:io';
56
import 'dart:ui';
67

8+
import 'package:analyzer/dart/analysis/features.dart';
9+
import 'package:analyzer/dart/analysis/results.dart';
10+
import 'package:analyzer/dart/analysis/utilities.dart';
11+
import 'package:analyzer/dart/ast/ast.dart';
712
import 'package:litetest/litetest.dart';
813

914
void main() {
@@ -39,4 +44,60 @@ void main() {
3944
return ViewConfiguration(view: view)..copyWith(view: view, window: view);
4045
});
4146
});
47+
48+
test("PlatformDispatcher and PlatformConfiguration have matching getters", () {
49+
final List<Pattern> exclusionCriteria = <Pattern>[
50+
RegExp(r'^_.*'), // exclude any private members
51+
RegExp(r'^on[A-Z].*'), // exclude any callbacks
52+
RegExp(r'locale$'), // locale is a convenience getter for interacting with `locales` which _is_ backed by the config
53+
'instance', // exclude the static instance of PlatformDispatcher
54+
'configuration', // the configuration should not reference itself
55+
'views', // views are backed by their own config and don't need to be referenced in the platform config
56+
'initialLifecycleState', // default route is stored/retrieved from the platform, not the engine
57+
'frameData', // framed data updates too often and would kick off too many config changes
58+
];
59+
60+
final String flutterDir = Platform.environment['FLUTTER_DIR']!;
61+
final List<ClassDeclaration> classes = parseFile(
62+
path: '$flutterDir/lib/ui/platform_dispatcher.dart',
63+
featureSet: FeatureSet.latestLanguageVersion(),
64+
throwIfDiagnostics: false,
65+
).unit.declarations.whereType<ClassDeclaration>().toList();
66+
final ClassDeclaration dispatcherClass = classes.singleWhere((ClassDeclaration c) => c.name.toString() == (PlatformDispatcher).toString());
67+
final ClassDeclaration configurationClass = classes.singleWhere((ClassDeclaration c) => c.name.toString() == (PlatformConfiguration).toString());
68+
69+
Iterable<String> getNameOfClassMember(ClassMember member) {
70+
if (member is MethodDeclaration) {
71+
return <String>[member.name.toString()];
72+
}else if (member is FieldDeclaration) {
73+
return member.fields.variables.map((VariableDeclaration variable) => variable.name.toString());
74+
} else {
75+
return [];
76+
}
77+
}
78+
79+
final Set<String> dispatcherMembers = dispatcherClass.members
80+
.where((ClassMember member) =>
81+
member is FieldDeclaration ||
82+
member is MethodDeclaration && member.isGetter)
83+
.expand<String>(getNameOfClassMember)
84+
.where((String name) =>
85+
exclusionCriteria.every((criteria) =>
86+
criteria.allMatches(name).isEmpty))
87+
.toSet();
88+
final Set<String> configurationMembers = configurationClass.members
89+
.where((ClassMember member) =>
90+
member is FieldDeclaration ||
91+
member is MethodDeclaration && member.isGetter)
92+
.expand(getNameOfClassMember)
93+
.toSet();
94+
95+
final Set<String> missingMembers = configurationMembers.difference(dispatcherMembers);
96+
final Set<String> extraMembers = dispatcherMembers.difference(configurationMembers);
97+
expect(missingMembers.isEmpty && extraMembers.isEmpty, isTrue,
98+
reason:
99+
'PlatformDispatcher is out of sync with PlatformConfiguration.\n'
100+
' Missing Members: $missingMembers\n'
101+
' Members not backed by PlatformConfiguration: $extraMembers');
102+
});
42103
}

0 commit comments

Comments
 (0)