Unverified Commit 5ba2830c authored by Jonah Williams's avatar Jonah Williams Committed by GitHub

[flutter_tool][gallery] Standardize target platform override behavior in tool...

[flutter_tool][gallery] Standardize target platform override behavior in tool and update flutter gallery to support it (#46206)
parent 187c89b6
...@@ -70,6 +70,12 @@ class _GalleryAppState extends State<GalleryApp> { ...@@ -70,6 +70,12 @@ class _GalleryAppState extends State<GalleryApp> {
model = AppStateModel()..loadProducts(); model = AppStateModel()..loadProducts();
} }
@override
void reassemble() {
_options = _options.copyWith(platform: defaultTargetPlatform);
super.reassemble();
}
@override @override
void dispose() { void dispose() {
_timeDilationTimer?.cancel(); _timeDilationTimer?.cancel();
......
...@@ -235,23 +235,12 @@ abstract class ResidentWebRunner extends ResidentRunner { ...@@ -235,23 +235,12 @@ abstract class ResidentWebRunner extends ResidentRunner {
final vmservice.Response response = await _vmService final vmservice.Response response = await _vmService
?.callServiceExtension('ext.flutter.platformOverride'); ?.callServiceExtension('ext.flutter.platformOverride');
final String currentPlatform = response.json['value'] as String; final String currentPlatform = response.json['value'] as String;
String nextPlatform; final String platform = nextPlatform(currentPlatform, featureFlags);
switch (currentPlatform) {
case 'android':
nextPlatform = 'iOS';
break;
case 'iOS':
nextPlatform = 'android';
break;
}
if (nextPlatform == null) {
return;
}
await _vmService?.callServiceExtension('ext.flutter.platformOverride', await _vmService?.callServiceExtension('ext.flutter.platformOverride',
args: <String, Object>{ args: <String, Object>{
'value': nextPlatform, 'value': platform,
}); });
printStatus('Switched operating system to $nextPlatform'); printStatus('Switched operating system to $platform');
} on vmservice.RPCError { } on vmservice.RPCError {
return; return;
} }
......
...@@ -363,16 +363,7 @@ class FlutterDevice { ...@@ -363,16 +363,7 @@ class FlutterDevice {
} }
Future<String> togglePlatform({ String from }) async { Future<String> togglePlatform({ String from }) async {
String to; final String to = nextPlatform(from, featureFlags);
switch (from) {
case 'iOS':
to = 'android';
break;
case 'android':
default:
to = 'iOS';
break;
}
for (FlutterView view in views) { for (FlutterView view in views) {
await view.uiIsolate.flutterPlatformOverride(to); await view.uiIsolate.flutterPlatformOverride(to);
} }
...@@ -1319,3 +1310,26 @@ class DebugConnectionInfo { ...@@ -1319,3 +1310,26 @@ class DebugConnectionInfo {
final Uri wsUri; final Uri wsUri;
final String baseUri; final String baseUri;
} }
/// Returns the next platform value for the switcher.
///
/// These values must match what is available in
/// packages/flutter/lib/src/foundation/binding.dart
String nextPlatform(String currentPlatform, FeatureFlags featureFlags) {
switch (currentPlatform) {
case 'android':
return 'iOS';
case 'iOS':
return 'fuchsia';
case 'fuchsia':
if (featureFlags.isMacOSEnabled) {
return 'macOS';
}
return 'android';
case 'macOS':
return 'android';
default:
assert(false); // Invalid current platform.
return 'android';
}
}
...@@ -704,6 +704,14 @@ void main() { ...@@ -704,6 +704,14 @@ void main() {
Device device, Device device,
}) async => mockVMService, }) async => mockVMService,
})); }));
test('nextPlatform moves through expected platforms', () {
expect(nextPlatform('android', TestFeatureFlags()), 'iOS');
expect(nextPlatform('iOS', TestFeatureFlags()), 'fuchsia');
expect(nextPlatform('fuchsia', TestFeatureFlags()), 'android');
expect(nextPlatform('fuchsia', TestFeatureFlags(isMacOSEnabled: true)), 'macOS');
expect(() => nextPlatform('unknown', TestFeatureFlags()), throwsA(isInstanceOf<AssertionError>()));
});
} }
class MockFlutterDevice extends Mock implements FlutterDevice {} class MockFlutterDevice extends Mock implements FlutterDevice {}
......
...@@ -773,9 +773,9 @@ void main() { ...@@ -773,9 +773,9 @@ void main() {
await residentWebRunner.debugTogglePlatform(); await residentWebRunner.debugTogglePlatform();
expect(testLogger.statusText, contains('Switched operating system to android')); expect(testLogger.statusText, contains('Switched operating system to fuchsia'));
verify(mockVmService.callServiceExtension('ext.flutter.platformOverride', verify(mockVmService.callServiceExtension('ext.flutter.platformOverride',
args: <String, Object>{'value': 'android'})).called(1); args: <String, Object>{'value': 'fuchsia'})).called(1);
})); }));
test('cleanup of resources is safe to call multiple times', () => testbed.run(() async { test('cleanup of resources is safe to call multiple times', () => testbed.run(() async {
......
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