tor-browser

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

test_notification_basics.html (4868B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <head>
      4  <title>Notification Basics</title>
      5  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      6  <script src="NotificationTest.js"></script>
      7  <script src="/tests/SimpleTest/GleanTest.js"></script>
      8  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
      9 </head>
     10 <body>
     11 <p id="display"></p>
     12 <div id="content" style="display: none"></div>
     13 <pre id="test"></pre>
     14 <script>
     15 
     16  var info = NotificationTest.info;
     17  var options;
     18 
     19  SimpleTest.requestFlakyTimeout("untriaged");
     20 
     21  var steps = [
     22    async function() {
     23      info("Test notification spec");
     24      ok(Notification, "Notification constructor exists");
     25      ok(Notification.permission, "Notification.permission exists");
     26      ok(Notification.requestPermission, "Notification.requestPermission exists");
     27    },
     28 
     29    function() {
     30      info("Test requestPermission without callback");
     31      Notification.requestPermission();
     32    },
     33 
     34    async function() {
     35      info("Test Glean telemetry");
     36      await GleanTest.testResetFOG();
     37 
     38      await Notification.requestPermission();
     39      const requestCount = await GleanTest.webNotification.requestPermissionOrigin.first_party.testGetValue();
     40      is(requestCount, 1, "Notification first party request permission counter should increment once.");
     41 
     42      Notification.permission;
     43      const permissionCount = await GleanTest.webNotification.permissionOrigin.first_party.testGetValue();
     44      is(permissionCount, 1, "Notification first party request permission counter should increment once.");
     45 
     46      await new Promise(r => new Notification("first party").onerror = r);
     47      const showCount = await GleanTest.webNotification.showOrigin.first_party.testGetValue();
     48      is(showCount, 1, "Notification first party show attempt counter should increment once.");
     49    },
     50 
     51    async function(done) {
     52      info("Test requestPermission deny");
     53      function assertPermissionDenied(perm) {
     54        is(perm, "denied", "Permission should be denied.");
     55        is(Notification.permission, "denied", "Permission should be denied.");
     56      }
     57      await NotificationTest.denyNotifications();
     58      Notification.requestPermission()
     59        .then(assertPermissionDenied)
     60        .then(_ => Notification.requestPermission(assertPermissionDenied))
     61        .catch(err => {
     62          ok(!err, "requestPermission should not reject promise");
     63        })
     64        .then(done);
     65    },
     66 
     67    async function(done) {
     68      info("Test requestPermission grant");
     69      function assertPermissionGranted(perm) {
     70        is(perm, "granted", "Permission should be granted.");
     71        is(Notification.permission, "granted", "Permission should be granted");
     72      }
     73      await NotificationTest.allowNotifications();
     74      Notification.requestPermission()
     75        .then(assertPermissionGranted)
     76        .then(_ => Notification.requestPermission(assertPermissionGranted))
     77        .catch(err => {
     78          ok(!err, "requestPermission should not reject promise");
     79        })
     80        .then(done);
     81    },
     82 
     83    function(done) {
     84      info("Test invalid requestPermission");
     85      Notification.requestPermission({})
     86        .then(_ => {
     87          ok(false, "Non callable arg to requestPermission should reject promise");
     88        }, () => {
     89          ok(true, "Non callable arg to requestPermission should reject promise");
     90        })
     91        .then(done);
     92    },
     93 
     94    function(done) {
     95      info("Test create notification");
     96 
     97      options = NotificationTest.payload;
     98 
     99      var notification = new Notification("This is a title", options);
    100 
    101      ok(notification, "Notification exists");
    102      is(notification.onclick, null, "onclick() should be null");
    103      is(notification.onshow, null, "onshow() should be null");
    104      is(notification.onerror, null, "onerror() should be null");
    105      is(notification.onclose, null, "onclose() should be null");
    106      is(typeof notification.close, "function", "close() should exist");
    107 
    108      is(notification.dir, options.dir, "auto should get set");
    109      is(notification.lang, options.lang, "lang should get set");
    110      is(notification.body, options.body, "body should get set");
    111      is(notification.tag, options.tag, "tag should get set");
    112      is(
    113        notification.icon,
    114        new URL(options.icon, location.href).toString(),
    115        "icon should get set"
    116      );
    117 
    118      // store notification in test context
    119      this.notification = notification;
    120 
    121      notification.onshow = function() {
    122        ok(true, "onshow handler should be called");
    123        done();
    124      };
    125    },
    126 
    127    function(done) {
    128      info("Test closing a notification");
    129      var notification = this.notification;
    130 
    131      notification.onclose = function() {
    132        ok(true, "onclose handler should be called");
    133        done();
    134      };
    135 
    136      notification.close();
    137    },
    138  ];
    139 
    140  NotificationTest.run(steps);
    141 </script>
    142 </body>
    143 </html>