Bug 1484127 - part 0: Add automated tests for nsITableEditor::GetCellAt() r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 23 Aug 2018 06:13:22 +0000
changeset 490738 b95c3522fa1e72dcd5f50726a08477521ab1e951
parent 490737 3cf8d56c3f33785c07812a41e172a10128878fb3
child 490739 3d27e4213ccc750e392a6b5717247886983e8d5a
push id1815
push userffxbld-merge
push dateMon, 15 Oct 2018 10:40:45 +0000
treeherdermozilla-release@18d4c09e9378 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1484127
milestone63.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1484127 - part 0: Add automated tests for nsITableEditor::GetCellAt() r=m_kato Differential Revision: https://phabricator.services.mozilla.com/D3955
editor/libeditor/tests/mochitest.ini
editor/libeditor/tests/test_nsITableEditor_getCellAt.html
--- a/editor/libeditor/tests/mochitest.ini
+++ b/editor/libeditor/tests/mochitest.ini
@@ -284,16 +284,17 @@ skip-if = android_version == '24'
 subsuite = clipboard
 skip-if = android_version == '24'
 [test_nsIHTMLEditor_getSelectedElement.html]
 skip-if = toolkit == 'android' && debug # bug 1480702, causes permanent failure of non-related test
 [test_nsIHTMLEditor_selectElement.html]
 skip-if = toolkit == 'android' && debug # bug 1480702, causes permanent failure of non-related test
 [test_nsIHTMLEditor_setCaretAfterElement.html]
 skip-if = toolkit == 'android' && debug # bug 1480702, causes permanent failure of non-related test
+[test_nsITableEditor_getCellAt.html]
 [test_nsITableEditor_getCellIndexes.html]
 [test_nsITableEditor_getFirstRow.html]
 [test_nsITableEditor_getTableSize.html]
 [test_resizers_appearance.html]
 [test_resizers_resizing_elements.html]
 skip-if = android_version == '18' || (verify && debug && os == 'win') # bug 1147989
 [test_root_element_replacement.html]
 [test_select_all_without_body.html]
new file mode 100644
--- /dev/null
+++ b/editor/libeditor/tests/test_nsITableEditor_getCellAt.html
@@ -0,0 +1,139 @@
+<!DOCTYPE>
+<html>
+<head>
+  <title>Test for nsITableEditor.getCellAt()</title>
+  <script src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" href="/tests/SimpleTest/test.css">
+</head>
+<body>
+<div id="display">
+</div>
+<div id="content" contenteditable></div>
+<pre id="test">
+</pre>
+
+<script class="testbody" type="application/javascript">
+
+SimpleTest.waitForExplicitFinish();
+SimpleTest.waitForFocus(function() {
+  let editor = document.getElementById("content");
+  let selection = document.getSelection();
+
+  try {
+    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(undefined, 0, 0));
+    ok(false, "nsITableEditor.getCellAt(undefined) should cause throwing an exception when editor does not have Selection");
+  } catch (e) {
+    ok(true, "nsITableEditor.getCellAt(undefined) should cause throwing an exception when editor does not have Selection");
+  }
+
+  try {
+    let cell = SpecialPowers.unwrap(getTableEditor().getTableSize(null, 0, 0));
+    ok(false, "nsITableEditor.getCellAt(null) should cause throwing an exception when editor does not have Selection");
+  } catch (e) {
+    ok(true, "nsITableEditor.getCellAt(null) should cause throwing an exception when editor does not have Selection");
+  }
+
+  // XXX This is inconsistent behavior with other APIs.
+  try {
+    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(editor, 0, 0));
+    ok(true, "nsITableEditor.getCellAt() should not cause throwing exception even if given node is not a <table>");
+    is(cell, null, "nsITableEditor.getCellAt() should return null if given node is not a <table>");
+  } catch (e) {
+    ok(false, "nsITableEditor.getCellAt() should not cause throwing exception even if given node is not a <table>");
+  }
+
+  editor.innerHTML =
+    '<table id="table">' +
+      '<tr><td id="c1-1">cell1-1</td><td id="c1-2">cell1-2</td><td id="c1-3">cell1-3</td><td id="c1-4" colspan="2" rowspan="2">cell1-4</td></tr>' +
+      '<tr><td id="c2-1" rowspan="2">cell2-1</td><td id="c2-2">cell2-2<td id="c2-3">cell2-3</td></tr>' +
+      '<tr><td id="c3-2">cell3-2</td><td id="c3-3">cell3-3</td><td id="c3-4" colspan="2">cell3-4</td></tr>' +
+      '<tr><td id="c4-1" rowspan="4">cell4-1</td><td id="c4-2">' +
+        '<table id="inner-table"><tr><td id="c2-1-1">cell2-1-1</td><td id="c2-1-2">cell2-1-2</td></tr>' +
+          '<tr><td id="c2-2-1">cell2-2-1</td><td id="c2-2-2">cell2-2-2</td></table>' +
+        '</td><td id="c4-3">cell4-3</td><td id="c4-4">cell4-4</td><td id="c4-5">cell4-5</td></tr>' +
+      '<tr><td id="c5-2">cell5-2</td><td id="c5-3" colspan="2">cell5-3</td><td id="c5-5">cell5-5</td></tr>' +
+      '<tr><td id="c6-2">cell6-2</td><td id="c6-3">cell6-3</td><td id="c6-4"><p>cell6-4</p></td><td id="c6-5">cell6-5</td></tr>' +
+      '<tr><td id="c7-2" colspan="4">cell7-2</td></tr>' +
+    '</table>';
+  editor.scrollTop; // compute layout now.
+
+  const kTestsInParent = [
+    { row: 0, column: 0, expected: "c1-1" },
+    { row: 0, column: 3, expected: "c1-4" },
+    { row: 0, column: 4, expected: "c1-4" },
+    { row: 1, column: 3, expected: "c1-4" },
+    { row: 1, column: 4, expected: "c1-4" },
+    { row: 1, column: 0, expected: "c2-1" },
+    { row: 2, column: 0, expected: "c2-1" },
+    { row: 3, column: 0, expected: "c4-1" },
+    { row: 4, column: 0, expected: "c4-1" },
+    { row: 5, column: 0, expected: "c4-1" },
+    { row: 6, column: 0, expected: "c4-1" },
+    { row: 4, column: 2, expected: "c5-3" },
+    { row: 4, column: 3, expected: "c5-3" },
+    { row: 4, column: 4, expected: "c5-5" },
+    { row: 6, column: 1, expected: "c7-2" },
+    { row: 6, column: 2, expected: "c7-2" },
+    { row: 6, column: 3, expected: "c7-2" },
+    { row: 6, column: 4, expected: "c7-2" },
+    { row: 6, column: 5, expected: null },
+  ];
+
+  let table = document.getElementById("table");
+  for (const kTest of kTestsInParent) {
+    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(table, kTest.row, kTest.column));
+    if (kTest.expected === null) {
+      is(cell, null,
+         `Specified the parent <table> element directly (${kTest.row} - ${kTest.column})`);
+    } else {
+      is(cell.getAttribute("id"), kTest.expected,
+         `Specified the parent <table> element directly (${kTest.row} - ${kTest.column})`);
+    }
+    if (cell && cell.firstChild && cell.firstChild.nodeType == Node.TEXT_NODE) {
+      selection.collapse(cell.firstChild, 0);
+      cell = getTableEditor().getCellAt(null, kTest.row, kTest.column);
+      is(cell.getAttribute("id"), kTest.expected,
+         `Selection is collapsed in a cell element in the parent <table> (${kTest.row} - ${kTest.column})`);
+    }
+  }
+
+  const kTestsInChild = [
+    { row: 0, column: 0, expected: "c2-1-1" },
+    { row: 0, column: 1, expected: "c2-1-2" },
+    { row: 0, column: 2, expected: null },
+    { row: 1, column: 0, expected: "c2-2-1" },
+    { row: 1, column: 1, expected: "c2-2-2" },
+    { row: 2, column: 0, expected: null },
+  ];
+
+  let innerTable = document.getElementById("inner-table");
+  for (const kTest of kTestsInChild) {
+    let cell = SpecialPowers.unwrap(getTableEditor().getCellAt(innerTable, kTest.row, kTest.column));
+    if (kTest.expected === null) {
+      is(cell, null,
+         `Specified the inner <table> element directly (${kTest.row} - ${kTest.column})`);
+    } else {
+      is(cell.getAttribute("id"), kTest.expected,
+         `Specified the inner <table> element directly (${kTest.row} - ${kTest.column})`);
+    }
+    if (cell && cell.firstChild && cell.firstChild.nodeType == Node.TEXT_NODE) {
+      selection.collapse(cell.firstChild, 0);
+      cell = getTableEditor().getCellAt(null, kTest.row, kTest.column);
+      is(cell.getAttribute("id"), kTest.expected,
+         `Selection is collapsed in a cell element in the inner <table> (${kTest.row} - ${kTest.column})`);
+    }
+  }
+
+  SimpleTest.finish();
+});
+
+function getTableEditor() {
+  var Ci = SpecialPowers.Ci;
+  var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
+  return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsITableEditor);
+}
+
+</script>
+</body>
+
+</html>