Skip to content

Commit

Permalink
Try waiting for Dart detection this way
Browse files Browse the repository at this point in the history
  • Loading branch information
elliette committed Sep 19, 2023
1 parent 3f82057 commit 482abfd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
7 changes: 7 additions & 0 deletions dwds/debug_extension_mv3/web/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ import 'messaging.dart';
import 'storage.dart';
import 'utils.dart';

// Note: The extension tests wait for this string to be logged to the console to
// ensure that the Dart app has been detected:
const dartAppDetectedMsg = 'Dart app detected.';

void main() {
_registerListeners();
}
Expand Down Expand Up @@ -210,6 +214,9 @@ Future<void> _updateIcon(int activeTabId) async {
tabId: activeTabId,
);
multipleApps == null ? _setDebuggableIcon() : _setWarningIcon();
// Do not remove. This is required by the tests to wait for detection of the
// Dart app:
debugLog(dartAppDetectedMsg);
}

void _setDebuggableIcon() {
Expand Down
17 changes: 13 additions & 4 deletions dwds/test/puppeteer/extension_common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ void testAll({
backgroundPage: backgroundPage,
);
// Click on the Dart Debug Extension icon:
await _waitForDartDetection(hasServiceWorker: isMV3);
await workerEvalDelay(secondsToWait: 2);
await clickOnExtensionIcon(
worker: worker,
Expand Down Expand Up @@ -290,7 +291,7 @@ void testAll({
final appTab =
await navigateToPage(browser, url: appUrl, isNew: true);
// Click on the Dart Debug Extension icon:
await workerEvalDelay(secondsToWait: 2);
await _waitForDartDetection(hasServiceWorker: isMV3);
await clickOnExtensionIcon(
worker: worker,
backgroundPage: backgroundPage,
Expand Down Expand Up @@ -320,7 +321,7 @@ void testAll({
final appTab =
await navigateToPage(browser, url: appUrl, isNew: true);
// Click on the Dart Debug Extension icon:
await workerEvalDelay(secondsToWait: 2);
await _waitForDartDetection(hasServiceWorker: isMV3);
await clickOnExtensionIcon(
worker: worker,
backgroundPage: backgroundPage,
Expand All @@ -347,7 +348,7 @@ void testAll({
final appTab =
await navigateToPage(browser, url: appUrl, isNew: true);
// Click on the Dart Debug Extension icon:
await workerEvalDelay(secondsToWait: 2);
await _waitForDartDetection(hasServiceWorker: isMV3);
print('[debug log] click on extension icon');
await clickOnExtensionIcon(
worker: worker,
Expand Down Expand Up @@ -423,7 +424,7 @@ void testAll({
final appTab =
await navigateToPage(browser, url: appUrl, isNew: true);
// Click on the Dart Debug Extension icon:
await workerEvalDelay(secondsToWait: 2);
await _waitForDartDetection(hasServiceWorker: isMV3);
await clickOnExtensionIcon(
worker: worker,
backgroundPage: backgroundPage,
Expand Down Expand Up @@ -963,6 +964,14 @@ void testAll({
});
}

Future<bool> _waitForDartDetection({
required bool hasServiceWorker,
}) {
final source =
hasServiceWorker ? ConsoleSource.worker : ConsoleSource.background;
return waitForConsoleLog('Dart app detected.', source: source);
}

Future<bool> _clickLaunchButton(
Browser browser, {
required Panel panel,
Expand Down
41 changes: 38 additions & 3 deletions dwds/test/puppeteer/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ enum ConsoleSource {
worker,
}

final _backgroundLogs = [];
final _devToolsLogs = [];
final _workerLogs = [];
final _backgroundLogs = <String>[];
final _devToolsLogs = <String>[];
final _workerLogs = <String>[];

Future<String> buildDebugExtension({required bool isMV3}) async {
final extensionDir = absolutePath(pathFromDwds: 'debug_extension_mv3');
Expand Down Expand Up @@ -198,6 +198,41 @@ String getExtensionOrigin(Browser browser) {
return '$chromeExtension//$extensionId';
}

Future<bool> waitForConsoleLog(
String textToMatch, {
required ConsoleSource source,
Duration timeBetween = const Duration(milliseconds: 500),
int retries = 10,
}) async {
await Future.delayed(timeBetween);
List<String> logs;
switch (source) {
case ConsoleSource.background:
logs = _backgroundLogs;
break;
case ConsoleSource.devTools:
logs = _devToolsLogs;
break;
case ConsoleSource.worker:
logs = _workerLogs;
break;
}
final foundText = logs.where((log) => log.contains(textToMatch)).isNotEmpty;
if (foundText) {
return true;
}
if (retries > 0) {
final retriesLeft = retries - 1;
return waitForConsoleLog(
textToMatch,
source: source,
timeBetween: timeBetween,
retries: retriesLeft,
);
}
return false;
}

void _saveConsoleMsg({
required ConsoleSource source,
required String type,
Expand Down

0 comments on commit 482abfd

Please sign in to comment.