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

Commit

Permalink
Bug 1075290 - Prevent a race condition between turnScreenOff and turn…
Browse files Browse the repository at this point in the history
…ScreenOn
  • Loading branch information
etiennesegonzac authored and rvandermeulen committed Oct 15, 2014
1 parent 1e72b5b commit 8cb2754
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
5 changes: 4 additions & 1 deletion apps/system/js/screen_manager.js
Expand Up @@ -95,6 +95,7 @@ var ScreenManager = {
* To track the reason caused screen off?
*/
_screenOffBy: null,
_screenOffTimeout: 0,

/*
* Request wakelock during in_call state.
Expand Down Expand Up @@ -367,7 +368,8 @@ var ScreenManager = {
self.screenEnabled = false;
self._inTransition = false;
self.screen.classList.add('screenoff');
setTimeout(function realScreenOff() {
clearTimeout(self._screenOffTimeout);
self._screenOffTimeout = setTimeout(function realScreenOff() {
self.setScreenBrightness(0, true);
// Sometimes the ScreenManager.screenEnabled and mozPower.screenEnabled
// values are out of sync. Since the rest of the world relies only on
Expand Down Expand Up @@ -401,6 +403,7 @@ var ScreenManager = {
},

turnScreenOn: function scm_turnScreenOn(instant) {
clearTimeout(this._screenOffTimeout);
if (this.screenEnabled) {
if (this._inTransition) {
// Cancel the dim out
Expand Down
18 changes: 17 additions & 1 deletion apps/system/test/unit/screen_manager_test.js
Expand Up @@ -424,6 +424,7 @@ suite('system/ScreenManager', function() {
var stubSetIdle,
stubRemoveListener,
stubScnClassListAdd,
stubScnClassListRemove,
stubScreen,
stubFireEvent,
stubUnlock,
Expand All @@ -433,7 +434,9 @@ suite('system/ScreenManager', function() {
stubSetIdle = this.sinon.stub(ScreenManager, '_setIdleTimeout');
stubRemoveListener = this.sinon.stub(window, 'removeEventListener');
stubScnClassListAdd = this.sinon.stub();
stubScreen = {'classList': {'add': stubScnClassListAdd}};
stubScnClassListRemove = this.sinon.stub();
stubScreen = {'classList': {'add': stubScnClassListAdd,
'remove': stubScnClassListRemove}};
stubFireEvent = this.sinon.stub(ScreenManager, 'fireScreenChangeEvent');
stubUnlock = this.sinon.stub();
stubSetBrightness = this.sinon.stub(ScreenManager, 'setScreenBrightness');
Expand Down Expand Up @@ -464,6 +467,19 @@ suite('system/ScreenManager', function() {
assert.isTrue(ScreenManager.fireScreenChangeEvent.called);
});

test('turn off instantly then on again', function() {
ScreenManager.screenEnabled = true;
assert.isTrue(ScreenManager.turnScreenOff(true, 'powerkey'));
this.sinon.clock.tick(10);
assert.isTrue(ScreenManager.turnScreenOn(true));
this.sinon.clock.tick(10);
assert.isTrue(ScreenManager.screenEnabled);
assert.isTrue(MockMozPower.screenEnabled);
assert.isTrue(stubScnClassListAdd.calledWith('screenoff'));
assert.isTrue(stubScnClassListRemove.calledWith('screenoff'));
sinon.assert.callOrder(stubScnClassListAdd, stubScnClassListRemove);
});

test('turn off screen wihout instant argument', function() {
ScreenManager.screenEnabled = true;
assert.isTrue(ScreenManager.turnScreenOff(false));
Expand Down

0 comments on commit 8cb2754

Please sign in to comment.