Skip to content

Commit 883fbf6

Browse files
committed
added simple tests, fixing globals
1 parent e7e0aca commit 883fbf6

File tree

7 files changed

+113
-12
lines changed

7 files changed

+113
-12
lines changed

.jshintrc

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"predef": [
3+
"define",
4+
"exports",
5+
"module",
6+
"describe",
7+
"beforeEach",
8+
"it",
9+
"expect",
10+
"SmartCrop",
11+
"console"
12+
],
13+
"globalstrict": false,
14+
"browser" : true,
15+
"jquery" : true,
16+
"boss" : true,
17+
"eqeqeq": false,
18+
"evil": false,
19+
"eqnull": true,
20+
"forin": false,
21+
"laxbreak": false,
22+
"newcap": false,
23+
"noarg": true,
24+
"noempty": false,
25+
"nonew": false,
26+
"nomen": false,
27+
"plusplus": false,
28+
"regexp": false,
29+
"undef": false,
30+
"sub": true,
31+
"strict": false,
32+
"white": false
33+
}

Gruntfile.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ grunt.initConfig({
1919
livereload: true
2020
},
2121
'static': {
22-
files: ['smartcrop.js', 'examples/*'],
22+
files: ['smartcrop.js', 'examples/*', 'test/*'],
2323
options: {
2424
livereload: true
2525
}
@@ -38,8 +38,12 @@ grunt.initConfig({
3838
dryRun: false
3939
}
4040
}
41+
},
42+
karma: {
43+
unit: {
44+
configFile: 'karma.conf.js'
45+
}
4146
}
42-
4347
});
4448
grunt.registerTask('default', ['connect', 'watch']);
4549
grunt.registerTask('fetchSamples', 'fetch sample images from 500px api', function(){

bower.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,12 @@
2222
"tests",
2323
"Gruntfile.js",
2424
"examples"
25-
]
25+
],
26+
"devDependencies": {
27+
"underscore": "~1.6.0",
28+
"jquery": "~2.1.0",
29+
"jasmine": "~2.0.0",
30+
"mocha": "~1.18.2",
31+
"chai": "~1.9.1"
32+
}
2633
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
"grunt-contrib-watch": "~0.6.1",
1010
"grunt-contrib-connect": "~0.7.1",
1111
"500px": "~0.3.2",
12-
"grunt-rsync": "~0.5.0"
12+
"grunt-rsync": "~0.5.0",
13+
"grunt": "^0.4.4",
14+
"mocha": "^1.18.2",
15+
"chai": "^1.9.1"
1316
},
1417
"license": "MIT",
1518
"repository": {

smartcrop.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2424
*/
2525

26-
// TODO: Penalty for detail/skin outside of viewport, reduce edge!
27-
28-
2926
(function(){
27+
"use strict";
3028

3129
function SmartCrop(options){
3230
this.options = extend({}, SmartCrop.DEFAULTS, options);
@@ -65,7 +63,7 @@ SmartCrop.DEFAULTS = {
6563
};
6664
SmartCrop.crop = function(image, options, callback){
6765
if(options.aspect){
68-
options.width = aspect;
66+
options.width = options.aspect;
6967
options.height = 1;
7068
}
7169

@@ -175,7 +173,7 @@ SmartCrop.prototype = {
175173
for(var y = 0; y < h; y++) {
176174
for(var x = 0; x < w; x++) {
177175
var p = (y*w+x)*4,
178-
lightness = this.cie(id[p], id[p+1], id[p+2])/255;
176+
lightness = this.cie(id[p], id[p+1], id[p+2])/255,
179177
skin = this.skinColor(id[p], id[p+1], id[p+2]);
180178
if(skin > options.skinThreshold && lightness >= options.skinBrightnessMin && lightness <= options.skinBrightnessMax){
181179
od[p] = (skin-options.skinThreshold)*(255/(1-options.skinThreshold));
@@ -197,7 +195,7 @@ SmartCrop.prototype = {
197195
for(var y = 0; y < h; y++) {
198196
for(var x = 0; x < w; x++) {
199197
var p = (y*w+x)*4,
200-
lightness = this.cie(id[p], id[p+1], id[p+2])/255;
198+
lightness = this.cie(id[p], id[p+1], id[p+2])/255,
201199
saturation = this.saturation(id[p], id[p+1], id[p+2]);
202200
if(saturation > options.saturationThreshold && lightness >= options.saturationBrightnessMin && lightness <= options.saturationBrightnessMax){
203201
od[p+2] = (saturation-options.saturationThreshold)*(255/(1-options.saturationThreshold));
@@ -339,7 +337,7 @@ SmartCrop.prototype = {
339337
topCrop = null,
340338
crops = this.crops(image);
341339

342-
for(i = 0, i_len = crops.length; i < i_len; i++) {
340+
for(var i = 0, i_len = crops.length; i < i_len; i++) {
343341
var crop = crops[i];
344342
crop.score = this.score(scoreOutput, crop);
345343
if(crop.score.total > topScore){
@@ -419,7 +417,7 @@ if (typeof define !== 'undefined' && define.amd) define(function(){return SmartC
419417
//common js
420418
if (typeof exports !== 'undefined') exports.SmartCrop = SmartCrop;
421419
// browser
422-
else if (typeof navigator !== 'undefined') this.SmartCrop = SmartCrop;
420+
else if (typeof navigator !== 'undefined') window.SmartCrop = SmartCrop;
423421
// nodejs
424422
if (typeof module !== 'undefined') {
425423
module.exports = SmartCrop;

test/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<html>
2+
<head>
3+
<meta charset="utf-8">
4+
<title>Mocha Tests</title>
5+
<link rel="stylesheet" href="../node_modules/mocha/mocha.css" />
6+
</head>
7+
<body>
8+
<div id="mocha"></div>
9+
<script src="../node_modules/mocha/mocha.js"></script>
10+
<script src="../node_modules/chai/chai.js"></script>
11+
<script src="../smartcrop.js"></script>
12+
<script>
13+
mocha.setup('bdd')
14+
window.expect = chai.expect;
15+
</script>
16+
<script src="smartcrop.js"></script>
17+
<script>
18+
mocha.checkLeaks();
19+
mocha.globals(['jQuery', 'LiveReload', '__screenCapturePageContext__']);
20+
mocha.run();
21+
</script>
22+
</body>
23+
</html>

test/smartcrop.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
(function(){
2+
var KITTY = '/examples/images/flickr/kitty.jpg';
3+
describe("SmartCrop", function() {
4+
var img;
5+
beforeEach(function(done){
6+
img = new Image();
7+
img.src = KITTY;
8+
img.onload = function(){done();};
9+
});
10+
function validResult(result){
11+
expect(result.topCrop.x).to.be.within(0, img.width-result.topCrop.width);
12+
expect(result.topCrop.y).to.be.within(0, img.height-result.topCrop.height);
13+
expect(result.topCrop.width).to.be.within(1, img.width);
14+
expect(result.topCrop.height).to.be.within(1, img.height);
15+
}
16+
describe("crop", function() {
17+
it("should adhere to minScale", function(done) {
18+
SmartCrop.crop(img, {minScale: 1}, function(result){
19+
validResult(result);
20+
expect(result.topCrop.y).to.equal(0);
21+
expect(result.topCrop.height).to.equal(img.height);
22+
done();
23+
});
24+
});
25+
it("should crop the kitty", function(done) {
26+
SmartCrop.crop(img, {}, function(result){
27+
validResult(result);
28+
done();
29+
});
30+
});
31+
});
32+
});
33+
})();

0 commit comments

Comments
 (0)