1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/alerts/test/test_multiple_alerts.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,128 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<head> 1.7 + <title>Test for multiple alerts</title> 1.8 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.9 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 1.10 +</head> 1.11 +<body> 1.12 +<pre id="test"> 1.13 +<script class="testbody" type="text/javascript"> 1.14 + 1.15 +const Cc = SpecialPowers.Cc; 1.16 +const Ci = SpecialPowers.Ci; 1.17 + 1.18 +const ww = Cc["@mozilla.org/embedcomp/window-watcher;1"] 1.19 + .getService(Ci.nsIWindowWatcher); 1.20 + 1.21 +function showSimpleAlert(alertService) { 1.22 + try { 1.23 + alertService.showAlertNotification(null, "title", "body"); 1.24 + ok(true, "Alert shown."); 1.25 + } catch (ex) { 1.26 + ok(false, "Failed to show alert.", ex); 1.27 + SimpleTest.finish(); 1.28 + } 1.29 +} 1.30 + 1.31 +var firstAlertXPosition = 0; 1.32 +var firstAlertYPosition = 0; 1.33 + 1.34 +function showFirstAlert(alertService) { 1.35 + // Set a timeout to finish the test in case we are on a platform 1.36 + // that doesn't use XUL alert windows. This timeout is cleared 1.37 + // when a XUL alert window does get created. 1.38 + var timer = setTimeout(function() { 1.39 + ok(true, "Platform does not use XUL alerts."); 1.40 + ww.unregisterNotification(windowObserver); 1.41 + SimpleTest.finish(); 1.42 + }, 2000); 1.43 + 1.44 + var windowObserver = function(aSubject, aTopic, aData) { 1.45 + if (aTopic != "domwindowopened") { 1.46 + return; 1.47 + } 1.48 + 1.49 + // Alerts are implemented using XUL, clear the timeout 1.50 + // that finishes the test. 1.51 + clearTimeout(timer); 1.52 + 1.53 + ww.unregisterNotification(windowObserver); 1.54 + 1.55 + var win = SpecialPowers.wrap(aSubject); 1.56 + win.addEventListener("pageshow", function() { 1.57 + win.removeEventListener("pageshow", arguments.callee, false); 1.58 + firstAlertXPosition = win.screenX; 1.59 + firstAlertYPosition = win.screenY; 1.60 + 1.61 + // Wait for alert window to close then open a second alert. 1.62 + win.addEventListener("pagehide", function() { 1.63 + win.removeEventListener("pagehide", arguments.callee, false); 1.64 + // Wait for alert to close before creating a new one. 1.65 + showSecondAlert(alertService); 1.66 + }, false); 1.67 + 1.68 + alertService.closeAlert(); 1.69 + }, false); 1.70 + }; 1.71 + 1.72 + ww.registerNotification(windowObserver); 1.73 + showSimpleAlert(alertService); 1.74 +} 1.75 + 1.76 +function showSecondAlert(alertService) { 1.77 + var windowObserver = function(aSubject, aTopic, aData) { 1.78 + if (aTopic != "domwindowopened") { 1.79 + return; 1.80 + } 1.81 + 1.82 + ww.unregisterNotification(windowObserver); 1.83 + 1.84 + var win = SpecialPowers.wrap(aSubject); 1.85 + win.addEventListener("pageshow", function() { 1.86 + win.removeEventListener("pageshow", arguments.callee, false); 1.87 + is(win.screenX, firstAlertXPosition, "Second alert should be opened in the same position."); 1.88 + is(win.screenY, firstAlertYPosition, "Second alert should be opened in the same position."); 1.89 + 1.90 + // Wait for alert window to close then finish the test. 1.91 + win.addEventListener("pagehide", function() { 1.92 + win.removeEventListener("pagehide", arguments.callee, false); 1.93 + // Wait for alert to close before finishing the test. 1.94 + SimpleTest.finish(); 1.95 + }, false); 1.96 + 1.97 + alertService.closeAlert(); 1.98 + }, false); 1.99 + }; 1.100 + 1.101 + ww.registerNotification(windowObserver); 1.102 + showSimpleAlert(alertService); 1.103 +} 1.104 + 1.105 +function runTest() { 1.106 + if (!("@mozilla.org/alerts-service;1" in Cc)) { 1.107 + todo(false, "Alerts service does not exist in this application."); 1.108 + return; 1.109 + } 1.110 + 1.111 + ok(true, "Alerts service exists in this application."); 1.112 + 1.113 + var alertService; 1.114 + try { 1.115 + alertService = Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService); 1.116 + ok(true, "Alerts service is available."); 1.117 + } catch (ex) { 1.118 + todo(false, "Alerts service is not available."); 1.119 + return; 1.120 + } 1.121 + 1.122 + SimpleTest.waitForExplicitFinish(); 1.123 + showFirstAlert(alertService); 1.124 +} 1.125 + 1.126 +runTest(); 1.127 + 1.128 +</script> 1.129 +</pre> 1.130 +</body> 1.131 +</html>