Debian Codegraph @

closure-library/closure/goog/editor/plugins/tableeditor_test.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.editor.plugins.TableEditorTest');
16 goog.setTestOnly('goog.editor.plugins.TableEditorTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.Range');
20 goog.require('goog.dom.TagName');
21 goog.require('goog.editor.plugins.TableEditor');
22 goog.require('goog.object');
23 goog.require('goog.string');
24 goog.require('goog.testing.ExpectedFailures');
25 goog.require('goog.testing.JsUnitException');
26 goog.require('goog.testing.TestCase');
27 goog.require('goog.testing.editor.FieldMock');
28 goog.require('goog.testing.editor.TestHelper');
29 goog.require('goog.testing.jsunit');
30 goog.require('goog.userAgent');
31
32 var field;
33 var plugin;
34 var fieldMock;
35 var expectedFailures;
36 var testHelper;
37
38 function setUpPage() {
39 field = goog.dom.getElement('field');
40 expectedFailures = new goog.testing.ExpectedFailures();
41 }
42
43 function setUp() {
44 // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
45 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
46
47 testHelper = new goog.testing.editor.TestHelper(goog.dom.getElement('field'));
48 testHelper.setUpEditableElement();
49 field.focus();
50 plugin = new goog.editor.plugins.TableEditor();
51 fieldMock = new goog.testing.editor.FieldMock();
52 plugin.registerFieldObject(fieldMock);
53 if (goog.userAgent.IE &&
54 (goog.userAgent.compare(goog.userAgent.VERSION, '7.0') >= 0)) {
55 goog.testing.TestCase.protectedTimeout_ = window.setTimeout;
56 }
57 }
58
59 function tearDown() {
60 testHelper.tearDownEditableElement();
61 expectedFailures.handleTearDown();
62 }
63
64 function testEnable() {
65 fieldMock.$replay();
66
67 plugin.enable(fieldMock);
68 assertTrue('Plugin should be enabled', plugin.isEnabled(fieldMock));
69
70 if (goog.userAgent.GECKO) {
71 // This code path is executed only for GECKO browsers but we can't
72 // verify it because of a GECKO bug while reading the value of the
73 // command "enableObjectResizing".
74 // See https://bugzilla.mozilla.org/show_bug.cgi?id=506368
75 expectedFailures.expectFailureFor(goog.userAgent.GECKO);
76 try {
77 var doc = plugin.getFieldDomHelper().getDocument();
78 assertTrue(
79 'Object resizing should be enabled',
80 doc.queryCommandValue('enableObjectResizing'));
81 } catch (e) {
82 // We need to marshal our exception in order for it to be handled
83 // properly.
84 expectedFailures.handleException(new goog.testing.JsUnitException(e));
85 }
86 }
87 fieldMock.$verify();
88 }
89
90 function testIsSupportedCommand() {
91 goog.object.forEach(
92 goog.editor.plugins.TableEditor.COMMAND, function(command) {
93 assertTrue(
94 goog.string.subs('Plugin should support %s', command),
95 plugin.isSupportedCommand(command));
96 });
97 assertFalse(
98 'Plugin shouldn\'t support a bogus command',
99 plugin.isSupportedCommand('+fable'));
100 }
101
102 function testCreateTable() {
103 fieldMock.$replay();
104 createTableAndSelectCell();
105 var table = plugin.getCurrentTable_();
106 assertNotNull('Table should not be null', table);
107 assertEquals(
108 'Table should have the default number of rows', 2, table.rows.length);
109 assertEquals(
110 'Table should have the default number of cells', 8, getCellCount(table));
111 fieldMock.$verify();
112 }
113
114 function testInsertRowBefore() {
115 fieldMock.$replay();
116 createTableAndSelectCell();
117 var table = plugin.getCurrentTable_();
118 var selectedRow = fieldMock.getRange().getContainerElement().parentNode;
119 assertNull(
120 'Selected row shouldn\'t have a previous sibling',
121 selectedRow.previousSibling);
122 assertEquals('Table should have two rows', 2, table.rows.length);
123 plugin.execCommandInternal(
124 goog.editor.plugins.TableEditor.COMMAND.INSERT_ROW_BEFORE);
125 assertEquals('A row should have been inserted', 3, table.rows.length);
126
127 // Assert that we inserted a row above the currently selected row.
128 assertNotNull(
129 'Selected row should have a previous sibling',
130 selectedRow.previousSibling);
131 fieldMock.$verify();
132 }
133
134 function testInsertRowAfter() {
135 fieldMock.$replay();
136 createTableAndSelectCell({width: 2, height: 1});
137 var selectedRow = fieldMock.getRange().getContainerElement().parentNode;
138 var table = plugin.getCurrentTable_();
139 assertEquals('Table should have one row', 1, table.rows.length);
140 assertNull(
141 'Selected row shouldn\'t have a next sibling', selectedRow.nextSibling);
142 plugin.execCommandInternal(
143 goog.editor.plugins.TableEditor.COMMAND.INSERT_ROW_AFTER);
144 assertEquals('A row should have been inserted', 2, table.rows.length);
145 // Assert that we inserted a row after the currently selected row.
146 assertNotNull(
147 'Selected row should have a next sibling', selectedRow.nextSibling);
148 fieldMock.$verify();
149 }
150
151 function testInsertColumnBefore() {
152 fieldMock.$replay();
153 createTableAndSelectCell({width: 1, height: 1});
154 var table = plugin.getCurrentTable_();
155 var selectedCell = fieldMock.getRange().getContainerElement();
156 assertEquals('Table should have one cell', 1, getCellCount(table));
157 assertNull(
158 'Selected cell shouldn\'t have a previous sibling',
159 selectedCell.previousSibling);
160 plugin.execCommandInternal(
161 goog.editor.plugins.TableEditor.COMMAND.INSERT_COLUMN_BEFORE);
162 assertEquals('A cell should have been inserted', 2, getCellCount(table));
163 assertNotNull(
164 'Selected cell should have a previous sibling',
165 selectedCell.previousSibling);
166 fieldMock.$verify();
167 }
168
169 function testInsertColumnAfter() {
170 fieldMock.$replay();
171 createTableAndSelectCell({width: 1, height: 1});
172 var table = plugin.getCurrentTable_();
173 var selectedCell = fieldMock.getRange().getContainerElement();
174 assertEquals('Table should have one cell', 1, getCellCount(table));
175 assertNull(
176 'Selected cell shouldn\'t have a next sibling', selectedCell.nextSibling);
177 plugin.execCommandInternal(
178 goog.editor.plugins.TableEditor.COMMAND.INSERT_COLUMN_AFTER);
179 assertEquals('A cell should have been inserted', 2, getCellCount(table));
180 assertNotNull(
181 'Selected cell should have a next sibling', selectedCell.nextSibling);
182 fieldMock.$verify();
183 }
184
185 function testRemoveRows() {
186 fieldMock.$replay();
187 createTableAndSelectCell({width: 1, height: 2});
188 var table = plugin.getCurrentTable_();
189 var selectedCell = fieldMock.getRange().getContainerElement();
190 selectedCell.id = 'selected';
191 assertEquals('Table should have two rows', 2, table.rows.length);
192 plugin.execCommandInternal(
193 goog.editor.plugins.TableEditor.COMMAND.REMOVE_ROWS);
194 assertEquals('A row should have been removed', 1, table.rows.length);
195 assertNull(
196 'The correct row should have been removed',
197 goog.dom.getElement('selected'));
198
199 // Verify that the table is removed if we don't have any rows.
200 plugin.execCommandInternal(
201 goog.editor.plugins.TableEditor.COMMAND.REMOVE_ROWS);
202 assertEquals(
203 'The table should have been removed', 0,
204 goog.dom.getElementsByTagName(goog.dom.TagName.TABLE, field).length);
205 fieldMock.$verify();
206 }
207
208 function testRemoveColumns() {
209 fieldMock.$replay();
210 createTableAndSelectCell({width: 2, height: 1});
211 var table = plugin.getCurrentTable_();
212 var selectedCell = fieldMock.getRange().getContainerElement();
213 selectedCell.id = 'selected';
214 assertEquals('Table should have two cells', 2, getCellCount(table));
215 plugin.execCommandInternal(
216 goog.editor.plugins.TableEditor.COMMAND.REMOVE_COLUMNS);
217 assertEquals('A cell should have been removed', 1, getCellCount(table));
218 assertNull(
219 'The correct cell should have been removed',
220 goog.dom.getElement('selected'));
221
222 // Verify that the table is removed if we don't have any columns.
223 plugin.execCommandInternal(
224 goog.editor.plugins.TableEditor.COMMAND.REMOVE_COLUMNS);
225 assertEquals(
226 'The table should have been removed', 0,
227 goog.dom.getElementsByTagName(goog.dom.TagName.TABLE, field).length);
228 fieldMock.$verify();
229 }
230
231 function testSplitCell() {
232 fieldMock.$replay();
233 createTableAndSelectCell({width: 1, height: 1});
234 var table = plugin.getCurrentTable_();
235 var selectedCell = fieldMock.getRange().getContainerElement();
236 // Splitting is only supported if we set these attributes.
237 selectedCell.rowSpan = '1';
238 selectedCell.colSpan = '2';
239 goog.dom.setTextContent(selectedCell, 'foo');
240 goog.dom.Range.createFromNodeContents(selectedCell).select();
241 assertEquals('Table should have one cell', 1, getCellCount(table));
242 plugin.execCommandInternal(
243 goog.editor.plugins.TableEditor.COMMAND.SPLIT_CELL);
244 assertEquals('The cell should have been split', 2, getCellCount(table));
245 assertEquals(
246 'The cell content should be intact', 'foo', selectedCell.innerHTML);
247 assertNotNull(
248 'The new cell should be inserted before', selectedCell.previousSibling);
249 fieldMock.$verify();
250 }
251
252 function testMergeCells() {
253 fieldMock.$replay();
254 createTableAndSelectCell({width: 2, height: 1});
255 var table = plugin.getCurrentTable_();
256 var selectedCell = fieldMock.getRange().getContainerElement();
257 goog.dom.setTextContent(selectedCell, 'foo');
258 goog.dom.setTextContent(selectedCell.nextSibling, 'bar');
259 var range = goog.dom.Range.createFromNodeContents(
260 goog.dom.getElementsByTagName(goog.dom.TagName.TR, table)[0]);
261 range.select();
262 plugin.execCommandInternal(
263 goog.editor.plugins.TableEditor.COMMAND.MERGE_CELLS);
264 expectedFailures.expectFailureFor(
265 goog.userAgent.IE && goog.userAgent.isVersionOrHigher('8'));
266 try {
267 // In IE8, even after explicitly setting the range to span
268 // multiple cells, the browser selection only contains the first TD
269 // which causes the merge operation to fail.
270 assertEquals('The cells should be merged', 1, getCellCount(table));
271 assertEquals(
272 'The cell should have expected colspan', 2, selectedCell.colSpan);
273 assertHTMLEquals(
274 'The content should be merged', 'foo bar', selectedCell.innerHTML);
275 } catch (e) {
276 expectedFailures.handleException(e);
277 }
278 fieldMock.$verify();
279 }
280
281
282 /**
283 * Helper routine which returns the number of cells in the table.
284 *
285 * @param {Element} table The table in question.
286 * @return {number} Number of cells.
287 */
288 function getCellCount(table) {
289 return table.cells ? table.cells.length :
290 table.rows[0].cells.length * table.rows.length;
291 }
292
293
294 /**
295 * Helper method which creates a table and puts the cursor on the first TD.
296 * In IE, the cursor isn't positioned in the first cell (TD) and we simulate
297 * that behavior explicitly to be consistent across all browsers.
298 *
299 * @param {Object} op_tableProps Optional table properties.
300 */
301 function createTableAndSelectCell(opt_tableProps) {
302 goog.dom.Range.createCaret(field, 1).select();
303 plugin.execCommandInternal(
304 goog.editor.plugins.TableEditor.COMMAND.TABLE, opt_tableProps);
305 if (goog.userAgent.IE) {
306 var range = goog.dom.Range.createFromNodeContents(
307 goog.dom.getElementsByTagName(goog.dom.TagName.TD, field)[0]);
308 range.select();
309 }
310 }