tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_source_map-pub-sub.js (2947B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Test that the source map service subscribe mechanism work as expected.
      5 
      6 "use strict";
      7 
      8 const JS_URL = URL_ROOT + "code_bundle_no_race.js";
      9 
     10 const PAGE_URL = `data:text/html,
     11 <!doctype html>
     12 <html>
     13  <head>
     14    <meta charset="utf-8"/>
     15  </head>
     16  <body>
     17    <script src="${JS_URL}"></script>
     18  </body>
     19 </html>`;
     20 
     21 const ORIGINAL_URL = "webpack:///code_no_race.js";
     22 
     23 const SOURCE_MAP_PREF = "devtools.source-map.client-service.enabled";
     24 
     25 const GENERATED_LINE = 84;
     26 const ORIGINAL_LINE = 11;
     27 
     28 add_task(async function () {
     29  // Push a pref env so any changes will be reset at the end of the test.
     30  await SpecialPowers.pushPrefEnv({});
     31 
     32  // Opening the debugger causes the source actors to be created.
     33  const toolbox = await openNewTabAndToolbox(PAGE_URL, "jsdebugger");
     34  const service = toolbox.sourceMapURLService;
     35 
     36  const cbCalls = [];
     37  const cb = originalLocation => cbCalls.push(originalLocation);
     38  const expectedArg = { url: ORIGINAL_URL, line: ORIGINAL_LINE, column: 0 };
     39 
     40  // Wait for the sources to fully populate so that waitForSubscriptionsToSettle
     41  // can be guaranteed that all actions have been queued.
     42  await service._ensureAllSourcesPopulated();
     43 
     44  const unsubscribe1 = service.subscribeByURL(JS_URL, GENERATED_LINE, 1, cb);
     45 
     46  // Wait for the query to finish and populate so that all of the later
     47  // logic with this position will run synchronously, and the subscribe has run.
     48  for (const map of service._mapsById.values()) {
     49    for (const query of map.queries.values()) {
     50      await query.action;
     51    }
     52  }
     53 
     54  is(
     55    cbCalls.length,
     56    1,
     57    "The callback function is called directly when subscribing"
     58  );
     59  Assert.deepEqual(
     60    cbCalls[0],
     61    expectedArg,
     62    "callback called with expected arguments"
     63  );
     64 
     65  const unsubscribe2 = service.subscribeByURL(JS_URL, GENERATED_LINE, 1, cb);
     66  is(cbCalls.length, 2, "Subscribing to the same location twice works");
     67  Assert.deepEqual(
     68    cbCalls[1],
     69    expectedArg,
     70    "callback called with expected arguments"
     71  );
     72 
     73  info("Manually call the dispatcher to ensure subscribers are called");
     74  Services.prefs.setBoolPref(SOURCE_MAP_PREF, false);
     75  is(cbCalls.length, 4, "both subscribers were called");
     76  Assert.deepEqual(cbCalls[2], null, "callback called with expected arguments");
     77  Assert.deepEqual(
     78    cbCalls[2],
     79    cbCalls[3],
     80    "callbacks were passed the same arguments"
     81  );
     82 
     83  info("Check unsubscribe functions");
     84  unsubscribe1();
     85  Services.prefs.setBoolPref(SOURCE_MAP_PREF, true);
     86  is(cbCalls.length, 5, "Only remainer subscriber callback was called");
     87  Assert.deepEqual(
     88    cbCalls[4],
     89    expectedArg,
     90    "callback called with expected arguments"
     91  );
     92 
     93  unsubscribe2();
     94  Services.prefs.setBoolPref(SOURCE_MAP_PREF, false);
     95  Services.prefs.setBoolPref(SOURCE_MAP_PREF, true);
     96  is(cbCalls.length, 5, "No callbacks were called");
     97 });