|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <title>Test for multiple alerts</title> |
|
5 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
|
7 </head> |
|
8 <body> |
|
9 <pre id="test"> |
|
10 <script class="testbody" type="text/javascript"> |
|
11 |
|
12 const Cc = SpecialPowers.Cc; |
|
13 const Ci = SpecialPowers.Ci; |
|
14 |
|
15 const ww = Cc["@mozilla.org/embedcomp/window-watcher;1"] |
|
16 .getService(Ci.nsIWindowWatcher); |
|
17 |
|
18 function showSimpleAlert(alertService) { |
|
19 try { |
|
20 alertService.showAlertNotification(null, "title", "body"); |
|
21 ok(true, "Alert shown."); |
|
22 } catch (ex) { |
|
23 ok(false, "Failed to show alert.", ex); |
|
24 SimpleTest.finish(); |
|
25 } |
|
26 } |
|
27 |
|
28 var firstAlertXPosition = 0; |
|
29 var firstAlertYPosition = 0; |
|
30 |
|
31 function showFirstAlert(alertService) { |
|
32 // Set a timeout to finish the test in case we are on a platform |
|
33 // that doesn't use XUL alert windows. This timeout is cleared |
|
34 // when a XUL alert window does get created. |
|
35 var timer = setTimeout(function() { |
|
36 ok(true, "Platform does not use XUL alerts."); |
|
37 ww.unregisterNotification(windowObserver); |
|
38 SimpleTest.finish(); |
|
39 }, 2000); |
|
40 |
|
41 var windowObserver = function(aSubject, aTopic, aData) { |
|
42 if (aTopic != "domwindowopened") { |
|
43 return; |
|
44 } |
|
45 |
|
46 // Alerts are implemented using XUL, clear the timeout |
|
47 // that finishes the test. |
|
48 clearTimeout(timer); |
|
49 |
|
50 ww.unregisterNotification(windowObserver); |
|
51 |
|
52 var win = SpecialPowers.wrap(aSubject); |
|
53 win.addEventListener("pageshow", function() { |
|
54 win.removeEventListener("pageshow", arguments.callee, false); |
|
55 firstAlertXPosition = win.screenX; |
|
56 firstAlertYPosition = win.screenY; |
|
57 |
|
58 // Wait for alert window to close then open a second alert. |
|
59 win.addEventListener("pagehide", function() { |
|
60 win.removeEventListener("pagehide", arguments.callee, false); |
|
61 // Wait for alert to close before creating a new one. |
|
62 showSecondAlert(alertService); |
|
63 }, false); |
|
64 |
|
65 alertService.closeAlert(); |
|
66 }, false); |
|
67 }; |
|
68 |
|
69 ww.registerNotification(windowObserver); |
|
70 showSimpleAlert(alertService); |
|
71 } |
|
72 |
|
73 function showSecondAlert(alertService) { |
|
74 var windowObserver = function(aSubject, aTopic, aData) { |
|
75 if (aTopic != "domwindowopened") { |
|
76 return; |
|
77 } |
|
78 |
|
79 ww.unregisterNotification(windowObserver); |
|
80 |
|
81 var win = SpecialPowers.wrap(aSubject); |
|
82 win.addEventListener("pageshow", function() { |
|
83 win.removeEventListener("pageshow", arguments.callee, false); |
|
84 is(win.screenX, firstAlertXPosition, "Second alert should be opened in the same position."); |
|
85 is(win.screenY, firstAlertYPosition, "Second alert should be opened in the same position."); |
|
86 |
|
87 // Wait for alert window to close then finish the test. |
|
88 win.addEventListener("pagehide", function() { |
|
89 win.removeEventListener("pagehide", arguments.callee, false); |
|
90 // Wait for alert to close before finishing the test. |
|
91 SimpleTest.finish(); |
|
92 }, false); |
|
93 |
|
94 alertService.closeAlert(); |
|
95 }, false); |
|
96 }; |
|
97 |
|
98 ww.registerNotification(windowObserver); |
|
99 showSimpleAlert(alertService); |
|
100 } |
|
101 |
|
102 function runTest() { |
|
103 if (!("@mozilla.org/alerts-service;1" in Cc)) { |
|
104 todo(false, "Alerts service does not exist in this application."); |
|
105 return; |
|
106 } |
|
107 |
|
108 ok(true, "Alerts service exists in this application."); |
|
109 |
|
110 var alertService; |
|
111 try { |
|
112 alertService = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService); |
|
113 ok(true, "Alerts service is available."); |
|
114 } catch (ex) { |
|
115 todo(false, "Alerts service is not available."); |
|
116 return; |
|
117 } |
|
118 |
|
119 SimpleTest.waitForExplicitFinish(); |
|
120 showFirstAlert(alertService); |
|
121 } |
|
122 |
|
123 runTest(); |
|
124 |
|
125 </script> |
|
126 </pre> |
|
127 </body> |
|
128 </html> |