Unverified Commit d6bf1447 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Update the tool to know about all our new platforms (#132423)

...and add a test so we remember to keep it in sync.
parent 5fa8de05
...@@ -87,6 +87,9 @@ Future<void> run(List<String> arguments) async { ...@@ -87,6 +87,9 @@ Future<void> run(List<String> arguments) async {
foundError(<String>['The analyze.dart script must be run with --enable-asserts.']); foundError(<String>['The analyze.dart script must be run with --enable-asserts.']);
} }
printProgress('TargetPlatform tool/framework consistency');
await verifyTargetPlatform(flutterRoot);
printProgress('No Double.clamp'); printProgress('No Double.clamp');
await verifyNoDoubleClamp(flutterRoot); await verifyNoDoubleClamp(flutterRoot);
...@@ -266,6 +269,84 @@ class _DoubleClampVisitor extends RecursiveAstVisitor<CompilationUnit> { ...@@ -266,6 +269,84 @@ class _DoubleClampVisitor extends RecursiveAstVisitor<CompilationUnit> {
} }
} }
Future<void> verifyTargetPlatform(String workingDirectory) async {
final File framework = File('$workingDirectory/packages/flutter/lib/src/foundation/platform.dart');
final Set<String> frameworkPlatforms = <String>{};
List<String> lines = framework.readAsLinesSync();
int index = 0;
while (true) {
if (index >= lines.length) {
foundError(<String>['${framework.path}: Can no longer find TargetPlatform enum.']);
return;
}
if (lines[index].startsWith('enum TargetPlatform {')) {
index += 1;
break;
}
index += 1;
}
while (true) {
if (index >= lines.length) {
foundError(<String>['${framework.path}: Could not find end of TargetPlatform enum.']);
return;
}
String line = lines[index].trim();
final int comment = line.indexOf('//');
if (comment >= 0) {
line = line.substring(0, comment);
}
if (line == '}') {
break;
}
if (line.isNotEmpty) {
if (line.endsWith(',')) {
frameworkPlatforms.add(line.substring(0, line.length - 1));
} else {
foundError(<String>['${framework.path}:$index: unparseable line when looking for TargetPlatform values']);
}
}
index += 1;
}
final File tool = File('$workingDirectory/packages/flutter_tools/lib/src/resident_runner.dart');
final Set<String> toolPlatforms = <String>{};
lines = tool.readAsLinesSync();
index = 0;
while (true) {
if (index >= lines.length) {
foundError(<String>['${tool.path}: Can no longer find nextPlatform logic.']);
return;
}
if (lines[index].trim().startsWith('const List<String> platforms = <String>[')) {
index += 1;
break;
}
index += 1;
}
while (true) {
if (index >= lines.length) {
foundError(<String>['${tool.path}: Could not find end of nextPlatform logic.']);
return;
}
final String line = lines[index].trim();
if (line.startsWith("'") && line.endsWith("',")) {
toolPlatforms.add(line.substring(1, line.length - 2));
} else if (line == '];') {
break;
} else {
foundError(<String>['${tool.path}:$index: unparseable line when looking for nextPlatform values']);
}
index += 1;
}
final Set<String> frameworkExtra = frameworkPlatforms.difference(toolPlatforms);
if (frameworkExtra.isNotEmpty) {
foundError(<String>['TargetPlatform has some extra values not found in the tool: ${frameworkExtra.join(", ")}']);
}
final Set<String> toolExtra = toolPlatforms.difference(frameworkPlatforms);
if (toolExtra.isNotEmpty) {
foundError(<String>['The nextPlatform logic in the tool has some extra values not found in TargetPlatform: ${toolExtra.join(", ")}']);
}
}
/// Verify that we use clampDouble instead of Double.clamp for performance reasons. /// Verify that we use clampDouble instead of Double.clamp for performance reasons.
/// ///
/// We currently can't distinguish valid uses of clamp from problematic ones so /// We currently can't distinguish valid uses of clamp from problematic ones so
......
...@@ -561,33 +561,22 @@ abstract class BindingBase { ...@@ -561,33 +561,22 @@ abstract class BindingBase {
name: FoundationServiceExtensions.platformOverride.name, name: FoundationServiceExtensions.platformOverride.name,
callback: (Map<String, String> parameters) async { callback: (Map<String, String> parameters) async {
if (parameters.containsKey('value')) { if (parameters.containsKey('value')) {
switch (parameters['value']) { final String value = parameters['value']!;
case 'android': debugDefaultTargetPlatformOverride = null;
debugDefaultTargetPlatformOverride = TargetPlatform.android; for (final TargetPlatform candidate in TargetPlatform.values) {
case 'fuchsia': if (candidate.name == value) {
debugDefaultTargetPlatformOverride = TargetPlatform.fuchsia; debugDefaultTargetPlatformOverride = candidate;
case 'iOS': break;
debugDefaultTargetPlatformOverride = TargetPlatform.iOS; }
case 'linux':
debugDefaultTargetPlatformOverride = TargetPlatform.linux;
case 'macOS':
debugDefaultTargetPlatformOverride = TargetPlatform.macOS;
case 'windows':
debugDefaultTargetPlatformOverride = TargetPlatform.windows;
case 'default':
default:
debugDefaultTargetPlatformOverride = null;
} }
_postExtensionStateChangedEvent( _postExtensionStateChangedEvent(
FoundationServiceExtensions.platformOverride.name, FoundationServiceExtensions.platformOverride.name,
defaultTargetPlatform.toString().substring('$TargetPlatform.'.length), defaultTargetPlatform.name,
); );
await reassembleApplication(); await reassembleApplication();
} }
return <String, dynamic>{ return <String, dynamic>{
'value': defaultTargetPlatform 'value': defaultTargetPlatform.name,
.toString()
.substring('$TargetPlatform.'.length),
}; };
}, },
); );
......
...@@ -35,7 +35,7 @@ import '_platform_io.dart' ...@@ -35,7 +35,7 @@ import '_platform_io.dart'
// //
// When adding support for a new platform (e.g. Windows Phone, Raspberry Pi), // When adding support for a new platform (e.g. Windows Phone, Raspberry Pi),
// first create a new value on the [TargetPlatform] enum, then add a rule for // first create a new value on the [TargetPlatform] enum, then add a rule for
// selecting that platform here. // selecting that platform in `_platform_io.dart` and `_platform_web.dart`.
// //
// It would be incorrect to make a platform that isn't supported by // It would be incorrect to make a platform that isn't supported by
// [TargetPlatform] default to the behavior of another platform, because doing // [TargetPlatform] default to the behavior of another platform, because doing
...@@ -47,6 +47,15 @@ TargetPlatform get defaultTargetPlatform => platform.defaultTargetPlatform; ...@@ -47,6 +47,15 @@ TargetPlatform get defaultTargetPlatform => platform.defaultTargetPlatform;
/// The platform that user interaction should adapt to target. /// The platform that user interaction should adapt to target.
/// ///
/// The [defaultTargetPlatform] getter returns the current platform. /// The [defaultTargetPlatform] getter returns the current platform.
///
/// When using the "flutter run" command, the "o" key will toggle between
/// values of this enum when updating [debugDefaultTargetPlatformOverride].
/// This lets one test how the application will work on various platforms
/// without having to switch emulators or physical devices.
//
// When you add values here, make sure to also add them to
// nextPlatform() in flutter_tools/lib/src/resident_runner.dart so that
// the tool can support the new platform for its "o" option.
enum TargetPlatform { enum TargetPlatform {
/// Android: <https://www.android.com/> /// Android: <https://www.android.com/>
android, android,
......
...@@ -1877,19 +1877,17 @@ class DebugConnectionInfo { ...@@ -1877,19 +1877,17 @@ class DebugConnectionInfo {
/// These values must match what is available in /// These values must match what is available in
/// `packages/flutter/lib/src/foundation/binding.dart`. /// `packages/flutter/lib/src/foundation/binding.dart`.
String nextPlatform(String currentPlatform) { String nextPlatform(String currentPlatform) {
switch (currentPlatform) { const List<String> platforms = <String>[
case 'android': 'android',
return 'iOS'; 'iOS',
case 'iOS': 'windows',
return 'fuchsia'; 'macOS',
case 'fuchsia': 'linux',
return 'macOS'; 'fuchsia',
case 'macOS': ];
return 'android'; final int index = platforms.indexOf(currentPlatform);
default: assert(index >= 0, 'unknown platform "$currentPlatform"');
assert(false); // Invalid current platform. return platforms[(index + 1) % platforms.length];
return 'android';
}
} }
/// A launcher for the devtools debugger and analysis tool. /// A launcher for the devtools debugger and analysis tool.
......
...@@ -2210,9 +2210,11 @@ flutter: ...@@ -2210,9 +2210,11 @@ flutter:
testUsingContext('nextPlatform moves through expected platforms', () { testUsingContext('nextPlatform moves through expected platforms', () {
expect(nextPlatform('android'), 'iOS'); expect(nextPlatform('android'), 'iOS');
expect(nextPlatform('iOS'), 'fuchsia'); expect(nextPlatform('iOS'), 'windows');
expect(nextPlatform('fuchsia'), 'macOS'); expect(nextPlatform('windows'), 'macOS');
expect(nextPlatform('macOS'), 'android'); expect(nextPlatform('macOS'), 'linux');
expect(nextPlatform('linux'), 'fuchsia');
expect(nextPlatform('fuchsia'), 'android');
expect(() => nextPlatform('unknown'), throwsAssertionError); expect(() => nextPlatform('unknown'), throwsAssertionError);
}); });
......
...@@ -464,10 +464,10 @@ void main() { ...@@ -464,10 +464,10 @@ void main() {
method: 'ext.flutter.platformOverride', method: 'ext.flutter.platformOverride',
args: <String, Object>{ args: <String, Object>{
'isolateId': '1', 'isolateId': '1',
'value': 'fuchsia', 'value': 'windows',
}, },
jsonResponse: <String, Object>{ jsonResponse: <String, Object>{
'value': 'fuchsia', 'value': 'windows',
}, },
), ),
// Request 2. // Request 2.
...@@ -496,7 +496,7 @@ void main() { ...@@ -496,7 +496,7 @@ void main() {
await terminalHandler.processTerminalInput('o'); await terminalHandler.processTerminalInput('o');
await terminalHandler.processTerminalInput('O'); await terminalHandler.processTerminalInput('O');
expect(terminalHandler.logger.statusText, contains('Switched operating system to fuchsia')); expect(terminalHandler.logger.statusText, contains('Switched operating system to windows'));
expect(terminalHandler.logger.statusText, contains('Switched operating system to iOS')); expect(terminalHandler.logger.statusText, contains('Switched operating system to iOS'));
}); });
...@@ -518,10 +518,10 @@ void main() { ...@@ -518,10 +518,10 @@ void main() {
method: 'ext.flutter.platformOverride', method: 'ext.flutter.platformOverride',
args: <String, Object>{ args: <String, Object>{
'isolateId': '1', 'isolateId': '1',
'value': 'fuchsia', 'value': 'windows',
}, },
jsonResponse: <String, Object>{ jsonResponse: <String, Object>{
'value': 'fuchsia', 'value': 'windows',
}, },
), ),
// Request 2. // Request 2.
...@@ -550,7 +550,7 @@ void main() { ...@@ -550,7 +550,7 @@ void main() {
await terminalHandler.processTerminalInput('o'); await terminalHandler.processTerminalInput('o');
await terminalHandler.processTerminalInput('O'); await terminalHandler.processTerminalInput('O');
expect(terminalHandler.logger.statusText, contains('Switched operating system to fuchsia')); expect(terminalHandler.logger.statusText, contains('Switched operating system to windows'));
expect(terminalHandler.logger.statusText, contains('Switched operating system to iOS')); expect(terminalHandler.logger.statusText, contains('Switched operating system to iOS'));
}); });
......
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