Skip to content

Commit

Permalink
Keeping the first instance of an app connection and extending timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Markzipan committed Dec 18, 2023
1 parent 5b9854e commit c606696
Show file tree
Hide file tree
Showing 5 changed files with 577 additions and 514 deletions.
196 changes: 101 additions & 95 deletions dwds/test/devtools_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,110 +32,115 @@ void main() {

final context = TestContext(TestProject.testWithSoundNullSafety, provider);

group('Injected client', () {
setUp(() async {
await context.setUp(
debugSettings: TestDebugSettings.withDevTools(context),
);
await context.webDriver.driver.keyboard.sendChord([Keyboard.alt, 'd']);
// Wait for DevTools to actually open.
await Future.delayed(const Duration(seconds: 2));
});

tearDown(() async {
await context.tearDown();
});

test(
'can launch devtools',
() async {
final windows = await context.webDriver.windows.toList();
await context.webDriver.driver.switchTo.window(windows.last);
expect(await context.webDriver.pageSource, contains('DevTools'));
expect(await context.webDriver.currentUrl, contains('ide=Dwds'));
// TODO(https://github.com/dart-lang/webdev/issues/1888): Re-enable.
},
skip: Platform.isWindows,
);

test('can not launch devtools for the same app in multiple tabs', () async {
final appUrl = await context.webDriver.currentUrl;
// Open a new tab, select it, and navigate to the app
await context.webDriver.driver
.execute("window.open('$appUrl', '_blank');", []);
await Future.delayed(const Duration(seconds: 2));
final newAppWindow = await context.webDriver.windows.last;
await newAppWindow.setAsActive();
group(
'Injected client',
() {
setUp(() async {
await context.setUp(
debugSettings: TestDebugSettings.withDevTools(context),
);
await context.webDriver.driver.keyboard.sendChord([Keyboard.alt, 'd']);
// Wait for DevTools to actually open.
await Future.delayed(const Duration(seconds: 2));
});

// Wait for the page to be ready before trying to open DevTools again.
await _waitForPageReady(context);
tearDown(() async {
await context.tearDown();
});

// Try to open devtools and check for the alert.
await context.webDriver.driver.keyboard.sendChord([Keyboard.alt, 'd']);
await Future.delayed(const Duration(seconds: 2));
final alert = context.webDriver.driver.switchTo.alert;
expect(alert, isNotNull);
expect(
await alert.text,
contains('This app is already being debugged in a different tab'),
test(
'can launch devtools',
() async {
final windows = await context.webDriver.windows.toList();
await context.webDriver.driver.switchTo.window(windows.last);
expect(await context.webDriver.pageSource, contains('DevTools'));
expect(await context.webDriver.currentUrl, contains('ide=Dwds'));
// TODO(https://github.com/dart-lang/webdev/issues/1888): Re-enable.
},
skip: Platform.isWindows,
);
await alert.accept();

var windows = await context.webDriver.windows.toList();
for (final window in windows) {
if (window.id != newAppWindow.id) {
await window.setAsActive();
await window.close();
}
}
test('can not launch devtools for the same app in multiple tabs',
() async {
final appUrl = await context.webDriver.currentUrl;
// Open a new tab, select it, and navigate to the app
await context.webDriver.driver
.execute("window.open('$appUrl', '_blank');", []);
await Future.delayed(const Duration(seconds: 2));
final newAppWindow = await context.webDriver.windows.last;
await newAppWindow.setAsActive();

await newAppWindow.setAsActive();
await context.webDriver.driver.keyboard.sendChord([Keyboard.alt, 'd']);
await Future.delayed(const Duration(seconds: 2));
windows = await context.webDriver.windows.toList();
final devToolsWindow =
windows.firstWhere((window) => window != newAppWindow);
await devToolsWindow.setAsActive();
expect(await context.webDriver.pageSource, contains('DevTools'));
});
// Wait for the page to be ready before trying to open DevTools again.
await _waitForPageReady(context);

test(
'destroys and recreates the isolate during a page refresh',
() async {
// This test is the same as one in reload_test, but runs here when there
// is a connected client (DevTools) since it can behave differently.
// https://github.com/dart-lang/webdev/pull/901#issuecomment-586438132
final client = context.debugConnection.vmService;
await client.streamListen('Isolate');
context.makeEditToDartEntryFile(
toReplace: 'Hello World!',
replaceWith: 'Bonjour le monde!',
);
await context.waitForSuccessfulBuild(propagateToBrowser: true);

final eventsDone = expectLater(
client.onIsolateEvent,
emitsThrough(
emitsInOrder([
_hasKind(EventKind.kIsolateExit),
_hasKind(EventKind.kIsolateStart),
_hasKind(EventKind.kIsolateRunnable),
]),
),
// Try to open devtools and check for the alert.
await context.webDriver.driver.keyboard.sendChord([Keyboard.alt, 'd']);
await Future.delayed(const Duration(seconds: 2));
final alert = context.webDriver.driver.switchTo.alert;
expect(alert, isNotNull);
expect(
await alert.text,
contains('This app is already being debugged in a different tab'),
);
await alert.accept();

await context.webDriver.driver.refresh();
var windows = await context.webDriver.windows.toList();
for (final window in windows) {
if (window.id != newAppWindow.id) {
await window.setAsActive();
await window.close();
}
}

await eventsDone;
// Re-set the edited file:
context.makeEditToDartEntryFile(
toReplace: 'Bonjour le monde!',
replaceWith: 'Hello World!',
);
},
skip: 'https://github.com/dart-lang/webdev/issues/1888',
);
});
await newAppWindow.setAsActive();
await context.webDriver.driver.keyboard.sendChord([Keyboard.alt, 'd']);
await Future.delayed(const Duration(seconds: 2));
windows = await context.webDriver.windows.toList();
final devToolsWindow =
windows.firstWhere((window) => window != newAppWindow);
await devToolsWindow.setAsActive();
expect(await context.webDriver.pageSource, contains('DevTools'));
});

test(
'destroys and recreates the isolate during a page refresh',
() async {
// This test is the same as one in reload_test, but runs here when there
// is a connected client (DevTools) since it can behave differently.
// https://github.com/dart-lang/webdev/pull/901#issuecomment-586438132
final client = context.debugConnection.vmService;
await client.streamListen('Isolate');
context.makeEditToDartEntryFile(
toReplace: 'Hello World!',
replaceWith: 'Bonjour le monde!',
);
await context.waitForSuccessfulBuild(propagateToBrowser: true);

final eventsDone = expectLater(
client.onIsolateEvent,
emitsThrough(
emitsInOrder([
_hasKind(EventKind.kIsolateExit),
_hasKind(EventKind.kIsolateStart),
_hasKind(EventKind.kIsolateRunnable),
]),
),
);

await context.webDriver.driver.refresh();

await eventsDone;
// Re-set the edited file:
context.makeEditToDartEntryFile(
toReplace: 'Bonjour le monde!',
replaceWith: 'Hello World!',
);
},
skip: 'https://github.com/dart-lang/webdev/issues/1888',
);
},
timeout: Timeout.factor(2),
);

group('Injected client without a DevTools server', () {
setUp(() async {
Expand Down Expand Up @@ -193,6 +198,7 @@ void main() {
},
tags: ['extension'],
skip: 'https://github.com/dart-lang/webdev/issues/2114',
timeout: Timeout.factor(2),
);
}

Expand Down
12 changes: 10 additions & 2 deletions dwds/test/fixtures/context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,13 @@ class TestContext {
if (testSettings.autoRun) {
connection.runMain();
}
appConnection = connection;
appConnectionCompleter.complete();

// We may reuse the app connection, so only save it the first time
// it's encountered.
if (!appConnectionCompleter.isCompleted) {
appConnection = connection;
appConnectionCompleter.complete();
}
});

_appUrl = basePath.isEmpty
Expand Down Expand Up @@ -451,6 +456,9 @@ class TestContext {
if (debugSettings.enableDebugging && !testSettings.waitToDebug) {
await startDebugging();
}
} else {
// No tab needs to be dicovered, so fulfill the relevant completer.
tabConnectionCompleter.complete();
}
} catch (e, s) {
_logger.severe('Failed to setup the service, $e:$s');
Expand Down
Loading

0 comments on commit c606696

Please sign in to comment.