Skip to content

Commit

Permalink
Unit test for ColorTracker
Browse files Browse the repository at this point in the history
Found a bug while writing the unit test, so fixed it as well here.
  • Loading branch information
mairatma authored and eduardolundgren committed Jul 20, 2014
1 parent cd1bbd1 commit b058fe2
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build/tracking-min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/tracking.js
Original file line number Diff line number Diff line change
Expand Up @@ -2014,7 +2014,7 @@
neighbours[6] = -4;
neighbours[7] = -width * 4 - 4;

tracking.ColorTracker.neighbours_ = neighbours;
tracking.ColorTracker.neighbours_[width] = neighbours;

return neighbours;
};
Expand Down
2 changes: 1 addition & 1 deletion src/trackers/ColorTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
neighbours[6] = -4;
neighbours[7] = -width * 4 - 4;

tracking.ColorTracker.neighbours_ = neighbours;
tracking.ColorTracker.neighbours_[width] = neighbours;

return neighbours;
};
Expand Down
119 changes: 113 additions & 6 deletions test/ColorTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,157 @@ module.exports = {
},

testConstructorEmpty: function(test) {
var colors;
var tracker;

test.doesNotThrow(
function() {
new tracking.ColorTracker();
tracker = new tracking.ColorTracker();
}
);

colors = tracker.getColors();
test.equal(1, colors.length, 'Colors array should have a single value');
test.equal('magenta', colors[0], 'Default color is magenta');

test.done();
},

testConstructorString: function(test) {
var colors;
var tracker;

test.doesNotThrow(
function() {
new tracking.ColorTracker('yellow');
tracker = new tracking.ColorTracker('yellow');
}
);

colors = tracker.getColors();
test.equal(1, colors.length, 'Colors array should have a single value');
test.equal('yellow', colors[0], 'The colors array should be set to value in the constructor');

test.throws(
function() {
new tracking.ColorTracker('notvalid');
tracker = new tracking.ColorTracker('notvalid');
}
);

test.done();
},

testConstructorArray: function(test) {
var colors;
var tracker;

test.doesNotThrow(
function() {
new tracking.ColorTracker([]);
tracker = new tracking.ColorTracker([]);
}
);

colors = tracker.getColors();
test.equal(0, colors.length, 'Colors array should be empty');

test.doesNotThrow(
function() {
new tracking.ColorTracker(['magenta', 'cyan', 'yellow']);
tracker = new tracking.ColorTracker(['magenta', 'cyan', 'yellow']);
}
);

colors = tracker.getColors();
test.equal(3, colors.length, 'Colors array have 3 values');
test.equal('magenta', colors[0], 'The colors array should be set to values in the constructor');
test.equal('cyan', colors[1], 'The colors array should be set to values in the constructor');
test.equal('yellow', colors[2], 'The colors array should be set to values in the constructor');

test.throws(
function() {
new tracking.ColorTracker(['magenta', null, 'yellow']);
tracker = new tracking.ColorTracker(['magenta', null, 'yellow']);
}
);

test.done();
},

testFindColor: function(test) {
var colors;
var pixels;
var tracker;

tracking.ColorTracker.registerColor('black', function(r, g, b) {
return r === 0 && g === 0 && b === 0;
});

tracker = new tracking.ColorTracker('black');
colors = tracker.getColors();

test.equal(1, colors.length, 'Colors array have a single value');
test.equal('black', colors[0], 'The colors array should be set to values in the constructor');

tracker.setMinDimension(2);
tracker.setMinGroupSize(6);

pixels = [
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
];

tracker.on('track', function(event) {
test.equal(1, event.data.length, 'There should only be one result rectangle');
test.equal(1, event.data[0].x, 'The first rectangle should be at x = 1');
test.equal(0, event.data[0].y, 'The first rectangle should be at y = 0');
test.equal(2, event.data[0].width, 'The first rectangle\'s width should be 2');
test.equal(3, event.data[0].height, 'The first rectangle\'s height should be 3');

test.done();
});

tracker.track(pixels, 5, 4);
},

testMergedRectangles: function(test) {
var pixels;
var tracker;

tracking.ColorTracker.registerColor('black', function(r, g, b) {
return r === 0 && g === 0 && b === 0;
});

tracker = new tracking.ColorTracker('black');
tracker.setMinDimension(1);
tracker.setMinGroupSize(6);

pixels = [
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0,
0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0
];

tracker.on('track', function(event) {
test.equal(2, event.data.length, 'There should be 2 result rectangles');
test.equal(0, event.data[0].x, 'The first rectangle should be at x = 0');
test.equal(0, event.data[0].y, 'The first rectangle should be at y = 0');
test.equal(5, event.data[0].width, 'The first rectangle\'s width should be 5');
test.equal(6, event.data[0].height, 'The first rectangle\'s height should be 6');
test.equal(2, event.data[1].x, 'The second rectangle should be at x = 2');
test.equal(8, event.data[1].y, 'The second rectangle should be at y = 8');
test.equal(1, event.data[1].width, 'The second rectangle\'s width should be 1');
test.equal(2, event.data[1].height, 'The second rectangle\'s height should be 2');

test.done();
});

tracker.track(pixels, 6, 11);
}
};

0 comments on commit b058fe2

Please sign in to comment.