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

Commit

Permalink
Merge pull request #18164 from wilsonpage/992341
Browse files Browse the repository at this point in the history
Bug 992341 - [B2G][Camera] Double tapping in camera causes self-timer to
  • Loading branch information
wilsonpage authored and rvandermeulen committed Apr 11, 2014
1 parent 7487208 commit 160dfeb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
30 changes: 26 additions & 4 deletions apps/camera/js/controllers/timer.js
Expand Up @@ -52,22 +52,43 @@ TimerController.prototype.bindEvents = function() {
* Start the timer counting down
* from the currently set timer value.
*
* We bind to the app event asynchronously
* Don't allow timer to start if
* one is already active.
*
* We bind to app events asynchronously
* so that the timer isn't instantly
* cleared by the 'click' that started it.
*
* @private
*/
TimerController.prototype.start = function() {
if (this.app.get('timerActive')) { return; }

this.seconds = this.settings.timer.selected('value');
this.interval = setInterval(this.tick, 1000);
this.view.set(this.seconds).show();
setTimeout(this.bindTimerEvents);
this.scheduleTick();

this.app.set('timerActive', true);
this.app.emit('timer:started');

debug('started');
};

/**
* Schedule the next tick.
*
* Make sure to clear any existing
* timeout to be absolutely sure that
* only one timeout is ever pending.
*
* @private
*/
TimerController.prototype.scheduleTick = function() {
clearTimeout(this.timeout);
this.timeout = setTimeout(this.tick, 1000);
};

/**
* Decrements the timer and checks
* if its still has second left.
Expand All @@ -82,13 +103,14 @@ TimerController.prototype.start = function() {
* @private
*/
TimerController.prototype.tick = function() {
if (!(--this.seconds)) {
if (--this.seconds <= 0) {
this.app.emit('timer:ended');
this._clear();
return;
}

this.view.set(this.seconds);
this.scheduleTick();
};

/**
Expand All @@ -110,7 +132,7 @@ TimerController.prototype.clear = function() {
* @private
*/
TimerController.prototype._clear = function() {
clearInterval(this.interval);
clearTimeout(this.timeout);
this.unbindTimerEvents();
this.view.hide();
this.view.reset();
Expand Down
33 changes: 25 additions & 8 deletions apps/camera/test/unit/controllers/timer_test.js
Expand Up @@ -32,9 +32,13 @@ suite('controllers/timer', function() {
timer: sinon.createStubInstance(this.TimerView)
};

this.timer = this.app.views.timer;
this.timer.set.returns(this.timer);
// Aliases
this.view = this.app.views.timer;

this.view.set.returns(this.view);
this.app.settings.timer.selected.returns(5);

this.controller = new this.TimerController(this.app);
});

teardown(function() {
Expand All @@ -46,7 +50,7 @@ suite('controllers/timer', function() {
suite('TimerController()', function() {
test('Should append the TimerView to the app', function() {
var controller = new this.TimerController(this.app);
assert.ok(this.timer.appendTo.calledWith(this.app.el));
assert.ok(this.view.appendTo.calledWith(this.app.el));
});

test('Should start the timer when the \'startcountdown\' event fires', function() {
Expand All @@ -62,8 +66,6 @@ suite('controllers/timer', function() {

suite('TimerController#start()', function() {
setup(function() {
this.controller = new this.TimerController(this.app);
this.view = this.controller.view;
this.controller.start();
});

Expand Down Expand Up @@ -116,7 +118,7 @@ suite('controllers/timer', function() {
assert.ok(this.app.set.calledWith('timerActive', false));
});

test('Should hide the view', function() {
test('Should hide the view once time is up', function() {
this.clock.tick(5000);
assert.ok(this.view.hide.called);
});
Expand All @@ -125,12 +127,27 @@ suite('controllers/timer', function() {
this.clock.tick(5000);
assert.ok(!this.app.emit.calledWith('timer:cleared'));
});

test('Should never be able to fall into negative numbers', function() {
this.controller.seconds = 0;
this.view.set.reset();

this.clock.tick(1000);

//
assert.isFalse(this.view.set.calledWith(-1));
});

test('Should not able to start a timer if one is already active', function() {
this.app.emit.reset();
this.app.get.withArgs('timerActive').returns(true);
this.controller.start();
assert.isFalse(this.app.emit.calledWith('timer:started'));
});
});

suite('TimerController#clear()', function() {
setup(function() {
this.controller = new this.TimerController(this.app);
this.view = this.controller.view;
this.controller.start();
this.controller.clear();
});
Expand Down

0 comments on commit 160dfeb

Please sign in to comment.