michael@0: XPCOMUtils.defineLazyModuleGetter(this, "AlarmService", michael@0: "resource://gre/modules/AlarmService.jsm"); michael@0: michael@0: /* michael@0: * Tests for Bug 867868 and related Alarm API bugs. michael@0: * michael@0: * NOTE: These tests pass the alarm time to AlarmService as a number and not as michael@0: * a date. See bug 810973 about Date truncating milliseconds. AlarmService does michael@0: * not officially allow a integer, but nor does it disallow it. Of course this michael@0: * test will break if AlarmService adds a type check, hence this note. michael@0: * FIXME: when bug 810973 is fixed. michael@0: */ michael@0: michael@0: function add_alarm_future(cb) { michael@0: let alarmId = undefined; michael@0: AlarmService.add({ michael@0: date: new Date(Date.now() + 143), michael@0: ignoreTimezone: true michael@0: }, michael@0: function onAlarmFired(aAlarm) { michael@0: ok(alarmId === aAlarm.id, "Future alarm fired successfully."); michael@0: cb(); michael@0: }, michael@0: function onSuccess(aAlarmId) { michael@0: alarmId = aAlarmId; michael@0: }, michael@0: function onError(error) { michael@0: ok(false, "Unexpected error adding future alarm " + error); michael@0: cb(); michael@0: }); michael@0: } michael@0: michael@0: function add_alarm_present(cb) { michael@0: let self = this; michael@0: let alarmId = undefined; michael@0: AlarmService.add({ michael@0: date: new Date(), michael@0: ignoreTimezone: true michael@0: }, michael@0: function onAlarmFired(aAlarm) { michael@0: ok(alarmId === aAlarm.id, "Present alarm fired successfully."); michael@0: cb(); michael@0: }, michael@0: function onSuccess(aAlarmId) { michael@0: alarmId = aAlarmId; michael@0: }, function onError(error) { michael@0: ok(false, "Unexpected error adding alarm for current time " + error); michael@0: cb(); michael@0: }); michael@0: } michael@0: michael@0: function add_alarm_past(cb) { michael@0: let self = this; michael@0: let alarmId = undefined; michael@0: AlarmService.add({ michael@0: date: new Date(Date.now() - 5), michael@0: ignoreTimezone: true michael@0: }, michael@0: function onAlarmFired(aAlarm) { michael@0: ok(alarmId === aAlarm.id, "Past alarm fired successfully."); michael@0: cb(); michael@0: }, michael@0: function onSuccess(aAlarmId) { michael@0: alarmId = aAlarmId; michael@0: }, michael@0: function onError(error) { michael@0: ok(false, "Unexpected error adding alarm for time in the past " + error); michael@0: cb(); michael@0: }); michael@0: } michael@0: michael@0: function trigger_all_alarms(cb) { michael@0: let n = 10; michael@0: let counter = 0; michael@0: let date = new Date(Date.now() + 57); michael@0: function onAlarmFired() { michael@0: counter++; michael@0: info("trigger_all_alarms count " + counter); michael@0: if (counter == n) { michael@0: ok(true, "All " + n + " alarms set to a particular time fired."); michael@0: cb(); michael@0: } michael@0: } michael@0: michael@0: for (let i = 0; i < n; i++) { michael@0: AlarmService.add( michael@0: { michael@0: date: date, michael@0: ignoreTimezone: true michael@0: }, michael@0: onAlarmFired michael@0: ); michael@0: } michael@0: } michael@0: michael@0: function multiple_handlers(cb) { michael@0: let d = new Date(Date.now() + 100); michael@0: let called = 0; michael@0: michael@0: function done() { michael@0: if (called == 2) { michael@0: ok(true, "Two alarms for the same time fired."); michael@0: cb(); michael@0: } michael@0: } michael@0: michael@0: function handler1() { michael@0: called++; michael@0: done(); michael@0: } michael@0: michael@0: function handler2() { michael@0: called++; michael@0: done(); michael@0: } michael@0: michael@0: AlarmService.add( michael@0: { michael@0: date: d, michael@0: ignoreTimezone: true michael@0: }, michael@0: handler1 michael@0: ); michael@0: AlarmService.add( michael@0: { michael@0: date: d, michael@0: ignoreTimezone: true michael@0: }, michael@0: handler2 michael@0: ); michael@0: } michael@0: michael@0: function same_time_alarms(cb) { michael@0: var fired = 0; michael@0: var delay = new Date(Date.now() + 100); michael@0: michael@0: function check() { michael@0: fired++; michael@0: if (fired == 4) { michael@0: ok(true, "All alarms set for the same time fired."); michael@0: cb(); michael@0: } michael@0: } michael@0: michael@0: function addImmediateAlarm() { michael@0: fired++; michael@0: AlarmService.add({ michael@0: date: delay, michael@0: ignoreTimezone: true michael@0: }, check); michael@0: } michael@0: michael@0: AlarmService.add({ michael@0: date: delay, michael@0: ignoreTimezone: true michael@0: }, addImmediateAlarm); michael@0: michael@0: AlarmService.add({ michael@0: date: delay, michael@0: ignoreTimezone: true michael@0: }, addImmediateAlarm); michael@0: } michael@0: michael@0: function test() { michael@0: var tests = [ michael@0: add_alarm_future, michael@0: add_alarm_present, michael@0: add_alarm_past, michael@0: trigger_all_alarms, michael@0: multiple_handlers, michael@0: same_time_alarms michael@0: ] michael@0: michael@0: var testIndex = -1; michael@0: function nextTest() { michael@0: testIndex++; michael@0: if (testIndex >= tests.length) michael@0: return; michael@0: michael@0: waitForExplicitFinish(); michael@0: tests[testIndex](function() { michael@0: finish(); michael@0: nextTest(); michael@0: }); michael@0: } michael@0: nextTest(); michael@0: }