tor-browser

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

test_error_reporting.html (3699B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 Bug 1246341: Report message delivery failures to the Push server.
      5 
      6 Any copyright is dedicated to the Public Domain.
      7 http://creativecommons.org/licenses/publicdomain/
      8 
      9 -->
     10 <head>
     11  <title>Test for Bug 1246341</title>
     12  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     13  <script type="text/javascript" src="/tests/dom/push/test/test_utils.js"></script>
     14  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     15  <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
     16 </head>
     17 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1246341">Mozilla Bug 1246341</a>
     18 <p id="display"></p>
     19 <div id="content" style="display: none">
     20 
     21 </div>
     22 <pre id="test">
     23 </pre>
     24 
     25 <script class="testbody" type="text/javascript">
     26  var pushNotifier = SpecialPowers.Cc["@mozilla.org/push/Notifier;1"]
     27                                  .getService(SpecialPowers.Ci.nsIPushNotifier);
     28 
     29  var reporters = new Map();
     30 
     31  var registration;
     32  add_task(async function start() {
     33    await setupPrefsAndReplaceService({
     34      reportDeliveryError(messageId, reason) {
     35        ok(reporters.has(messageId),
     36          "Unexpected error reported for message " + messageId);
     37        var resolve = reporters.get(messageId);
     38        reporters.delete(messageId);
     39        resolve(reason);
     40      },
     41    });
     42    await setPushPermission(true);
     43 
     44    var url = "error_worker.js?caller=test_error_reporting.html";
     45    registration = await navigator.serviceWorker.register(url, {scope: "."});
     46    await waitForActive(registration);
     47  });
     48 
     49  var controlledFrame;
     50  add_task(async function createControlledIFrame() {
     51    controlledFrame = await injectControlledFrame();
     52  });
     53 
     54  var idCounter = 1;
     55  function waitForDeliveryError(request) {
     56    return new Promise(resolve => {
     57      var data = new TextEncoder().encode(JSON.stringify(request));
     58      var principal = SpecialPowers.wrap(window).clientPrincipal;
     59 
     60      let messageId = "message-" + (idCounter++);
     61      reporters.set(messageId, resolve);
     62      pushNotifier.notifyPushWithData(registration.scope, principal, messageId,
     63                                      data);
     64    });
     65  }
     66 
     67  add_task(async function reportDeliveryErrors() {
     68    var reason = await waitForDeliveryError({ type: "exception" });
     69    is(reason, SpecialPowers.Ci.nsIPushErrorReporter.DELIVERY_UNCAUGHT_EXCEPTION,
     70      "Should report uncaught exceptions");
     71 
     72    reason = await waitForDeliveryError({ type: "rejection" });
     73    is(reason, SpecialPowers.Ci.nsIPushErrorReporter.DELIVERY_UNHANDLED_REJECTION,
     74      "Should report unhandled rejections");
     75  });
     76 
     77  add_task(async function reportDecryptionError() {
     78    var message = await new Promise(resolve => {
     79      SpecialPowers.registerConsoleListener(msg => {
     80        if (!msg.isScriptError && !msg.isConsoleEvent) {
     81          return;
     82        }
     83        const scope = "http://mochi.test:8888/tests/dom/push/test/";
     84        if (msg.innerWindowID === "ServiceWorker" &&
     85            msg.windowID === scope) {
     86          SpecialPowers.postConsoleSentinel();
     87          resolve(msg);
     88        }
     89      });
     90 
     91      var principal = SpecialPowers.wrap(window).clientPrincipal;
     92      pushNotifier.notifyError(registration.scope, principal, "Push error",
     93        SpecialPowers.Ci.nsIScriptError.errorFlag);
     94    });
     95 
     96    is(message.sourceName, registration.scope,
     97      "Should use the qualified scope URL as the source");
     98    is(message.errorMessage, "Push error",
     99      "Should report the given error string");
    100  });
    101 
    102  add_task(async function unsubscribe() {
    103    controlledFrame.remove();
    104  });
    105 
    106  add_task(async function unregister() {
    107    await registration.unregister();
    108  });
    109 
    110 </script>
    111 </body>
    112 </html>