Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 979102 - [B2G][Clock] If a user lets an alarm go off for 10 minut…
Browse files Browse the repository at this point in the history
…es without stopping it, the stop and snooze buttons will stop functioning
  • Loading branch information
mcav committed Mar 4, 2014
1 parent 6781459 commit 68c8737
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
2 changes: 1 addition & 1 deletion apps/clock/js/app.js
Expand Up @@ -97,7 +97,7 @@ var App = {
}
instance.active = true;
instance.visible = true;
if (currentIndex !== -1) {
if (currentIndex !== -1 && currentIndex !== panelIndex) {
var direction = currentIndex < panelIndex;
rAF(function startAnimation(oldPanel) {
instance.transition =
Expand Down
42 changes: 22 additions & 20 deletions apps/clock/js/panels/alarm/active_alarm.js
Expand Up @@ -7,7 +7,6 @@ define(function(require) {
var AlarmsDB = require('alarmsdb');
var Timer = require('timer');
var Utils = require('utils');
var View = require('view');

function ActiveAlarm() {
this.childWindow = null;
Expand All @@ -34,6 +33,8 @@ define(function(require) {
if (!this.initialized) {
var handler = this.handler.bind(this);
navigator.mozSetMessageHandler('alarm', handler);
// Add a handler to make integration tests easier:
window.addEventListener('test-alarm', handler);
navigator.mozSetMessageHandler('message', handler);
window.addEventListener('message', handler, false);
AlarmManager.updateAlarmStatusBar();
Expand All @@ -42,14 +43,18 @@ define(function(require) {
},

handler: function aac_handler(message) {
// If this is a 'test-alarm' CustomEvent, data is stored in 'detail'.
var data = message.data || message.detail;
data.date = message.date || new Date();

// Set a watchdog to avoid locking the CPU wake lock too long,
// because it'd exhaust the battery quickly which is very bad.
// This could probably happen if the app failed to launch or
// handle the alarm message due to any unexpected reasons.
Utils.safeWakeLock({timeoutMs: 30000}, function(done) {
try {
this[messageHandlerMapping[message.data.type]].call(
this, message, done);
this[messageHandlerMapping[data.type]].call(
this, data, done);
} catch (err) {
console.error('Error calling handler', err);
done();
Expand All @@ -58,8 +63,8 @@ define(function(require) {
}.bind(this));
},

onRingerReady: function aac_ringerReady(msg, done) {
if (msg.data.status === 'READY') {
onRingerReady: function aac_ringerReady(data, done) {
if (data.status === 'READY') {
while (true) {
var el = this.ringerWaitList.shift();
if (!el) {
Expand Down Expand Up @@ -87,7 +92,7 @@ define(function(require) {
}
},

onAlarm: function aac_onAlarm(message, done) {
onAlarm: function aac_onAlarm(data, done) {
/*
* We maintain an alarm's life cycle immediately when the alarm goes off.
* If user click the snooze button when the alarm goes off,
Expand All @@ -101,9 +106,9 @@ define(function(require) {
* A snooze alarm should be turned off.
*/
// receive and parse the alarm id from the message
var id = message.data.id;
var date = message.date;
var type = message.data.type;
var id = data.id;
var date = data.date;
var type = data.type;

// Unlock the CPU when these functions have been called
var finalizer = Utils.async.namedParallel([
Expand Down Expand Up @@ -141,7 +146,7 @@ define(function(require) {
}.bind(this));
},

onTimer: function aac_onTimer(message, done) {
onTimer: function aac_onTimer(data, done) {
Timer.request(function(err, timer) {
if (err && !timer) {
timer = Timer.singleton().toSerializable();
Expand All @@ -156,8 +161,8 @@ define(function(require) {
}.bind(this));
},

scheduleSnooze: function aac_scheduleSnooze(message, done) {
var id = message.data.id;
scheduleSnooze: function aac_scheduleSnooze(data, done) {
var id = data.id;
AlarmsDB.getAlarm(id, function aac_gotAlarm(err, alarm) {
if (err) {
return;
Expand All @@ -172,23 +177,20 @@ define(function(require) {
});
},

onClose: function aac_onClose(message, done) {
onClose: function aac_onClose(data, done) {
this.childwindow = null;
switch (message.data.type) {
switch (data.type) {
case 'close-timer':
App.navigate('#timer-panel');
View.instance(document.querySelector('#timer-panel'),
Timer.Panel).dialog();
Timer.singleton(function(err, timer) {
if (!err) {
timer.cancel();
timer.save(done);
timer.save();
}
App.navigate({ hash: '#timer-panel' }, done);
});
break;
case 'close-alarm':
App.navigate('#alarm-panel');
done();
App.navigate({ hash: '#alarm-panel' }, done);
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion apps/clock/js/ring_view.js
Expand Up @@ -207,7 +207,8 @@ RingView.prototype.startNotify = function rv_startNotify(timeout) {
}
setTimeout(function rv_clearVibration() {
if (this.started === unique) {
this.stopNotify();
this.vibrate(false);
this.ring(false);
}
done();
}.bind(this), timeout);
Expand Down
35 changes: 35 additions & 0 deletions apps/clock/test/marionette/alarm_interaction_test.js
Expand Up @@ -2,6 +2,7 @@ marionette('Alarm interaction', function() {
'use strict';
var assert = require('./lib/assert');
var Alarm = require('./lib/alarm');
var Clock = require('./lib/clock');
var client = marionette.client();
var alarm, twentyFromNow, thirtyFromNow;

Expand Down Expand Up @@ -198,6 +199,40 @@ marionette('Alarm interaction', function() {
alarm.waitForBannerHidden();
});

test('firing', function() {
// Trigger a fake 'alarm' event:
client.executeScript(function() {
var id = document.querySelector('.alarm-item').dataset.id;
var alarm = new CustomEvent('test-alarm', {
detail: {
id: parseInt(id, 10),
type: 'normal'
}
});
window.dispatchEvent(alarm);
});
// Switch to the system frame
client.switchToFrame();
// Now grab the Attention Screen frame
client.switchToFrame(
client.findElement('iframe[data-frame-name="_blank"]'));
// Click the "stop" button
var el = client.helper.waitForElement('#ring-button-stop');
try {
el.click();
} catch(e) {
// Marionette throws an error because the frame closes while
// handling the click event. This is expected.
}
// Now switch back to the system, then to the clock.
client.switchToFrame();
client.apps.switchToApp(Clock.ORIGIN);

// Make sure we can see the analog clock again.
var clock = client.helper.waitForElement('#analog-clock');
assert(clock.displayed());
});

test('deletion', function() {
alarm.openForm(0);
alarm.formDelete();
Expand Down

0 comments on commit 68c8737

Please sign in to comment.