From ba491736c1faa34e55618a229050481a79eea8e0 Mon Sep 17 00:00:00 2001 From: juliandescottes Date: Mon, 13 Apr 2015 13:02:12 +0200 Subject: [PATCH] test : add unit test for ColorUtils --- .../exportimage/GifExportController.js | 11 ++++--- src/js/utils/ColorUtils.js | 16 +++++----- test/js/utils/ColorUtilsTest.js | 29 +++++++++++++++++++ 3 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 test/js/utils/ColorUtilsTest.js diff --git a/src/js/controller/settings/exportimage/GifExportController.js b/src/js/controller/settings/exportimage/GifExportController.js index d1793b21..ead09a61 100644 --- a/src/js/controller/settings/exportimage/GifExportController.js +++ b/src/js/controller/settings/exportimage/GifExportController.js @@ -117,10 +117,10 @@ ns.GifExportController.prototype.renderAsImageDataAnimatedGIF = function(zoom, fps, cb) { var currentColors = pskl.app.currentColorsService.computeCurrentColors(); - + var preserveColors = currentColors.length < MAX_GIF_COLORS; var transparentColor = this.getTransparentColor(currentColors); - + var gif = new window.GIF({ workers: 5, quality: 1, @@ -155,17 +155,16 @@ ns.GifExportController.prototype.getTransparentColor = function(currentColors) { var transparentColor = pskl.utils.ColorUtils.getUnusedColor(currentColors); + if (!transparentColor) { console.error('Unable to find unused color to use as transparent color in the current sprite'); transparentColor = MAGIC_PINK; - } else { - transparentColor = window.tinycolor(transparentColor).toHexString(); } + return transparentColor; }; - // FIXME : HORRIBLE COPY/PASTA - + // FIXME : JD : HORRIBLE COPY/PASTA (JD later : where???) ns.GifExportController.prototype.updateStatus_ = function (imageUrl, error) { if (imageUrl) { var linkTpl = "{{shortLink}}"; diff --git a/src/js/utils/ColorUtils.js b/src/js/utils/ColorUtils.js index a1635fe3..34ac60f3 100644 --- a/src/js/utils/ColorUtils.js +++ b/src/js/utils/ColorUtils.js @@ -3,25 +3,25 @@ ns.ColorUtils = { getUnusedColor : function (usedColors) { - // start with white - var color = { - r : 255, - g : 255, - b : 255 - }; - + usedColors = usedColors || []; // create check map var colorMap = {}; usedColors.forEach(function (color) { colorMap[color.toUpperCase()] = true; }); + // start with white + var color = { + r : 255, + g : 255, + b : 255 + }; var match = null; while (true) { var hex = tinycolor(color).toHexString().toUpperCase(); if (!colorMap[hex]) { - match = color; + match = hex; break; } else { // pick a non null component to decrease its value diff --git a/test/js/utils/ColorUtilsTest.js b/test/js/utils/ColorUtilsTest.js new file mode 100644 index 00000000..bcc68e1c --- /dev/null +++ b/test/js/utils/ColorUtilsTest.js @@ -0,0 +1,29 @@ +describe("Color utils", function() { + + beforeEach(function() {}); + afterEach(function() {}); + + it("returns a color when provided with array of colors", function() { + // when/then + var unusedColor = pskl.utils.ColorUtils.getUnusedColor(['#ffffff', '#feffff', '#fdffff']); + // verify + expect(unusedColor).toBe('#FCFFFF'); + + // when/then + unusedColor = pskl.utils.ColorUtils.getUnusedColor(['#fcffff', '#feffff', '#fdffff']); + // verify + expect(unusedColor).toBe('#FFFFFF'); + }); + + it("returns a color for an empty array", function() { + // when/then + var unusedColor = pskl.utils.ColorUtils.getUnusedColor([]); + // verify + expect(unusedColor).toBe('#FFFFFF'); + + // when/then + unusedColor = pskl.utils.ColorUtils.getUnusedColor(); + // verify + expect(unusedColor).toBe('#FFFFFF'); + }); +}); \ No newline at end of file