tor-browser

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

notification.https.html (3796B)


      1 <!DOCTYPE html>
      2 <title>Test Notification</title>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <script src="/resources/testdriver.js"></script>
      6 <script src="/resources/testdriver-vendor.js"></script>
      7 <script src="/common/utils.js"></script>
      8 <script src="/common/dispatcher/dispatcher.js"></script>
      9 <script src="resources/utils.js"></script>
     10 
     11 <body>
     12 <script>
     13 promise_test(async () => {
     14  // Notification permission must be allowed or we cannot confirm that
     15  // "new Notification" failed in a fenced frame.
     16  await test_driver.set_permission({name: 'notifications'}, 'granted', true);
     17  assert_equals(Notification.permission, 'granted');
     18 
     19  const frame = attachFencedFrameContext();
     20  try {
     21    await frame.execute(() => {
     22      const notification = new Notification('Test notification');
     23      return new Promise((resolve, reject) => {
     24        // "new Notification" inside the fenced frame should fail even if it is
     25        // allowed in the primary main frame.
     26        notification.addEventListener('error', resolve);
     27        notification.addEventListener('show', reject);
     28      });
     29    });
     30  } catch(e) {
     31    assert_unreached('Notification was shown; want not to be shown.' + e);
     32  }
     33 }, 'new Notification should fail inside a fenced frame');
     34 
     35 promise_test(async () => {
     36  // Notification permission must be allowed or we cannot confirm that
     37  // "new Notification" failed in a fenced frame.
     38  await test_driver.set_permission({name: 'notifications'}, 'granted', true);
     39  assert_equals(Notification.permission, 'granted');
     40 
     41  const frame = attachFencedFrameContext();
     42  const message = await frame.execute(async () => {
     43    const getController = () => {
     44      if (navigator.serviceWorker.controller) {
     45        return navigator.serviceWorker.controller;
     46      }
     47      return new Promise(resolve => {
     48        navigator.serviceWorker.addEventListener('controllerchange', () => {
     49          resolve(navigator.serviceWorker.controller);
     50        });
     51      });
     52    };
     53 
     54    await navigator.serviceWorker.register(
     55      'notification-sw.js', { scope: location.href });
     56    const ctrl = await getController();
     57 
     58    return new Promise(resolve => {
     59      ctrl.postMessage('constructor');
     60      navigator.serviceWorker.onmessage = e => {
     61        resolve(e.data);
     62      };
     63    });
     64  });
     65  assert_equals(
     66    message, "Failed to construct 'Notification': Illegal constructor.");
     67 }, 'new Notification should fail from the service worker in a fenced frame');
     68 
     69 promise_test(async () => {
     70  // Notification permission must be allowed or we cannot confirm that
     71  // "new Notification" failed in a fenced frame.
     72  await test_driver.set_permission({name: 'notifications'}, 'granted', true);
     73  assert_equals(Notification.permission, 'granted');
     74 
     75  const frame = attachFencedFrameContext();
     76  const message = await frame.execute(async () => {
     77    const getController = () => {
     78      if (navigator.serviceWorker.controller) {
     79        return navigator.serviceWorker.controller;
     80      }
     81      return new Promise(resolve => {
     82        navigator.serviceWorker.addEventListener('controllerchange', () => {
     83          resolve(navigator.serviceWorker.controller);
     84        });
     85      });
     86    };
     87 
     88    await navigator.serviceWorker.register(
     89      'notification-sw.js', { scope: location.href });
     90    const ctrl = await getController();
     91 
     92    return new Promise(resolve => {
     93      ctrl.postMessage('showNotification');
     94      navigator.serviceWorker.onmessage = e => {
     95        resolve(e.data);
     96      };
     97    });
     98  });
     99  assert_equals(message,
    100    "Failed to execute 'showNotification' on 'ServiceWorkerRegistration': " + "showNotification() is not allowed in fenced frames.",);
    101 }, 'showNotification() should fail from the service worker in a fenced frame');
    102 </script>
    103 </body>