Added tests for selecting camera; related to #128

This commit is contained in:
Christoph Oberhofer
2016-10-03 14:46:01 -04:00
parent 473f4366a5
commit c4cbf10976
9 changed files with 313 additions and 232 deletions
+1
View File
@@ -24,6 +24,7 @@ module.exports = function(config) {
resolve: { resolve: {
modules: [ modules: [
path.resolve('./src/input/'), path.resolve('./src/input/'),
path.resolve('./src/common/'),
'node_modules' 'node_modules'
] ]
}, },
+1
View File
@@ -29,6 +29,7 @@ module.exports = function(config) {
resolve: { resolve: {
modules: [ modules: [
path.resolve('./src/input/'), path.resolve('./src/input/'),
path.resolve('./test/mocks/'),
'node_modules' 'node_modules'
] ]
}, },
+17
View File
@@ -0,0 +1,17 @@
export function enumerateDevices() {
if (navigator.mediaDevices
&& typeof navigator.mediaDevices.enumerateDevices === 'function') {
return navigator.mediaDevices.enumerateDevices();
}
return Promise.reject(new Error('enumerateDevices is not defined'));
};
export function getUserMedia(constraints) {
if (navigator.mediaDevices
&& typeof navigator.mediaDevices.getUserMedia === 'function') {
return navigator.mediaDevices
.getUserMedia(constraints);
}
return Promise.reject(new Error('getUserMedia is not defined'));
}
+16 -20
View File
@@ -1,4 +1,5 @@
import {omit, pick} from 'lodash'; import {omit, pick} from 'lodash';
import {getUserMedia, enumerateDevices} from 'mediaDevices';
const facingMatching = { const facingMatching = {
"user": /front/i, "user": /front/i,
@@ -37,24 +38,19 @@ function waitForVideo(video) {
* @param {Object} video * @param {Object} video
*/ */
function initCamera(video, constraints) { function initCamera(video, constraints) {
if (navigator.mediaDevices return getUserMedia(constraints)
&& typeof navigator.mediaDevices.getUserMedia === 'function') { .then((stream) => {
return navigator.mediaDevices return new Promise((resolve) => {
.getUserMedia(constraints) streamRef = stream;
.then((stream) => { video.setAttribute("autoplay", 'true');
return new Promise((resolve) => { video.srcObject = stream;
streamRef = stream; video.addEventListener('loadedmetadata', () => {
video.setAttribute("autoplay", 'true'); video.play();
video.srcObject = stream; resolve();
video.addEventListener('loadedmetadata', () => { });
video.play(); });
resolve(); })
}); .then(waitForVideo.bind(null, video));
});
})
.then(waitForVideo.bind(null, video));
}
return Promise.reject(new Error('getUserMedia is not defined'));
} }
function deprecatedConstraints(videoConstraints) { function deprecatedConstraints(videoConstraints) {
@@ -80,7 +76,7 @@ function pickDevice(constraints) {
if (!facingMatch) { if (!facingMatch) {
return Promise.resolve(constraints); return Promise.resolve(constraints);
} }
return navigator.mediaDevices.enumerateDevices() return enumerateDevices()
.then(devices => { .then(devices => {
const selectedDeviceId = devices const selectedDeviceId = devices
.filter(device => device.kind === 'videoinput' && facingMatch.test(device.label)) .filter(device => device.kind === 'videoinput' && facingMatch.test(device.label))
@@ -98,7 +94,7 @@ function pickDevice(constraints) {
}); });
} }
function pickConstraints(videoConstraints) { export function pickConstraints(videoConstraints) {
const normalizedConstraints = { const normalizedConstraints = {
audio: false, audio: false,
video: deprecatedConstraints(videoConstraints) video: deprecatedConstraints(videoConstraints)
+36
View File
@@ -0,0 +1,36 @@
let devices = [],
stream,
_constraints,
_supported = true;
export function enumerateDevices() {
console.log("enumerateDevices!!!!");
return Promise.resolve(devices);
};
export function getUserMedia(constraints) {
console.log("getUserMedia!!!!");
_constraints = constraints;
if (_supported) {
return Promise.resolve(stream);
}
return Promise.reject(new Error("das"));
}
export function setDevices(newDevices) {
devices = [...newDevices];
}
export function setStream(newStream) {
stream = newStream;
}
export function getConstraints() {
return _constraints;
}
export function setSupported(supported) {
console.log("Supported: " + supported);
_supported = supported;
}
+153 -128
View File
@@ -1,147 +1,172 @@
import CameraAccess from '../../src/input/camera_access'; import CameraAccess, {pickConstraints} from '../../src/input/camera_access';
import {setDevices, setStream, getConstraints, setSupported} from 'mediaDevices';
var originalURL, var originalURL,
originalMediaStreamTrack, originalMediaStreamTrack,
video, video,
stream; stream,
devices = [];
beforeEach(function() { describe("camera_access", () => {
var tracks = [{
stop: function() {}
}];
originalURL = window.URL;
originalMediaStreamTrack = window.MediaStreamTrack;
window.MediaStreamTrack = {};
window.URL = {
createObjectURL(stream) {
return stream;
}
};
stream = {
getVideoTracks: function() {
return tracks;
}
};
sinon.spy(tracks[0], "stop");
video = {
srcObject: null,
addEventListener: function() {},
removeEventListener: function() {},
setAttribute: sinon.spy(),
play: function() {},
videoWidth: 320,
videoHeight: 480
};
sinon.stub(video, "addEventListener", function(event, cb) {
cb();
});
sinon.stub(video, "play");
});
afterEach(function() {
window.URL = originalURL;
window.MediaStreamTrack = originalMediaStreamTrack;
});
describe('success', function() {
beforeEach(function() { beforeEach(function() {
sinon.stub(navigator.mediaDevices, "getUserMedia", function(constraints) { var tracks = [{
return Promise.resolve(stream); stop: function() {}
}];
originalURL = window.URL;
originalMediaStreamTrack = window.MediaStreamTrack;
window.MediaStreamTrack = {};
window.URL = {
createObjectURL(stream) {
return stream;
}
};
stream = {
getVideoTracks: function() {
return tracks;
}
};
setStream(stream);
sinon.spy(tracks[0], "stop");
video = {
srcObject: null,
addEventListener: function() {},
removeEventListener: function() {},
setAttribute: sinon.spy(),
play: function() {},
videoWidth: 320,
videoHeight: 480
};
sinon.stub(video, "addEventListener", function(event, cb) {
cb();
}); });
sinon.stub(video, "play");
}); });
afterEach(function() { afterEach(function() {
navigator.mediaDevices.getUserMedia.restore(); window.URL = originalURL;
window.MediaStreamTrack = originalMediaStreamTrack;
}); });
describe('request', function () {
it('should request the camera', function (done) { describe('success', function() {
CameraAccess.request(video, {}) describe('request', function () {
.then(function () { it('should request the camera', function (done) {
expect(navigator.mediaDevices.getUserMedia.calledOnce).to.equal(true); CameraAccess.request(video, {})
expect(video.srcObject).to.deep.equal(stream); .then(function () {
done(); expect(video.srcObject).to.deep.equal(stream);
}) done();
})
});
it("should allow deprecated constraints to be used", (done) => {
CameraAccess.request(video, {
width: 320,
height: 240,
facing: "user",
minAspectRatio: 2,
maxAspectRatio: 100
})
.then(function () {
const constraints = getConstraints();
expect(constraints.video.width).to.equal(320);
expect(constraints.video.height).to.equal(240);
expect(constraints.video.facingMode).to.equal("user");
expect(constraints.video.aspectRatio).to.equal(2);
expect(constraints.video.facing).not.to.be.defined;
expect(constraints.video.minAspectRatio).not.to.be.defined;
expect(constraints.video.maxAspectRatio).not.to.be.defined;
done();
})
});
}); });
it("should allow deprecated constraints to be used", (done) => { describe('release', function () {
CameraAccess.request(video, { it('should release the camera', function (done) {
width: 320, CameraAccess.request(video, {})
height: 240, .then(function () {
facing: "user", expect(video.srcObject).to.deep.equal(stream);
minAspectRatio: 2, CameraAccess.release();
maxAspectRatio: 100 expect(video.srcObject.getVideoTracks()).to.have.length(1);
}) expect(video.srcObject.getVideoTracks()[0].stop.calledOnce).to.equal(true);
.then(function () { done();
const call = navigator.mediaDevices.getUserMedia.getCall(0), });
args = call.args; });
expect(call).to.be.defined;
expect(args[0].video.width).to.equal(320);
expect(args[0].video.height).to.equal(240);
expect(args[0].video.facingMode).to.equal("user");
expect(args[0].video.aspectRatio).to.equal(2);
expect(args[0].video.facing).not.to.be.defined;
expect(args[0].video.minAspectRatio).not.to.be.defined;
expect(args[0].video.maxAspectRatio).not.to.be.defined;
done();
})
}); });
}); });
describe('release', function () { describe('failure', function() {
it('should release the camera', function (done) { beforeEach(() => {
CameraAccess.request(video, {}) setSupported(false);
.then(function () { });
expect(video.srcObject).to.deep.equal(stream);
CameraAccess.release(); afterEach(() => {
expect(video.srcObject.getVideoTracks()).to.have.length(1); setSupported(true);
expect(video.srcObject.getVideoTracks()[0].stop.calledOnce).to.equal(true); });
done();
}); describe("permission denied", function(){
}); it('should throw if getUserMedia not available', function(done) {
}); CameraAccess.request(video, {})
}); .catch(function (err) {
expect(err).to.be.defined;
describe('failure', function() { done();
describe("permission denied", function(){ });
beforeEach(function() { });
sinon.stub(navigator.mediaDevices, "getUserMedia", function(constraints, success, failure) { });
return Promise.reject(new Error());
}); describe("not available", function(){
}); var originalGetUserMedia;
afterEach(function() { it('should throw if getUserMedia not available', function(done) {
navigator.mediaDevices.getUserMedia.restore(); CameraAccess.request(video, {})
}); .catch((err) => {
expect(err).to.be.defined;
it('should throw if getUserMedia not available', function(done) { done();
CameraAccess.request(video, {}) });
.catch(function (err) { });
expect(err).to.be.defined; });
done();
}); describe("pickConstraints", () => {
}); it("should return the given constraints if no facingMode is defined", (done) => {
}); const givenConstraints = {width: 180};
return pickConstraints(givenConstraints).then((actualConstraints) => {
describe("not available", function(){ expect(actualConstraints.video).to.deep.equal(givenConstraints);
var originalGetUserMedia; done();
})
beforeEach(function() { .catch((err) => {
originalGetUserMedia = navigator.mediaDevices.getUserMedia; expect(err).to.equal(null);
navigator.mediaDevices.getUserMedia = undefined; console.log(err);
}); done();
});
afterEach(function() { });
navigator.mediaDevices.getUserMedia = originalGetUserMedia;
}); it("should return the given constraints if deviceId is defined", (done) => {
const givenConstraints = {width: 180, deviceId: "4343"};
it('should throw if getUserMedia not available', function(done) { return pickConstraints(givenConstraints).then((actualConstraints) => {
CameraAccess.request(video, {}) expect(actualConstraints.video).to.deep.equal(givenConstraints);
.catch((err) => { done();
expect(err).to.be.defined; })
done(); .catch((err) => {
expect(err).to.equal(null);
console.log(err);
done();
});
});
it("should set deviceId if facingMode is set to environment", (done) => {
setDevices([{deviceId: "front", kind: "videoinput", label: "front Facing"},
{deviceId: "back", label: "back Facing", kind: "videoinput"}]);
const givenConstraints = {width: 180, facingMode: "environment"};
return pickConstraints(givenConstraints).then((actualConstraints) => {
expect(actualConstraints.video).to.deep.equal({width: 180, deviceId: "back"});
done();
})
.catch((err) => {
console.log(err);
expect(err).to.equal(null);
done();
});
}); });
}); });
}); });
+87 -84
View File
@@ -5,99 +5,102 @@ var canvasMock,
imageSize, imageSize,
config; config;
beforeEach(function() {
imageSize = {x: 320, y: 240};
config = {
capture: true,
capacity: 20,
blacklist: [{code: "3574660239843", format: "ean_13"}],
filter: function() {
return true;
}
};
canvasMock = {
getContext: function() {
return {};
},
toDataURL: sinon.spy(),
width: 0,
height: 0
};
sinon.stub(document, "createElement", function(type) {
if (type === "canvas") {
return canvasMock;
}
});
});
afterEach(function() { describe("resultCollector", () => {
document.createElement.restore();
});
describe('create', function () {
it("should return a new collector", function() {
ResultCollector.create(config);
expect(document.createElement.calledOnce).to.be.equal(true);
expect(document.createElement.getCall(0).args[0]).to.equal("canvas");
});
});
describe('addResult', function() {
beforeEach(function() { beforeEach(function() {
sinon.stub(ImageDebug, "drawImage", function() {}); imageSize = {x: 320, y: 240};
}); config = {
capture: true,
afterEach(function() { capacity: 20,
ImageDebug.drawImage.restore(); blacklist: [{code: "3574660239843", format: "ean_13"}],
}); filter: function() {
return true;
it("should not add result if capacity is full", function(){ }
config.capacity = 1; };
var collector = ResultCollector.create(config); canvasMock = {
collector.addResult([], imageSize, {}); getContext: function() {
collector.addResult([], imageSize, {}); return {};
collector.addResult([], imageSize, {}); },
expect(collector.getResults()).to.have.length(1); toDataURL: sinon.spy(),
}); width: 0,
height: 0
it("should only add results which match constraints", function(){ };
var collector = ResultCollector.create(config), sinon.stub(document, "createElement", function(type) {
results; if (type === "canvas") {
return canvasMock;
collector.addResult([], imageSize, {code: "423423443", format: "ean_13"}); }
collector.addResult([], imageSize, {code: "3574660239843", format: "ean_13"});
collector.addResult([], imageSize, {code: "3574660239843", format: "code_128"});
results = collector.getResults();
expect(results).to.have.length(2);
results.forEach(function(result) {
expect(result).not.to.deep.equal(config.blacklist[0]);
}); });
}); });
it("should add result if no filter is set", function() { afterEach(function() {
delete config.filter; document.createElement.restore();
var collector = ResultCollector.create(config);
collector.addResult([], imageSize, {code: "423423443", format: "ean_13"});
expect(collector.getResults()).to.have.length(1);
}); });
it("should not add results if filter returns false", function() {
config.filter = () => (false);
var collector = ResultCollector.create(config);
collector.addResult([], imageSize, {code: "423423443", format: "ean_13"}); describe('create', function () {
expect(collector.getResults()).to.have.length(0); it("should return a new collector", function() {
ResultCollector.create(config);
expect(document.createElement.calledOnce).to.be.equal(true);
expect(document.createElement.getCall(0).args[0]).to.equal("canvas");
});
}); });
it("should add result if no blacklist is set", function() { describe('addResult', function() {
delete config.blacklist; beforeEach(function() {
var collector = ResultCollector.create(config); sinon.stub(ImageDebug, "drawImage", function() {});
});
collector.addResult([], imageSize, {code: "3574660239843", format: "ean_13"}); afterEach(function() {
expect(collector.getResults()).to.have.length(1); ImageDebug.drawImage.restore();
});
it("should not add result if capacity is full", function(){
config.capacity = 1;
var collector = ResultCollector.create(config);
collector.addResult([], imageSize, {});
collector.addResult([], imageSize, {});
collector.addResult([], imageSize, {});
expect(collector.getResults()).to.have.length(1);
});
it("should only add results which match constraints", function(){
var collector = ResultCollector.create(config),
results;
collector.addResult([], imageSize, {code: "423423443", format: "ean_13"});
collector.addResult([], imageSize, {code: "3574660239843", format: "ean_13"});
collector.addResult([], imageSize, {code: "3574660239843", format: "code_128"});
results = collector.getResults();
expect(results).to.have.length(2);
results.forEach(function(result) {
expect(result).not.to.deep.equal(config.blacklist[0]);
});
});
it("should add result if no filter is set", function() {
delete config.filter;
var collector = ResultCollector.create(config);
collector.addResult([], imageSize, {code: "423423443", format: "ean_13"});
expect(collector.getResults()).to.have.length(1);
});
it("should not add results if filter returns false", function() {
config.filter = () => (false);
var collector = ResultCollector.create(config);
collector.addResult([], imageSize, {code: "423423443", format: "ean_13"});
expect(collector.getResults()).to.have.length(0);
});
it("should add result if no blacklist is set", function() {
delete config.blacklist;
var collector = ResultCollector.create(config);
collector.addResult([], imageSize, {code: "3574660239843", format: "ean_13"});
expect(collector.getResults()).to.have.length(1);
});
}); });
}); })
+1
View File
@@ -17,6 +17,7 @@ module.exports = {
resolve: { resolve: {
modules: [ modules: [
path.resolve('./src/input/'), path.resolve('./src/input/'),
path.resolve('./src/common/'),
'node_modules' 'node_modules'
] ]
}, },
+1
View File
@@ -6,6 +6,7 @@ module.exports = require('./webpack.config.js');
module.exports.resolve = { module.exports.resolve = {
modules: [ modules: [
path.resolve('./lib/'), path.resolve('./lib/'),
path.resolve('./src/common/'),
'node_modules' 'node_modules'
] ]
}; };