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 #31763 from aosmond/bug1203149
Browse files Browse the repository at this point in the history
Bug 1203149 - Release camera viewfinder screen wake lock when in power save mode. r=jdarcangelo
  • Loading branch information
BavarianTomcat committed Sep 18, 2015
2 parents fe53f73 + bf9cc11 commit c4ae787
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 0 deletions.
38 changes: 38 additions & 0 deletions apps/camera/js/controllers/battery.js
Expand Up @@ -41,6 +41,44 @@ BatteryController.prototype.bindEvents = function() {
bind(this.battery, 'levelchange', this.updateStatus);
bind(this.battery, 'chargingchange', this.updateStatus);
this.app.on('change:batteryStatus', this.onStatusChange);
this.app.on('change:recording', this.updatePowerSave);

var mozSettings = navigator.mozSettings;
mozSettings.addObserver('powersave.enabled', this.onPowerSaveChange);
mozSettings.createLock().get('powersave.enabled').then(
this.onPowerSaveChange);
};

/**
* Callback from settings when the power save state changes.
*
* @private
*/
BatteryController.prototype.onPowerSaveChange = function(values) {
var value;
if (values.settingValue !== undefined) {
value = values.settingValue;
} else {
value = values['powersave.enabled'];
}
this.powerSaveEnabled = value;
this.updatePowerSave();
};

/**
* Emits powersave event if state has changed.
*
* @private
*/
BatteryController.prototype.updatePowerSave = function() {
var state = this.powerSaveEnabled && !this.app.get('recording');
if (this.powerSave === state) {
return;
}

this.powerSave = state;
debug('power save: ' + state);
this.app.emit('battery:powersave', state);
};

/**
Expand Down
15 changes: 15 additions & 0 deletions apps/camera/js/controllers/viewfinder.js
Expand Up @@ -137,6 +137,9 @@ ViewfinderController.prototype.bindEvents = function() {
this.app.settings.grid.on('change:selected',
this.views.viewfinder.setter('grid'));

// Battery
this.app.on('battery:powersave', this.onPowerSave);

// App
this.app.on('pinch:changed', this.onPinchChanged);
this.app.on('hidden', this.stopStream);
Expand Down Expand Up @@ -279,6 +282,18 @@ ViewfinderController.prototype.stopStream = function() {
debug('stream stopped');
};

/**
* Power save state changed. If entering power
* save mode, release the screen wake lock to
* allow it to timeout without user interaction,
* otherwise acquire the wake lock.
*
* @private
*/
ViewfinderController.prototype.onPowerSave = function(powerSave) {
this.views.viewfinder.els.video.mozUseScreenWakeLock = !powerSave;
};

/**
* Configure the size and postion
* of the preview video stream.
Expand Down
75 changes: 75 additions & 0 deletions apps/camera/test/unit/controllers/battery_test.js
Expand Up @@ -49,20 +49,51 @@ suite('controllers/battery', function() {
// Shortcuts
this.notification = this.app.views.notification;

// Keep reference of
// things we want to restore
this.backup = {
mozSettings: navigator.mozSettings
};

// Mock object that mimicks
// mozSettings get API. Inside
// tests set this.mozSettingsGetResult
// define the result of the mock call.
navigator.mozSettings = {
createLock: function() { return this; },
get: sinon.stub(),
set: sinon.stub(),
addObserver: sinon.stub()
};

navigator.mozSettings.get.withArgs('powersave.enabled').returns(
Promise.resolve({'powersave.enabled': false})
);

// Our test instance
this.controller = new this.BatteryController(this.app);
});

teardown(function() {
navigator.mozSettings = this.backup.mozSettings;
});

suite('BatteryController()', function() {
test('Should listen to the following events', function() {
assert.ok(this.app.on.calledWith('change:batteryStatus'));
assert.ok(this.app.on.calledWith('change:recording'));
assert.ok(this.app.battery.addEventListener.calledWith('levelchange'));
assert.ok(this.app.battery.addEventListener.calledWith('chargingchange'));
assert.ok(navigator.mozSettings.addObserver.calledWith('powersave.enabled'));
});

test('Should update the status initially', function() {
assert.ok(this.app.set.calledWith('batteryStatus', 'healthy'));
});

test('Should query power save state', function() {
assert.ok(navigator.mozSettings.get.calledWith('powersave.enabled'));
});
});

suite('BatteryController#updateStatus()', function() {
Expand Down Expand Up @@ -168,4 +199,48 @@ suite('controllers/battery', function() {
assert.isTrue(this.notification.clear.called);
});
});

suite('BatteryController#updatePowerSave()', function() {
setup(function() {
this.controller.powerSave = true;
this.controller.powerSaveEnabled = true;
this.app.get.withArgs('recording').returns(false);
});

test('Should emit `battery:powersave` if changed', function() {
this.controller.powerSave = false;
this.controller.updatePowerSave();
assert.isTrue(this.controller.powerSave);
assert.ok(this.app.emit.calledWith('battery:powersave', true));
});

test('Should disable if recording', function() {
this.app.get.withArgs('recording').returns(true);
this.controller.updatePowerSave();
assert.isFalse(this.controller.powerSave);
assert.ok(this.app.emit.calledWith('battery:powersave', false));
});

test('Should disable if power save disabled', function() {
this.controller.powerSaveEnabled = false;
this.controller.updatePowerSave();
assert.isFalse(this.controller.powerSave);
assert.ok(this.app.emit.calledWith('battery:powersave', false));
});
});

suite('BatteryController#onPowerSaveChange()', function() {
setup(function() {
sinon.stub(this.controller, 'updatePowerSave');
this.controller.onPowerSaveChange({settingValue: 'result'});
});

test('Should cache power save enabled state', function() {
assert.isTrue(this.controller.powerSaveEnabled === 'result');
});

test('Should call `updatePowerSave`', function() {
assert.ok(this.controller.updatePowerSave.called);
});
});
});
14 changes: 14 additions & 0 deletions apps/camera/test/unit/controllers/viewfinder_test.js
Expand Up @@ -305,6 +305,20 @@ suite('controllers/viewfinder', function() {
});
});

suite('ViewfinderController#onPowerSave', function() {
test('When power save enabled, the viewfinder should disable screen ' +
'wakelocks', function() {
this.controller.onPowerSave(false);
assert.isTrue(this.viewfinder.els.video.mozUseScreenWakeLock);
});

test('When power save disabled, the viewfinder should enable screen ' +
'wakelocks', function() {
this.controller.onPowerSave(true);
assert.isFalse(this.viewfinder.els.video.mozUseScreenWakeLock);
});
});

suite('click:viewfinder', function() {
test('Should set the grid depending on the setting', function() {
this.app.settings.grid.selected.withArgs('key').returns('on');
Expand Down

0 comments on commit c4ae787

Please sign in to comment.