mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
Added result-collector; Relates to #48
This commit is contained in:
@@ -71,9 +71,9 @@ define(['quagga', 'async'], function(Quagga, async) {
|
||||
var config = generateConfig(),
|
||||
testSet = [
|
||||
{"name": "image-001.jpg", "result": "0001285112001000040801"},
|
||||
// {"name": "image-002.jpg", "result": "FANAVF1461710"},
|
||||
{"name": "image-002.jpg", "result": "FANAVF1461710"},
|
||||
// {"name": "image-003.jpg", "result": "673023"},
|
||||
{"name": "image-004.jpg", "result": "010210150301625334"},
|
||||
// {"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"},
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
|
||||
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);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user