diff --git a/test.js b/test.js index c8cbda5b..f1bafa2c 100644 --- a/test.js +++ b/test.js @@ -25,7 +25,8 @@ testrunner.run({ './test/unit/parser.js', './test/unit/canvas.js', './test/unit/canvas_static.js', - './test/unit/gradient.js' + './test/unit/gradient.js', + './test/unit/pattern.js' ] }, function(err, report) { if(report.failed > 0){ diff --git a/test/fixtures/greyfloral.png b/test/fixtures/greyfloral.png new file mode 100644 index 00000000..91525ccd Binary files /dev/null and b/test/fixtures/greyfloral.png differ diff --git a/test/unit/pattern.js b/test/unit/pattern.js new file mode 100644 index 00000000..035d1b14 --- /dev/null +++ b/test/unit/pattern.js @@ -0,0 +1,77 @@ +(function() { + + function createImageElement() { + return fabric.isLikelyNode ? new (require('canvas').Image) : fabric.document.createElement('img'); + } + function setSrc(img, src, callback) { + if (fabric.isLikelyNode) { + require('fs').readFile(src, function(err, imgData) { + if (err) throw err; + img.src = imgData; + img._src = src; + callback && callback(); + }); + } + else { + img.src = src; + callback && callback(); + } + } + + QUnit.module('fabric.Pattern'); + + var img = createImageElement(); + setSrc(img, fabric.isLikelyNode ? + (__dirname + '/../fixtures/greyfloral.png') + : '../fixtures/greyfloral.png'); + + function createPattern() { + return new fabric.Pattern({ + source: img + }); + } + + test('constructor', function() { + ok(fabric.Pattern); + + var pattern = createPattern(); + ok(pattern instanceof fabric.Pattern, 'should inherit from fabric.Pattern'); + }); + + test('properties', function() { + var pattern = createPattern(); + + equal(pattern.source, img); + equal(pattern.repeat, 'repeat'); + }); + + test('toObject', function() { + var pattern = createPattern(); + + ok(typeof pattern.toObject == 'function'); + + var object = pattern.toObject(); + + // node-canvas doesn't give "src" + if (img.src) { + equal(object.source, '../fixtures/greyfloral.png'); + } + equal(object.repeat, 'repeat'); + + var sourceExecuted; + var patternWithGetSource = new fabric.Pattern({ + source: function() {return fabric.document.createElement("canvas")} + }); + + var object2 = patternWithGetSource.toObject(); + equal(object2.source, 'return fabric.document.createElement("canvas")'); + equal(object2.repeat, 'repeat'); + }); + + test('toLive', function() { + var pattern = createPattern(); + + ok(typeof pattern.toLive == 'function'); + }); + +})(); \ No newline at end of file