diff --git a/dwds/debug_extension_mv3/web/background.dart b/dwds/debug_extension_mv3/web/background.dart index 1ccec7d2e..97cd2df94 100644 --- a/dwds/debug_extension_mv3/web/background.dart +++ b/dwds/debug_extension_mv3/web/background.dart @@ -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(); } @@ -210,6 +214,9 @@ Future _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() { diff --git a/dwds/test/puppeteer/extension_common.dart b/dwds/test/puppeteer/extension_common.dart index e9f34ce2a..0800d77fe 100644 --- a/dwds/test/puppeteer/extension_common.dart +++ b/dwds/test/puppeteer/extension_common.dart @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, @@ -963,6 +964,14 @@ void testAll({ }); } +Future _waitForDartDetection({ + required bool hasServiceWorker, +}) { + final source = + hasServiceWorker ? ConsoleSource.worker : ConsoleSource.background; + return waitForConsoleLog('Dart app detected.', source: source); +} + Future _clickLaunchButton( Browser browser, { required Panel panel, diff --git a/dwds/test/puppeteer/test_utils.dart b/dwds/test/puppeteer/test_utils.dart index dcf7df31d..3b7bcbd5c 100644 --- a/dwds/test/puppeteer/test_utils.dart +++ b/dwds/test/puppeteer/test_utils.dart @@ -17,9 +17,9 @@ enum ConsoleSource { worker, } -final _backgroundLogs = []; -final _devToolsLogs = []; -final _workerLogs = []; +final _backgroundLogs = []; +final _devToolsLogs = []; +final _workerLogs = []; Future buildDebugExtension({required bool isMV3}) async { final extensionDir = absolutePath(pathFromDwds: 'debug_extension_mv3'); @@ -198,6 +198,41 @@ String getExtensionOrigin(Browser browser) { return '$chromeExtension//$extensionId'; } +Future waitForConsoleLog( + String textToMatch, { + required ConsoleSource source, + Duration timeBetween = const Duration(milliseconds: 500), + int retries = 10, +}) async { + await Future.delayed(timeBetween); + List 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,