Skip to content

[MV3 Debug Extension] Clean up tests in preparation for supporting compiling to MV2 #1964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions dwds/test/puppeteer/extension_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,10 @@ void main() async {
final devToolsTabTarget = await browser.waitForTarget(
(target) => target.url.contains(devToolsUrlFragment));
// There should be no warning notifications:
var chromeNotifications = await worker.evaluate(_getNotifications());
var chromeNotifications = await evaluate(
_getNotifications(),
worker: worker,
);
expect(chromeNotifications, isEmpty);
// Navigate back to Dart app:
await navigateToPage(browser, url: appUrl, isNew: false);
Expand All @@ -286,7 +289,8 @@ void main() async {
await clickOnExtensionIcon(worker);
await workerEvalDelay();
// There should now be a warning notificiation:
chromeNotifications = await worker.evaluate(_getNotifications());
chromeNotifications =
await evaluate(_getNotifications(), worker: worker);
expect(chromeNotifications, isNotEmpty);
// Close the Dart app and the associated Dart DevTools:
await appTab.close();
Expand Down Expand Up @@ -705,15 +709,21 @@ Future<int> _getTabId(
required Worker worker,
}) async {
final jsExpression = _tabIdForTabJs(url);
return (await worker.evaluate(jsExpression)) as int;
return (await evaluate(
jsExpression,
worker: worker,
)) as int;
}

Future<int?> _getWindowId(
String url, {
required Worker worker,
}) async {
final jsExpression = _windowIdForTabJs(url);
return (await worker.evaluate(jsExpression)) as int?;
return (await evaluate(
jsExpression,
worker: worker,
)) as int?;
}

Future<T> _fetchStorageObj<T>(
Expand All @@ -722,10 +732,13 @@ Future<T> _fetchStorageObj<T>(
required Worker worker,
}) async {
final json = await retryFnAsync<String>(() async {
final storageObj = await worker.evaluate(_fetchStorageObjJs(
storageKey,
storageArea: storageArea,
));
final storageObj = await evaluate(
_fetchStorageObjJs(
storageKey,
storageArea: storageArea,
),
worker: worker,
);
return storageObj[storageKey];
});
if (T == String) return json as T;
Expand Down
Binary file modified dwds/test/puppeteer/test_images/chromeDevTools_externalBuild.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 35 additions & 22 deletions dwds/test/puppeteer/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import '../fixtures/context.dart';
import '../fixtures/utilities.dart';

enum ConsoleSource {
worker,
devTools,
worker,
}

final _workerLogs = [];
final _devToolsLogs = [];
final _workerLogs = [];

Future<String> buildDebugExtension() async {
final extensionDir = absolutePath(pathFromDwds: 'debug_extension_mv3');
Expand Down Expand Up @@ -65,7 +65,7 @@ Future<void> tearDownHelper({required Worker worker}) async {
_logConsoleMsgsOnFailure();
_workerLogs.clear();
_devToolsLogs.clear();
await worker.evaluate(_clearStorageJs).catchError((_) {});
await _clearStorage(worker: worker);
}

Future<Worker> getServiceWorker(Browser browser) async {
Expand All @@ -77,7 +77,7 @@ Future<Worker> getServiceWorker(Browser browser) async {
worker.url,
onConsoleApiCalled: (type, jsHandles, _) {
for (var handle in jsHandles) {
saveConsoleMsg(
_saveConsoleMsg(
source: ConsoleSource.worker, type: '$type', msg: '$handle');
}
},
Expand All @@ -91,7 +91,7 @@ Future<Page> getChromeDevToolsPage(Browser browser) async {
chromeDevToolsTarget.type = 'page';
final chromeDevToolsPage = await chromeDevToolsTarget.page;
chromeDevToolsPage.onConsole.listen((msg) {
saveConsoleMsg(
_saveConsoleMsg(
source: ConsoleSource.devTools,
type: '${msg.type}',
msg: msg.text ?? '',
Expand All @@ -100,26 +100,12 @@ Future<Page> getChromeDevToolsPage(Browser browser) async {
return chromeDevToolsPage;
}

void saveConsoleMsg({
required ConsoleSource source,
required String type,
required String msg,
}) {
if (msg.isEmpty) return;
final consiseMsg = msg.startsWith('JSHandle:') ? msg.substring(9) : msg;
final formatted = 'console.$type: $consiseMsg';
switch (source) {
case ConsoleSource.worker:
_workerLogs.add(formatted);
break;
case ConsoleSource.devTools:
_devToolsLogs.add(formatted);
break;
}
Future evaluate(String jsExpression, {required Worker worker}) async {
return worker.evaluate(jsExpression);
}

Future<void> clickOnExtensionIcon(Worker worker) async {
return worker.evaluate(_clickIconJs);
return evaluate(_clickIconJs, worker: worker);
}

// Note: The following delay is required to reduce flakiness. It makes
Expand Down Expand Up @@ -156,6 +142,24 @@ String getExtensionOrigin(Browser browser) {
return '$chromeExtension//$extensionId';
}

void _saveConsoleMsg({
required ConsoleSource source,
required String type,
required String msg,
}) {
if (msg.isEmpty) return;
final consiseMsg = msg.startsWith('JSHandle:') ? msg.substring(9) : msg;
final formatted = 'console.$type: $consiseMsg';
switch (source) {
case ConsoleSource.devTools:
_devToolsLogs.add(formatted);
break;
case ConsoleSource.worker:
_workerLogs.add(formatted);
break;
}
}

void _logConsoleMsgsOnFailure() {
if (_workerLogs.isNotEmpty) {
printOnFailure(['Service Worker logs:', ..._workerLogs].join('\n'));
Expand All @@ -174,6 +178,15 @@ Future<Page> _getPageForUrl(Browser browser, {required String url}) {
return pageTarget.page;
}

Future<void> _clearStorage({
required Worker worker,
}) async {
return evaluate(
_clearStorageJs,
worker: worker,
).catchError((_) {});
}

final _clickIconJs = '''
async () => {
const activeTabs = await chrome.tabs.query({ active: true });
Expand Down