diff --git a/src/js/utils/MergeUtils.js b/src/js/utils/MergeUtils.js index 8f69e49e..24bd56ad 100644 --- a/src/js/utils/MergeUtils.js +++ b/src/js/utils/MergeUtils.js @@ -12,7 +12,7 @@ * - index: {Number} index where the new frames should be appended * - resize: {String} either "expand" or "keep" * - origin: {String} can be any of the existing AnchorWidget origins. - * - addFrames: {Boolean} true if all the imported frames should be added as new frames. + * - insertMode: {String} either "insert" or "add" * * @return {Piskel} The new Piskel instance created */ diff --git a/test/js/utils/MergeUtilsTest.js b/test/js/utils/MergeUtilsTest.js new file mode 100644 index 00000000..d47a2c84 --- /dev/null +++ b/test/js/utils/MergeUtilsTest.js @@ -0,0 +1,153 @@ +describe("MergeUtils suite", function() { + var B = '#000000'; + var R = '#ff0000'; + var T = Constants.TRANSPARENT_COLOR; + + var createPiskelFromGrid = function (grid, name) { + var frame = pskl.model.Frame.fromPixelGrid(grid); + var layer = pskl.model.Layer.fromFrames("l1", [frame]); + return pskl.model.Piskel.fromLayers([layer], 12, { + name: name || "piskel", + description: "desc" + }); + }; + + /** + * Simple helper to create a monochrome sprite for the provided color, + * number of rows and columns. + */ + var getPiskel = function (color, rows, cols) { + var grid = []; + for (var i = 0 ; i < rows ; i++) { + grid[i] = []; + for (var j = 0 ; j < cols ; j++) { + grid[i][j] = color; + } + } + return createPiskelFromGrid(grid); + }; + + it("merges 2 piskel - insertMode:add same size", function () { + var piskel1 = getPiskel(B, 2, 2); + var piskel2 = getPiskel(R, 2, 2); + + var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, { + index: 0, + resize: "expand", + origin: "TOPLEFT", + insertMode: "add" + }); + + expect(mergedPiskel.getWidth()).toBe(2); + expect(mergedPiskel.getHeight()).toBe(2); + expect(mergedPiskel.getLayers().length).toBe(2); + expect(mergedPiskel.getLayers()[0].getFrames().length).toBe(2); + }); + + it("merges 2 piskel - insertMode:insert same size", function () { + var piskel1 = getPiskel(B, 2, 2); + var piskel2 = getPiskel(R, 2, 2); + + var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, { + index: 0, + resize: "expand", + origin: "TOPLEFT", + insertMode: "insert" + }); + + expect(mergedPiskel.getWidth()).toBe(2); + expect(mergedPiskel.getHeight()).toBe(2); + expect(mergedPiskel.getLayers().length).toBe(2); + expect(mergedPiskel.getLayers()[0].getFrames().length).toBe(1); + }); + + it("merges 2 piskel - resize:expand with bigger imported piskel", function () { + var piskel1 = getPiskel(B, 2, 2); + var piskel2 = getPiskel(R, 4, 4); + + var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, { + index: 0, + resize: "expand", + origin: "TOPLEFT", + insertMode: "insert" + }); + + expect(mergedPiskel.getWidth()).toBe(4); + expect(mergedPiskel.getHeight()).toBe(4); + }); + + it("merges 2 piskel - resize:keep with bigger imported piskel", function () { + var piskel1 = getPiskel(B, 2, 2); + var piskel2 = getPiskel(R, 4, 4); + + var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, { + index: 0, + resize: "keep", + origin: "TOPLEFT", + insertMode: "insert" + }); + + expect(mergedPiskel.getWidth()).toBe(2); + expect(mergedPiskel.getHeight()).toBe(2); + }); + + it("merges 2 piskel - resize:expand with taller but thinner imported piskel", function () { + var piskel1 = getPiskel(B, 2, 2); + var piskel2 = getPiskel(R, 1, 4); + + var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, { + index: 0, + resize: "expand", + origin: "TOPLEFT", + insertMode: "insert" + }); + + expect(mergedPiskel.getWidth()).toBe(2); + expect(mergedPiskel.getHeight()).toBe(4); + }); + + it("merges 2 piskel - resize:expand with wider but shorter imported piskel", function () { + var piskel1 = getPiskel(B, 2, 2); + var piskel2 = getPiskel(R, 4, 1); + + var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, { + index: 0, + resize: "expand", + origin: "TOPLEFT", + insertMode: "insert" + }); + + expect(mergedPiskel.getWidth()).toBe(4); + expect(mergedPiskel.getHeight()).toBe(2); + }); + + it("merges 2 piskel - resize:expand with bigger original piskel", function () { + var piskel1 = getPiskel(B, 3, 3); + var piskel2 = getPiskel(R, 1, 1); + + var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, { + index: 0, + resize: "expand", + origin: "TOPLEFT", + insertMode: "insert" + }); + + expect(mergedPiskel.getWidth()).toBe(3); + expect(mergedPiskel.getHeight()).toBe(3); + }); + + it("merges 2 piskel - resize:keep with bigger original piskel", function () { + var piskel1 = getPiskel(B, 3, 3); + var piskel2 = getPiskel(R, 1, 1); + + var mergedPiskel = pskl.utils.MergeUtils.merge(piskel1, piskel2, { + index: 0, + resize: "keep", + origin: "TOPLEFT", + insertMode: "insert" + }); + + expect(mergedPiskel.getWidth()).toBe(3); + expect(mergedPiskel.getHeight()).toBe(3); + }); +});