Skip to content

Commit

Permalink
Added tests for selecting camera; related to #128
Browse files Browse the repository at this point in the history
  • Loading branch information
serratus committed Oct 3, 2016
1 parent 473f436 commit c4cbf10
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 211 deletions.
1 change: 1 addition & 0 deletions karma-integration.conf.js
Expand Up @@ -24,6 +24,7 @@ module.exports = function(config) {
resolve: {
modules: [
path.resolve('./src/input/'),
path.resolve('./src/common/'),
'node_modules'
]
},
Expand Down
1 change: 1 addition & 0 deletions karma.conf.js
Expand Up @@ -29,6 +29,7 @@ module.exports = function(config) {
resolve: {
modules: [
path.resolve('./src/input/'),
path.resolve('./test/mocks/'),
'node_modules'
]
},
Expand Down
17 changes: 17 additions & 0 deletions src/common/mediaDevices.js
@@ -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'));
}
36 changes: 16 additions & 20 deletions src/input/camera_access.js
@@ -1,4 +1,5 @@
import {omit, pick} from 'lodash';
import {getUserMedia, enumerateDevices} from 'mediaDevices';

const facingMatching = {
"user": /front/i,
Expand Down Expand Up @@ -37,24 +38,19 @@ function waitForVideo(video) {
* @param {Object} video
*/
function initCamera(video, constraints) {
if (navigator.mediaDevices
&& typeof navigator.mediaDevices.getUserMedia === 'function') {
return navigator.mediaDevices
.getUserMedia(constraints)
.then((stream) => {
return new Promise((resolve) => {
streamRef = stream;
video.setAttribute("autoplay", 'true');
video.srcObject = stream;
video.addEventListener('loadedmetadata', () => {
video.play();
resolve();
});
});
})
.then(waitForVideo.bind(null, video));
}
return Promise.reject(new Error('getUserMedia is not defined'));
return getUserMedia(constraints)
.then((stream) => {
return new Promise((resolve) => {
streamRef = stream;
video.setAttribute("autoplay", 'true');
video.srcObject = stream;
video.addEventListener('loadedmetadata', () => {
video.play();
resolve();
});
});
})
.then(waitForVideo.bind(null, video));
}

function deprecatedConstraints(videoConstraints) {
Expand All @@ -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))
Expand All @@ -98,7 +94,7 @@ function pickDevice(constraints) {
});
}

function pickConstraints(videoConstraints) {
export function pickConstraints(videoConstraints) {
const normalizedConstraints = {
audio: false,
video: deprecatedConstraints(videoConstraints)
Expand Down
36 changes: 36 additions & 0 deletions test/mocks/mediaDevices.js
@@ -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;
}

0 comments on commit c4cbf10

Please sign in to comment.