Fixed unit/integration testing; fixed a few linting errors;
parent
38da5b0d41
commit
8668e79b63
@ -0,0 +1,75 @@
|
||||
{
|
||||
"settings" : {
|
||||
"ecmascript": 6,
|
||||
"jsx": true
|
||||
},
|
||||
"env": {
|
||||
"browser": true,
|
||||
"node": true
|
||||
},
|
||||
"ecmaFeatures": {
|
||||
"blockBindings": true,
|
||||
"forOf": true,
|
||||
"blockBindings": true,
|
||||
"defaultParams": true,
|
||||
"globalReturn": false,
|
||||
"modules": true,
|
||||
"objectLiteralShorthandMethods": true,
|
||||
"objectLiteralShorthandProperties": true,
|
||||
"templateStrings": true,
|
||||
"spread": true,
|
||||
"jsx": true,
|
||||
"arrowFunctions": true,
|
||||
"classes": true,
|
||||
"destructuring": true,
|
||||
"objectLiteralComputedProperties": true
|
||||
},
|
||||
"globals": {},
|
||||
"rules": {
|
||||
"no-unused-expressions": 1,
|
||||
"no-extra-boolean-cast": 1,
|
||||
"no-multi-spaces": 2,
|
||||
"no-underscore-dangle": 0,
|
||||
"comma-dangle": 2,
|
||||
"camelcase": 0,
|
||||
"curly": 2,
|
||||
"eqeqeq": 2,
|
||||
"guard-for-in": 2,
|
||||
"wrap-iife": 0,
|
||||
"no-use-before-define": [1, "nofunc"],
|
||||
"new-cap": 2,
|
||||
"quotes": 0,
|
||||
"strict": 0,
|
||||
"no-caller": 2,
|
||||
"no-empty": 1,
|
||||
"no-new": 2,
|
||||
"no-plusplus": 0,
|
||||
"no-unused-vars": 1,
|
||||
"no-trailing-spaces": 2,
|
||||
|
||||
// STYLE
|
||||
"max-params": [2, 7],
|
||||
"key-spacing": [1, {
|
||||
beforeColon: false,
|
||||
afterColon: true
|
||||
}],
|
||||
"indent": [2, 4],
|
||||
"brace-style": [2, "1tbs"],
|
||||
"comma-spacing": [2, {before: false, after: true}],
|
||||
"comma-style": [2, "last"],
|
||||
"consistent-this": [1, "self"],
|
||||
"eol-last": 0,
|
||||
"new-cap": 0,
|
||||
"new-parens": 2,
|
||||
"no-array-constructor": 2,
|
||||
"no-mixed-spaces-and-tabs": 2,
|
||||
"no-multiple-empty-lines": 2,
|
||||
"semi-spacing": 2,
|
||||
"no-spaced-func": 1,
|
||||
"padded-blocks": [2, "never"],
|
||||
"semi": [2, "always"],
|
||||
"space-after-keywords": [2, "always"],
|
||||
"space-infix-ops": 2,
|
||||
"max-len" : [1, 120]
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
@ -1,32 +1,43 @@
|
||||
module.exports = function(config) {
|
||||
config.set({
|
||||
basePath: '',
|
||||
frameworks: ['mocha', 'requirejs', 'chai', 'sinon', 'sinon-chai'],
|
||||
files: [
|
||||
'test-main.js',
|
||||
'src/typedefs.js',
|
||||
{pattern: 'node_modules/async/lib/async.js', included: false},
|
||||
{pattern: 'node_modules/gl-matrix/dist/gl-matrix-min.js', included: false},
|
||||
{pattern: 'src/*.js', included: false},
|
||||
{pattern: 'spec/**/*integration.spec.js', included: false},
|
||||
{pattern: 'test/**/*.*', included: false}
|
||||
],
|
||||
exclude: [
|
||||
],
|
||||
plugins: [
|
||||
'karma-chrome-launcher',
|
||||
'karma-mocha',
|
||||
'karma-requirejs',
|
||||
'karma-chai',
|
||||
'karma-sinon',
|
||||
'karma-sinon-chai'
|
||||
],
|
||||
reporters: ['progress'],
|
||||
port: 9876,
|
||||
colors: true,
|
||||
logLevel: config.LOG_INFO,
|
||||
autoWatch: true,
|
||||
browsers: ['Chrome'],
|
||||
singleRun: false
|
||||
});
|
||||
config.set({
|
||||
basePath: '',
|
||||
frameworks: ['mocha', 'chai', 'sinon', 'sinon-chai'],
|
||||
files: [
|
||||
'test/test-main-integration.js',
|
||||
{pattern: 'test/integration/**/*.js', included: false},
|
||||
{pattern: 'test/fixtures/**/*.*', included: false}
|
||||
],
|
||||
preprocessors: {
|
||||
'test/test-main-integration.js': ['webpack']
|
||||
},
|
||||
webpack: {
|
||||
module: {
|
||||
preLoaders: [
|
||||
{
|
||||
test: /\.js$/,
|
||||
exclude: [
|
||||
/node_modules/
|
||||
],
|
||||
loader: 'babel'
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
plugins: [
|
||||
'karma-chrome-launcher',
|
||||
'karma-mocha',
|
||||
'karma-requirejs',
|
||||
'karma-chai',
|
||||
'karma-sinon',
|
||||
'karma-sinon-chai',
|
||||
require('karma-webpack')
|
||||
],
|
||||
reporters: ['progress'],
|
||||
port: 9876,
|
||||
colors: true,
|
||||
logLevel: config.LOG_INFO,
|
||||
autoWatch: true,
|
||||
browsers: ['Chrome'],
|
||||
singleRun: false
|
||||
});
|
||||
};
|
||||
|
@ -1,42 +1,57 @@
|
||||
var path = require('path');
|
||||
|
||||
module.exports = function(config) {
|
||||
config.set({
|
||||
basePath: '',
|
||||
frameworks: ['mocha', 'requirejs', 'chai', 'sinon', 'sinon-chai'],
|
||||
files: [
|
||||
'test-main.js',
|
||||
'src/typedefs.js',
|
||||
{pattern: 'node_modules/async/lib/async.js', included: false},
|
||||
{pattern: 'node_modules/gl-matrix/dist/gl-matrix-min.js', included: false},
|
||||
{pattern: 'src/*.js', included: false},
|
||||
{pattern: 'spec/**/*.js', included: false},
|
||||
{pattern: 'test/**/*.*', included: false}
|
||||
],
|
||||
exclude: [
|
||||
'spec/**/*integration.spec.js'
|
||||
],
|
||||
preprocessors: {
|
||||
'src/*.js': ['coverage']
|
||||
},
|
||||
plugins: [
|
||||
'karma-chrome-launcher',
|
||||
'karma-coverage',
|
||||
'karma-mocha',
|
||||
'karma-requirejs',
|
||||
'karma-chai',
|
||||
'karma-sinon',
|
||||
'karma-sinon-chai',
|
||||
'karma-phantomjs-launcher'
|
||||
],
|
||||
reporters: ['progress', 'coverage'],
|
||||
port: 9876,
|
||||
colors: true,
|
||||
logLevel: config.LOG_INFO,
|
||||
autoWatch: true,
|
||||
browsers: ['Chrome'],
|
||||
singleRun: false,
|
||||
coverageReporter: {
|
||||
type : 'html',
|
||||
dir : 'coverage/'
|
||||
}
|
||||
});
|
||||
config.set({
|
||||
basePath: '',
|
||||
frameworks: ['source-map-support', 'mocha', 'chai', 'sinon', 'sinon-chai'],
|
||||
files: [
|
||||
'test/test-main.js',
|
||||
{pattern: 'test/spec/**/*.js', included: false}
|
||||
],
|
||||
preprocessors: {
|
||||
'test/test-main.js': ['webpack']
|
||||
},
|
||||
webpack: {
|
||||
module: {
|
||||
preLoaders: [
|
||||
{
|
||||
test: /\.js$/,
|
||||
exclude: [
|
||||
/node_modules/
|
||||
],
|
||||
loader: 'babel'
|
||||
},
|
||||
{
|
||||
test: /\.js$/,
|
||||
include: [
|
||||
path.resolve('src')
|
||||
],
|
||||
exclude: /node_modules/,
|
||||
loader: 'isparta'
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
plugins: [
|
||||
'karma-chrome-launcher',
|
||||
'karma-coverage',
|
||||
'karma-mocha',
|
||||
'karma-chai',
|
||||
'karma-sinon',
|
||||
'karma-sinon-chai',
|
||||
'karma-source-map-support',
|
||||
require('karma-webpack')
|
||||
],
|
||||
reporters: ['progress', 'coverage'],
|
||||
port: 9876,
|
||||
colors: true,
|
||||
logLevel: config.LOG_INFO,
|
||||
autoWatch: true,
|
||||
browsers: ['Chrome'],
|
||||
singleRun: false,
|
||||
coverageReporter: {
|
||||
type: 'html',
|
||||
dir: 'coverage/'
|
||||
}
|
||||
});
|
||||
};
|
||||
|
@ -1,53 +0,0 @@
|
||||
define(['array_helper'], function(ArrayHelper){
|
||||
describe('init', function() {
|
||||
it('initializes an array with the given value', function() {
|
||||
var input = [0, 0, 0];
|
||||
ArrayHelper.init(input, 5);
|
||||
expect(input).to.deep.equal([5, 5, 5]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shuffle', function() {
|
||||
before(function() {
|
||||
sinon.stub(Math, 'random').returns(0.5);
|
||||
});
|
||||
|
||||
after(function() {
|
||||
sinon.restore(Math);
|
||||
});
|
||||
it('shuffles the content of an array', function() {
|
||||
var input = [1, 2, 3];
|
||||
expect(ArrayHelper.shuffle(input)).to.deep.equal([3, 1, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toPointList', function() {
|
||||
it('converts an Array to a List of poitns', function() {
|
||||
var input = [[1, 2], [2, 2], [3, 2]];
|
||||
expect(ArrayHelper.toPointList(input)).to.equal("[[1,2],\r\n[2,2],\r\n[3,2]]");
|
||||
});
|
||||
});
|
||||
|
||||
describe('threshold', function() {
|
||||
it('returns all elements above the given threshold', function() {
|
||||
var input = [1, 2, 3];
|
||||
expect(ArrayHelper.threshold(input, 2, function(score) {
|
||||
return score * 1.5;
|
||||
})).to.deep.equal([2, 3]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('maxIndex', function() {
|
||||
it('gets the index of the biggest element in the array', function() {
|
||||
var input = [1, 2, 3];
|
||||
expect(ArrayHelper.maxIndex(input)).to.equal(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('max', function() {
|
||||
it('gets the biggest element in the array', function() {
|
||||
var input = [1, 3, 2];
|
||||
expect(ArrayHelper.max(input)).to.equal(3);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,131 +0,0 @@
|
||||
|
||||
define(['barcode_locator', 'config', 'html_utils'],
|
||||
function(BarcodeLocator, Config, HtmlUtils){
|
||||
|
||||
describe('checkImageConstraints', function() {
|
||||
var config,
|
||||
inputStream,
|
||||
imageSize,
|
||||
streamConfig = {};
|
||||
|
||||
beforeEach(function() {
|
||||
imageSize = {
|
||||
x: 640, y: 480
|
||||
};
|
||||
config = HtmlUtils.mergeObjects({}, Config);
|
||||
inputStream = {
|
||||
getWidth: function() {
|
||||
return imageSize.x;
|
||||
},
|
||||
getHeight: function() {
|
||||
return imageSize.y;
|
||||
},
|
||||
setWidth: function() {},
|
||||
setHeight: function() {},
|
||||
setTopRight: function() {},
|
||||
setCanvasSize: function() {},
|
||||
getConfig: function() {
|
||||
return streamConfig;
|
||||
}
|
||||
};
|
||||
sinon.stub(inputStream, "setWidth", function(width) {
|
||||
imageSize.x = width;
|
||||
});
|
||||
sinon.stub(inputStream, "setHeight", function(height) {
|
||||
imageSize.y = height;
|
||||
});
|
||||
sinon.stub(inputStream, "setTopRight");
|
||||
sinon.stub(inputStream, "setCanvasSize");
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
inputStream.setWidth.restore();
|
||||
inputStream.setHeight.restore();
|
||||
});
|
||||
|
||||
it('should not adjust the image-size if not needed', function() {
|
||||
var expected = {x: imageSize.x, y: imageSize.y};
|
||||
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||
});
|
||||
|
||||
it('should adjust the image-size', function() {
|
||||
var expected = {x: imageSize.x, y: imageSize.y};
|
||||
|
||||
config.locator.halfSample = true;
|
||||
imageSize.y += 1;
|
||||
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||
});
|
||||
|
||||
it('should adjust the image-size', function() {
|
||||
var expected = {x: imageSize.x, y: imageSize.y};
|
||||
|
||||
imageSize.y += 1;
|
||||
config.locator.halfSample = false;
|
||||
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||
});
|
||||
|
||||
it("should take the defined area into account", function() {
|
||||
var expectedSize = {
|
||||
x: 420,
|
||||
y: 315
|
||||
},
|
||||
expectedTopRight = {
|
||||
x: 115,
|
||||
y: 52
|
||||
},
|
||||
expectedCanvasSize = {
|
||||
x: 640,
|
||||
y: 480
|
||||
};
|
||||
|
||||
streamConfig.area = {
|
||||
top: "11%",
|
||||
right: "15%",
|
||||
bottom: "20%",
|
||||
left: "18%"
|
||||
};
|
||||
|
||||
config.locator.halfSample = false;
|
||||
BarcodeLocator.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);
|
||||
expect(inputStream.setCanvasSize.getCall(0).args[0]).to.deep.equal(expectedCanvasSize);
|
||||
});
|
||||
|
||||
it("should return the original size if set to full image", function() {
|
||||
var expectedSize = {
|
||||
x: 640,
|
||||
y: 480
|
||||
},
|
||||
expectedTopRight = {
|
||||
x: 0,
|
||||
y: 0
|
||||
},
|
||||
expectedCanvasSize = {
|
||||
x: 640,
|
||||
y: 480
|
||||
};
|
||||
|
||||
streamConfig.area = {
|
||||
top: "0%",
|
||||
right: "0%",
|
||||
bottom: "0%",
|
||||
left: "0%"
|
||||
};
|
||||
|
||||
config.locator.halfSample = false;
|
||||
BarcodeLocator.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);
|
||||
expect(inputStream.setCanvasSize.getCall(0).args[0]).to.deep.equal(expectedCanvasSize);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,123 +0,0 @@
|
||||
define(['camera_access'], function(CameraAccess){
|
||||
var originalURL,
|
||||
originalMediaStreamTrack,
|
||||
video,
|
||||
stream;
|
||||
|
||||
beforeEach(function() {
|
||||
var tracks = [{
|
||||
stop: function() {}
|
||||
}];
|
||||
|
||||
originalURL = window.URL;
|
||||
originalMediaStreamTrack = window.MediaStreamTrack;
|
||||
window.MediaStreamTrack = {};
|
||||
window.URL = null;
|
||||
|
||||
|
||||
stream = {
|
||||
getVideoTracks: function() {
|
||||
return tracks;
|
||||
}
|
||||
};
|
||||
sinon.spy(tracks[0], "stop");
|
||||
|
||||
video = {
|
||||
src: null,
|
||||
addEventListener: function() {
|
||||
|
||||
},
|
||||
removeEventListener: function() {
|
||||
|
||||
},
|
||||
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() {
|
||||
beforeEach(function() {
|
||||
sinon.stub(navigator, "getUserMedia", function(constraints, success) {
|
||||
success(stream);
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
navigator.getUserMedia.restore();
|
||||
});
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('failure', function() {
|
||||
describe("permission denied", function(){
|
||||
before(function() {
|
||||
sinon.stub(navigator, "getUserMedia", function(constraints, success, failure) {
|
||||
failure(new Error());
|
||||
});
|
||||
});
|
||||
|
||||
after(function() {
|
||||
navigator.getUserMedia.restore();
|
||||
});
|
||||
|
||||
it('should throw if getUserMedia not available', function(done) {
|
||||
CameraAccess.request(video, {}, function(err) {
|
||||
expect(err).to.be.defined;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("not available", function(){
|
||||
var originalGetUserMedia;
|
||||
|
||||
before(function() {
|
||||
originalGetUserMedia = navigator.getUserMedia;
|
||||
navigator.getUserMedia = undefined;
|
||||
});
|
||||
|
||||
after(function() {
|
||||
navigator.getUserMedia = originalGetUserMedia;
|
||||
});
|
||||
|
||||
it('should throw if getUserMedia not available', function(done) {
|
||||
CameraAccess.request(video, {}, function(err) {
|
||||
expect(err).to.be.defined;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -1,145 +0,0 @@
|
||||
|
||||
define(['cv_utils'], function(CVUtils){
|
||||
describe('imageRef', function() {
|
||||
it('gets the image Reference for a coordinate', function() {
|
||||
var res = CVUtils.imageRef(1, 2);
|
||||
expect(res.x).to.equal(1);
|
||||
expect(res.y).to.equal(2);
|
||||
expect(res.toVec2()[0]).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculatePatchSize', function() {
|
||||
it('should not throw an error in case of valid image size', function() {
|
||||
var expected = {x: 32, y: 32},
|
||||
patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480});
|
||||
|
||||
expect(patchSize).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it('should thow an error if image size it not valid', function() {
|
||||
var expected = {x: 32, y: 32},
|
||||
patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480});
|
||||
|
||||
expect(patchSize).to.be.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_parseCSSDimensionValues', function() {
|
||||
it("should convert a percentual value correctly", function() {
|
||||
var expected = {
|
||||
value: 10,
|
||||
unit: "%"
|
||||
},
|
||||
result = CVUtils._parseCSSDimensionValues("10%");
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a 0% value correctly", function() {
|
||||
var expected = {
|
||||
value: 100,
|
||||
unit: "%"
|
||||
},
|
||||
result = CVUtils._parseCSSDimensionValues("100%");
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a 100% value correctly", function() {
|
||||
var expected = {
|
||||
value: 0,
|
||||
unit: "%"
|
||||
},
|
||||
result = CVUtils._parseCSSDimensionValues("0%");
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a pixel value to percentage", function() {
|
||||
var expected = {
|
||||
value: 26.3,
|
||||
unit: "%"
|
||||
},
|
||||
result = CVUtils._parseCSSDimensionValues("26.3px");
|
||||
|
||||
console.log(result);
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("_dimensionsConverters", function(){
|
||||
var context;
|
||||
|
||||
beforeEach(function() {
|
||||
context = {
|
||||
width: 640,
|
||||
height: 480
|
||||
};
|
||||
});
|
||||
|
||||
it("should convert a top-value correclty", function() {
|
||||
var expected = 48,
|
||||
result = CVUtils._dimensionsConverters.top({value: 10, unit: "%"}, context);
|
||||
|
||||
expect(result).to.be.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a right-value correclty", function() {
|
||||
var expected = 640 - 128,
|
||||
result = CVUtils._dimensionsConverters.right({value: 20, unit: "%"}, context);
|
||||
|
||||
expect(result).to.be.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a bottom-value correclty", function() {
|
||||
var expected = 480 - 77,
|
||||
result = CVUtils._dimensionsConverters.bottom({value: 16, unit: "%"}, context);
|
||||
|
||||
expect(result).to.be.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a left-value correclty", function() {
|
||||
var expected = 57,
|
||||
result = CVUtils._dimensionsConverters.left({value: 9, unit: "%"}, context);
|
||||
|
||||
expect(result).to.be.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeImageArea", function() {
|
||||
it("should calculate an image-area", function() {
|
||||
var expected = {
|
||||
sx: 115,
|
||||
sy: 48,
|
||||
sw: 429,
|
||||
sh: 336
|
||||
},
|
||||
result = CVUtils.computeImageArea(640, 480, {
|
||||
top: "10%",
|
||||
right: "15%",
|
||||
bottom: "20%",
|
||||
left: "18%"
|
||||
});
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should calculate full image-area", function() {
|
||||
var expected = {
|
||||
sx: 0,
|
||||
sy: 0,
|
||||
sw: 640,
|
||||
sh: 480
|
||||
},
|
||||
result = CVUtils.computeImageArea(640, 480, {
|
||||
top: "0%",
|
||||
right: "0%",
|
||||
bottom: "0%",
|
||||
left: "0%"
|
||||
});
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,114 +0,0 @@
|
||||
define(['events'], function(Events){
|
||||
beforeEach(function() {
|
||||
Events.unsubscribe();
|
||||
});
|
||||
describe("subscribe", function() {
|
||||
|
||||
|
||||
it("should call one callback for a single event", function() {
|
||||
var callbackA = sinon.stub(),
|
||||
callbackB = sinon.stub();
|
||||
|
||||
Events.subscribe("test", callbackA);
|
||||
Events.subscribe("test", callbackB);
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
});
|
||||
|
||||
it("should call one callback twice if published twice", function() {
|
||||
var callback = sinon.stub();
|
||||
|
||||
Events.subscribe("test", callback);
|
||||
|
||||
Events.publish("test");
|
||||
Events.publish("test");
|
||||
|
||||
expect(callback.calledTwice).to.be.equal(true);
|
||||
});
|
||||
|
||||
it("should call the callback asynchronuously", function(done) {
|
||||
var test = {
|
||||
callback: function() {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
sinon.stub(test, "callback", function() {
|
||||
expect(test.callback.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
Events.subscribe("test", test.callback, true);
|
||||
Events.publish("test");
|
||||
expect(test.callback.called).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe("once", function() {
|
||||
it("should call the callback once, even when published twice", function() {
|
||||
var callbackA = sinon.stub(),
|
||||
callbackB = sinon.stub();
|
||||
|
||||
Events.once("test", callbackA);
|
||||
Events.subscribe("test", callbackB);
|
||||
|
||||
Events.publish("test");
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledTwice).to.be.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unsubscribe", function() {
|
||||
it("should unsubscribe all callbacks from a single event", function() {
|
||||
var callbackA = sinon.stub(),
|
||||
callbackB = sinon.stub(),
|
||||
callbackC = sinon.stub();
|
||||
|
||||
Events.subscribe("test", callbackA);
|
||||
Events.subscribe("test", callbackB);
|
||||
Events.subscribe("testC", callbackC);
|
||||
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackC.called).to.be.equal(false);
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
|
||||
Events.publish("testC");
|
||||
|
||||
expect(callbackC.calledOnce).to.be.equal(true);
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
|
||||
Events.unsubscribe("test");
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackC.calledOnce).to.be.equal(true);
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
});
|
||||
|
||||
it("should unsubscribe a single callback from a single event", function() {
|
||||
var callbackA = sinon.stub(),
|
||||
callbackB = sinon.stub();
|
||||
|
||||
Events.subscribe("test", callbackA);
|
||||
Events.subscribe("test", callbackB);
|
||||
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
|
||||
|
||||
Events.unsubscribe("test", callbackB);
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackA.calledTwice).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,248 +0,0 @@
|
||||
|
||||
|
||||
define(['quagga', 'async'], function(Quagga, async) {
|
||||
describe('decodeSingle', function () {
|
||||
var baseFolder = "base/test/fixtures/";
|
||||
|
||||
function generateConfig() {
|
||||
return {
|
||||
inputStream: {
|
||||
size: 640
|
||||
},
|
||||
locator: {
|
||||
patchSize: "medium",
|
||||
halfSample: true
|
||||
},
|
||||
numOfWorkers: 0,
|
||||
decoder: {
|
||||
readers: ["ean_reader"]
|
||||
},
|
||||
locate: true,
|
||||
src: null
|
||||
};
|
||||
}
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
function _runTestSet(testSet, config) {
|
||||
var readers = config.decoder.readers.slice(),
|
||||
format,
|
||||
folder;
|
||||
|
||||
if (typeof readers[0] === 'string'){
|
||||
format = readers[0];
|
||||
} else {
|
||||
format = readers[0].format;
|
||||
}
|
||||
|
||||
folder = baseFolder + format.split('_').slice(0, -1).join('_') + "/";
|
||||
|
||||
it('should decode ' + folder + " correctly", function(done) {
|
||||
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();
|
||||
});
|
||||
}, function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe("EAN", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "3574660239843"},
|
||||
{"name": "image-002.jpg", "result": "8032754490297"},
|
||||
{"name": "image-003.jpg", "result": "4006209700068"},
|
||||
/* {"name": "image-004.jpg", "result": "9002233139084"}, */
|
||||
/* {"name": "image-005.jpg", "result": "8004030044005"}, */
|
||||
{"name": "image-006.jpg", "result": "4003626011159"},
|
||||
{"name": "image-007.jpg", "result": "2111220009686"},
|
||||
{"name": "image-008.jpg", "result": "9000275609022"},
|
||||
{"name": "image-009.jpg", "result": "9004593978587"},
|
||||
{"name": "image-010.jpg", "result": "9002244845578"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "ean_13";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['ean_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("Code128", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "0001285112001000040801"},
|
||||
// {"name": "image-002.jpg", "result": "FANAVF1461710"},
|
||||
// {"name": "image-003.jpg", "result": "673023"},
|
||||
// {"name": "image-004.jpg", "result": "010210150301625334"},
|
||||
{"name": "image-005.jpg", "result": "419055603900009001012999"},
|
||||
{"name": "image-006.jpg", "result": "419055603900009001012999"},
|
||||
{"name": "image-007.jpg", "result": "T 000003552345"},
|
||||
{"name": "image-008.jpg", "result": "FANAVF1461710"},
|
||||
{"name": "image-009.jpg", "result": "0001285112001000040801"},
|
||||
{"name": "image-010.jpg", "result": "673023"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "code_128";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['code_128_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("Code39", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "B3% $DAD$"},
|
||||
{"name": "image-003.jpg", "result": "CODE39"},
|
||||
{"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-008.jpg", "result": "CODE39"},
|
||||
{"name": "image-009.jpg", "result": "2/4-8/16-32"},
|
||||
{"name": "image-010.jpg", "result": "CODE39"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "code_39";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['code_39_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("EAN-8", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "42191605"},
|
||||
{"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-006.jpg", "result": "24036153"},
|
||||
{"name": "image-007.jpg", "result": "42176817"},
|
||||
{"name": "image-008.jpg", "result": "42191605"},
|
||||
{"name": "image-009.jpg", "result": "42242215"},
|
||||
{"name": "image-010.jpg", "result": "42184799"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "ean_8";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['ean_8_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("UPC", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "882428015268"},
|
||||
{"name": "image-002.jpg", "result": "882428015268"},
|
||||
{"name": "image-003.jpg", "result": "882428015084"},
|
||||
{"name": "image-004.jpg", "result": "882428015343"},
|
||||
{"name": "image-005.jpg", "result": "882428015343"},
|
||||
/* {"name": "image-006.jpg", "result": "882428015046"}, */
|
||||
{"name": "image-007.jpg", "result": "882428015084"},
|
||||
{"name": "image-008.jpg", "result": "882428015046"},
|
||||
{"name": "image-009.jpg", "result": "039047013551"},
|
||||
{"name": "image-010.jpg", "result": "039047013551"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "upc_a";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['upc_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("UPC-E", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"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-006.jpg", "result": "05096893"},
|
||||
{"name": "image-007.jpg", "result": "03897425"},
|
||||
{"name": "image-008.jpg", "result": "01264904"},
|
||||
/*{"name": "image-009.jpg", "result": "01264904"},*/
|
||||
{"name": "image-010.jpg", "result": "01264904"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "upc_e";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['upc_e_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("Codabar", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "A10/53+17-70D"},
|
||||
{"name": "image-002.jpg", "result": "B546745735B"},
|
||||
{"name": "image-003.jpg", "result": "C$399.95A"},
|
||||
{"name": "image-004.jpg", "result": "B546745735B"},
|
||||
{"name": "image-005.jpg", "result": "C$399.95A"},
|
||||
{"name": "image-006.jpg", "result": "B546745735B"},
|
||||
{"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"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "codabar";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['codabar_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("I2of5 with localization", function() {
|
||||
var config = {
|
||||
inputStream: {
|
||||
size: 800,
|
||||
singleChannel: false
|
||||
},
|
||||
locator: {
|
||||
patchSize: "small",
|
||||
halfSample: false
|
||||
},
|
||||
numOfWorkers: 0,
|
||||
decoder: {
|
||||
readers: ["i2of5_reader"],
|
||||
},
|
||||
locate: true,
|
||||
src: null
|
||||
}, testSet = [
|
||||
{"name": "image-001.jpg", "result": "2167361334"},
|
||||
{"name": "image-002.jpg", "result": "2167361334"},
|
||||
{"name": "image-003.jpg", "result": "2167361334"},
|
||||
{"name": "image-004.jpg", "result": "2167361334"},
|
||||
{"name": "image-005.jpg", "result": "2167361334"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "i2of5";
|
||||
});
|
||||
|
||||
_runTestSet(testSet, config);
|
||||
|
||||
});
|
||||
});
|
||||
});
|
@ -1,103 +0,0 @@
|
||||
|
||||
define(['result_collector', 'image_debug'], function(ResultCollector, ImageDebug) {
|
||||
var canvasMock,
|
||||
imageSize,
|
||||
config;
|
||||
|
||||
beforeEach(function() {
|
||||
imageSize = {x: 320, y: 240};
|
||||
config = {
|
||||
capture: true,
|
||||
capacity: 20,
|
||||
blacklist: [{code: "3574660239843", format: "ean_13"}],
|
||||
filter: function(codeResult) {
|
||||
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() {
|
||||
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]);
|
||||
});
|
||||
});
|
||||
|
||||
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 = function(){ return 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);
|
||||
});
|
||||
});
|
||||
});
|
@ -1,55 +0,0 @@
|
||||
var allTestFiles = [];
|
||||
var TEST_REGEXP = /(spec|test)\.js$/i;
|
||||
|
||||
var pathToModule = function(path) {
|
||||
return path.replace(/^\/base\//, '').replace(/\.js$/, '');
|
||||
};
|
||||
|
||||
Object.keys(window.__karma__.files).forEach(function(file) {
|
||||
if (TEST_REGEXP.test(file)) {
|
||||
allTestFiles.push(pathToModule(file));
|
||||
}
|
||||
});
|
||||
|
||||
require.config({
|
||||
baseUrl: '/base',
|
||||
|
||||
paths: {
|
||||
'array_helper': 'src/array_helper',
|
||||
'cv_utils': 'src/cv_utils',
|
||||
'typedefs': 'src/typedefs',
|
||||
'cluster': 'src/cluster',
|
||||
'camera_access': 'src/camera_access',
|
||||
'events': 'src/events',
|
||||
'html_utils': 'src/html_utils',
|
||||
'quagga': 'src/quagga',
|
||||
'barcode_decoder': 'src/barcode_decoder',
|
||||
'barcode_locator': 'src/barcode_locator',
|
||||
'barcode_reader': 'src/barcode_reader',
|
||||
'bresenham': 'src/bresenham',
|
||||
'codabar_reader': 'src/codabar_reader',
|
||||
'code_39_reader': 'src/code_39_reader',
|
||||
'code_39_vin_reader': 'src/code_39_vin_reader',
|
||||
'code_128_reader': 'src/code_128_reader',
|
||||
'config': 'src/config',
|
||||
'ean_8_reader': 'src/ean_8_reader',
|
||||
'ean_reader': 'src/ean_reader',
|
||||
'frame_grabber': 'src/frame_grabber',
|
||||
'image_debug': 'src/image_debug',
|
||||
'image_loader': 'src/image_loader',
|
||||
'image_wrapper': 'src/image_wrapper',
|
||||
'input_stream': 'src/input_stream',
|
||||
'rasterizer': 'src/rasterizer',
|
||||
'skeletonizer': 'src/skeletonizer',
|
||||
'subImage': 'src/subImage',
|
||||
'tracer': 'src/tracer',
|
||||
'upc_e_reader': 'src/upc_e_reader',
|
||||
'upc_reader': 'src/upc_reader',
|
||||
'async': 'node_modules/async/lib/async',
|
||||
'gl-matrix': 'node_modules/gl-matrix/dist/gl-matrix-min',
|
||||
'result_collector': 'src/result_collector',
|
||||
'i2of5_reader': 'src/i2of5_reader'
|
||||
},
|
||||
deps: allTestFiles,
|
||||
callback: window.__karma__.start
|
||||
});
|
@ -0,0 +1,245 @@
|
||||
const Quagga = require('../../src/quagga');
|
||||
const async = require('async');
|
||||
|
||||
describe('decodeSingle', function () {
|
||||
var baseFolder = "base/test/fixtures/";
|
||||
|
||||
function generateConfig() {
|
||||
return {
|
||||
inputStream: {
|
||||
size: 640
|
||||
},
|
||||
locator: {
|
||||
patchSize: "medium",
|
||||
halfSample: true
|
||||
},
|
||||
numOfWorkers: 0,
|
||||
decoder: {
|
||||
readers: ["ean_reader"]
|
||||
},
|
||||
locate: true,
|
||||
src: null
|
||||
};
|
||||
}
|
||||
|
||||
this.timeout(10000);
|
||||
|
||||
function _runTestSet(testSet, config) {
|
||||
var readers = config.decoder.readers.slice(),
|
||||
format,
|
||||
folder;
|
||||
|
||||
if (typeof readers[0] === 'string'){
|
||||
format = readers[0];
|
||||
} else {
|
||||
format = readers[0].format;
|
||||
}
|
||||
|
||||
folder = baseFolder + format.split('_').slice(0, -1).join('_') + "/";
|
||||
|
||||
it('should decode ' + folder + " correctly", function(done) {
|
||||
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();
|
||||
});
|
||||
}, function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
describe("EAN", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "3574660239843"},
|
||||
{"name": "image-002.jpg", "result": "8032754490297"},
|
||||
{"name": "image-003.jpg", "result": "4006209700068"},
|
||||
/* {"name": "image-004.jpg", "result": "9002233139084"}, */
|
||||
/* {"name": "image-005.jpg", "result": "8004030044005"}, */
|
||||
{"name": "image-006.jpg", "result": "4003626011159"},
|
||||
{"name": "image-007.jpg", "result": "2111220009686"},
|
||||
{"name": "image-008.jpg", "result": "9000275609022"},
|
||||
{"name": "image-009.jpg", "result": "9004593978587"},
|
||||
{"name": "image-010.jpg", "result": "9002244845578"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "ean_13";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['ean_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("Code128", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "0001285112001000040801"},
|
||||
// {"name": "image-002.jpg", "result": "FANAVF1461710"},
|
||||
// {"name": "image-003.jpg", "result": "673023"},
|
||||
// {"name": "image-004.jpg", "result": "010210150301625334"},
|
||||
{"name": "image-005.jpg", "result": "419055603900009001012999"},
|
||||
{"name": "image-006.jpg", "result": "419055603900009001012999"},
|
||||
{"name": "image-007.jpg", "result": "T 000003552345"},
|
||||
{"name": "image-008.jpg", "result": "FANAVF1461710"},
|
||||
{"name": "image-009.jpg", "result": "0001285112001000040801"},
|
||||
{"name": "image-010.jpg", "result": "673023"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "code_128";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['code_128_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("Code39", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "B3% $DAD$"},
|
||||
{"name": "image-003.jpg", "result": "CODE39"},
|
||||
{"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-008.jpg", "result": "CODE39"},
|
||||
{"name": "image-009.jpg", "result": "2/4-8/16-32"},
|
||||
{"name": "image-010.jpg", "result": "CODE39"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "code_39";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['code_39_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("EAN-8", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "42191605"},
|
||||
{"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-006.jpg", "result": "24036153"},
|
||||
{"name": "image-007.jpg", "result": "42176817"},
|
||||
{"name": "image-008.jpg", "result": "42191605"},
|
||||
{"name": "image-009.jpg", "result": "42242215"},
|
||||
{"name": "image-010.jpg", "result": "42184799"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "ean_8";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['ean_8_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("UPC", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "882428015268"},
|
||||
{"name": "image-002.jpg", "result": "882428015268"},
|
||||
{"name": "image-003.jpg", "result": "882428015084"},
|
||||
{"name": "image-004.jpg", "result": "882428015343"},
|
||||
{"name": "image-005.jpg", "result": "882428015343"},
|
||||
/* {"name": "image-006.jpg", "result": "882428015046"}, */
|
||||
{"name": "image-007.jpg", "result": "882428015084"},
|
||||
{"name": "image-008.jpg", "result": "882428015046"},
|
||||
{"name": "image-009.jpg", "result": "039047013551"},
|
||||
{"name": "image-010.jpg", "result": "039047013551"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "upc_a";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['upc_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("UPC-E", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"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-006.jpg", "result": "05096893"},
|
||||
{"name": "image-007.jpg", "result": "03897425"},
|
||||
{"name": "image-008.jpg", "result": "01264904"},
|
||||
/*{"name": "image-009.jpg", "result": "01264904"},*/
|
||||
{"name": "image-010.jpg", "result": "01264904"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "upc_e";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['upc_e_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("Codabar", function() {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "A10/53+17-70D"},
|
||||
{"name": "image-002.jpg", "result": "B546745735B"},
|
||||
{"name": "image-003.jpg", "result": "C$399.95A"},
|
||||
{"name": "image-004.jpg", "result": "B546745735B"},
|
||||
{"name": "image-005.jpg", "result": "C$399.95A"},
|
||||
{"name": "image-006.jpg", "result": "B546745735B"},
|
||||
{"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"}
|
||||
];
|
||||
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "codabar";
|
||||
});
|
||||
|
||||
config.decoder.readers = ['codabar_reader'];
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
|
||||
describe("I2of5 with localization", function() {
|
||||
var config = {
|
||||
inputStream: {
|
||||
size: 800,
|
||||
singleChannel: false
|
||||
},
|
||||
locator: {
|
||||
patchSize: "small",
|
||||
halfSample: false
|
||||
},
|
||||
numOfWorkers: 0,
|
||||
decoder: {
|
||||
readers: ["i2of5_reader"]
|
||||
},
|
||||
locate: true,
|
||||
src: null
|
||||
},
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "2167361334"},
|
||||
{"name": "image-002.jpg", "result": "2167361334"},
|
||||
{"name": "image-003.jpg", "result": "2167361334"},
|
||||
{"name": "image-004.jpg", "result": "2167361334"},
|
||||
{"name": "image-005.jpg", "result": "2167361334"}
|
||||
];
|
||||
testSet.forEach(function(sample) {
|
||||
sample.format = "i2of5";
|
||||
});
|
||||
_runTestSet(testSet, config);
|
||||
});
|
||||
});
|
@ -0,0 +1,53 @@
|
||||
const ArrayHelper = require('../../src/array_helper');
|
||||
|
||||
describe('init', function() {
|
||||
it('initializes an array with the given value', function() {
|
||||
var input = [0, 0, 0];
|
||||
ArrayHelper.init(input, 5);
|
||||
expect(input).to.deep.equal([5, 5, 5]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shuffle', function() {
|
||||
before(function() {
|
||||
sinon.stub(Math, 'random').returns(0.5);
|
||||
});
|
||||
|
||||
after(function() {
|
||||
sinon.restore(Math);
|
||||
});
|
||||
it('shuffles the content of an array', function() {
|
||||
var input = [1, 2, 3];
|
||||
expect(ArrayHelper.shuffle(input)).to.deep.equal([3, 1, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('toPointList', function() {
|
||||
it('converts an Array to a List of poitns', function() {
|
||||
var input = [[1, 2], [2, 2], [3, 2]];
|
||||
expect(ArrayHelper.toPointList(input)).to.equal("[[1,2],\r\n[2,2],\r\n[3,2]]");
|
||||
});
|
||||
});
|
||||
|
||||
describe('threshold', function() {
|
||||
it('returns all elements above the given threshold', function() {
|
||||
var input = [1, 2, 3];
|
||||
expect(ArrayHelper.threshold(input, 2, function(score) {
|
||||
return score * 1.5;
|
||||
})).to.deep.equal([2, 3]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('maxIndex', function() {
|
||||
it('gets the index of the biggest element in the array', function() {
|
||||
var input = [1, 2, 3];
|
||||
expect(ArrayHelper.maxIndex(input)).to.equal(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('max', function() {
|
||||
it('gets the biggest element in the array', function() {
|
||||
var input = [1, 3, 2];
|
||||
expect(ArrayHelper.max(input)).to.equal(3);
|
||||
});
|
||||
});
|
@ -0,0 +1,130 @@
|
||||
const BarcodeLocator = require('../../src/barcode_locator');
|
||||
const Config = require('../../src/config');
|
||||
const merge = require('lodash/object/merge');
|
||||
|
||||
describe('checkImageConstraints', function() {
|
||||
var config,
|
||||
inputStream,
|
||||
imageSize,
|
||||
streamConfig = {};
|
||||
|
||||
beforeEach(function() {
|
||||
imageSize = {
|
||||
x: 640, y: 480
|
||||
};
|
||||
config = merge({}, Config);
|
||||
inputStream = {
|
||||
getWidth: function() {
|
||||
return imageSize.x;
|
||||
},
|
||||
getHeight: function() {
|
||||
return imageSize.y;
|
||||
},
|
||||
setWidth: function() {},
|
||||
setHeight: function() {},
|
||||
setTopRight: function() {},
|
||||
setCanvasSize: function() {},
|
||||
getConfig: function() {
|
||||
return streamConfig;
|
||||
}
|
||||
};
|
||||
sinon.stub(inputStream, "setWidth", function(width) {
|
||||
imageSize.x = width;
|
||||
});
|
||||
sinon.stub(inputStream, "setHeight", function(height) {
|
||||
imageSize.y = height;
|
||||
});
|
||||
sinon.stub(inputStream, "setTopRight");
|
||||
sinon.stub(inputStream, "setCanvasSize");
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
inputStream.setWidth.restore();
|
||||
inputStream.setHeight.restore();
|
||||
});
|
||||
|
||||
it('should not adjust the image-size if not needed', function() {
|
||||
var expected = {x: imageSize.x, y: imageSize.y};
|
||||
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||
});
|
||||
|
||||
it('should adjust the image-size', function() {
|
||||
var expected = {x: imageSize.x, y: imageSize.y};
|
||||
|
||||
config.locator.halfSample = true;
|
||||
imageSize.y += 1;
|
||||
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||
});
|
||||
|
||||
it('should adjust the image-size', function() {
|
||||
var expected = {x: imageSize.x, y: imageSize.y};
|
||||
|
||||
imageSize.y += 1;
|
||||
config.locator.halfSample = false;
|
||||
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||
});
|
||||
|
||||
it("should take the defined area into account", function() {
|
||||
var expectedSize = {
|
||||
x: 420,
|
||||
y: 315
|
||||
},
|
||||
expectedTopRight = {
|
||||
x: 115,
|
||||
y: 52
|
||||
},
|
||||
expectedCanvasSize = {
|
||||
x: 640,
|
||||
y: 480
|
||||
};
|
||||
|
||||
streamConfig.area = {
|
||||
top: "11%",
|
||||
right: "15%",
|
||||
bottom: "20%",
|
||||
left: "18%"
|
||||
};
|
||||
|
||||
config.locator.halfSample = false;
|
||||
BarcodeLocator.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);
|
||||
expect(inputStream.setCanvasSize.getCall(0).args[0]).to.deep.equal(expectedCanvasSize);
|
||||
});
|
||||
|
||||
it("should return the original size if set to full image", function() {
|
||||
var expectedSize = {
|
||||
x: 640,
|
||||
y: 480
|
||||
},
|
||||
expectedTopRight = {
|
||||
x: 0,
|
||||
y: 0
|
||||
},
|
||||
expectedCanvasSize = {
|
||||
x: 640,
|
||||
y: 480
|
||||
};
|
||||
|
||||
streamConfig.area = {
|
||||
top: "0%",
|
||||
right: "0%",
|
||||
bottom: "0%",
|
||||
left: "0%"
|
||||
};
|
||||
|
||||
config.locator.halfSample = false;
|
||||
BarcodeLocator.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);
|
||||
expect(inputStream.setCanvasSize.getCall(0).args[0]).to.deep.equal(expectedCanvasSize);
|
||||
});
|
||||
});
|
@ -0,0 +1,116 @@
|
||||
const CameraAccess = require('../../src/camera_access');
|
||||
|
||||
var originalURL,
|
||||
originalMediaStreamTrack,
|
||||
video,
|
||||
stream;
|
||||
|
||||
beforeEach(function() {
|
||||
var tracks = [{
|
||||
stop: function() {}
|
||||
}];
|
||||
|
||||
originalURL = window.URL;
|
||||
originalMediaStreamTrack = window.MediaStreamTrack;
|
||||
window.MediaStreamTrack = {};
|
||||
window.URL = null;
|
||||
|
||||
stream = {
|
||||
getVideoTracks: function() {
|
||||
return tracks;
|
||||
}
|
||||
};
|
||||
sinon.spy(tracks[0], "stop");
|
||||
|
||||
video = {
|
||||
src: null,
|
||||
addEventListener: function() {},
|
||||
removeEventListener: function() {},
|
||||
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() {
|
||||
beforeEach(function() {
|
||||
sinon.stub(navigator, "getUserMedia", function(constraints, success) {
|
||||
success(stream);
|
||||
});
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
navigator.getUserMedia.restore();
|
||||
});
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('failure', function() {
|
||||
describe("permission denied", function(){
|
||||
before(function() {
|
||||
sinon.stub(navigator, "getUserMedia", function(constraints, success, failure) {
|
||||
failure(new Error());
|
||||
});
|
||||
});
|
||||
|
||||
after(function() {
|
||||
navigator.getUserMedia.restore();
|
||||
});
|
||||
|
||||
it('should throw if getUserMedia not available', function(done) {
|
||||
CameraAccess.request(video, {}, function(err) {
|
||||
expect(err).to.be.defined;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("not available", function(){
|
||||
var originalGetUserMedia;
|
||||
|
||||
before(function() {
|
||||
originalGetUserMedia = navigator.getUserMedia;
|
||||
navigator.getUserMedia = undefined;
|
||||
});
|
||||
|
||||
after(function() {
|
||||
navigator.getUserMedia = originalGetUserMedia;
|
||||
});
|
||||
|
||||
it('should throw if getUserMedia not available', function(done) {
|
||||
CameraAccess.request(video, {}, function(err) {
|
||||
expect(err).to.be.defined;
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
@ -0,0 +1,144 @@
|
||||
const CVUtils = require('../../src/cv_utils');
|
||||
|
||||
describe('imageRef', function() {
|
||||
it('gets the image Reference for a coordinate', function() {
|
||||
var res = CVUtils.imageRef(1, 2);
|
||||
expect(res.x).to.equal(1);
|
||||
expect(res.y).to.equal(2);
|
||||
expect(res.toVec2()[0]).to.equal(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('calculatePatchSize', function() {
|
||||
it('should not throw an error in case of valid image size', function() {
|
||||
var expected = {x: 32, y: 32},
|
||||
patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480});
|
||||
|
||||
expect(patchSize).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it('should thow an error if image size it not valid', function() {
|
||||
var expected = {x: 32, y: 32},
|
||||
patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480});
|
||||
|
||||
expect(patchSize).to.be.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('_parseCSSDimensionValues', function() {
|
||||
it("should convert a percentual value correctly", function() {
|
||||
var expected = {
|
||||
value: 10,
|
||||
unit: "%"
|
||||
},
|
||||
result = CVUtils._parseCSSDimensionValues("10%");
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a 0% value correctly", function() {
|
||||
var expected = {
|
||||
value: 100,
|
||||
unit: "%"
|
||||
},
|
||||
result = CVUtils._parseCSSDimensionValues("100%");
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a 100% value correctly", function() {
|
||||
var expected = {
|
||||
value: 0,
|
||||
unit: "%"
|
||||
},
|
||||
result = CVUtils._parseCSSDimensionValues("0%");
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a pixel value to percentage", function() {
|
||||
var expected = {
|
||||
value: 26.3,
|
||||
unit: "%"
|
||||
},
|
||||
result = CVUtils._parseCSSDimensionValues("26.3px");
|
||||
|
||||
console.log(result);
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("_dimensionsConverters", function(){
|
||||
var context;
|
||||
|
||||
beforeEach(function() {
|
||||
context = {
|
||||
width: 640,
|
||||
height: 480
|
||||
};
|
||||
});
|
||||
|
||||
it("should convert a top-value correclty", function() {
|
||||
var expected = 48,
|
||||
result = CVUtils._dimensionsConverters.top({value: 10, unit: "%"}, context);
|
||||
|
||||
expect(result).to.be.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a right-value correclty", function() {
|
||||
var expected = 640 - 128,
|
||||
result = CVUtils._dimensionsConverters.right({value: 20, unit: "%"}, context);
|
||||
|
||||
expect(result).to.be.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a bottom-value correclty", function() {
|
||||
var expected = 480 - 77,
|
||||
result = CVUtils._dimensionsConverters.bottom({value: 16, unit: "%"}, context);
|
||||
|
||||
expect(result).to.be.equal(expected);
|
||||
});
|
||||
|
||||
it("should convert a left-value correclty", function() {
|
||||
var expected = 57,
|
||||
result = CVUtils._dimensionsConverters.left({value: 9, unit: "%"}, context);
|
||||
|
||||
expect(result).to.be.equal(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe("computeImageArea", function() {
|
||||
it("should calculate an image-area", function() {
|
||||
var expected = {
|
||||
sx: 115,
|
||||
sy: 48,
|
||||
sw: 429,
|
||||
sh: 336
|
||||
},
|
||||
result = CVUtils.computeImageArea(640, 480, {
|
||||
top: "10%",
|
||||
right: "15%",
|
||||
bottom: "20%",
|
||||
left: "18%"
|
||||
});
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
|
||||
it("should calculate full image-area", function() {
|
||||
var expected = {
|
||||
sx: 0,
|
||||
sy: 0,
|
||||
sw: 640,
|
||||
sh: 480
|
||||
},
|
||||
result = CVUtils.computeImageArea(640, 480, {
|
||||
top: "0%",
|
||||
right: "0%",
|
||||
bottom: "0%",
|
||||
left: "0%"
|
||||
});
|
||||
|
||||
expect(result).to.be.deep.equal(expected);
|
||||
});
|
||||
});
|
@ -0,0 +1,114 @@
|
||||
const Events = require('../../src/events');
|
||||
|
||||
beforeEach(function() {
|
||||
Events.unsubscribe();
|
||||
});
|
||||
describe("subscribe", function() {
|
||||
|
||||
|
||||
it("should call one callback for a single event", function() {
|
||||
var callbackA = sinon.stub(),
|
||||
callbackB = sinon.stub();
|
||||
|
||||
Events.subscribe("test", callbackA);
|
||||
Events.subscribe("test", callbackB);
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
});
|
||||
|
||||
it("should call one callback twice if published twice", function() {
|
||||
var callback = sinon.stub();
|
||||
|
||||
Events.subscribe("test", callback);
|
||||
|
||||
Events.publish("test");
|
||||
Events.publish("test");
|
||||
|
||||
expect(callback.calledTwice).to.be.equal(true);
|
||||
});
|
||||
|
||||
it("should call the callback asynchronuously", function(done) {
|
||||
var test = {
|
||||
callback: function() {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
sinon.stub(test, "callback", function() {
|
||||
expect(test.callback.calledOnce).to.be.true;
|
||||
done();
|
||||
});
|
||||
Events.subscribe("test", test.callback, true);
|
||||
Events.publish("test");
|
||||
expect(test.callback.called).to.be.false;
|
||||
});
|
||||
});
|
||||
|
||||
describe("once", function() {
|
||||
it("should call the callback once, even when published twice", function() {
|
||||
var callbackA = sinon.stub(),
|
||||
callbackB = sinon.stub();
|
||||
|
||||
Events.once("test", callbackA);
|
||||
Events.subscribe("test", callbackB);
|
||||
|
||||
Events.publish("test");
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledTwice).to.be.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("unsubscribe", function() {
|
||||
it("should unsubscribe all callbacks from a single event", function() {
|
||||
var callbackA = sinon.stub(),
|
||||
callbackB = sinon.stub(),
|
||||
callbackC = sinon.stub();
|
||||
|
||||
Events.subscribe("test", callbackA);
|
||||
Events.subscribe("test", callbackB);
|
||||
Events.subscribe("testC", callbackC);
|
||||
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackC.called).to.be.equal(false);
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
|
||||
Events.publish("testC");
|
||||
|
||||
expect(callbackC.calledOnce).to.be.equal(true);
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
|
||||
Events.unsubscribe("test");
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackC.calledOnce).to.be.equal(true);
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
});
|
||||
|
||||
it("should unsubscribe a single callback from a single event", function() {
|
||||
var callbackA = sinon.stub(),
|
||||
callbackB = sinon.stub();
|
||||
|
||||
Events.subscribe("test", callbackA);
|
||||
Events.subscribe("test", callbackB);
|
||||
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackA.calledOnce).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
|
||||
|
||||
Events.unsubscribe("test", callbackB);
|
||||
Events.publish("test");
|
||||
|
||||
expect(callbackA.calledTwice).to.be.equal(true);
|
||||
expect(callbackB.calledOnce).to.be.equal(true);
|
||||
});
|
||||
});
|
@ -0,0 +1,103 @@
|
||||
const ResultCollector = require('../../src/result_collector');
|
||||
const ImageDebug = require('../../src/image_debug');
|
||||
|
||||
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() {
|
||||
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]);
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
@ -0,0 +1,8 @@
|
||||
require('events').EventEmitter.prototype._maxListeners = 0;
|
||||
require('core-js/es5');
|
||||
|
||||
const testsContext = require.context("./integration", true, /.*js$/);
|
||||
testsContext.keys().forEach(testsContext);
|
||||
|
||||
const componentsContext = require.context('../src/', true, /\.*js$/);
|
||||
componentsContext.keys().forEach(componentsContext);
|
@ -0,0 +1,8 @@
|
||||
require('events').EventEmitter.prototype._maxListeners = 0;
|
||||
require('core-js/es5');
|
||||
|
||||
const testsContext = require.context("./spec", true, /.*js$/);
|
||||
testsContext.keys().forEach(testsContext);
|
||||
|
||||
const componentsContext = require.context('../src/', true, /\.*js$/);
|
||||
componentsContext.keys().forEach(componentsContext);
|
Loading…
Reference in New Issue