Add 'v' hotkey to open DevTools in the browser (#82227)

* Add 'v' hotkey to open DevTools in the browser
This commit is contained in:
Kenzie Schmoll
2021-05-13 09:54:10 -07:00
committed by GitHub
parent 1363527248
commit 766b4509c6
60 changed files with 343 additions and 186 deletions

View File

@@ -156,6 +156,11 @@ class CommandHelp {
'debugDumpRenderTree',
);
late final CommandHelpOption v = _makeOption(
'v',
'Open Flutter DevTools.',
);
late final CommandHelpOption w = _makeOption(
'w',
'Dump widget hierarchy to the console.',

View File

@@ -6,8 +6,10 @@
import 'dart:async';
import 'package:browser_launcher/browser_launcher.dart';
import 'package:meta/meta.dart';
import 'base/common.dart';
import 'base/logger.dart';
import 'build_info.dart';
import 'resident_runner.dart';
@@ -33,7 +35,12 @@ abstract class ResidentDevtoolsHandler {
Future<void> hotRestart(List<FlutterDevice> flutterDevices);
Future<void> serveAndAnnounceDevTools({Uri devToolsServerAddress, List<FlutterDevice> flutterDevices});
Future<void> serveAndAnnounceDevTools({
Uri devToolsServerAddress,
@required List<FlutterDevice> flutterDevices,
});
bool launchDevToolsInBrowser({@required List<FlutterDevice> flutterDevices});
Future<void> shutdown();
}
@@ -41,12 +48,17 @@ abstract class ResidentDevtoolsHandler {
class FlutterResidentDevtoolsHandler implements ResidentDevtoolsHandler {
FlutterResidentDevtoolsHandler(this._devToolsLauncher, this._residentRunner, this._logger);
static const Duration launchInBrowserTimeout = Duration(seconds: 15);
final DevtoolsLauncher _devToolsLauncher;
final ResidentRunner _residentRunner;
final Logger _logger;
bool _shutdown = false;
bool _served = false;
@visibleForTesting
bool launchedInBrowser = false;
@override
DevToolsServerAddress get activeDevToolsServer => _devToolsLauncher?.activeDevToolsServer;
@@ -66,8 +78,8 @@ class FlutterResidentDevtoolsHandler implements ResidentDevtoolsHandler {
if (devToolsServerAddress != null) {
_devToolsLauncher.devToolsUrl = devToolsServerAddress;
} else {
_served = true;
await _devToolsLauncher.serve();
_served = true;
}
await _devToolsLauncher.ready;
final List<FlutterDevice> devicesWithExtension = await _devicesWithExtensions(flutterDevices);
@@ -81,6 +93,35 @@ class FlutterResidentDevtoolsHandler implements ResidentDevtoolsHandler {
}
}
// This must be guaranteed not to return a Future that fails.
@override
bool launchDevToolsInBrowser({@required List<FlutterDevice> flutterDevices}) {
if (!_residentRunner.supportsServiceProtocol || _devToolsLauncher == null) {
return false;
}
if (_devToolsLauncher.devToolsUrl == null) {
_logger.startProgress('Waiting for Flutter DevTools to be served...');
unawaited(_devToolsLauncher.ready.then((_) {
_launchDevToolsForDevices(flutterDevices);
}));
} else {
_launchDevToolsForDevices(flutterDevices);
}
return true;
}
void _launchDevToolsForDevices(List<FlutterDevice> flutterDevices) {
assert(activeDevToolsServer != null);
for (final FlutterDevice device in flutterDevices) {
final String devToolsUrl = activeDevToolsServer.uri?.replace(
queryParameters: <String, dynamic>{'uri': '${device.vmService.httpAddress}'},
).toString();
_logger.printStatus('Launching Flutter DevTools for ${device.device.name} at $devToolsUrl');
unawaited(Chrome.start(<String>[devToolsUrl]));
}
launchedInBrowser = true;
}
Future<void> _maybeCallDevToolsUriServiceExtension(
List<FlutterDevice> flutterDevices,
) async {
@@ -233,6 +274,11 @@ class NoOpDevtoolsHandler implements ResidentDevtoolsHandler {
return;
}
@override
bool launchDevToolsInBrowser({List<FlutterDevice> flutterDevices}) {
return false;
}
@override
Future<void> shutdown() async {
wasShutdown = true;

View File

@@ -672,6 +672,8 @@ abstract class ResidentHandlers {
/// Whether all of the connected devices support hot reload.
bool get canHotReload;
ResidentDevtoolsHandler get residentDevtoolsHandler;
@protected
Logger get logger;
@@ -1113,6 +1115,7 @@ abstract class ResidentRunner extends ResidentHandlers {
final CommandHelp commandHelp;
final bool machine;
@override
ResidentDevtoolsHandler get residentDevtoolsHandler => _residentDevtoolsHandler;
ResidentDevtoolsHandler _residentDevtoolsHandler;
@@ -1454,6 +1457,7 @@ abstract class ResidentRunner extends ResidentHandlers {
}
void printHelpDetails() {
commandHelp.v.print();
if (flutterDevices.any((FlutterDevice d) => d.device.supportsScreenshot)) {
commandHelp.s.print();
}
@@ -1694,6 +1698,9 @@ class TerminalHandler {
return residentRunner.debugDumpRenderTree();
case 'U':
return residentRunner.debugDumpSemanticsTreeInInverseHitTestOrder();
case 'v':
case 'V':
return residentRunner.residentDevtoolsHandler.launchDevToolsInBrowser(flutterDevices: residentRunner.flutterDevices);
case 'w':
case 'W':
return residentRunner.debugDumpApp();

View File

@@ -10,15 +10,16 @@ dependencies:
# To update these, use "flutter update-packages --force-upgrade".
archive: 3.1.2
args: 2.1.0
browser_launcher: 1.0.0
dds: 1.7.6
dwds: 11.0.1
dwds: 11.0.2
completion: 1.0.0
coverage: 1.0.2
crypto: 3.0.1
file: 6.1.0
flutter_template_images: 1.0.1
html: 0.15.0
http: 0.13.2
http: 0.13.3
intl: 0.17.0
meta: 1.3.0
multicast_dns: 0.3.0
@@ -33,10 +34,10 @@ dependencies:
xml: 5.1.0
yaml: 3.1.0
native_stack_traces: 0.4.0
shelf: 1.1.1
shelf: 1.1.2
vm_snapshot_analysis: 0.7.0
uuid: 3.0.4
web_socket_channel: 2.0.0
web_socket_channel: 2.1.0
stream_channel: 2.1.0
shelf_web_socket: 1.0.1
shelf_static: 1.0.0
@@ -47,7 +48,7 @@ dependencies:
logging: 1.0.1
http_multi_server: 3.0.1
convert: 3.0.0
async: 2.6.0
async: 2.6.1
ffi: 1.0.0
win32: 2.0.5
@@ -106,4 +107,4 @@ dartdoc:
# Exclude this package from the hosted API docs.
nodoc: true
# PUBSPEC CHECKSUM: e23e
# PUBSPEC CHECKSUM: 2647

View File

@@ -206,6 +206,7 @@ void main() {
expect(commandHelp.r.toString(), equals('\x1B[1mr\x1B[22m Hot reload. $fire$fire$fire'));
expect(commandHelp.s.toString(), equals('\x1B[1ms\x1B[22m Save a screenshot to flutter.png.'));
expect(commandHelp.t.toString(), equals('\x1B[1mt\x1B[22m Dump rendering tree to the console. \x1B[90m(debugDumpRenderTree)\x1B[39m\x1B[22m'));
expect(commandHelp.v.toString(), equals('\x1B[1mv\x1B[22m Open Flutter DevTools.'));
expect(commandHelp.w.toString(), equals('\x1B[1mw\x1B[22m Dump widget hierarchy to the console. \x1B[90m(debugDumpApp)\x1B[39m\x1B[22m'));
expect(commandHelp.z.toString(), equals('\x1B[1mz\x1B[22m Toggle elevation checker. \x1B[90m(debugCheckElevationsEnabled)\x1B[39m\x1B[22m'));
});
@@ -237,6 +238,7 @@ void main() {
expect(commandHelp.r.toString(), equals('r Hot reload. $fire$fire$fire'));
expect(commandHelp.s.toString(), equals('s Save a screenshot to flutter.png.'));
expect(commandHelp.t.toString(), equals('t Dump rendering tree to the console. (debugDumpRenderTree)'));
expect(commandHelp.v.toString(), equals('v Open Flutter DevTools.'));
expect(commandHelp.w.toString(), equals('w Dump widget hierarchy to the console. (debugDumpApp)'));
expect(commandHelp.z.toString(), equals('z Toggle elevation checker. (debugCheckElevationsEnabled)'));
});

View File

@@ -4,6 +4,8 @@
// @dart = 2.8
import 'dart:async';
import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/base/platform.dart';
import 'package:flutter_tools/src/build_info.dart';
@@ -353,19 +355,84 @@ void main() {
],
);
});
testWithoutContext('Does not launch devtools in browser if launcher is null', () async {
final FlutterResidentDevtoolsHandler handler = FlutterResidentDevtoolsHandler(
null,
FakeResidentRunner(),
BufferLogger.test(),
);
handler.launchDevToolsInBrowser(flutterDevices: <FlutterDevice>[]);
expect(handler.launchedInBrowser, isFalse);
expect(handler.activeDevToolsServer, null);
});
testWithoutContext('Does not launch devtools in browser if ResidentRunner does not support the service protocol', () async {
final FlutterResidentDevtoolsHandler handler = FlutterResidentDevtoolsHandler(
FakeDevtoolsLauncher(),
FakeResidentRunner()..supportsServiceProtocol = false,
BufferLogger.test(),
);
handler.launchDevToolsInBrowser(flutterDevices: <FlutterDevice>[]);
expect(handler.launchedInBrowser, isFalse);
expect(handler.activeDevToolsServer, null);
});
testWithoutContext('launchDevToolsInBrowser launches after _devToolsLauncher.ready completes', () async {
final Completer<void> completer = Completer<void>();
final FlutterResidentDevtoolsHandler handler = FlutterResidentDevtoolsHandler(
FakeDevtoolsLauncher()
..devToolsUrl = null
// We need to set [activeDevToolsServer] to simulate the state we would
// be in after serving devtools completes.
..activeDevToolsServer = DevToolsServerAddress('localhost', 8080)
..readyCompleter = completer,
FakeResidentRunner(),
BufferLogger.test(),
);
expect(handler.launchDevToolsInBrowser(flutterDevices: <FlutterDevice>[]), isTrue);
expect(handler.launchedInBrowser, isFalse);
completer.complete();
// Await a short delay to give DevTools time to launch.
await Future<void>.delayed(const Duration(microseconds: 100));
expect(handler.launchedInBrowser, isTrue);
});
testWithoutContext('launchDevToolsInBrowser launches successfully', () async {
final FlutterResidentDevtoolsHandler handler = FlutterResidentDevtoolsHandler(
FakeDevtoolsLauncher()
..devToolsUrl = Uri(host: 'localhost', port: 8080)
..activeDevToolsServer = DevToolsServerAddress('localhost', 8080),
FakeResidentRunner(),
BufferLogger.test(),
);
expect(handler.launchDevToolsInBrowser(flutterDevices: <FlutterDevice>[]), isTrue);
expect(handler.launchedInBrowser, isTrue);
});
}
class FakeDevtoolsLauncher extends Fake implements DevtoolsLauncher {
@override
DevToolsServerAddress activeDevToolsServer;
@override
Uri devToolsUrl;
@override
Future<DevToolsServerAddress> serve() {
return null;
}
@override
Future<void> get ready => Future<void>.value();
Future<void> get ready => readyCompleter.future;
Completer<void> readyCompleter = Completer<void>()..complete();
}
class FakeResidentRunner extends Fake implements ResidentRunner {

View File

@@ -1514,6 +1514,7 @@ void main() {
'Flutter run key commands.',
commandHelp.r,
commandHelp.R,
commandHelp.v,
commandHelp.s,
commandHelp.w,
commandHelp.t,
@@ -1598,6 +1599,7 @@ void main() {
expect(testLogger.statusText, equals(
<dynamic>[
'Flutter run key commands.',
commandHelp.v,
commandHelp.s,
commandHelp.hWithDetails,
commandHelp.c,

View File

@@ -17,6 +17,7 @@ import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/compile.dart';
import 'package:flutter_tools/src/convert.dart';
import 'package:flutter_tools/src/device.dart';
import 'package:flutter_tools/src/resident_devtools_handler.dart';
import 'package:flutter_tools/src/resident_runner.dart';
import 'package:flutter_tools/src/vmservice.dart';
import 'package:test/fake.dart';
@@ -794,6 +795,18 @@ void main() {
expect(terminalHandler.logger.statusText, contains('WIDGET DATA 2'));
});
testWithoutContext('v - launchDevToolsInBrowser', () async {
final TerminalHandler terminalHandler = setUpTerminalHandler(
<FakeVmServiceRequest>[],
supportsServiceProtocol: true);
final FakeResidentRunner runner = terminalHandler.residentRunner as FakeResidentRunner;
final FakeResidentDevtoolsHandler devtoolsHandler = runner.residentDevtoolsHandler as FakeResidentDevtoolsHandler;
expect(devtoolsHandler.calledLaunchDevToolsInBrowser, isFalse);
await terminalHandler.processTerminalInput('v');
expect(devtoolsHandler.calledLaunchDevToolsInBrowser, isTrue);
});
testWithoutContext('w,W - debugDumpApp without service protocol is skipped', () async {
final TerminalHandler terminalHandler = setUpTerminalHandler(<FakeVmServiceRequest>[], supportsServiceProtocol: false);
await terminalHandler.processTerminalInput('w');
@@ -1349,6 +1362,19 @@ class FakeResidentRunner extends ResidentHandlers {
}
return OperationResult(reloadExitCode, '', fatal: fatalReloadError);
}
@override
ResidentDevtoolsHandler get residentDevtoolsHandler => _residentDevtoolsHandler;
final ResidentDevtoolsHandler _residentDevtoolsHandler = FakeResidentDevtoolsHandler();
}
class FakeResidentDevtoolsHandler extends Fake implements ResidentDevtoolsHandler {
bool calledLaunchDevToolsInBrowser = false;
@override
bool launchDevToolsInBrowser({List<FlutterDevice> flutterDevices}) {
return calledLaunchDevToolsInBrowser = true;
}
}
class FakeDevice extends Fake implements Device {

View File

@@ -563,6 +563,7 @@ void main() {
'Flutter run key commands.',
startsWith('r Hot reload.'),
'R Hot restart.',
'v Open Flutter DevTools.',
'w Dump widget hierarchy to the console. (debugDumpApp)',
't Dump rendering tree to the console. (debugDumpRenderTree)',
'L Dump layer tree to the console. (debugDumpLayerTree)',