From 8292112ecd16cb827b9a8413b1db9b4f4eb672d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Mon, 8 Apr 2013 07:07:41 -0700 Subject: [PATCH] More work on Color and testing. --- src/color/Color.js | 2 +- test/lib/helpers.js | 49 +++++++++++++++++++++++++------------------- test/tests/Color.js | 12 +++++------ test/tests/Item.js | 8 ++++---- test/tests/Raster.js | 14 ++++++------- 5 files changed, 46 insertions(+), 39 deletions(-) diff --git a/src/color/Color.js b/src/color/Color.js index 794f4417..d5c1903e 100644 --- a/src/color/Color.js +++ b/src/color/Color.js @@ -221,7 +221,7 @@ var Color = this.Color = Base.extend(new function() { initialize: function(arg) { // We are storing color internally as an array of components - var argType = arg && typeof arg, + var argType = arg != null && typeof arg, type, components = argType === 'number' ? arguments diff --git a/test/lib/helpers.js b/test/lib/helpers.js index 60b1b11f..6c8dc8ec 100644 --- a/test/lib/helpers.js +++ b/test/lib/helpers.js @@ -60,32 +60,36 @@ function asyncTest(testName, expected) { } function compareNumbers(number1, number2, message) { - equals(Format.number(number1, 5), Format.number(number2, 5), message); + equals(Format.number(number1), Format.number(number2), message); +} + +function compareArrays(array1, array2, message) { + function format(array) { + return Base.each(array, function(value, index) { + this[index] = Format.number(value); + }, []).toString(); + } + equals(format(array1), format(array2), message); } function comparePoints(point1, point2, message) { - compareNumbers(point1.x, point2.x, - (message || '') + ' x'); - compareNumbers(point1.y, point2.y, - (message || '') + ' y'); + compareNumbers(point1.x, point2.x, (message || '') + ' x'); + compareNumbers(point1.y, point2.y, (message || '') + ' y'); } function compareRectangles(rect1, rect2, message) { - compareNumbers(rect1.x, rect2.x, - (message || '') + ' x'); - compareNumbers(rect1.y, rect2.y, - (message || '') + ' y'); - compareNumbers(rect1.width, rect2.width, - (message || '') + ' width'); - compareNumbers(rect1.height, rect2.height, - (message || '') + ' height'); + compareNumbers(rect1.x, rect2.x, (message || '') + ' x'); + compareNumbers(rect1.y, rect2.y, (message || '') + ' y'); + compareNumbers(rect1.width, rect2.width, (message || '') + ' width'); + compareNumbers(rect1.height, rect2.height, (message || '') + ' height'); } function compareColors(color1, color2, message) { color1 = new Color(color1); color2 = new Color(color2); equals(color1.type, color2.type, (message || '') + ' type'); - equals(color1.components.toString(), color2.components.toString(), (message || '') + ' components'); + compareArrays(color1.components, color2.components, + (message || '') + ' components'); } function compareGradientColors(gradientColor, gradientColor2, checkIdentity) { @@ -95,8 +99,9 @@ function compareGradientColors(gradientColor, gradientColor2, checkIdentity) { return gradientColor[key] !== gradientColor2[key]; }, true, 'Strict compare GradientColor#' + key); } - equals(gradientColor[key] && gradientColor[key].toString(), gradientColor2[key] && gradientColor2[key].toString(), - 'Compare GradientColor#' + key); + equals(gradientColor[key] && gradientColor[key].toString(), + gradientColor2[key] && gradientColor2[key].toString(), + 'Compare GradientColor#' + key); }); equals(function() { return gradientColor.gradient.equals(gradientColor2.gradient); @@ -125,7 +130,8 @@ function comparePathStyles(style, style2, checkIdentity) { } compareGradientColors(style[key], style2[key], checkIdentity); } else { - equals(style[key] && style[key].toString(), style2[key] && style2[key].toString(), + equals(style[key] && style[key].toString(), + style2[key] && style2[key].toString(), 'Compare PathStyle#' + key); } } @@ -141,7 +147,7 @@ function comparePathStyles(style, style2, checkIdentity) { }); if (style.dashArray) { - equals(style.dashArray.toString(), style2.dashArray.toString(), + compareArrays(style.dashArray, style2.dashArray, 'Compare CharacterStyle#dashArray'); } } @@ -250,7 +256,7 @@ function compareItems(item, item2, cloned, checkIdentity, dontShareProject) { 'Compare Item#position'); equals(function() { - return Base.equals(item.data, item2.data) + return Base.equals(item.data, item2.data); }, true); if (item.matrix) { @@ -319,7 +325,8 @@ function compareItems(item, item2, cloned, checkIdentity, dontShareProject) { } equals(item.size.toString(), item2.size.toString(), 'Compare Raster#size'); - equals(item.toDataURL() == item2.toDataURL(), true, 'Compare Raster#toDataUrl()') + equals(item.toDataURL() == item2.toDataURL(), true, + 'Compare Raster#toDataUrl()'); } // TextItem specific: @@ -371,7 +378,7 @@ function compareProjects(project, project2) { // Compare Project#layers: equals(function() { - return project.layers.length == project2.layers.length + return project.layers.length == project2.layers.length; }, true); for (var i = 0, l = project.layers.length; i < l; i++) { compareItems(project.layers[i], project2.layers[i], false, false, true); diff --git a/test/tests/Color.js b/test/tests/Color.js index c5a2de9a..1e80fed8 100644 --- a/test/tests/Color.js +++ b/test/tests/Color.js @@ -59,7 +59,7 @@ test('Creating colors', function() { new Color(1, 0, 1), 'Color from rgb object literal'); compareColors(new Color({ gray: 0.2 }), - new Color(0.8, 0.8, 0.8).convert('gray'), 'Color from gray object literal'); + new Color(0.2), 'Color from gray object literal'); compareColors(new Color({ hue: 0, saturation: 1, brightness: 1}), new Color(1, 0, 0).convert('hsb'), 'Color from hsb object literal'); @@ -73,15 +73,15 @@ test('Creating colors', function() { test('Get gray from RGB Color', function() { var color = new Color(1, 0.5, 0.2); - compareNumbers(color.gray, 0.38458251953125); + compareNumbers(color.gray, 0.3848); var color = new Color(0.5, 0.2, 0.1); - compareNumbers(color.gray, 0.72137451171875); + compareNumbers(color.gray, 0.72175); }); test('Get gray from HSB Color', function() { var color = new HsbColor(0, 0, 0.2); - compareNumbers(color.gray, 0.8); + compareNumbers(color.gray, 0.80002); }); test('Get red from HSB Color', function() { @@ -115,7 +115,7 @@ test('Gray Color', function() { test('Converting Colors', function() { var rgbColor = new Color(1, 0.5, 0.2); - compareNumbers(rgbColor.gray, 0.38299560546875); + compareNumbers(rgbColor.gray, 0.3848); var grayColor = new Color(0.2); compareColors(grayColor.convert('rgb'), new Color(0.8, 0.8, 0.8)); compareColors(grayColor.convert('hsb'), { hue: 0, saturation: 0, brightness: 0.8 }); @@ -131,7 +131,7 @@ test('Setting Color#gray', function() { test('Setting Color#red', function() { var color = new Color({ hue: 180, saturation: 0, brightness: 0 }); color.red = 1; - compareColors(color, new Color(0, 1, 1)); + compareColors(color, new Color(1, 0, 0)); }); test('Setting Color#gray', function() { diff --git a/test/tests/Item.js b/test/tests/Item.js index 2c5a408a..33920d78 100644 --- a/test/tests/Item.js +++ b/test/tests/Item.js @@ -641,9 +641,9 @@ test('Item#blendMode in a transformed Group', function() { }); var raster = layer.rasterize(); - compareColors(raster.getPixel(0, 0), new Color(1, 0, 0), + compareColors(raster.getPixel(0, 0), new Color(1, 0, 0, 1), 'Top left pixel should be red:'); - compareColors(raster.getPixel(50, 50), new Color(1, 1, 0), + compareColors(raster.getPixel(50, 50), new Color(1, 1, 0, 1), 'Middle center pixel should be yellow:'); raster.remove(); @@ -653,8 +653,8 @@ test('Item#blendMode in a transformed Group', function() { group.position = [50, 50]; var raster = layer.rasterize(); - compareColors(raster.getPixel(0, 0), new Color(1, 0, 0), + compareColors(raster.getPixel(0, 0), new Color(1, 0, 0, 1), 'Top left pixel should be red:'); - compareColors(raster.getPixel(50, 50), new Color(1, 1, 0), + compareColors(raster.getPixel(50, 50), new Color(1, 1, 0, 1), 'Middle center pixel should be yellow:'); }); diff --git a/test/tests/Raster.js b/test/tests/Raster.js index e2d1e420..772e42a4 100644 --- a/test/tests/Raster.js +++ b/test/tests/Raster.js @@ -78,12 +78,12 @@ asyncTest('Create a raster from a dom id', function(callback) { asyncTest('Raster#getPixel / setPixel', function(callback) { var raster = new Raster('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAIAAAD91JpzAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAABlJREFUeNpi+s/AwPCfgYmR4f9/hv8AAQYAHiAFAS8Lwy8AAAAASUVORK5CYII='); raster.onLoad = function() { - compareColors(raster.getPixel(0, 0), new Color(1, 0, 0)); - compareColors(raster.getPixel(1, 0), new Color(0, 1, 0)); - compareColors(raster.getPixel(0, 1), new Color(0, 0, 1)); - compareColors(raster.getPixel(1, 1), new Color(1, 1, 1)); + compareColors(raster.getPixel(0, 0), new Color(1, 0, 0, 1)); + compareColors(raster.getPixel(1, 0), new Color(0, 1, 0, 1)); + compareColors(raster.getPixel(0, 1), new Color(0, 0, 1, 1)); + compareColors(raster.getPixel(1, 1), new Color(1, 1, 1, 1)); - var color = new Color(1, 1, 0, 0.5); + var color = new Color(1, 1, 0, 0.5, 1); raster.setPixel([0, 0], color); compareColors(raster.getPixel([0, 0]), color); callback(); @@ -130,7 +130,7 @@ test('Raster#getAverageColor(path)', function() { }); var raster = paper.project.activeLayer.rasterize(); path.scale(0.9); - compareColors(raster.getAverageColor(path), new Color(1, 0, 0)); + compareColors(raster.getAverageColor(path), new Color(1, 0, 0, 1)); }); test('Raster#getAverageColor(path) with compound path', function() { @@ -152,5 +152,5 @@ test('Raster#getAverageColor(path) with compound path', function() { var raster = paper.project.activeLayer.rasterize(); path.scale(0.9); path2.scale(1.1); - compareColors(raster.getAverageColor(compoundPath), new Color(1, 0, 0)); + compareColors(raster.getAverageColor(compoundPath), new Color(1, 0, 0, 1)); });