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

Bug 1109240 - remove use of unsolicited callbacks from Camera app, r=justindarc #26699

Merged
merged 1 commit into from Dec 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions apps/camera/js/lib/camera/camera.js
Expand Up @@ -368,10 +368,12 @@ Camera.prototype.setupNewCamera = function(mozCamera) {
this.mozCamera = mozCamera;

// Bind to some events
this.mozCamera.onShutter = this.onShutter;
this.mozCamera.onClosed = this.onClosed;
this.mozCamera.onPreviewStateChange = this.onPreviewStateChange;
this.mozCamera.onRecorderStateChange = this.onRecorderStateChange;
this.mozCamera.addEventListener('shutter', this.onShutter);
this.mozCamera.addEventListener('close', this.onClosed);
this.mozCamera.addEventListener('previewstatechange',
this.onPreviewStateChange);
this.mozCamera.addEventListener('recorderstatechange',
this.onRecorderStateChange);

this.capabilities = this.formatCapabilities(capabilities);

Expand Down Expand Up @@ -1145,27 +1147,27 @@ Camera.prototype.onShutter = function() {
this.emit('shutter');
};


/**
* Emit a 'closed' event when camera controller
* closes
*
* @private
*/
Camera.prototype.onClosed = function(reason) {
Camera.prototype.onClosed = function(e) {
this.shutdown();
this.emit('closed', reason);
this.emit('closed', e.reason);
};

/**
* The preview state change events come
* from the camera hardware. If 'stopped'
* or 'paused' the camera must not be used.
*
* @param {String} state ['stopped', 'paused']
* @param event with {String} newState ['started', 'stopped', 'paused']
* @private
*/
Camera.prototype.onPreviewStateChange = function(state) {
Camera.prototype.onPreviewStateChange = function(e) {
var state = e.newState;
debug('preview state change: %s', state);
this.previewState = state;
this.emit('preview:' + state);
Expand All @@ -1177,7 +1179,8 @@ Camera.prototype.onPreviewStateChange = function(state) {
* @param {String} msg
* @private
*/
Camera.prototype.onRecorderStateChange = function(msg) {
Camera.prototype.onRecorderStateChange = function(e) {
var msg = e.newState;
if (msg === 'FileSizeLimitReached') {
this.emit('filesizelimitreached');
}
Expand Down
24 changes: 14 additions & 10 deletions apps/camera/js/lib/camera/focus.js
Expand Up @@ -80,13 +80,13 @@ Focus.prototype.configureFocusModes = function(mode) {
// Face detection only enabled on picture mode (disabled on video)
this.faceDetection =
faceDetectionUserEnabled && faceDetectionSupported && mode === 'picture';
this.mozCamera.onAutoFocusMoving = this.onAutoFocusMoving;
this.mozCamera.onAutoFocusCompleted = this.onAutoFocusCompleted;
this.mozCamera.addEventListener('focus', this.onAutoFocusStateChange);
};

Focus.prototype.startFaceDetection = function() {
if (!this.faceDetection) { return; }
this.mozCamera.onFacesDetected = this.focusOnLargestFace;
this.mozCamera.addEventListener('facesdetected',
this.handleFaceDetectionEvent);
this.mozCamera.startFaceDetection();
};

Expand All @@ -95,11 +95,17 @@ Focus.prototype.stopFaceDetection = function() {
clearTimeout(this.faceDetectionSuspended);
clearTimeout(this.faceDetectionSuspensionTimer);
if (this.mozCamera.stopFaceDetection) {
this.mozCamera.removeEventListener('facesdetected',
this.handleFaceDetectionEvent);
this.mozCamera.stopFaceDetection();
}
this.clearFaceDetection();
};

Focus.prototype.handleFaceDetectionEvent = function(e) {
this.focusOnLargestFace(e.faces);
};

Focus.prototype.clearFaceDetection = function() {
this.focusOnLargestFace([]);
};
Expand Down Expand Up @@ -156,14 +162,12 @@ Focus.prototype.updateFocusState = function(state) {
}
};

Focus.prototype.onAutoFocusMoving = function(moving) {
if (moving) {
this.updateFocusState('focusing');
Focus.prototype.onAutoFocusStateChange = function(e) {
var state = e.newState;
if (state === 'unfocused') {
state = 'fail';
}
};

Focus.prototype.onAutoFocusCompleted = function(state) {
this.updateFocusState(state ? 'focused' : 'fail');
this.updateFocusState(state);
};

Focus.prototype.onAutoFocusChanged = function(state) {
Expand Down
16 changes: 8 additions & 8 deletions apps/camera/test/unit/lib/camera/camera_test.js
Expand Up @@ -37,7 +37,9 @@ suite('lib/camera/camera', function() {
this.mozCamera = {
release: sinon.stub(),
setConfiguration: sinon.stub(),
capabilities: {}
capabilities: {},
addEventListener: sinon.stub(),
removeEventListener: sinon.stub()
};

this.options = {
Expand Down Expand Up @@ -659,8 +661,7 @@ suite('lib/camera/camera', function() {
assert.isTrue(takePicture.calledBefore(resumePreview));
});

test('Should call mozCamera.takePicture with the current rotation',
function() {
test('Should call mozCamera.takePicture with the current rotation', function() {
this.camera.orientation.get.returns(90);
this.camera.takePicture();

Expand All @@ -685,22 +686,22 @@ suite('lib/camera/camera', function() {

suite('Camera#onPreviewStateChange()', function() {
test('It doesn\'t fire \'busy\' event if \'stopped\' or \'paused\'', function() {
this.camera.onPreviewStateChange('stopped');
this.camera.onPreviewStateChange({ newState: 'stopped' });
assert.isFalse(this.camera.emit.calledWith('busy'));
});

test('It doesn\'t fire \'ready\' event if \'started\'', function() {
this.camera.onPreviewStateChange('started');
this.camera.onPreviewStateChange({ newState: 'started' });
assert.isFalse(this.camera.emit.calledWith('ready'));
});

test('It fire a `preview:*` event', function() {
this.camera.onPreviewStateChange('started');
this.camera.onPreviewStateChange({ newState: 'started' });
sinon.assert.calledWith(this.camera.emit, 'preview:started');
});

test('It stores the last known preview state', function() {
this.camera.onPreviewStateChange('started');
this.camera.onPreviewStateChange({ newState: 'started' });
assert.equal(this.camera.previewState, 'started');
});
});
Expand Down Expand Up @@ -781,7 +782,6 @@ suite('lib/camera/camera', function() {
});

test('Should call requestCamera with selectedCamera and mozCameraConfig', function() {

this.camera.mozCameraConfig = '<moz-camera-config>';
this.camera.selectedCamera = '<selected-camera>';
this.camera.load();
Expand Down
77 changes: 53 additions & 24 deletions apps/camera/test/unit/lib/camera/focus_test.js
Expand Up @@ -14,6 +14,7 @@ suite('lib/camera/focus', function() {

// Fake mozCamera
this.mozCamera = {
_listeners: {},
capabilities: {
focusModes : ["auto", "infinity", "normal",
"macro", "continuous-picture", "continuous-video" ],
Expand All @@ -28,7 +29,17 @@ suite('lib/camera/focus', function() {
stopContinuousFocus: sinon.stub(),
resumeContinuousFocus: sinon.stub(),
startFaceDetection: sinon.spy(),
stopFaceDetection: sinon.spy()
stopFaceDetection: sinon.spy(),
addEventListener: function(eventname, listener) {
this._listeners[eventname] = listener;
},
removeEventListener: function(eventname, listener) {
if (this._listeners.hasOwnProperty(eventname)) {
if (this._listeners[eventname] === listener) {
delete this._listeners[eventname];
}
}
}
};

this.focus = new this.Focus({});
Expand Down Expand Up @@ -278,7 +289,7 @@ suite('lib/camera/focus', function() {
});
});

suite('Focus#onAutoFocusMoving', function() {
suite('Focus#onfocus', function() {
setup(function() {
this.sandbox.spy(this.focus, 'updateFocusState');
});
Expand All @@ -288,29 +299,14 @@ suite('lib/camera/focus', function() {
});

test('should call updateFocusState', function() {
this.focus.onAutoFocusMoving(true);
this.focus.configureFocusModes();
var listener = this.mozCamera._listeners['focus'];
assert.ok(listener);
listener({ newState: 'focusing' });
assert.ok(this.focus.updateFocusState.calledWith('focusing'));
});

test('should not call updateFocusState', function() {
this.focus.onAutoFocusMoving(false);
assert.ok(!this.focus.updateFocusState.called);
});
});

suite('Focus#onAutoFocusCompleted', function() {
setup(function() {
this.sandbox.spy(this.focus, 'updateFocusState');
});

teardown(function() {
this.sandbox.restore();
});

test('should call updateFocusState', function() {
this.focus.onAutoFocusCompleted(true);
listener({ newState: 'focused' });
assert.ok(this.focus.updateFocusState.calledWith('focused'));
this.focus.onAutoFocusCompleted(false);
listener({ newState: 'unfocused' });
assert.ok(this.focus.updateFocusState.calledWith('fail'));
});
});
Expand Down Expand Up @@ -370,7 +366,6 @@ suite('lib/camera/focus', function() {
});

suite('Focus#focusOnLargestFace', function() {

setup(function() {
this.sandbox.spy(this.focus, 'onFacesDetected');
});
Expand All @@ -393,6 +388,40 @@ suite('lib/camera/focus', function() {
assert.ok(this.focus.onFacesDetected.calledWith([]));
});

test('it should focus on the singleton face', function() {
this.focus.touchFocus = true;
this.focus.faceDetectionSuspended = false;
var face = {
id: 3,
score: 80,
bounds: {
height: 300,
width: 300
}
};
this.focus.focusOnLargestFace([face]);
assert.ok(
Object.is(this.focus.onFacesDetected.firstCall.args[0][0], face)
);
});

test('it should focus on the singleton face (event)', function() {
this.focus.touchFocus = true;
this.focus.faceDetectionSuspended = false;
var face = {
id: 3,
score: 80,
bounds: {
height: 300,
width: 300
}
};
var event = { faces: [face] };
this.focus.handleFaceDetectionEvent(event);
assert.ok(
Object.is(this.focus.onFacesDetected.firstCall.args[0][0], face)
);
});
});

suite('Focus#focus()', function() {
Expand Down