|
|
|
@ -1,10 +1,13 @@
|
|
|
|
|
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 = [];
|
|
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
@ -47,20 +51,10 @@ afterEach(function() {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
describe('success', function() {
|
|
|
|
|
beforeEach(function() {
|
|
|
|
|
sinon.stub(navigator.mediaDevices, "getUserMedia", function(constraints) {
|
|
|
|
|
return Promise.resolve(stream);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
|
navigator.mediaDevices.getUserMedia.restore();
|
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
@ -105,17 +97,15 @@ 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());
|
|
|
|
|
});
|
|
|
|
|
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,20 +118,55 @@ 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();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|