mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
Added test for releasing camera, fixed code-style
This commit is contained in:
Vendored
+12
@@ -6899,6 +6899,7 @@ define('events',[],function() {
|
|||||||
|
|
||||||
define('camera_access',[],function() {
|
define('camera_access',[],function() {
|
||||||
|
|
||||||
|
var streamRef;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Wraps browser-specific getUserMedia
|
* Wraps browser-specific getUserMedia
|
||||||
@@ -6908,6 +6909,7 @@ define('camera_access',[],function() {
|
|||||||
*/
|
*/
|
||||||
function getUserMedia(constraints, success, failure) {
|
function getUserMedia(constraints, success, failure) {
|
||||||
navigator.getUserMedia(constraints, function(stream) {
|
navigator.getUserMedia(constraints, function(stream) {
|
||||||
|
streamRef = stream;
|
||||||
var videoSrc = (window.URL && window.URL.createObjectURL(stream)) || stream;
|
var videoSrc = (window.URL && window.URL.createObjectURL(stream)) || stream;
|
||||||
success.apply(null, [videoSrc]);
|
success.apply(null, [videoSrc]);
|
||||||
}, failure);
|
}, failure);
|
||||||
@@ -6985,6 +6987,13 @@ define('camera_access',[],function() {
|
|||||||
return {
|
return {
|
||||||
request : function(video, callback) {
|
request : function(video, callback) {
|
||||||
request(video, callback);
|
request(video, callback);
|
||||||
|
},
|
||||||
|
release : function() {
|
||||||
|
var tracks = streamRef && streamRef.getVideoTracks();
|
||||||
|
if (tracks.length) {
|
||||||
|
tracks[0].stop();
|
||||||
|
}
|
||||||
|
streamRef = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@@ -7181,6 +7190,9 @@ function(Code128Reader, EANReader, InputStream, ImageWrapper, BarcodeLocator, Ba
|
|||||||
},
|
},
|
||||||
stop : function() {
|
stop : function() {
|
||||||
_stopped = true;
|
_stopped = true;
|
||||||
|
if (_config.inputStream.type === "LiveStream") {
|
||||||
|
CameraAccess.release();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
onDetected : function(callback) {
|
onDetected : function(callback) {
|
||||||
Events.subscribe("detected", callback);
|
Events.subscribe("detected", callback);
|
||||||
|
|||||||
Vendored
+2
-2
File diff suppressed because one or more lines are too long
@@ -0,0 +1,74 @@
|
|||||||
|
define(['camera_access'], function(CameraAccess){
|
||||||
|
var originalURL,
|
||||||
|
originalUserMedia,
|
||||||
|
originalMediaStreamTrack,
|
||||||
|
video,
|
||||||
|
stream;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
var tracks = [{
|
||||||
|
stop: function() {}
|
||||||
|
}];
|
||||||
|
|
||||||
|
originalURL = window.URL;
|
||||||
|
originalUserMedia = window.getUserMedia;
|
||||||
|
originalMediaStreamTrack = window.MediaStreamTrack;
|
||||||
|
window.MediaStreamTrack = {};
|
||||||
|
window.URL = null;
|
||||||
|
|
||||||
|
|
||||||
|
stream = {
|
||||||
|
getVideoTracks: function() {
|
||||||
|
return tracks;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
sinon.spy(tracks[0], "stop");
|
||||||
|
navigator.getUserMedia = function(constraints, cb) {
|
||||||
|
cb(stream);
|
||||||
|
};
|
||||||
|
sinon.spy(navigator, "getUserMedia");
|
||||||
|
video = {
|
||||||
|
src: null,
|
||||||
|
addEventListener: function() {
|
||||||
|
|
||||||
|
},
|
||||||
|
play: function() {
|
||||||
|
|
||||||
|
},
|
||||||
|
videoWidth: 320,
|
||||||
|
videoHeight: 480
|
||||||
|
};
|
||||||
|
sinon.stub(video, "addEventListener", function(event, cb) {
|
||||||
|
cb();
|
||||||
|
});
|
||||||
|
sinon.stub(video, "play");
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
navigator.getUserMedia = originalUserMedia;
|
||||||
|
window.URL = originalURL;
|
||||||
|
window.MediaStreamTrack = originalMediaStreamTrack;
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('request', function() {
|
||||||
|
it('should request the camera', function(done) {
|
||||||
|
CameraAccess.request(video, function() {
|
||||||
|
expect(navigator.getUserMedia.calledOnce).to.equal(true);
|
||||||
|
expect(video.src).to.deep.equal(stream);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('release', function() {
|
||||||
|
it('should release the camera', function(done) {
|
||||||
|
CameraAccess.request(video, function() {
|
||||||
|
expect(video.src).to.deep.equal(stream);
|
||||||
|
CameraAccess.release();
|
||||||
|
expect(video.src.getVideoTracks()).to.have.length(1);
|
||||||
|
expect(video.src.getVideoTracks()[0].stop.calledOnce).to.equal(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -93,10 +93,11 @@ define(function() {
|
|||||||
request(video, callback);
|
request(video, callback);
|
||||||
},
|
},
|
||||||
release : function() {
|
release : function() {
|
||||||
var tracks = streamRef && streamRef.getVideoTracks();
|
var tracks = streamRef && streamRef.getVideoTracks();
|
||||||
if (tracks.length)
|
if (tracks.length) {
|
||||||
tracks[0].stop();
|
tracks[0].stop();
|
||||||
streamRef = null;
|
}
|
||||||
|
streamRef = null;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
+2
-2
@@ -191,8 +191,8 @@ function(Code128Reader, EANReader, InputStream, ImageWrapper, BarcodeLocator, Ba
|
|||||||
},
|
},
|
||||||
stop : function() {
|
stop : function() {
|
||||||
_stopped = true;
|
_stopped = true;
|
||||||
if (_config.inputStream.type == "LiveStream") {
|
if (_config.inputStream.type === "LiveStream") {
|
||||||
CameraAccess.release();
|
CameraAccess.release();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
onDetected : function(callback) {
|
onDetected : function(callback) {
|
||||||
|
|||||||
+2
-1
@@ -19,7 +19,8 @@ require.config({
|
|||||||
'cv_utils': 'src/cv_utils',
|
'cv_utils': 'src/cv_utils',
|
||||||
'typedefs': 'src/typedefs',
|
'typedefs': 'src/typedefs',
|
||||||
'glMatrixAddon': 'src/glMatrixAddon',
|
'glMatrixAddon': 'src/glMatrixAddon',
|
||||||
'cluster': 'src/cluster'
|
'cluster': 'src/cluster',
|
||||||
|
'camera_access': 'src/camera_access'
|
||||||
},
|
},
|
||||||
deps: allTestFiles,
|
deps: allTestFiles,
|
||||||
callback: window.__karma__.start
|
callback: window.__karma__.start
|
||||||
|
|||||||
Reference in New Issue
Block a user