hal/tests/browser_alarms.js

Fri, 16 Jan 2015 18:13:44 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 18:13:44 +0100
branch
TOR_BUG_9701
changeset 14
925c144e1f1f
permissions
-rw-r--r--

Integrate suggestion from review to improve consistency with existing code.

michael@0 1 XPCOMUtils.defineLazyModuleGetter(this, "AlarmService",
michael@0 2 "resource://gre/modules/AlarmService.jsm");
michael@0 3
michael@0 4 /*
michael@0 5 * Tests for Bug 867868 and related Alarm API bugs.
michael@0 6 *
michael@0 7 * NOTE: These tests pass the alarm time to AlarmService as a number and not as
michael@0 8 * a date. See bug 810973 about Date truncating milliseconds. AlarmService does
michael@0 9 * not officially allow a integer, but nor does it disallow it. Of course this
michael@0 10 * test will break if AlarmService adds a type check, hence this note.
michael@0 11 * FIXME: when bug 810973 is fixed.
michael@0 12 */
michael@0 13
michael@0 14 function add_alarm_future(cb) {
michael@0 15 let alarmId = undefined;
michael@0 16 AlarmService.add({
michael@0 17 date: new Date(Date.now() + 143),
michael@0 18 ignoreTimezone: true
michael@0 19 },
michael@0 20 function onAlarmFired(aAlarm) {
michael@0 21 ok(alarmId === aAlarm.id, "Future alarm fired successfully.");
michael@0 22 cb();
michael@0 23 },
michael@0 24 function onSuccess(aAlarmId) {
michael@0 25 alarmId = aAlarmId;
michael@0 26 },
michael@0 27 function onError(error) {
michael@0 28 ok(false, "Unexpected error adding future alarm " + error);
michael@0 29 cb();
michael@0 30 });
michael@0 31 }
michael@0 32
michael@0 33 function add_alarm_present(cb) {
michael@0 34 let self = this;
michael@0 35 let alarmId = undefined;
michael@0 36 AlarmService.add({
michael@0 37 date: new Date(),
michael@0 38 ignoreTimezone: true
michael@0 39 },
michael@0 40 function onAlarmFired(aAlarm) {
michael@0 41 ok(alarmId === aAlarm.id, "Present alarm fired successfully.");
michael@0 42 cb();
michael@0 43 },
michael@0 44 function onSuccess(aAlarmId) {
michael@0 45 alarmId = aAlarmId;
michael@0 46 }, function onError(error) {
michael@0 47 ok(false, "Unexpected error adding alarm for current time " + error);
michael@0 48 cb();
michael@0 49 });
michael@0 50 }
michael@0 51
michael@0 52 function add_alarm_past(cb) {
michael@0 53 let self = this;
michael@0 54 let alarmId = undefined;
michael@0 55 AlarmService.add({
michael@0 56 date: new Date(Date.now() - 5),
michael@0 57 ignoreTimezone: true
michael@0 58 },
michael@0 59 function onAlarmFired(aAlarm) {
michael@0 60 ok(alarmId === aAlarm.id, "Past alarm fired successfully.");
michael@0 61 cb();
michael@0 62 },
michael@0 63 function onSuccess(aAlarmId) {
michael@0 64 alarmId = aAlarmId;
michael@0 65 },
michael@0 66 function onError(error) {
michael@0 67 ok(false, "Unexpected error adding alarm for time in the past " + error);
michael@0 68 cb();
michael@0 69 });
michael@0 70 }
michael@0 71
michael@0 72 function trigger_all_alarms(cb) {
michael@0 73 let n = 10;
michael@0 74 let counter = 0;
michael@0 75 let date = new Date(Date.now() + 57);
michael@0 76 function onAlarmFired() {
michael@0 77 counter++;
michael@0 78 info("trigger_all_alarms count " + counter);
michael@0 79 if (counter == n) {
michael@0 80 ok(true, "All " + n + " alarms set to a particular time fired.");
michael@0 81 cb();
michael@0 82 }
michael@0 83 }
michael@0 84
michael@0 85 for (let i = 0; i < n; i++) {
michael@0 86 AlarmService.add(
michael@0 87 {
michael@0 88 date: date,
michael@0 89 ignoreTimezone: true
michael@0 90 },
michael@0 91 onAlarmFired
michael@0 92 );
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 function multiple_handlers(cb) {
michael@0 97 let d = new Date(Date.now() + 100);
michael@0 98 let called = 0;
michael@0 99
michael@0 100 function done() {
michael@0 101 if (called == 2) {
michael@0 102 ok(true, "Two alarms for the same time fired.");
michael@0 103 cb();
michael@0 104 }
michael@0 105 }
michael@0 106
michael@0 107 function handler1() {
michael@0 108 called++;
michael@0 109 done();
michael@0 110 }
michael@0 111
michael@0 112 function handler2() {
michael@0 113 called++;
michael@0 114 done();
michael@0 115 }
michael@0 116
michael@0 117 AlarmService.add(
michael@0 118 {
michael@0 119 date: d,
michael@0 120 ignoreTimezone: true
michael@0 121 },
michael@0 122 handler1
michael@0 123 );
michael@0 124 AlarmService.add(
michael@0 125 {
michael@0 126 date: d,
michael@0 127 ignoreTimezone: true
michael@0 128 },
michael@0 129 handler2
michael@0 130 );
michael@0 131 }
michael@0 132
michael@0 133 function same_time_alarms(cb) {
michael@0 134 var fired = 0;
michael@0 135 var delay = new Date(Date.now() + 100);
michael@0 136
michael@0 137 function check() {
michael@0 138 fired++;
michael@0 139 if (fired == 4) {
michael@0 140 ok(true, "All alarms set for the same time fired.");
michael@0 141 cb();
michael@0 142 }
michael@0 143 }
michael@0 144
michael@0 145 function addImmediateAlarm() {
michael@0 146 fired++;
michael@0 147 AlarmService.add({
michael@0 148 date: delay,
michael@0 149 ignoreTimezone: true
michael@0 150 }, check);
michael@0 151 }
michael@0 152
michael@0 153 AlarmService.add({
michael@0 154 date: delay,
michael@0 155 ignoreTimezone: true
michael@0 156 }, addImmediateAlarm);
michael@0 157
michael@0 158 AlarmService.add({
michael@0 159 date: delay,
michael@0 160 ignoreTimezone: true
michael@0 161 }, addImmediateAlarm);
michael@0 162 }
michael@0 163
michael@0 164 function test() {
michael@0 165 var tests = [
michael@0 166 add_alarm_future,
michael@0 167 add_alarm_present,
michael@0 168 add_alarm_past,
michael@0 169 trigger_all_alarms,
michael@0 170 multiple_handlers,
michael@0 171 same_time_alarms
michael@0 172 ]
michael@0 173
michael@0 174 var testIndex = -1;
michael@0 175 function nextTest() {
michael@0 176 testIndex++;
michael@0 177 if (testIndex >= tests.length)
michael@0 178 return;
michael@0 179
michael@0 180 waitForExplicitFinish();
michael@0 181 tests[testIndex](function() {
michael@0 182 finish();
michael@0 183 nextTest();
michael@0 184 });
michael@0 185 }
michael@0 186 nextTest();
michael@0 187 }

mercurial