summaryrefslogtreecommitdiffstats
path: root/dom/notification/test/chrome/test_notification_system_principal.xhtml
blob: 27891caeede0deb3f2f354341776eb22393020c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=874090
-->
<window title="Mozilla Bug 874090" onload="runTests()"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>
  <script src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=874090"
     target="_blank">Mozilla Bug 874090</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[
  /** Test for Bug 874090 **/
  const MOCK_CID = Components.ID("{2a0f83c4-8818-4914-a184-f1172b4eaaa7}");
  const ALERTS_SERVICE_CONTRACT_ID = "@mozilla.org/alerts-service;1";

  var mockAlertsService = {
    showAlert: function(alert, alertListener) {
      ok(true, "System principal was granted permission and is able to call showAlert.");
      unregisterMock();
      SimpleTest.finish();
    },

    showAlertNotification: function(imageUrl, title, text, textClickable,
                                    cookie, alertListener, name, dir, lang, data) {
      this.showAlert();
    },

    QueryInterface: function(aIID) {
      if (aIID.equals(Ci.nsISupports) ||
          aIID.equals(Ci.nsIAlertsService)) {
        return this;
      }
      throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
    },

    createInstance: function(aIID) {
      return this.QueryInterface(aIID);
    }
  };

  function registerMock() {
    Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
      registerFactory(MOCK_CID, "alerts service", ALERTS_SERVICE_CONTRACT_ID, mockAlertsService);
  }

  function unregisterMock() {
    Components.manager.QueryInterface(Ci.nsIComponentRegistrar).
      unregisterFactory(MOCK_CID, mockAlertsService);
  }

  function runTests() {
    registerMock();

    is(Notification.permission, "granted", "System principal should be automatically granted permission.");

    Notification.requestPermission(function(permission) {
      is(permission, "granted", "System principal should be granted permission when calling requestPermission.");

      if (permission == "granted") {
        // Create a notification and make sure that it is able to call into
        // the mock alert service to show the notification.
        new Notification("Hello");
      } else {
        unregisterMock();
        SimpleTest.finish();
      }
    });
  }

  SimpleTest.waitForExplicitFinish();
  ]]>
  </script>
</window>