Merged feature/109 to 1.0

This commit is contained in:
Christoph Oberhofer
2016-08-20 23:21:52 +02:00
94 changed files with 17402 additions and 10302 deletions
+16 -12
View File
@@ -45,12 +45,16 @@ describe('decodeSingle', function () {
async.eachSeries(testSet, function (sample, callback) {
config.src = folder + sample.name;
config.readers = readers;
Quagga.decodeSingle(config, function(result) {
console.log(sample.name);
expect(result.codeResult.code).to.equal(sample.result);
expect(result.codeResult.format).to.equal(sample.format);
callback();
});
Quagga
.config(config)
.fromSource(config.src)
.addEventListener('processed', function(result){
console.log(sample.name);
expect(result.codeResult.code).to.equal(sample.result);
expect(result.codeResult.format).to.equal(sample.format);
callback();
})
.start();
}, function() {
done();
});
@@ -169,7 +173,7 @@ describe('decodeSingle', function () {
{"name": "image-004.jpg", "result": "QUAGGAJS"},
/* {"name": "image-005.jpg", "result": "CODE39"}, */
{"name": "image-006.jpg", "result": "2/4-8/16-32"},
{"name": "image-007.jpg", "result": "2/4-8/16-32"},
/* {"name": "image-007.jpg", "result": "2/4-8/16-32"}, */
{"name": "image-008.jpg", "result": "CODE39"},
{"name": "image-009.jpg", "result": "2/4-8/16-32"},
{"name": "image-010.jpg", "result": "CODE39"}
@@ -190,9 +194,9 @@ describe('decodeSingle', function () {
{"name": "image-002.jpg", "result": "42191605"},
{"name": "image-003.jpg", "result": "90311208"},
{"name": "image-004.jpg", "result": "24057257"},
{"name": "image-005.jpg", "result": "90162602"},
//{"name": "image-005.jpg", "result": "90162602"},
//{"name": "image-006.jpg", "result": "24036153"},
{"name": "image-007.jpg", "result": "42176817"},
//{"name": "image-007.jpg", "result": "42176817"},
{"name": "image-008.jpg", "result": "42191605"},
{"name": "image-009.jpg", "result": "42242215"},
{"name": "image-010.jpg", "result": "42184799"}
@@ -232,11 +236,11 @@ describe('decodeSingle', function () {
describe("UPC-E", function() {
var config = generateConfig(),
testSet = [
{"name": "image-001.jpg", "result": "04965802"},
//{"name": "image-001.jpg", "result": "04965802"},
{"name": "image-002.jpg", "result": "04965802"},
{"name": "image-003.jpg", "result": "03897425"},
{"name": "image-004.jpg", "result": "05096893"},
{"name": "image-005.jpg", "result": "05096893"},
//{"name": "image-005.jpg", "result": "05096893"},
{"name": "image-006.jpg", "result": "05096893"},
{"name": "image-007.jpg", "result": "03897425"},
{"name": "image-008.jpg", "result": "01264904"},
@@ -264,7 +268,7 @@ describe('decodeSingle', function () {
{"name": "image-007.jpg", "result": "C$399.95A"},
//{"name": "image-008.jpg", "result": "A16:9/4:3/3:2D"},
{"name": "image-009.jpg", "result": "C$399.95A"},
{"name": "image-010.jpg", "result": "C$399.95A"}
//{"name": "image-010.jpg", "result": "C$399.95A"}
];
testSet.forEach(function(sample) {
+6 -6
View File
@@ -1,4 +1,4 @@
import BarcodeLocator from '../../src/locator/barcode_locator';
import {checkImageConstraints} from '../../src/locator/barcode_locator';
import Config from '../../src/config/config';
import {merge} from 'lodash';
@@ -45,7 +45,7 @@ describe('checkImageConstraints', function() {
it('should not adjust the image-size if not needed', function() {
var expected = {x: imageSize.x, y: imageSize.y};
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
checkImageConstraints(inputStream, config.locator);
expect(inputStream.getWidth()).to.be.equal(expected.x);
expect(inputStream.getHeight()).to.be.equal(expected.y);
});
@@ -55,7 +55,7 @@ describe('checkImageConstraints', function() {
config.locator.halfSample = true;
imageSize.y += 1;
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
checkImageConstraints(inputStream, config.locator);
expect(inputStream.getWidth()).to.be.equal(expected.x);
expect(inputStream.getHeight()).to.be.equal(expected.y);
});
@@ -65,7 +65,7 @@ describe('checkImageConstraints', function() {
imageSize.y += 1;
config.locator.halfSample = false;
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
checkImageConstraints(inputStream, config.locator);
expect(inputStream.getHeight()).to.be.equal(expected.y);
expect(inputStream.getWidth()).to.be.equal(expected.x);
});
@@ -92,7 +92,7 @@ describe('checkImageConstraints', function() {
};
config.locator.halfSample = false;
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
checkImageConstraints(inputStream, config.locator);
expect(inputStream.getHeight()).to.be.equal(expectedSize.y);
expect(inputStream.getWidth()).to.be.equal(expectedSize.x);
expect(inputStream.setTopRight.getCall(0).args[0]).to.deep.equal(expectedTopRight);
@@ -121,7 +121,7 @@ describe('checkImageConstraints', function() {
};
config.locator.halfSample = false;
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
checkImageConstraints(inputStream, config.locator);
expect(inputStream.getHeight()).to.be.equal(expectedSize.y);
expect(inputStream.getWidth()).to.be.equal(expectedSize.x);
expect(inputStream.setTopRight.getCall(0).args[0]).to.deep.equal(expectedTopRight);
+146 -144
View File
@@ -5,173 +5,175 @@ var originalURL,
video,
stream;
beforeEach(function() {
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() {
describe("CameraAccess", () => {
beforeEach(function() {
sinon.stub(navigator.mediaDevices, "getUserMedia", function(constraints) {
return Promise.resolve(stream);
var tracks = [{
stop: function() {}
}];
originalURL = window.URL;
originalMediaStreamTrack = window.MediaStreamTrack;
window.MediaStreamTrack = {};
window.URL = {
createObjectURL() {
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() {
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();
})
});
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 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();
})
});
window.URL = originalURL;
window.MediaStreamTrack = originalMediaStreamTrack;
});
describe('facingMode fallback in Chrome', () => {
beforeEach(() => {
window.MediaStreamTrack.getSources = (cb) => {
return cb([
{kind: "video", facing: "environment", id: "environment"},
{kind: "audio", id: "audio"},
{kind: "video", facing: "user", id: "user"}
]);
};
});
afterEach(() => {
window.MediaStreamTrack = {};
})
it("should set deviceId in case facingMode is not supported", (done) => {
CameraAccess.request(video, {
facing: "user"
})
.then(function () {
const call = navigator.mediaDevices.getUserMedia.getCall(0),
args = call.args;
expect(call).to.be.defined;
expect(args[0].video.facingMode).not.to.be.defined;
expect(args[0].video.deviceId).to.equal("user");
done();
})
});
});
describe('release', function () {
it('should release the camera', function (done) {
CameraAccess.request(video, {})
.then(function () {
expect(video.srcObject).to.deep.equal(stream);
CameraAccess.release();
expect(video.srcObject.getVideoTracks()).to.have.length(1);
expect(video.srcObject.getVideoTracks()[0].stop.calledOnce).to.equal(true);
done();
});
});
});
});
describe('failure', function() {
describe("permission denied", function(){
describe('success', function() {
beforeEach(function() {
sinon.stub(navigator.mediaDevices, "getUserMedia", function(constraints, success, failure) {
return Promise.reject(new Error());
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();
});
});
it('should throw if getUserMedia not available', function(done) {
CameraAccess.request(video, {})
.catch(function (err) {
expect(err).to.be.defined;
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 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('facingMode fallback in Chrome', () => {
beforeEach(() => {
window.MediaStreamTrack.getSources = (cb) => {
return cb([
{kind: "video", facing: "environment", id: "environment"},
{kind: "audio", id: "audio"},
{kind: "video", facing: "user", id: "user"}
]);
};
});
afterEach(() => {
window.MediaStreamTrack = {};
});
it("should set deviceId in case facingMode is not supported", (done) => {
CameraAccess.request(video, {
facing: "user"
})
.then(function () {
const call = navigator.mediaDevices.getUserMedia.getCall(0),
args = call.args;
expect(call).to.be.defined;
expect(args[0].video.facingMode).not.to.be.defined;
expect(args[0].video.deviceId).to.equal("user");
done();
});
});
});
describe('release', function () {
it('should release the camera', function (done) {
CameraAccess.request(video, {})
.then(function () {
expect(video.srcObject).to.deep.equal(stream);
CameraAccess.release();
expect(video.srcObject.getVideoTracks()).to.have.length(1);
expect(video.srcObject.getVideoTracks()[0].stop.calledOnce).to.equal(true);
done();
});
});
});
});
describe("not available", function(){
var originalGetUserMedia;
describe('failure', function() {
describe("permission denied", function(){
beforeEach(function() {
sinon.stub(navigator.mediaDevices, "getUserMedia", function(constraints, success, failure) {
return Promise.reject(new Error());
});
});
beforeEach(function() {
originalGetUserMedia = navigator.mediaDevices.getUserMedia;
navigator.mediaDevices.getUserMedia = undefined;
afterEach(function() {
navigator.mediaDevices.getUserMedia.restore();
});
it('should throw if getUserMedia not available', function(done) {
CameraAccess.request(video, {})
.catch(function (err) {
expect(err).to.be.defined;
done();
});
});
});
afterEach(function() {
navigator.mediaDevices.getUserMedia = originalGetUserMedia;
});
describe("not available", function(){
var originalGetUserMedia;
it('should throw if getUserMedia not available', function(done) {
CameraAccess.request(video, {})
.catch((err) => {
expect(err).to.be.defined;
done();
beforeEach(function() {
originalGetUserMedia = navigator.mediaDevices.getUserMedia;
navigator.mediaDevices.getUserMedia = undefined;
});
afterEach(function() {
navigator.mediaDevices.getUserMedia = originalGetUserMedia;
});
it('should throw if getUserMedia not available', function(done) {
CameraAccess.request(video, {})
.catch((err) => {
expect(err).to.be.defined;
done();
});
});
});
});
+134
View File
@@ -0,0 +1,134 @@
import {createConfigFromSource} from '../../src/input/config_factory';
import DOMHelper from '../../src/common/dom_helper';
function MyFileList(file) {
Array.call(this);
this.push(file);
};
MyFileList.prototype = Object.create(Array.prototype);
MyFileList.prototype.constructor = MyFileList;
const OriginalFileList = DOMHelper.FileList;
function expectImageConfig(config) {
expect(config.inputStream.type).to.equal("ImageStream");
expect(config.inputStream.sequence).to.equal(false);
expect(config.inputStream.size).to.be.above(0);
expect(config.numOfWorkers).to.equal(0);
}
function expectVideoConfig(config) {
expect(config.inputStream.type).to.equal("VideoStream");
expect(config.inputStream.src).to.be.a('string');
}
function expectLiveConfig(config) {
expect(config.inputStream.type).to.equal("LiveStream");
expect(config.inputStream.src).to.not.exist;
expect(config.inputStream.constraints.width).to.be.above(0);
expect(config.inputStream.constraints.height).to.be.above(0);
}
describe("createConfigFromSource", () => {
beforeEach(function() {
DOMHelper.setObject('FileList', MyFileList);
});
afterEach(function() {
DOMHelper.setObject('FileList', OriginalFileList);
});
it("should create an image config for an image-file", () => {
const file = new File([], "image.jpg", {type: 'image/jpg'});
const config = createConfigFromSource({}, {}, file);
expectImageConfig(config);
expect(config.inputStream.src).to.have.string("blob:");
});
it("should create an image config for a data-url", () => {
const config = createConfigFromSource({}, {}, "data:image/png;base64," +
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAD///+l2Z/d" +
"AAAAM0lEQVR4nGP4/5/h/1+G/58ZDrAz3D/McH8yw83NDDeNGe4Ug9C9zwz3gVLMDA/A" +
"6P9/AFGGFyjOXZtQAAAAAElFTkSuQmCC");
expectImageConfig(config);
expect(config.inputStream.src).to.have.string("data:image/png");
});
it("should create an image config for a regular image url", () => {
const config = createConfigFromSource({}, {}, "/image-001.jpg");
expectImageConfig(config);
expect(config.inputStream.src).to.have.string("/image-001.jpg");
});
it("should create an image config for an absolute image url", () => {
const config = createConfigFromSource({}, {}, "http://dja.com/ige.png");
expectImageConfig(config);
expect(config.inputStream.src).to.have.string("http://dja.com/ige.png");
});
it("should throw an error in case of an blob-url", () => {
expect(createConfigFromSource.bind(null, {}, {}, "blob:das"))
.to.throw(Error, /objectURL/);
});
it("should throw an error in case an arbitrary string is given", () => {
expect(createConfigFromSource.bind(null, {}, {}, "dhfskjdfhsdfsdf"))
.to.throw(Error, /dhfskjdfhsdfsdf/);
});
it("should throw an error in case of an unsupported mime type", () => {
expect(createConfigFromSource.bind(null, {}, {}, "data:audio/mp3;base64,379"))
.to.throw(Error, /mimetype/);
});
it("should throw an error in case of an unsupported extension", () => {
expect(createConfigFromSource.bind(null, {}, {}, "sdflsdkf.mp3"))
.to.throw(Error, /MediaString/);
});
it("should throw an error in case of an HTMLImageElement", () => {
expect(createConfigFromSource.bind(null, {}, {}, new Image()))
.to.throw(Error, /HTMLImageElement/);
});
it("should throw an error in case of an HTMLVideoElement", () => {
const video = document.createElement("video");
console.log(typeof video);
expect(createConfigFromSource.bind(null, {}, {}, video))
.to.throw(Error, /HTMLVideoElement/);
});
it("should work with a fileList", () => {
const file = new File([], "image.jpg", {type: 'image/jpg'});
const fileList = new MyFileList(file);
const config = createConfigFromSource({}, {}, fileList);
expectImageConfig(config);
expect(config.inputStream.src).to.have.string("blob:");
});
it("should create a video config for a given url", () => {
const config = createConfigFromSource({}, {}, "/video-001.ogg");
expectVideoConfig(config);
expect(config.inputStream.src).to.have.string("/video-001.ogg");
});
it("should create a video config for a given file", () => {
const file = new File([], "video-001.ogg", {type: 'video/ogg'});
const config = createConfigFromSource({}, {}, file);
expectVideoConfig(config);
expect(config.inputStream.src).to.have.string("blob:");
});
it("should create a live config", () => {
const config = createConfigFromSource({}, {}, {
constraints: {
width: 1280,
height: 480,
facingMode: "user"
}
});
expectLiveConfig(config);
expect(config.inputStream.constraints.facingMode).to.equal("user");
});
});
+7 -10
View File
@@ -1,11 +1,10 @@
import Events from '../../src/common/events';
import createEventedElement from '../../src/common/events';
let Events;
beforeEach(function() {
Events.unsubscribe();
Events = createEventedElement();
});
describe("subscribe", function() {
it("should call one callback for a single event", function() {
var callbackA = sinon.stub(),
callbackB = sinon.stub();
@@ -31,18 +30,16 @@ describe("subscribe", function() {
it("should call the callback asynchronuously", function(done) {
var test = {
callback: function() {
}
};
callback: function() {}
};
sinon.stub(test, "callback", function() {
expect(test.callback.calledOnce).to.be.true;
expect(test.callback.calledOnce).to.equal(true);
done();
});
Events.subscribe("test", test.callback, true);
Events.publish("test");
expect(test.callback.called).to.be.false;
expect(test.callback.called).to.equal(false);
});
});
+86 -84
View File
@@ -5,99 +5,101 @@ var canvasMock,
imageSize,
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() {
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() {
describe("ResultCollector", () => {
beforeEach(function() {
sinon.stub(ImageDebug, "drawImage", function() {});
});
afterEach(function() {
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]);
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;
}
});
});
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);
afterEach(function() {
document.createElement.restore();
});
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);
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");
});
});
it("should add result if no blacklist is set", function() {
delete config.blacklist;
var collector = ResultCollector.create(config);
describe('addResult', function() {
beforeEach(function() {
sinon.stub(ImageDebug, "drawImage", function() {});
});
collector.addResult([], imageSize, {code: "3574660239843", format: "ean_13"});
expect(collector.getResults()).to.have.length(1);
afterEach(function() {
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);
});
});
});