Bug 1484125 - part 0: Add automated tests for nsITableEditor::GetTableSize() r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 23 Aug 2018 06:42:11 +0000
changeset 830997 5c0edfef06dd997312d977fbe69fa6feceb4a43b
parent 830996 8221a40c73b09c4fd013cae2afd6e641b1a82541
child 830998 3cf8d56c3f33785c07812a41e172a10128878fb3
push id118868
push userbmo:zjz@zjz.name
push dateFri, 24 Aug 2018 07:04:39 +0000
reviewersm_kato
bugs1484125
milestone63.0a1
Bug 1484125 - part 0: Add automated tests for nsITableEditor::GetTableSize() r=m_kato Differential Revision: https://phabricator.services.mozilla.com/D3951
editor/libeditor/tests/mochitest.ini
editor/libeditor/tests/test_nsITableEditor_getTableSize.html
--- a/editor/libeditor/tests/mochitest.ini
+++ b/editor/libeditor/tests/mochitest.ini
@@ -286,16 +286,17 @@ 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_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]
 [test_spellcheck_pref.html]
 skip-if = toolkit == 'android'
 [test_undo_after_spellchecker_replaces_word.html]
new file mode 100644
--- /dev/null
+++ b/editor/libeditor/tests/test_nsITableEditor_getTableSize.html
@@ -0,0 +1,95 @@
+<!DOCTYPE>
+<html>
+<head>
+  <title>Test for nsITableEditor.getTableSize()</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();
+  let rowCount = {}, columnCount = {};
+
+  try {
+    getTableEditor().getTableSize(undefined, rowCount, columnCount);
+    ok(false, "nsITableEditor.getTableSize(undefined) should cause throwing an exception");
+  } catch (e) {
+    ok(true, "nsITableEditor.getTableSize(undefined) should cause throwing an exception");
+  }
+
+  try {
+    getTableEditor().getTableSize(null, rowCount, columnCount);
+    ok(false, "nsITableEditor.getTableSize(null) should cause throwing an exception");
+  } catch (e) {
+    ok(true, "nsITableEditor.getTableSize(null) should cause throwing an exception");
+  }
+
+  try {
+    getTableEditor().getTableSize(editor, rowCount, columnCount);
+    ok(false, "nsITableEditor.getTableSize() should cause throwing an exception if given node is not in a <table>");
+  } catch (e) {
+    ok(true, "nsITableEditor.getTableSize() should cause throwing an exception if given node is not in a <table>");
+  }
+
+  // Set id to "test" for the argument for getTableSize().
+  // Set data-rows and data-cols to expected count of them.
+  kTests = [
+    '<table><tr><td id="test" data-rows="2" data-cols="3">cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
+    '<table><tr id="test" data-rows="2" data-cols="3"><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
+    '<table id="test" data-rows="2" data-cols="3"><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
+    '<table><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td><p id="test" data-rows="2" data-cols="3">cell2-3</p></td></tr></table>',
+    '<table><caption id="test" data-rows="2" data-cols="3">caption</caption><tr><td>cell1-1</td><td>cell1-2</td><td>cell1-3</td></tr><tr><td>cell2-1</td><td>cell2-2</td><td>cell2-3</td></tr></table>',
+    '<table id="test" data-rows="0" data-cols="0"></table>',
+    '<table id="test" data-rows="0" data-cols="0"><caption>caption</caption></table>',
+    '<table id="test" data-rows="1" data-cols="1"><td>cell1-1</td></table>',
+    // rowspan does not affect, but colspan affects...
+    '<table id="test" data-rows="1" data-cols="12"><tr><td rowspan="8" colspan="12">cell1-1</td></tr></table>',
+    '<table id="test" data-rows="1" data-cols="1"><tr><td><table><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
+    '<table><tr><td id="test" data-rows="1" data-cols="1"><table><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
+    '<table><tr><td><table id="test" data-rows="3" data-cols="2"><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
+    '<table><tr><td><table><tr><td id="test" data-rows="3" data-cols="2">cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td>cell2-2</td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
+    '<table><tr><td><table><tr><td>cell1-1</td><td>cell1-2</td></tr><tr><td>cell2-1</td><td><p id="test" data-rows="3" data-cols="2">cell2-2</p></td></tr><tr><td>cell3-1</td><td>cell3-2</td></tr></table></td></tr></table>',
+  ]
+
+  for (const kTest of kTests) {
+    editor.innerHTML = kTest;
+    editor.scrollTop; // compute layout now.
+    let element = document.getElementById("test");
+    getTableEditor().getTableSize(element, rowCount, columnCount);
+    is(rowCount.value.toString(10), element.getAttribute("data-rows"),
+       `Specified an element in a <table> directly, its parent table row count should be retrieved: ${kTest}`);
+    is(columnCount.value.toString(10), element.getAttribute("data-cols"),
+       `Specified an element in a <table> directly, its parent table column count should be retrieved: ${kTest}`);
+    if (element.firstChild && element.firstChild.nodeType == Node.TEXT_NODE) {
+      selection.collapse(element.firstChild, 0);
+      getTableEditor().getTableSize(null, rowCount, columnCount);
+      is(rowCount.value.toString(10), element.getAttribute("data-rows"),
+         `Selection is collapsed in a cell element, its parent table row count should be retrieved: ${kTest}`);
+      is(columnCount.value.toString(10), element.getAttribute("data-cols"),
+         `Selection is collapsed in a cell element, its parent table column count should be retrieved: ${kTest}`);
+    }
+  }
+
+  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>