Bug 867868 - Alarms tests. r=jlebar

This commit is contained in:
Nikhil Marathe 2013-06-04 13:30:06 -07:00
parent 446e740f2f
commit 1c58ac8302
5 changed files with 215 additions and 0 deletions

View File

@ -17,6 +17,8 @@ VPATH = \
$(srcdir)/cocoa \
$(NULL)
relativesrcdir = @relativesrcdir@
include $(DEPTH)/config/autoconf.mk
LIBRARY_NAME = hal_s
@ -25,6 +27,10 @@ LIBXUL_LIBRARY = 1
EXPORT_LIBRARY = 1
FAIL_ON_WARNINGS = 1
MOCHITEST_BROWSER_FILES += \
tests/browser_alarms.js \
$(NULL)
ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
CMMSRCS += \
CocoaSensor.mm \

View File

@ -19,6 +19,7 @@ EXPORTS.mozilla += [
'HalWakeLock.h',
]
TEST_DIRS += ['tests']
CPP_SOURCES += [
'Hal.cpp',
'HalWakeLock.cpp',

18
hal/tests/Makefile.in Normal file
View File

@ -0,0 +1,18 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
DEPTH = @DEPTH@
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
relativesrcdir = @relativesrcdir@
include $(DEPTH)/config/autoconf.mk
MOCHITEST_BROWSER_FILES = \
browser_alarms.js \
$(NULL)
include $(topsrcdir)/config/rules.mk

187
hal/tests/browser_alarms.js Normal file
View File

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

3
hal/tests/moz.build Normal file
View File

@ -0,0 +1,3 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.