diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 14:29:10 +0000 |
commit | 2aa4a82499d4becd2284cdb482213d541b8804dd (patch) | |
tree | b80bf8bf13c3766139fbacc530efd0dd9d54394c /devtools/client/framework/test/browser_source_map-pub-sub.js | |
parent | Initial commit. (diff) | |
download | firefox-upstream.tar.xz firefox-upstream.zip |
Adding upstream version 86.0.1.upstream/86.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/client/framework/test/browser_source_map-pub-sub.js')
-rw-r--r-- | devtools/client/framework/test/browser_source_map-pub-sub.js | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/devtools/client/framework/test/browser_source_map-pub-sub.js b/devtools/client/framework/test/browser_source_map-pub-sub.js new file mode 100644 index 0000000000..4e05f353c7 --- /dev/null +++ b/devtools/client/framework/test/browser_source_map-pub-sub.js @@ -0,0 +1,97 @@ +/* Any copyright is dedicated to the Public Domain. + http://creativecommons.org/publicdomain/zero/1.0/ */ + +// Test that the source map service subscribe mechanism work as expected. + +"use strict"; + +const JS_URL = URL_ROOT + "code_bundle_no_race.js"; + +const PAGE_URL = `data:text/html, +<!doctype html> +<html> + <head> + <meta charset="utf-8"/> + </head> + <body> + <script src="${JS_URL}"></script> + </body> +</html>`; + +const ORIGINAL_URL = "webpack:///code_no_race.js"; + +const SOURCE_MAP_PREF = "devtools.source-map.client-service.enabled"; + +const GENERATED_LINE = 84; +const ORIGINAL_LINE = 11; + +add_task(async function() { + // Push a pref env so any changes will be reset at the end of the test. + await SpecialPowers.pushPrefEnv({}); + + // Opening the debugger causes the source actors to be created. + const toolbox = await openNewTabAndToolbox(PAGE_URL, "jsdebugger"); + const service = toolbox.sourceMapURLService; + + const cbCalls = []; + const cb = originalLocation => cbCalls.push(originalLocation); + const expectedArg = { url: ORIGINAL_URL, line: ORIGINAL_LINE, column: 0 }; + + // Wait for the sources to fully populate so that waitForSubscriptionsToSettle + // can be guaranteed that all actions have been queued. + await service._ensureAllSourcesPopulated(); + + const unsubscribe1 = service.subscribeByURL(JS_URL, GENERATED_LINE, 1, cb); + + // Wait for the query to finish and populate so that all of the later + // logic with this position will run synchronously, and the subscribe has run. + for (const map of service._mapsById.values()) { + for (const query of map.queries.values()) { + await query.action; + } + } + + is( + cbCalls.length, + 1, + "The callback function is called directly when subscribing" + ); + Assert.deepEqual( + cbCalls[0], + expectedArg, + "callback called with expected arguments" + ); + + const unsubscribe2 = service.subscribeByURL(JS_URL, GENERATED_LINE, 1, cb); + is(cbCalls.length, 2, "Subscribing to the same location twice works"); + Assert.deepEqual( + cbCalls[1], + expectedArg, + "callback called with expected arguments" + ); + + info("Manually call the dispatcher to ensure subscribers are called"); + Services.prefs.setBoolPref(SOURCE_MAP_PREF, false); + is(cbCalls.length, 4, "both subscribers were called"); + Assert.deepEqual(cbCalls[2], null, "callback called with expected arguments"); + Assert.deepEqual( + cbCalls[2], + cbCalls[3], + "callbacks were passed the same arguments" + ); + + info("Check unsubscribe functions"); + unsubscribe1(); + Services.prefs.setBoolPref(SOURCE_MAP_PREF, true); + is(cbCalls.length, 5, "Only remainer subscriber callback was called"); + Assert.deepEqual( + cbCalls[4], + expectedArg, + "callback called with expected arguments" + ); + + unsubscribe2(); + Services.prefs.setBoolPref(SOURCE_MAP_PREF, false); + Services.prefs.setBoolPref(SOURCE_MAP_PREF, true); + is(cbCalls.length, 5, "No callbacks were called"); +}); |