|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <meta charset="utf-8"> |
|
5 <title>Test date Paramter for Alarm API</title> |
|
6 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> |
|
7 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> |
|
8 </head> |
|
9 <body> |
|
10 <p id="display"></p> |
|
11 <div id="content" style="display: none"></div> |
|
12 <pre id="test"> |
|
13 <script type="application/javascript"> |
|
14 |
|
15 "use strict"; |
|
16 |
|
17 // Verify passing a Date in the future doesn't fail |
|
18 function testFutureDate() { |
|
19 var tomorrow = new Date(); |
|
20 tomorrow.setDate(tomorrow.getDate() + 1); |
|
21 |
|
22 var domRequest; |
|
23 try { |
|
24 domRequest = navigator.mozAlarms.add(tomorrow, "honorTimezone", {}); |
|
25 } catch (e) { |
|
26 ok(false, |
|
27 "Unexpected exception trying to add alarm for tomorrow."); |
|
28 |
|
29 // Proceed to next test. |
|
30 return testPastDate(); |
|
31 } |
|
32 domRequest.onsuccess = function(e) { |
|
33 navigator.mozAlarms.remove(e.target.result); |
|
34 ok(true, "Add alarm for future date."); |
|
35 |
|
36 // Awesome, no error so proceed to next test. |
|
37 testPastDate(); |
|
38 }; |
|
39 domRequest.onerror = function(e) { |
|
40 ok(false, "Unable to add alarm for tomorrow`."); |
|
41 |
|
42 // Proceed to next test. |
|
43 testPastDate(); |
|
44 }; |
|
45 } |
|
46 |
|
47 // Verify passing a Date that's already past doesn't fail (it should fire immediately). |
|
48 function testPastDate() { |
|
49 var yesterday = new Date(); |
|
50 yesterday.setDate(yesterday.getDate() - 1); |
|
51 |
|
52 var domRequest; |
|
53 try { |
|
54 domRequest = navigator.mozAlarms.add(yesterday, "honorTimezone", {}); |
|
55 } catch (e) { |
|
56 ok(false, |
|
57 "Unexpected exception trying to add alarm for yesterday."); |
|
58 |
|
59 // Move on to the next test. |
|
60 testNullDate(); |
|
61 } |
|
62 domRequest.onsuccess = function(e) { |
|
63 navigator.mozAlarms.remove(e.target.result); |
|
64 |
|
65 ok(true, "Should be able to add alarm for already past date, which should fire immediately."); |
|
66 |
|
67 // Move on to the next test. |
|
68 testNullDate(); |
|
69 }; |
|
70 domRequest.onerror = function(e) { |
|
71 ok(false, "Unable to add alarm for yesterday."); |
|
72 |
|
73 // Move on to the next test. |
|
74 testNullDate(); |
|
75 } |
|
76 } |
|
77 |
|
78 // Verify passing null does indeed fail |
|
79 function testNullDate() { |
|
80 try { |
|
81 navigator.mozAlarms.add(null, "honorTimezone", {}); |
|
82 ok(false, "Expected an exception to be thrown for alarm with null date."); |
|
83 } catch(e) { |
|
84 ok(true, "Exception thrown for alarm with null date."); |
|
85 } |
|
86 |
|
87 // Move on to the next test. |
|
88 testInvalidTimeZone() |
|
89 } |
|
90 |
|
91 function testInvalidTimeZone() { |
|
92 try { |
|
93 navigator.mozAlarms.add(new Date(), "badTimeZoneArg", {}); |
|
94 ok(false, "Expected an exception to be thrown while testing bad time zone arg."); |
|
95 } catch(e) { |
|
96 ok(true, "Exception thrown while testing bad time zone arg."); |
|
97 } |
|
98 SimpleTest.finish(); |
|
99 } |
|
100 |
|
101 function startTests() { |
|
102 |
|
103 SpecialPowers.pushPrefEnv({"set": [["dom.mozAlarms.enabled", true]]}, function() { |
|
104 // Currently applicable only on FxOS |
|
105 if (navigator.userAgent.indexOf("Mobile") != -1 && |
|
106 navigator.appVersion.indexOf("Android") == -1) |
|
107 { |
|
108 testFutureDate(); |
|
109 } else { |
|
110 ok(true, "mozAlarms on Firefox OS only."); |
|
111 SimpleTest.finish(); |
|
112 } |
|
113 }); |
|
114 } |
|
115 |
|
116 SimpleTest.expectAssertions(0, 9); |
|
117 SimpleTest.waitForExplicitFinish(); |
|
118 if (SpecialPowers.hasPermission("alarms", document)) { |
|
119 startTests(); |
|
120 } else { |
|
121 // Add the permissions and reload so they propogate |
|
122 SpecialPowers.addPermission("alarms", true, document); |
|
123 window.location.reload(); |
|
124 } |
|
125 |
|
126 </script> |
|
127 </pre> |
|
128 </body> |
|
129 </html> |