summaryrefslogtreecommitdiffstats
path: root/toolkit/components/alerts/test/test_alerts.html
blob: cb087e48acef5279306c2e72413c14ad9f02f588 (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
83
84
85
86
87
88
89
<!DOCTYPE HTML>
<!-- Any copyright is dedicated to the Public Domain.
     http://creativecommons.org/publicdomain/zero/1.0/ -->
<html>
<head>
  <title>Test for Alerts Service</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>

<body>
<p id="display"></p>

<br>Alerts service, with observer "synchronous" case.
<br>
<br>Did a notification appear anywhere?
<br>If so, the test will finish once the notification disappears.

<pre id="test">
<script class="testbody" type="text/javascript">

var observer = {
  alertShow: false,
  observe: function (aSubject, aTopic, aData) {
    is(aData, "foobarcookie", "Checking whether the alert cookie was passed correctly");
    if (aTopic == "alertclickcallback") {
      todo(false, "Did someone click the notification while running mochitests? (Please don't.)");
    } else if (aTopic == "alertshow") {
      ok(!this.alertShow, "Alert should not be shown more than once");
      this.alertShow = true;
    } else {
      is(aTopic, "alertfinished", "Checking the topic for a finished notification");
      SimpleTest.finish();
    }
  }
};

function runTest() {
  const Cc = SpecialPowers.Cc;
  const Ci = SpecialPowers.Ci;

  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");

  var notifier;
  try {
    notifier = Cc["@mozilla.org/alerts-service;1"].
               getService(Ci.nsIAlertsService);
    ok(true, "Alerts service is available");
  } catch (ex) {
    todo(false,
         "Alerts service is not available.", ex);
    return;
  }

  try {
    var alertName = "fiorello";
    SimpleTest.waitForExplicitFinish();
    notifier.showAlertNotification(null, "Notification test",
                                   "Surprise! I'm here to test notifications!",
                                   false, "foobarcookie", observer, alertName);
    ok(true, "showAlertNotification() succeeded.  Waiting for notification...");

    if ("@mozilla.org/system-alerts-service;1" in Cc) {
      // Notifications are native on OS X 10.8 and later, as well as GNOME
      // Shell with libnotify (bug 1236036). These notifications persist in the
      // Notification Center, and only fire the `alertfinished` event when
      // closed. For platforms where native notifications may be used, we need
      // to close explicitly to avoid a hang. This also works for XUL
      // notifications when running this test on OS X < 10.8, or a window
      // manager like Ubuntu Unity with incomplete libnotify support.
      notifier.closeAlert(alertName);
    }
  } catch (ex) {
    todo(false, "showAlertNotification() failed.", ex);
    SimpleTest.finish();
  }
}

runTest();

</script>
</pre>
</body>
</html>