From 4950e3562758329701d86de05c31310669f438e5 Mon Sep 17 00:00:00 2001 From: kangax Date: Mon, 25 Nov 2013 16:39:50 +0100 Subject: [PATCH] Add remaining i-text unit tests --- test/unit/itext.js | 151 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 131 insertions(+), 20 deletions(-) diff --git a/test/unit/itext.js b/test/unit/itext.js index f7ed2c8d..986bd7c0 100644 --- a/test/unit/itext.js +++ b/test/unit/itext.js @@ -279,26 +279,6 @@ equal(iText.selectionEnd, 20); }); - // test('getSelectionStyles', function() { - // // TODO: - // }); - - // test('setSelectionStyles', function() { - // // TODO: - // }); - - // test('getCurrentCharFontSize', function() { - // // TODO: - // }); - - // test('getCurrentCharColor', function() { - // // TODO: - // }); - - // test('toSVG', function() { - // // TODO: - // }); - test('findWordBoundaryLeft', function() { var iText = new fabric.IText('test foo bar-baz\nqux'); @@ -352,4 +332,135 @@ equal(iText.getNumNewLinesInSelectedText(), 1); }); + test('getSelectionStyles', function() { + var iText = new fabric.IText('test foo bar-baz\nqux', { + styles: { + 0: { + 0: { textDecoration: 'underline' }, + 2: { textDecoration: 'overline' }, + 4: { textBackgroundColor: '#ffc' } + }, + 1: { + 0: { fill: 'red' }, + 1: { fill: 'green' }, + 2: { fill: 'blue' } + } + } + }); + + equal(typeof iText.getSelectionStyles, 'function'); + + iText.selectionStart = 0; + iText.selectionEnd = 0; + + deepEqual(iText.getSelectionStyles(), { + textDecoration: 'underline' + }); + + iText.selectionStart = 2; + iText.selectionEnd = 2; + + deepEqual(iText.getSelectionStyles(), { + textDecoration: 'overline' + }); + + iText.selectionStart = 17; + iText.selectionStart = 17; + + deepEqual(iText.getSelectionStyles(), { + fill: 'red' + }); + }); + + test('setSelectionStyles', function() { + var iText = new fabric.IText('test foo bar-baz\nqux', { + styles: { + 0: { + 0: { fill: '#112233' }, + 2: { stroke: '#223344' } + } + } + }); + + equal(typeof iText.setSelectionStyles, 'function'); + + iText.setSelectionStyles({ + fill: 'red', + stroke: 'yellow' + }); + + deepEqual(iText.styles[0][0], { + fill: 'red', + stroke: 'yellow' + }); + + iText.selectionStart = 2; + iText.selectionEnd = 2; + + iText.setSelectionStyles({ + fill: '#998877', + stroke: 'yellow' + }); + + deepEqual(iText.styles[0][2], { + fill: '#998877', + stroke: 'yellow' + }); + }); + + test('getCurrentCharFontSize', function() { + var iText = new fabric.IText('test foo bar-baz\nqux', { + styles: { + 0: { + 0: { fontSize: 20 }, + 1: { fontSize: 60 } + } + } + }); + + equal(typeof iText.getCurrentCharFontSize, 'function'); + + equal(iText.getCurrentCharFontSize(0, 0), 20); + equal(iText.getCurrentCharFontSize(0, 1), 20); + equal(iText.getCurrentCharFontSize(0, 2), 60); + equal(iText.getCurrentCharFontSize(1, 0), 40); + }); + + test('getCurrentCharColor', function() { + var iText = new fabric.IText('test foo bar-baz\nqux', { + styles: { + 0: { + 0: { fill: 'red' }, + 1: { fill: 'green' } + } + } + }); + + equal(typeof iText.getCurrentCharColor, 'function'); + + equal(iText.getCurrentCharColor(0, 0), 'red'); + equal(iText.getCurrentCharColor(0, 1), 'red'); + equal(iText.getCurrentCharColor(0, 2), 'green'); + + // or cursor color + equal(iText.getCurrentCharColor(1, 0), '#333'); + }); + + test('toSVG', function() { + var iText = new fabric.IText('test foo bar-baz\nqux', { + styles: { + 0: { + 0: { fill: '#112233' }, + 2: { stroke: '#223344' } + } + } + }); + + equal(typeof iText.toSVG, 'function'); + + equal(iText.toSVG(), 'test foo bar-bazqux'); + + // TODO: more SVG tests here? + }); + })();