summaryrefslogtreecommitdiffstats
path: root/toolkit/components/alerts/test/test_multiple_alerts.html
blob: 9d939b63a0059a8f95eae18bb8ee4cdc34f152e4 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for multiple alerts</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<pre id="test">
<script class="testbody" type="text/javascript">

const Cc = SpecialPowers.Cc;
const Ci = SpecialPowers.Ci;

const chromeScript = SpecialPowers.loadChromeScript(_ => {
  Components.utils.import("resource://gre/modules/Services.jsm");
  Components.utils.import("resource://gre/modules/Timer.jsm");

  const alertService = Components.classes["@mozilla.org/alerts-service;1"]
                                 .getService(Components.interfaces.nsIAlertsService);

  addMessageListener("waitForPosition", function() {
    var timer = setTimeout(function() {
      Services.ww.unregisterNotification(windowObserver);
      sendAsyncMessage("waitedForPosition", null);
    }, 2000);

    var windowObserver = function(aSubject, aTopic, aData) {
      if (aTopic != "domwindowopened") {
        return;
      }

      // Alerts are implemented using XUL.
      clearTimeout(timer);

      Services.ww.unregisterNotification(windowObserver);

      var win = aSubject.QueryInterface(Components.interfaces.nsIDOMWindow);
      win.addEventListener("pageshow", function onPageShow() {
        win.removeEventListener("pageshow", onPageShow, false);

        var x = win.screenX;
        var y = win.screenY;

        win.addEventListener("pagehide", function onPageHide() {
          win.removeEventListener("pagehide", onPageHide, false);
          sendAsyncMessage("waitedForPosition", { x, y });
        }, false);

        alertService.closeAlert();
      }, false);
    };

    Services.ww.registerNotification(windowObserver);
  });
});

function promiseAlertPosition(alertService) {
  return new Promise(resolve => {
    chromeScript.addMessageListener("waitedForPosition", function waitedForPosition(result) {
      chromeScript.removeMessageListener("waitedForPosition", waitedForPosition);
      resolve(result);
    });
    chromeScript.sendAsyncMessage("waitForPosition");

    alertService.showAlertNotification(null, "title", "body");
    ok(true, "Alert shown.");
  });
}

add_task(function* test_multiple_alerts() {
  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 alertService;
  try {
    alertService = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService);
    ok(true, "Alerts service is available.");
  } catch (ex) {
    todo(false, "Alerts service is not available.");
    return;
  }

  var firstAlertPosition = yield promiseAlertPosition(alertService);
  if (!firstAlertPosition) {
    ok(true, "Platform does not use XUL alerts.");
    return;
  }

  var secondAlertPosition = yield promiseAlertPosition(alertService);
  is(secondAlertPosition.x, firstAlertPosition.x, "Second alert should be opened in the same position.");
  is(secondAlertPosition.y, firstAlertPosition.y, "Second alert should be opened in the same position.");
});

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