From a61b38468401878259b7cdd9b5a36d8a78893682 Mon Sep 17 00:00:00 2001 From: Kienz Date: Fri, 9 Aug 2013 20:37:56 +0200 Subject: [PATCH] Add toJSON back to image filters Add toJSON to unit tests --- src/filters/brightness_filter.class.js | 9 ++ src/filters/convolute_filter.class.js | 9 ++ .../gradienttransparency_filter.class.js | 9 ++ src/filters/grayscale_filter.class.js | 9 ++ src/filters/invert_filter.class.js | 9 ++ src/filters/noise_filter.class.js | 9 ++ src/filters/pixelate_filter.class.js | 9 ++ src/filters/removewhite_filter.class.js | 9 ++ src/filters/sepia2_filter.class.js | 9 ++ src/filters/sepia_filter.class.js | 9 ++ src/filters/tint_filter.class.js | 9 ++ test/unit/image_filters.js | 88 +++++++++++++++++++ 12 files changed, 187 insertions(+) diff --git a/src/filters/brightness_filter.class.js b/src/filters/brightness_filter.class.js index 8e59ef61..70a21c65 100644 --- a/src/filters/brightness_filter.class.js +++ b/src/filters/brightness_filter.class.js @@ -50,6 +50,15 @@ fabric.Image.filters.Brightness = fabric.util.createClass(/** @lends fabric.Imag type: this.type, brightness: this.brightness }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/convolute_filter.class.js b/src/filters/convolute_filter.class.js index 4a8ffd36..f8de639e 100644 --- a/src/filters/convolute_filter.class.js +++ b/src/filters/convolute_filter.class.js @@ -102,6 +102,15 @@ fabric.Image.filters.Convolute = fabric.util.createClass(/** @lends fabric.Image opaque: this.opaque, matrix: this.matrix }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/gradienttransparency_filter.class.js b/src/filters/gradienttransparency_filter.class.js index 882cbbee..bdf625c9 100644 --- a/src/filters/gradienttransparency_filter.class.js +++ b/src/filters/gradienttransparency_filter.class.js @@ -49,6 +49,15 @@ fabric.Image.filters.GradientTransparency = fabric.util.createClass(/** @lends f type: this.type, threshold: this.threshold }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/grayscale_filter.class.js b/src/filters/grayscale_filter.class.js index 6f3b528e..4850b827 100644 --- a/src/filters/grayscale_filter.class.js +++ b/src/filters/grayscale_filter.class.js @@ -42,6 +42,15 @@ fabric.Image.filters.Grayscale = fabric.util.createClass(/** @lends fabric.Image */ toObject: function() { return { type: this.type }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/invert_filter.class.js b/src/filters/invert_filter.class.js index 202ca90d..dfc30f8f 100644 --- a/src/filters/invert_filter.class.js +++ b/src/filters/invert_filter.class.js @@ -38,6 +38,15 @@ fabric.Image.filters.Invert = fabric.util.createClass(/** @lends fabric.Image.fi */ toObject: function() { return { type: this.type }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/noise_filter.class.js b/src/filters/noise_filter.class.js index 41db45f4..ef7dc0af 100644 --- a/src/filters/noise_filter.class.js +++ b/src/filters/noise_filter.class.js @@ -53,6 +53,15 @@ fabric.Image.filters.Noise = fabric.util.createClass(/** @lends fabric.Image.fil type: this.type, noise: this.noise }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/pixelate_filter.class.js b/src/filters/pixelate_filter.class.js index d8101bda..b282d56d 100644 --- a/src/filters/pixelate_filter.class.js +++ b/src/filters/pixelate_filter.class.js @@ -78,6 +78,15 @@ fabric.Image.filters.Pixelate = fabric.util.createClass(/** @lends fabric.Image. type: this.type, blocksize: this.blocksize }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/removewhite_filter.class.js b/src/filters/removewhite_filter.class.js index 0703e2a6..43f090e1 100644 --- a/src/filters/removewhite_filter.class.js +++ b/src/filters/removewhite_filter.class.js @@ -66,6 +66,15 @@ fabric.Image.filters.RemoveWhite = fabric.util.createClass(/** @lends fabric.Ima threshold: this.threshold, distance: this.distance }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/sepia2_filter.class.js b/src/filters/sepia2_filter.class.js index 388769b5..03eca1b5 100644 --- a/src/filters/sepia2_filter.class.js +++ b/src/filters/sepia2_filter.class.js @@ -42,6 +42,15 @@ fabric.Image.filters.Sepia2 = fabric.util.createClass(/** @lends fabric.Image.fi */ toObject: function() { return { type: this.type }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/sepia_filter.class.js b/src/filters/sepia_filter.class.js index c6fb2c4a..ff182e52 100644 --- a/src/filters/sepia_filter.class.js +++ b/src/filters/sepia_filter.class.js @@ -39,6 +39,15 @@ fabric.Image.filters.Sepia = fabric.util.createClass(/** @lends fabric.Image.fil */ toObject: function() { return { type: this.type }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/src/filters/tint_filter.class.js b/src/filters/tint_filter.class.js index 21a08849..c231b455 100644 --- a/src/filters/tint_filter.class.js +++ b/src/filters/tint_filter.class.js @@ -60,6 +60,15 @@ fabric.Image.filters.Tint = fabric.util.createClass(/** @lends fabric.Image.filt type: this.type, color: this.color }; + }, + + /** + * Returns a JSON representation of an instance + * @return {Object} JSON + */ + toJSON: function() { + // delegate, not alias + return this.toObject(); } }); diff --git a/test/unit/image_filters.js b/test/unit/image_filters.js index 85045d16..b3af5118 100644 --- a/test/unit/image_filters.js +++ b/test/unit/image_filters.js @@ -32,6 +32,14 @@ equal(JSON.stringify(object), '{"type":"Brightness","brightness":100}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.Brightness(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"Brightness","brightness":100}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.Brightness(); @@ -73,6 +81,14 @@ equal(JSON.stringify(object), '{"type":"Convolute","opaque":1,"matrix":[0,0,0,0,1,0,0,0,0]}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.Convolute({opaque: 1}); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"Convolute","opaque":1,"matrix":[0,0,0,0,1,0,0,0,0]}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.Convolute(); @@ -111,6 +127,14 @@ equal(JSON.stringify(object), '{"type":"GradientTransparency","threshold":100}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.GradientTransparency(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"GradientTransparency","threshold":100}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.GradientTransparency(); @@ -146,6 +170,14 @@ equal(JSON.stringify(object), '{"type":"Grayscale"}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.Grayscale(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"Grayscale"}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.Grayscale(); @@ -181,6 +213,14 @@ equal(JSON.stringify(object), '{"type":"Invert"}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.Invert(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"Invert"}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.Invert(); @@ -220,6 +260,14 @@ equal(JSON.stringify(object), '{"type":"Noise","noise":100}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.Noise(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"Noise","noise":100}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.Noise(); @@ -259,6 +307,14 @@ equal(JSON.stringify(object), '{"type":"Pixelate","blocksize":4}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.Pixelate(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"Pixelate","blocksize":4}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.Pixelate(); @@ -300,6 +356,14 @@ equal(JSON.stringify(object), '{"type":"RemoveWhite","threshold":30,"distance":20}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.RemoveWhite(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"RemoveWhite","threshold":30,"distance":20}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.RemoveWhite(); @@ -335,6 +399,14 @@ equal(JSON.stringify(object), '{"type":"Sepia2"}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.Sepia2(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"Sepia2"}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.Sepia2(); @@ -370,6 +442,14 @@ equal(JSON.stringify(object), '{"type":"Sepia"}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.Sepia(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"Sepia"}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.Sepia(); @@ -409,6 +489,14 @@ equal(JSON.stringify(object), '{"type":"Tint","color":0}'); }); + test('toJSON', function() { + var filter = new fabric.Image.filters.Tint(); + ok(typeof filter.toJSON == 'function'); + + var json = filter.toJSON(); + equal(JSON.stringify(json), '{"type":"Tint","color":0}'); + }); + test('fromObject', function() { var filter = new fabric.Image.filters.Tint();