| 1 |
// Copyright 2011 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.TableTest'); |
| 16 |
goog.setTestOnly('goog.editor.TableTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.editor.Table'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
|
| 23 |
function setUp() { |
| 24 |
var inputTables = goog.dom.getElementsByTagName(goog.dom.TagName.TABLE); |
| 25 |
testElements = {}; |
| 26 |
testObjects = {}; |
| 27 |
for (var i = 0; i < inputTables.length; i++) { |
| 28 |
var originalTable = inputTables[i]; |
| 29 |
if (originalTable.id.substring(0, 5) == 'test-') { |
| 30 |
var tableName = originalTable.id.substring(5); |
| 31 |
var testTable = originalTable.cloneNode(true); |
| 32 |
testTable.id = tableName; |
| 33 |
testElements[tableName] = testTable; |
| 34 |
document.body.appendChild(testTable); |
| 35 |
testObjects[tableName] = new goog.editor.Table(testTable); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
function tearDown() { |
| 41 |
for (var tableName in testElements) { |
| 42 |
document.body.removeChild(testElements[tableName]); |
| 43 |
delete testElements[tableName]; |
| 44 |
delete testObjects[tableName]; |
| 45 |
} |
| 46 |
testElements = null; |
| 47 |
testObjects = null; |
| 48 |
} |
| 49 |
|
| 50 |
function tableSanityCheck(editableTable, rowCount, colCount) { |
| 51 |
assertEquals( |
| 52 |
'Table has expected number of rows', rowCount, editableTable.rows.length); |
| 53 |
for (var i = 0, row; row = editableTable.rows[i]; i++) { |
| 54 |
assertEquals( |
| 55 |
'Row ' + i + ' has expected number of columns', colCount, |
| 56 |
row.columns.length); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
function testBasicTable() { |
| 61 |
// Do some basic sanity checking on the editable table structure |
| 62 |
tableSanityCheck(testObjects.basic, 4, 3); |
| 63 |
var originalRows = |
| 64 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.basic); |
| 65 |
assertEquals( |
| 66 |
'Basic table row count, compared to source', originalRows.length, |
| 67 |
testObjects.basic.rows.length); |
| 68 |
assertEquals( |
| 69 |
'Basic table row count, known value', 4, testObjects.basic.rows.length); |
| 70 |
assertEquals( |
| 71 |
'Basic table first row element', originalRows[0], |
| 72 |
testObjects.basic.rows[0].element); |
| 73 |
assertEquals( |
| 74 |
'Basic table last row element', originalRows[3], |
| 75 |
testObjects.basic.rows[3].element); |
| 76 |
assertEquals( |
| 77 |
'Basic table first row length', 3, |
| 78 |
testObjects.basic.rows[0].columns.length); |
| 79 |
assertEquals( |
| 80 |
'Basic table last row length', 3, |
| 81 |
testObjects.basic.rows[3].columns.length); |
| 82 |
} |
| 83 |
|
| 84 |
function testTortureTable() { |
| 85 |
// Do basic sanity checking on torture table structure |
| 86 |
tableSanityCheck(testObjects.torture, 9, 3); |
| 87 |
var originalRows = |
| 88 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.torture); |
| 89 |
assertEquals( |
| 90 |
'Torture table row count, compared to source', originalRows.length, |
| 91 |
testObjects.torture.rows.length); |
| 92 |
assertEquals( |
| 93 |
'Torture table row count, known value', 9, |
| 94 |
testObjects.torture.rows.length); |
| 95 |
} |
| 96 |
|
| 97 |
function _testInsertRowResult(element, editableTable, newTr, index) { |
| 98 |
var originalRowCount; |
| 99 |
if (element == testElements.basic) { |
| 100 |
originalRowCount = 4; |
| 101 |
} else if (element == testElements.torture) { |
| 102 |
originalRowCount = 9; |
| 103 |
} |
| 104 |
|
| 105 |
assertEquals( |
| 106 |
'Row was added to table', originalRowCount + 1, |
| 107 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, element).length); |
| 108 |
assertEquals( |
| 109 |
'Row was added at position ' + index, |
| 110 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, element)[index], |
| 111 |
newTr); |
| 112 |
assertEquals( |
| 113 |
'Row knows its own position', index, editableTable.rows[index].index); |
| 114 |
assertEquals( |
| 115 |
'EditableTable shows row at position ' + index, newTr, |
| 116 |
editableTable.rows[index].element); |
| 117 |
assertEquals( |
| 118 |
'New row has correct number of TDs', 3, |
| 119 |
goog.dom.getElementsByTagName(goog.dom.TagName.TD, newTr).length); |
| 120 |
} |
| 121 |
|
| 122 |
function testInsertRowAtBeginning() { |
| 123 |
var tr = testObjects.basic.insertRow(0); |
| 124 |
_testInsertRowResult(testElements.basic, testObjects.basic, tr, 0); |
| 125 |
} |
| 126 |
|
| 127 |
function testInsertRowInMiddle() { |
| 128 |
var tr = testObjects.basic.insertRow(2); |
| 129 |
_testInsertRowResult(testElements.basic, testObjects.basic, tr, 2); |
| 130 |
} |
| 131 |
|
| 132 |
function testInsertRowAtEnd() { |
| 133 |
assertEquals( |
| 134 |
'Table has expected number of existing rows', 4, |
| 135 |
testObjects.basic.rows.length); |
| 136 |
var tr = testObjects.basic.insertRow(4); |
| 137 |
_testInsertRowResult(testElements.basic, testObjects.basic, tr, 4); |
| 138 |
} |
| 139 |
|
| 140 |
function testInsertRowAtEndNoIndexArgument() { |
| 141 |
assertEquals( |
| 142 |
'Table has expected number of existing rows', 4, |
| 143 |
testObjects.basic.rows.length); |
| 144 |
var tr = testObjects.basic.insertRow(); |
| 145 |
_testInsertRowResult(testElements.basic, testObjects.basic, tr, 4); |
| 146 |
} |
| 147 |
|
| 148 |
function testInsertRowAtBeginningRowspan() { |
| 149 |
// Test inserting a row when the existing DOM row at that index has |
| 150 |
// a cell with a rowspan. This should be just like a regular insert - |
| 151 |
// the rowspan shouldn't have any effect. |
| 152 |
assertEquals( |
| 153 |
'Cell has starting rowspan', 2, |
| 154 |
goog.dom |
| 155 |
.getFirstElementChild(goog.dom.getElementsByTagName( |
| 156 |
goog.dom.TagName.TR, testElements.torture)[0]) |
| 157 |
.rowSpan); |
| 158 |
var tr = testObjects.torture.insertRow(0); |
| 159 |
// Among other things this verifies that the new row has 3 child TDs. |
| 160 |
_testInsertRowResult(testElements.torture, testObjects.torture, tr, 0); |
| 161 |
} |
| 162 |
|
| 163 |
function testInsertRowAtEndingRowspan() { |
| 164 |
// Test inserting a row when there's a cell in a previous DOM row |
| 165 |
// with a rowspan that extends into the row with the given index |
| 166 |
// and ends there. This should be just like a regular insert - |
| 167 |
// the rowspan shouldn't have any effect. |
| 168 |
assertEquals( |
| 169 |
'Cell has ending rowspan', 4, |
| 170 |
goog.dom |
| 171 |
.getLastElementChild(goog.dom.getElementsByTagName( |
| 172 |
goog.dom.TagName.TR, testElements.torture)[5]) |
| 173 |
.rowSpan); |
| 174 |
var tr = testObjects.torture.insertRow(); |
| 175 |
// Among other things this verifies that the new row has 3 child TDs. |
| 176 |
_testInsertRowResult(testElements.torture, testObjects.torture, tr, 9); |
| 177 |
} |
| 178 |
|
| 179 |
function testInsertRowAtSpanningRowspan() { |
| 180 |
// Test inserting a row at an index where there's a cell with a rowspan |
| 181 |
// that begins in a previous row and continues into the next row. In this |
| 182 |
// case the existing cell's rowspan should be extended, and the new |
| 183 |
// tr should have one less child element. |
| 184 |
var rowSpannedCell = testObjects.torture.rows[7].columns[2]; |
| 185 |
assertTrue( |
| 186 |
'Existing cell has overlapping rowspan', |
| 187 |
rowSpannedCell.startRow == 5 && rowSpannedCell.endRow == 8); |
| 188 |
var tr = testObjects.torture.insertRow(7); |
| 189 |
assertEquals( |
| 190 |
'New DOM row has one less cell', 2, |
| 191 |
goog.dom.getElementsByTagName(goog.dom.TagName.TD, tr).length); |
| 192 |
assertEquals( |
| 193 |
'Rowspanned cell listed in new EditableRow\'s columns', |
| 194 |
testObjects.torture.rows[6].columns[2].element, |
| 195 |
testObjects.torture.rows[7].columns[2].element); |
| 196 |
} |
| 197 |
|
| 198 |
function _testInsertColumnResult(newCells, element, editableTable, index) { |
| 199 |
for (var rowNo = 0, row; row = editableTable.rows[rowNo]; rowNo++) { |
| 200 |
assertEquals('Row includes new column', 4, row.columns.length); |
| 201 |
} |
| 202 |
assertEquals( |
| 203 |
'New cell in correct position', newCells[0], |
| 204 |
editableTable.rows[0].columns[index].element); |
| 205 |
} |
| 206 |
|
| 207 |
function testInsertColumnAtBeginning() { |
| 208 |
var startColCount = testObjects.basic.rows[0].columns.length; |
| 209 |
var newCells = testObjects.basic.insertColumn(0); |
| 210 |
assertEquals( |
| 211 |
'New cell added for each row', testObjects.basic.rows.length, |
| 212 |
newCells.length); |
| 213 |
assertEquals( |
| 214 |
'Insert column incremented column length', startColCount + 1, |
| 215 |
testObjects.basic.rows[0].columns.length); |
| 216 |
_testInsertColumnResult(newCells, testElements.basic, testObjects.basic, 0); |
| 217 |
} |
| 218 |
|
| 219 |
function testInsertColumnAtEnd() { |
| 220 |
var startColCount = testObjects.basic.rows[0].columns.length; |
| 221 |
var newCells = testObjects.basic.insertColumn(3); |
| 222 |
assertEquals( |
| 223 |
'New cell added for each row', testObjects.basic.rows.length, |
| 224 |
newCells.length); |
| 225 |
assertEquals( |
| 226 |
'Insert column incremented column length', startColCount + 1, |
| 227 |
testObjects.basic.rows[0].columns.length); |
| 228 |
_testInsertColumnResult(newCells, testElements.basic, testObjects.basic, 3); |
| 229 |
} |
| 230 |
|
| 231 |
function testInsertColumnAtEndNoIndexArgument() { |
| 232 |
var startColCount = testObjects.basic.rows[0].columns.length; |
| 233 |
var newCells = testObjects.basic.insertColumn(); |
| 234 |
assertEquals( |
| 235 |
'New cell added for each row', testObjects.basic.rows.length, |
| 236 |
newCells.length); |
| 237 |
assertEquals( |
| 238 |
'Insert column incremented column length', startColCount + 1, |
| 239 |
testObjects.basic.rows[0].columns.length); |
| 240 |
_testInsertColumnResult(newCells, testElements.basic, testObjects.basic, 3); |
| 241 |
} |
| 242 |
|
| 243 |
function testInsertColumnInMiddle() { |
| 244 |
var startColCount = testObjects.basic.rows[0].columns.length; |
| 245 |
var newCells = testObjects.basic.insertColumn(2); |
| 246 |
assertEquals( |
| 247 |
'New cell added for each row', testObjects.basic.rows.length, |
| 248 |
newCells.length); |
| 249 |
assertEquals( |
| 250 |
'Insert column incremented column length', startColCount + 1, |
| 251 |
testObjects.basic.rows[0].columns.length); |
| 252 |
_testInsertColumnResult(newCells, testElements.basic, testObjects.basic, 2); |
| 253 |
} |
| 254 |
|
| 255 |
function testInsertColumnAtBeginningColSpan() { |
| 256 |
var cells = testObjects.torture.insertColumn(0); |
| 257 |
tableSanityCheck(testObjects.torture, 9, 4); |
| 258 |
assertEquals( |
| 259 |
'New cell was added before colspanned cell', 1, |
| 260 |
testObjects.torture.rows[3].columns[0].colSpan); |
| 261 |
assertEquals( |
| 262 |
'New cell was added and returned', |
| 263 |
testObjects.torture.rows[3].columns[0].element, cells[3]); |
| 264 |
} |
| 265 |
|
| 266 |
function testInsertColumnAtEndingColSpan() { |
| 267 |
var cells = testObjects.torture.insertColumn(); |
| 268 |
tableSanityCheck(testObjects.torture, 9, 4); |
| 269 |
assertEquals( |
| 270 |
'New cell was added after colspanned cell', 1, |
| 271 |
testObjects.torture.rows[0].columns[3].colSpan); |
| 272 |
assertEquals( |
| 273 |
'New cell was added and returned', |
| 274 |
testObjects.torture.rows[0].columns[3].element, cells[0]); |
| 275 |
} |
| 276 |
|
| 277 |
function testInsertColumnAtSpanningColSpan() { |
| 278 |
assertEquals( |
| 279 |
'Existing cell has expected colspan', 3, |
| 280 |
testObjects.torture.rows[4].columns[1].colSpan); |
| 281 |
var cells = testObjects.torture.insertColumn(1); |
| 282 |
tableSanityCheck(testObjects.torture, 9, 4); |
| 283 |
assertEquals( |
| 284 |
'Existing cell increased colspan', 4, |
| 285 |
testObjects.torture.rows[4].columns[1].colSpan); |
| 286 |
assertEquals( |
| 287 |
'3 cells weren\'t created due to existing colspans', 6, cells.length); |
| 288 |
} |
| 289 |
|
| 290 |
function testRemoveFirstRow() { |
| 291 |
var originalRow = |
| 292 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.basic)[0]; |
| 293 |
testObjects.basic.removeRow(0); |
| 294 |
tableSanityCheck(testObjects.basic, 3, 3); |
| 295 |
assertNotEquals( |
| 296 |
'Row was removed from table element', originalRow, |
| 297 |
goog.dom.getElementsByTagName( |
| 298 |
goog.dom.TagName.TR, testElements.basic)[0]); |
| 299 |
} |
| 300 |
|
| 301 |
function testRemoveLastRow() { |
| 302 |
var originalRow = |
| 303 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.basic)[3]; |
| 304 |
testObjects.basic.removeRow(3); |
| 305 |
tableSanityCheck(testObjects.basic, 3, 3); |
| 306 |
assertNotEquals( |
| 307 |
'Row was removed from table element', originalRow, |
| 308 |
goog.dom.getElementsByTagName( |
| 309 |
goog.dom.TagName.TR, testElements.basic)[3]); |
| 310 |
} |
| 311 |
|
| 312 |
function testRemoveMiddleRow() { |
| 313 |
var originalRow = |
| 314 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.basic)[2]; |
| 315 |
testObjects.basic.removeRow(2); |
| 316 |
tableSanityCheck(testObjects.basic, 3, 3); |
| 317 |
assertNotEquals( |
| 318 |
'Row was removed from table element', originalRow, |
| 319 |
goog.dom.getElementsByTagName( |
| 320 |
goog.dom.TagName.TR, testElements.basic)[2]); |
| 321 |
} |
| 322 |
|
| 323 |
function testRemoveRowAtBeginingRowSpan() { |
| 324 |
var originalRow = testObjects.torture.removeRow(0); |
| 325 |
tableSanityCheck(testObjects.torture, 8, 3); |
| 326 |
assertNotEquals( |
| 327 |
'Row was removed from table element', originalRow, |
| 328 |
goog.dom.getElementsByTagName( |
| 329 |
goog.dom.TagName.TR, testElements.basic)[0]); |
| 330 |
assertEquals( |
| 331 |
'Rowspan correctly adjusted', 1, |
| 332 |
testObjects.torture.rows[0].columns[0].rowSpan); |
| 333 |
} |
| 334 |
|
| 335 |
function testRemoveRowAtEndingRowSpan() { |
| 336 |
var originalRow = goog.dom.getElementsByTagName( |
| 337 |
goog.dom.TagName.TR, testElements.torture)[8]; |
| 338 |
testObjects.torture.removeRow(8); |
| 339 |
tableSanityCheck(testObjects.torture, 8, 3); |
| 340 |
assertNotEquals( |
| 341 |
'Row was removed from table element', originalRow, |
| 342 |
goog.dom.getElementsByTagName( |
| 343 |
goog.dom.TagName.TR, testElements.basic)[8]); |
| 344 |
assertEquals( |
| 345 |
'Rowspan correctly adjusted', 3, |
| 346 |
testObjects.torture.rows[7].columns[2].rowSpan); |
| 347 |
} |
| 348 |
|
| 349 |
function testRemoveRowAtSpanningRowSpan() { |
| 350 |
var originalRow = goog.dom.getElementsByTagName( |
| 351 |
goog.dom.TagName.TR, testElements.torture)[7]; |
| 352 |
testObjects.torture.removeRow(7); |
| 353 |
tableSanityCheck(testObjects.torture, 8, 3); |
| 354 |
assertNotEquals( |
| 355 |
'Row was removed from table element', originalRow, |
| 356 |
goog.dom.getElementsByTagName( |
| 357 |
goog.dom.TagName.TR, testElements.basic)[7]); |
| 358 |
assertEquals( |
| 359 |
'Rowspan correctly adjusted', 3, |
| 360 |
testObjects.torture.rows[6].columns[2].rowSpan); |
| 361 |
} |
| 362 |
|
| 363 |
function _testRemoveColumn(index) { |
| 364 |
var tr = |
| 365 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.basic)[0]; |
| 366 |
var sampleCell = |
| 367 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, tr)[index]; |
| 368 |
testObjects.basic.removeColumn(index); |
| 369 |
tableSanityCheck(testObjects.basic, 4, 2); |
| 370 |
tr = |
| 371 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.basic)[0]; |
| 372 |
assertNotEquals( |
| 373 |
'Test cell removed from column', sampleCell, |
| 374 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, tr)[index]); |
| 375 |
} |
| 376 |
|
| 377 |
function testRemoveFirstColumn() { |
| 378 |
_testRemoveColumn(0); |
| 379 |
} |
| 380 |
|
| 381 |
function testRemoveMiddleColumn() { |
| 382 |
_testRemoveColumn(1); |
| 383 |
} |
| 384 |
|
| 385 |
function testRemoveLastColumn() { |
| 386 |
_testRemoveColumn(2); |
| 387 |
} |
| 388 |
|
| 389 |
function testRemoveColumnAtStartingColSpan() { |
| 390 |
testObjects.torture.removeColumn(0); |
| 391 |
tableSanityCheck(testObjects.torture, 9, 2); |
| 392 |
var tr = goog.dom.getElementsByTagName( |
| 393 |
goog.dom.TagName.TR, testElements.torture)[5] |
| 394 |
assertEquals( |
| 395 |
'Colspan was decremented correctly', 1, |
| 396 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, tr)[0].colSpan); |
| 397 |
} |
| 398 |
|
| 399 |
function testRemoveColumnAtEndingColSpan() { |
| 400 |
testObjects.torture.removeColumn(2); |
| 401 |
tableSanityCheck(testObjects.torture, 9, 2); |
| 402 |
var tr = goog.dom.getElementsByTagName( |
| 403 |
goog.dom.TagName.TR, testElements.torture)[1]; |
| 404 |
assertEquals( |
| 405 |
'Colspan was decremented correctly', 1, |
| 406 |
goog.dom.getElementsByTagName(goog.dom.TagName.TD, tr)[0].colSpan); |
| 407 |
} |
| 408 |
|
| 409 |
function testRemoveColumnAtSpanningColSpan() { |
| 410 |
testObjects.torture.removeColumn(2); |
| 411 |
tableSanityCheck(testObjects.torture, 9, 2); |
| 412 |
var tr = goog.dom.getElementsByTagName( |
| 413 |
goog.dom.TagName.TR, testElements.torture)[4] |
| 414 |
assertEquals( |
| 415 |
'Colspan was decremented correctly', 2, |
| 416 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, tr)[0].colSpan); |
| 417 |
} |
| 418 |
|
| 419 |
function testMergeCellsInRow() { |
| 420 |
testObjects.basic.mergeCells(0, 0, 0, 2); |
| 421 |
tableSanityCheck(testObjects.basic, 4, 3); |
| 422 |
var trs = |
| 423 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.basic); |
| 424 |
assertEquals( |
| 425 |
'Cells merged', 1, |
| 426 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, trs[0]).length); |
| 427 |
assertEquals( |
| 428 |
'Merged cell has correct colspan', 3, |
| 429 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, trs[0])[0].colSpan); |
| 430 |
assertEquals( |
| 431 |
'Merged cell has correct rowspan', 1, |
| 432 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, trs[0])[0].rowSpan); |
| 433 |
} |
| 434 |
|
| 435 |
function testMergeCellsInColumn() { |
| 436 |
testObjects.basic.mergeCells(0, 0, 2, 0); |
| 437 |
tableSanityCheck(testObjects.basic, 4, 3); |
| 438 |
var trs = |
| 439 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.basic); |
| 440 |
assertEquals( |
| 441 |
'Other cells still in row', 3, |
| 442 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, trs[0]).length); |
| 443 |
assertEquals( |
| 444 |
'Merged cell has correct colspan', 1, |
| 445 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, trs[0])[0].colSpan); |
| 446 |
assertEquals( |
| 447 |
'Merged cell has correct rowspan', 3, |
| 448 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, trs[0])[0].rowSpan); |
| 449 |
assert( |
| 450 |
'Cell appears in multiple rows after merge', |
| 451 |
testObjects.basic.rows[0].columns[0] == |
| 452 |
testObjects.basic.rows[2].columns[0]); |
| 453 |
} |
| 454 |
|
| 455 |
function testMergeCellsInRowAndColumn() { |
| 456 |
testObjects.basic.mergeCells(1, 1, 3, 2); |
| 457 |
tableSanityCheck(testObjects.basic, 4, 3); |
| 458 |
var trs = |
| 459 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.basic); |
| 460 |
var mergedCell = |
| 461 |
goog.dom.getElementsByTagName(goog.dom.TagName.TD, trs[1])[1]; |
| 462 |
assertEquals('Merged cell has correct rowspan', 3, mergedCell.rowSpan); |
| 463 |
assertEquals('Merged cell has correct colspan', 2, mergedCell.colSpan); |
| 464 |
} |
| 465 |
|
| 466 |
function testMergeCellsAlreadyMerged() { |
| 467 |
testObjects.torture.mergeCells(5, 0, 8, 2); |
| 468 |
tableSanityCheck(testObjects.torture, 9, 3); |
| 469 |
var trs = |
| 470 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.torture); |
| 471 |
var mergedCell = |
| 472 |
goog.dom.getElementsByTagName(goog.dom.TagName.TH, trs[5])[0]; |
| 473 |
assertEquals('Merged cell has correct rowspan', 4, mergedCell.rowSpan); |
| 474 |
assertEquals('Merged cell has correct colspan', 3, mergedCell.colSpan); |
| 475 |
} |
| 476 |
|
| 477 |
function testIllegalMergeNonRectangular() { |
| 478 |
// This should fail because it involves trying to merge two parts |
| 479 |
// of a 3-colspan cell with other cells |
| 480 |
var mergeSucceeded = testObjects.torture.mergeCells(3, 1, 5, 2); |
| 481 |
if (mergeSucceeded) { |
| 482 |
throw 'EditableTable allowed impossible merge!'; |
| 483 |
} |
| 484 |
tableSanityCheck(testObjects.torture, 9, 3); |
| 485 |
} |
| 486 |
|
| 487 |
function testIllegalMergeSingleCell() { |
| 488 |
// This should fail because it involves merging a single cell |
| 489 |
var mergeSucceeded = testObjects.torture.mergeCells(0, 1, 0, 1); |
| 490 |
if (mergeSucceeded) { |
| 491 |
throw 'EditableTable allowed impossible merge!'; |
| 492 |
} |
| 493 |
tableSanityCheck(testObjects.torture, 9, 3); |
| 494 |
} |
| 495 |
|
| 496 |
function testSplitCell() { |
| 497 |
testObjects.torture.splitCell(1, 1); |
| 498 |
tableSanityCheck(testObjects.torture, 9, 3); |
| 499 |
var trs = |
| 500 |
goog.dom.getElementsByTagName(goog.dom.TagName.TR, testElements.torture); |
| 501 |
assertEquals( |
| 502 |
'Cell was split into multiple columns in row 1', 3, |
| 503 |
trs[1].getElementsByTagName('*').length); |
| 504 |
assertEquals( |
| 505 |
'Cell was split into multiple columns in row 2', 3, |
| 506 |
trs[2].getElementsByTagName('*').length); |
| 507 |
} |
| 508 |
|
| 509 |
function testChildTableRowsNotCountedInParentTable() { |
| 510 |
tableSanityCheck(testObjects.nested, 2, 3); |
| 511 |
for (var i = 0; i < testObjects.nested.rows.length; i++) { |
| 512 |
var tr = testObjects.nested.rows[i].element; |
| 513 |
// A tr's parent is tbody, parent of that is table - check to |
| 514 |
// make sure the ancestor table is as expected. This means |
| 515 |
// that none of the child table's rows have been erroneously |
| 516 |
// loaded into the EditableTable. |
| 517 |
assertEquals( |
| 518 |
'Row is child of parent table', testElements.nested, |
| 519 |
tr.parentNode.parentNode); |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
/* |
| 524 |
// TODO(user): write more unit tests for selection stuff. |
| 525 |
// The following code is left in here for reference in implementing |
| 526 |
// this TODO. |
| 527 |
|
| 528 |
var tds = goog.dom.getElementsByTagName( |
| 529 |
goog.dom.TagName.TD, goog.dom.getElement('test1')); |
| 530 |
var range = goog.dom.Range.createFromNodes(tds[7], tds[9]); |
| 531 |
range.select(); |
| 532 |
var cellSelection = new goog.editor.Table.CellSelection(range); |
| 533 |
assertEquals(0, cellSelection.getFirstColumnIndex()); |
| 534 |
assertEquals(2, cellSelection.getLastColumnIndex()); |
| 535 |
assertEquals(2, cellSelection.getFirstRowIndex()); |
| 536 |
assertEquals(2, cellSelection.getLastRowIndex()); |
| 537 |
assertTrue(cellSelection.isRectangle()); |
| 538 |
|
| 539 |
range = goog.dom.Range.createFromNodes(tds[7], tds[12]); |
| 540 |
range.select(); |
| 541 |
var cellSelection2 = new goog.editor.Table.CellSelection(range); |
| 542 |
assertFalse(cellSelection2.isRectangle()); |
| 543 |
*/ |