Unverified Commit 7e73cd74 authored by Danny Tuppeny's avatar Danny Tuppeny Committed by GitHub

Fix visibility of web server device when Chrome is not available (#40757)

* Fix visbility of web server device when Chrome is not available

* Add tests

* Update workflow test

* Fix tests to not rely on Chrome being on the underlying machine
parent 839fdbd2
......@@ -71,6 +71,16 @@ class ChromeLauncher {
static final Completer<Chrome> _currentCompleter = Completer<Chrome>();
/// Whether we can locate the chrome executable.
bool canFindChrome() {
final String chrome = findChromeExecutable();
try {
return processManager.canRun(chrome);
} on ArgumentError {
return false;
}
}
/// Launch the chrome browser to a particular `host` page.
///
/// `headless` defaults to false, and controls whether we open a headless or
......
......@@ -15,7 +15,6 @@ import '../features.dart';
import '../globals.dart';
import '../project.dart';
import 'chrome.dart';
import 'workflow.dart';
class WebApplicationPackage extends ApplicationPackage {
WebApplicationPackage(this.flutterProject) : super(id: flutterProject.manifest.appName);
......@@ -81,7 +80,7 @@ class ChromeDevice extends Device {
Future<String> get emulatorId async => null;
@override
bool isSupported() => featureFlags.isWebEnabled && canFindChrome();
bool isSupported() => featureFlags.isWebEnabled && chromeLauncher.canFindChrome();
@override
String get name => 'Chrome';
......@@ -162,6 +161,7 @@ class ChromeDevice extends Device {
class WebDevices extends PollingDeviceDiscovery {
WebDevices() : super('chrome');
final bool _chromeIsAvailable = chromeLauncher.canFindChrome();
final ChromeDevice _webDevice = ChromeDevice();
final WebServerDevice _webServerDevice = WebServerDevice();
......@@ -171,6 +171,7 @@ class WebDevices extends PollingDeviceDiscovery {
@override
Future<List<Device>> pollingGetDevices() async {
return <Device>[
if (_chromeIsAvailable)
_webDevice,
_webServerDevice,
];
......
......@@ -5,7 +5,6 @@
import '../base/platform.dart';
import '../doctor.dart';
import 'chrome.dart';
import 'workflow.dart';
/// A validator that checks whether chrome is installed and can run.
class WebValidator extends DoctorValidator {
......@@ -14,7 +13,7 @@ class WebValidator extends DoctorValidator {
@override
Future<ValidationResult> validate() async {
final String chrome = findChromeExecutable();
final bool canRunChrome = canFindChrome();
final bool canRunChrome = chromeLauncher.canFindChrome();
final List<ValidationMessage> messages = <ValidationMessage>[
if (platform.environment.containsKey(kChromeEnvironment))
if (!canRunChrome)
......
......@@ -4,10 +4,8 @@
import '../base/context.dart';
import '../base/platform.dart';
import '../base/process_manager.dart';
import '../doctor.dart';
import '../features.dart';
import 'chrome.dart';
/// The web workflow instance.
WebWorkflow get webWorkflow => context.get<WebWorkflow>();
......@@ -19,21 +17,11 @@ class WebWorkflow extends Workflow {
bool get appliesToHostPlatform => featureFlags.isWebEnabled && (platform.isWindows || platform.isMacOS || platform.isLinux);
@override
bool get canLaunchDevices => featureFlags.isWebEnabled && canFindChrome();
bool get canLaunchDevices => featureFlags.isWebEnabled;
@override
bool get canListDevices => featureFlags.isWebEnabled && canFindChrome();
bool get canListDevices => featureFlags.isWebEnabled;
@override
bool get canListEmulators => false;
}
/// Whether we can locate the chrome executable.
bool canFindChrome() {
final String chrome = findChromeExecutable();
try {
return processManager.canRun(chrome);
} on ArgumentError {
return false;
}
}
......@@ -60,6 +60,36 @@ void main() {
expect(await device.portForwarder.forward(1), 1);
});
testUsingContext('Chrome device is listed when Chrome is available', () async {
when(mockChromeLauncher.canFindChrome()).thenReturn(true);
final WebDevices deviceDiscoverer = WebDevices();
final List<Device> devices = await deviceDiscoverer.pollingGetDevices();
expect(devices, contains(isInstanceOf<ChromeDevice>()));
}, overrides: <Type, Generator>{
ChromeLauncher: () => mockChromeLauncher,
});
testUsingContext('Chrome device is not listed when Chrome is not available', () async {
when(mockChromeLauncher.canFindChrome()).thenReturn(false);
final WebDevices deviceDiscoverer = WebDevices();
final List<Device> devices = await deviceDiscoverer.pollingGetDevices();
expect(devices, isNot(contains(isInstanceOf<ChromeDevice>())));
}, overrides: <Type, Generator>{
ChromeLauncher: () => mockChromeLauncher,
});
testUsingContext('Web Server device is listed even when Chrome is not available', () async {
when(mockChromeLauncher.canFindChrome()).thenReturn(false);
final WebDevices deviceDiscoverer = WebDevices();
final List<Device> devices = await deviceDiscoverer.pollingGetDevices();
expect(devices, contains(isInstanceOf<WebServerDevice>()));
}, overrides: <Type, Generator>{
ChromeLauncher: () => mockChromeLauncher,
});
testUsingContext('Chrome invokes version command on non-Windows platforms', () async{
when(mockPlatform.isWindows).thenReturn(false);
when(mockProcessManager.canRun('chrome.foo')).thenReturn(true);
......
......@@ -68,11 +68,7 @@ void main() {
}));
test('does not apply on other platforms', () => testbed.run(() {
when(mockProcessManager.canRun('chrome')).thenReturn(false);
expect(workflow.appliesToHostPlatform, false);
expect(workflow.canLaunchDevices, false);
expect(workflow.canListDevices, false);
expect(workflow.canListEmulators, false);
}, overrides: <Type, Generator>{
Platform: () => notSupported,
}));
......
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