File: test_principal.html

package info (click to toggle)
firefox-esr 128.13.0esr-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,230,012 kB
  • sloc: cpp: 7,103,971; javascript: 6,088,450; ansic: 3,653,980; python: 1,212,330; xml: 594,604; asm: 420,652; java: 182,969; sh: 71,124; makefile: 20,747; perl: 13,449; objc: 12,399; yacc: 4,583; cs: 3,846; pascal: 2,973; lex: 1,720; ruby: 1,194; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (129 lines) | stat: -rw-r--r-- 4,250 bytes parent folder | download | duplicates (10)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for Bug 1202933</title>
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>

<body>
<iframe id="iframe" src="http://mochi.test:8888/chrome/toolkit/components/alerts/test/chrome/empty.html"></iframe>
<p id="display"></p>

<pre id="test">
<script class="testbody" type="text/javascript">
const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;
const Services = SpecialPowers.Services;

const notifier = Cc["@mozilla.org/alerts-service;1"]
                   .getService(Ci.nsIAlertsService);

const chromeScript = SpecialPowers.loadChromeScript(_ => {
  /* eslint-env mozilla/chrome-script */
  addMessageListener("anyXULAlertsVisible", function() {
    var windows = Services.wm.getEnumerator("alert:alert");
    return windows.hasMoreElements();
  });

  addMessageListener("getAlertSource", function() {
    var alertWindows = Services.wm.getEnumerator("alert:alert");
    if (!alertWindows) {
      return null;
    }
    var alertWindow = alertWindows.getNext();
    return alertWindow.document.getElementById("alertSourceLabel").getAttribute("value");
  });
});

function notify(alertName, principal) {
  return new Promise((resolve, reject) => {
    var source;
    async function observe(subject, topic) {
      if (topic == "alertclickcallback") {
        reject(new Error("Alerts should not be clicked during test"));
      } else if (topic == "alertshow") {
        source = await chromeScript.sendQuery("getAlertSource");
        notifier.closeAlert(alertName);
      } else {
        is(topic, "alertfinished", "Should hide alert");
        resolve(source);
      }
    }
    notifier.showAlertNotification(null, "Notification test",
                                   "Surprise! I'm here to test notifications!",
                                   false, alertName, observe, alertName,
                                   null, null, null, principal);
    if (SpecialPowers.Services.appinfo.OS == "Darwin") {
      notifier.closeAlert(alertName);
    }
  });
}

async function testNoPrincipal() {
  var source = await notify("noPrincipal", null);
  ok(!source, "Should omit source without principal");
}

async function testSystemPrincipal() {
  var principal = Services.scriptSecurityManager.getSystemPrincipal();
  var source = await notify("systemPrincipal", principal);
  ok(!source, "Should omit source for system principal");
}

async function testNullPrincipal() {
  var principal = Services.scriptSecurityManager.createNullPrincipal({});
  var source = await notify("nullPrincipal", principal);
  ok(!source, "Should omit source for null principal");
}

async function testNodePrincipal() {
  const { iframe } = document.all;
  if (iframe.contentDocument.readyState !== "complete") {
    await new Promise(r => iframe.onload = r);
  }

  var principal = iframe.contentDocument.nodePrincipal;
  var source = await notify("nodePrincipal", principal);

  var stringBundle = Services.strings.createBundle(
    "chrome://alerts/locale/alert.properties"
  );
  var localizedSource = stringBundle.formatStringFromName(
    "source.label", [principal.hostPort]);
  is(source, localizedSource, "Should include source for node principal");
}

function runTest() {
  if (!("@mozilla.org/alerts-service;1" in Cc)) {
    todo(false, "Alerts service does not exist in this application");
    return;
  }

  ok(true, "Alerts service exists in this application");

  add_setup(async () => {
    // This test verifies behavior specific to XUL alerts.
    await SpecialPowers.pushPrefEnv({
      set: [["alerts.useSystemBackend", false]],
    });
  });

  // sendSyncMessage returns an array of arrays. See the comments in
  // test_alerts_noobserve.html and test_SpecialPowersLoadChromeScript.html.
  add_task(async () => {
    var alertsVisible = await chromeScript.sendQuery("anyXULAlertsVisible");
    ok(!alertsVisible, "Alerts should not be present at the start of the test.");
  });

  add_task(testNoPrincipal);
  add_task(testSystemPrincipal);
  add_task(testNullPrincipal);
  add_task(testNodePrincipal);
}

runTest();
</script>
</pre>
</body>
</html>