Unverified Commit a317c2c0 authored by Lau Ching Jun's avatar Lau Ching Jun Committed by GitHub

Wrap launching devtools in DevtoolsLauncher (#62364)

parent 78efd3eb
......@@ -14,6 +14,7 @@ import 'artifacts.dart';
import 'asset.dart';
import 'base/command_help.dart';
import 'base/common.dart';
import 'base/context.dart';
import 'base/file_system.dart';
import 'base/io.dart' as io;
import 'base/logger.dart';
......@@ -799,7 +800,7 @@ abstract class ResidentRunner {
final CommandHelp commandHelp;
final bool machine;
io.HttpServer _devtoolsServer;
DevtoolsLauncher _devtoolsLauncher;
bool _exited = false;
Completer<int> _finished = Completer<int>();
......@@ -1254,28 +1255,14 @@ abstract class ResidentRunner {
}
Future<void> launchDevTools() async {
try {
assert(supportsServiceProtocol);
_devtoolsServer ??= await devtools_server.serveDevTools(
enableStdinCommands: false,
);
await devtools_server.launchDevTools(
<String, dynamic>{
'reuseWindows': true,
},
flutterDevices.first.vmService.httpAddress,
'http://${_devtoolsServer.address.host}:${_devtoolsServer.port}',
false, // headless mode,
false, // machine mode
);
} on Exception catch (e, st) {
globals.printTrace('Failed to launch DevTools: $e\n$st');
}
assert(supportsServiceProtocol);
_devtoolsLauncher ??= DevtoolsLauncher.instance;
return _devtoolsLauncher.launch(flutterDevices.first.vmService.httpAddress);
}
Future<void> shutdownDevtools() async {
await _devtoolsServer?.close();
_devtoolsServer = null;
await _devtoolsLauncher?.close();
_devtoolsLauncher = null;
}
Future<void> _serviceProtocolDone(dynamic object) async {
......@@ -1720,3 +1707,32 @@ String nextPlatform(String currentPlatform, FeatureFlags featureFlags) {
return 'android';
}
}
class DevtoolsLauncher {
io.HttpServer _devtoolsServer;
Future<void> launch(Uri observatoryAddress) async {
try {
_devtoolsServer ??= await devtools_server.serveDevTools(
enableStdinCommands: false,
);
await devtools_server.launchDevTools(
<String, dynamic>{
'reuseWindows': true,
},
observatoryAddress,
'http://${_devtoolsServer.address.host}:${_devtoolsServer.port}',
false, // headless mode,
false, // machine mode
);
} on Exception catch (e, st) {
globals.printTrace('Failed to launch DevTools: $e\n$st');
}
}
Future<void> close() async {
await _devtoolsServer?.close();
_devtoolsServer = null;
}
static DevtoolsLauncher get instance => context.get<DevtoolsLauncher>() ?? DevtoolsLauncher();
}
......@@ -122,6 +122,7 @@ void main() {
ResidentRunner residentRunner;
MockDevice mockDevice;
FakeVmServiceHost fakeVmServiceHost;
MockDevtoolsLauncher mockDevtoolsLauncher;
setUp(() {
testbed = Testbed(setup: () {
......@@ -142,6 +143,7 @@ void main() {
mockDevice = MockDevice();
mockVMService = MockVMService();
mockDevFS = MockDevFS();
mockDevtoolsLauncher = MockDevtoolsLauncher();
// DevFS Mocks
when(mockDevFS.lastCompiled).thenReturn(DateTime(2000));
......@@ -1298,6 +1300,18 @@ void main() {
)
}));
testUsingContext('ResidentRunner invokes DevtoolsLauncher when launching and shutting down Devtools', () => testbed.run(() async {
when(mockFlutterDevice.vmService).thenReturn(fakeVmServiceHost.vmService);
setHttpAddress(testUri, fakeVmServiceHost.vmService);
await residentRunner.launchDevTools();
verify(mockDevtoolsLauncher.launch(testUri)).called(1);
await residentRunner.shutdownDevtools();
verify(mockDevtoolsLauncher.close()).called(1);
}), overrides: <Type, Generator>{
DevtoolsLauncher: () => mockDevtoolsLauncher,
});
testUsingContext('ResidentRunner can take screenshot on debug device', () => testbed.run(() async {
fakeVmServiceHost = FakeVmServiceHost(requests: <VmServiceExpectation>[
listViews,
......@@ -2076,6 +2090,7 @@ class MockDevFS extends Mock implements DevFS {}
class MockDevice extends Mock implements Device {}
class MockDeviceLogReader extends Mock implements DeviceLogReader {}
class MockDevicePortForwarder extends Mock implements DevicePortForwarder {}
class MockDevtoolsLauncher extends Mock implements DevtoolsLauncher {}
class MockUsage extends Mock implements Usage {}
class MockProcessManager extends Mock implements ProcessManager {}
class MockResidentCompiler extends Mock implements ResidentCompiler {}
......
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