Added tests for selecting camera; related to #128

pull/131/head
Christoph Oberhofer 9 years ago
parent 473f4366a5
commit c4cbf10976

@ -24,6 +24,7 @@ module.exports = function(config) {
resolve: {
modules: [
path.resolve('./src/input/'),
path.resolve('./src/common/'),
'node_modules'
]
},

@ -29,6 +29,7 @@ module.exports = function(config) {
resolve: {
modules: [
path.resolve('./src/input/'),
path.resolve('./test/mocks/'),
'node_modules'
]
},

@ -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'));
}

@ -1,4 +1,5 @@
import {omit, pick} from 'lodash';
import {getUserMedia, enumerateDevices} from 'mediaDevices';
const facingMatching = {
"user": /front/i,
@ -37,10 +38,7 @@ function waitForVideo(video) {
* @param {Object} video
*/
function initCamera(video, constraints) {
if (navigator.mediaDevices
&& typeof navigator.mediaDevices.getUserMedia === 'function') {
return navigator.mediaDevices
.getUserMedia(constraints)
return getUserMedia(constraints)
.then((stream) => {
return new Promise((resolve) => {
streamRef = stream;
@ -53,8 +51,6 @@ function initCamera(video, constraints) {
});
})
.then(waitForVideo.bind(null, video));
}
return Promise.reject(new Error('getUserMedia is not defined'));
}
function deprecatedConstraints(videoConstraints) {
@ -80,7 +76,7 @@ function pickDevice(constraints) {
if (!facingMatch) {
return Promise.resolve(constraints);
}
return navigator.mediaDevices.enumerateDevices()
return enumerateDevices()
.then(devices => {
const selectedDeviceId = devices
.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 = {
audio: false,
video: deprecatedConstraints(videoConstraints)

@ -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;
}

@ -1,11 +1,14 @@
import CameraAccess from '../../src/input/camera_access';
import CameraAccess, {pickConstraints} from '../../src/input/camera_access';
import {setDevices, setStream, getConstraints, setSupported} from 'mediaDevices';
var originalURL,
originalMediaStreamTrack,
video,
stream;
stream,
devices = [];
beforeEach(function() {
describe("camera_access", () => {
beforeEach(function() {
var tracks = [{
stop: function() {}
}];
@ -24,6 +27,7 @@ beforeEach(function() {
return tracks;
}
};
setStream(stream);
sinon.spy(tracks[0], "stop");
video = {
@ -39,28 +43,18 @@ beforeEach(function() {
cb();
});
sinon.stub(video, "play");
});
});
afterEach(function() {
afterEach(function() {
window.URL = originalURL;
window.MediaStreamTrack = originalMediaStreamTrack;
});
describe('success', function() {
beforeEach(function() {
sinon.stub(navigator.mediaDevices, "getUserMedia", function(constraints) {
return Promise.resolve(stream);
});
});
afterEach(function() {
navigator.mediaDevices.getUserMedia.restore();
});
describe('success', function() {
describe('request', function () {
it('should request the camera', function (done) {
CameraAccess.request(video, {})
.then(function () {
expect(navigator.mediaDevices.getUserMedia.calledOnce).to.equal(true);
expect(video.srcObject).to.deep.equal(stream);
done();
})
@ -75,16 +69,14 @@ describe('success', function() {
maxAspectRatio: 100
})
.then(function () {
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;
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();
})
});
@ -102,20 +94,18 @@ describe('success', function() {
});
});
});
});
describe('failure', function() {
describe("permission denied", function(){
beforeEach(function() {
sinon.stub(navigator.mediaDevices, "getUserMedia", function(constraints, success, failure) {
return Promise.reject(new Error());
});
describe('failure', function() {
beforeEach(() => {
setSupported(false);
});
afterEach(function() {
navigator.mediaDevices.getUserMedia.restore();
afterEach(() => {
setSupported(true);
});
describe("permission denied", function(){
it('should throw if getUserMedia not available', function(done) {
CameraAccess.request(video, {})
.catch(function (err) {
@ -128,21 +118,56 @@ describe('failure', function() {
describe("not available", function(){
var originalGetUserMedia;
beforeEach(function() {
originalGetUserMedia = navigator.mediaDevices.getUserMedia;
navigator.mediaDevices.getUserMedia = undefined;
it('should throw if getUserMedia not available', function(done) {
CameraAccess.request(video, {})
.catch((err) => {
expect(err).to.be.defined;
done();
});
});
});
afterEach(function() {
navigator.mediaDevices.getUserMedia = originalGetUserMedia;
describe("pickConstraints", () => {
it("should return the given constraints if no facingMode is defined", (done) => {
const givenConstraints = {width: 180};
return pickConstraints(givenConstraints).then((actualConstraints) => {
expect(actualConstraints.video).to.deep.equal(givenConstraints);
done();
})
.catch((err) => {
expect(err).to.equal(null);
console.log(err);
done();
});
});
it('should throw if getUserMedia not available', function(done) {
CameraAccess.request(video, {})
it("should return the given constraints if deviceId is defined", (done) => {
const givenConstraints = {width: 180, deviceId: "4343"};
return pickConstraints(givenConstraints).then((actualConstraints) => {
expect(actualConstraints.video).to.deep.equal(givenConstraints);
done();
})
.catch((err) => {
expect(err).to.be.defined;
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();
});
});
});
});
});

@ -5,7 +5,9 @@ var canvasMock,
imageSize,
config;
beforeEach(function() {
describe("resultCollector", () => {
beforeEach(function() {
imageSize = {x: 320, y: 240};
config = {
capture: true,
@ -28,22 +30,22 @@ beforeEach(function() {
return canvasMock;
}
});
});
});
afterEach(function() {
afterEach(function() {
document.createElement.restore();
});
});
describe('create', function () {
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() {
describe('addResult', function() {
beforeEach(function() {
sinon.stub(ImageDebug, "drawImage", function() {});
});
@ -100,4 +102,5 @@ describe('addResult', function() {
collector.addResult([], imageSize, {code: "3574660239843", format: "ean_13"});
expect(collector.getResults()).to.have.length(1);
});
});
});
})

@ -17,6 +17,7 @@ module.exports = {
resolve: {
modules: [
path.resolve('./src/input/'),
path.resolve('./src/common/'),
'node_modules'
]
},

@ -6,6 +6,7 @@ module.exports = require('./webpack.config.js');
module.exports.resolve = {
modules: [
path.resolve('./lib/'),
path.resolve('./src/common/'),
'node_modules'
]
};

Loading…
Cancel
Save