Bug 1482017 - part 0: Add automated tests for nsIHTMLEditor.setCaretAfterElement() r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 15 Aug 2018 12:46:51 +0000
changeset 486725 b6f1266dd6c5bf04a6dc68a5e7a19dd5f5230f51
parent 486724 413c0f58374c3fd8af375ea1b680cb86c4a1492b
child 486726 581fb40d63fb10e1ea8b29ca2b52deb12b914bc3
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1482017, 1480702
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 1482017 - part 0: Add automated tests for nsIHTMLEditor.setCaretAfterElement() r=m_kato The new test is disabled only debug build on Android because adding this test causes permanent orange of non-related test, dom/tests/mochitest/fetch/test_request.html, see bug 1480702. Differential Revision: https://phabricator.services.mozilla.com/D3187
editor/libeditor/tests/mochitest.ini
editor/libeditor/tests/test_nsIHTMLEditor_setCaretAfterElement.html
--- a/editor/libeditor/tests/mochitest.ini
+++ b/editor/libeditor/tests/mochitest.ini
@@ -278,16 +278,18 @@ skip-if = android_version == '24'
 [test_inlineTableEditing.html]
 [test_insertParagraph_in_inline_editing_host.html]
 [test_keypress_untrusted_event.html]
 [test_middle_click_paste.html]
 subsuite = clipboard
 skip-if = android_version == '24'
 [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_objectResizing.html]
 [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]
 skip-if = toolkit == 'android'
 [test_undo_redo_stack_after_setting_value.html]
new file mode 100644
--- /dev/null
+++ b/editor/libeditor/tests/test_nsIHTMLEditor_setCaretAfterElement.html
@@ -0,0 +1,149 @@
+<!DOCTYPE>
+<html>
+<head>
+  <title>Test for nsIHTMLEditor.setCaretAfterElement()</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 = window.getSelection();
+
+  editor.innerHTML = "<p>p1<b>b1</b><i>i1</i></p><p><b>b2</b><i>i2</i>p2</p>";
+
+  editor.focus();
+  try {
+    getHTMLEditor().setCaretAfterElement(editor.firstChild.firstChild);
+    ok(false, "nsIHTMLEditor.setCaretAfterElement() should throw an exception if given node is not an element");
+  } catch (e) {
+    ok(true, `nsIHTMLEditor.setCaretAfterElement() should throw an exception if given node is not an element: ${e}`);
+  }
+
+  editor.focus();
+  try {
+    getHTMLEditor().setCaretAfterElement(editor.firstChild.firstChild.nextSibling);
+    ok(selection.isCollapsed,
+       "nsIHTMLEditor.setCaretAfterElement() should collapse Selection #1");
+    is(selection.anchorNode, editor.firstChild,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor node to parent of <b> element in the first paragraph");
+    is(selection.anchorOffset, 2,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor offset to the index of <b> element + 1 in the first paragraph");
+  } catch (e) {
+    ok(false, `nsIHTMLEditor.setCaretAfterElement() shouldn't throw exception when selecting an element in focused editor #1: ${e}`);
+  }
+
+  editor.focus();
+  try {
+    getHTMLEditor().setCaretAfterElement(editor.firstChild.firstChild.nextSibling.nextSibling);
+    ok(selection.isCollapsed,
+       "nsIHTMLEditor.setCaretAfterElement() should collapse Selection #2");
+    is(selection.anchorNode, editor.firstChild,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor node to parent of <i> element in the first paragraph");
+    is(selection.anchorOffset, 3,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor offset to the index of <i> element + 1 in the first paragraph");
+  } catch (e) {
+    ok(false, `nsIHTMLEditor.setCaretAfterElement() shouldn't throw exception when selecting an element in focused editor #2: ${e}`);
+  }
+
+  editor.focus();
+  try {
+    getHTMLEditor().setCaretAfterElement(editor.firstChild.nextSibling.firstChild);
+    ok(selection.isCollapsed,
+       "nsIHTMLEditor.setCaretAfterElement() should collapse Selection #3");
+    is(selection.anchorNode, editor.firstChild.nextSibling,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor node to parent of <b> element in the second paragraph");
+    is(selection.anchorOffset, 1,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor offset to the index of <b> element + 1 in the second paragraph");
+  } catch (e) {
+    ok(false, `nsIHTMLEditor.setCaretAfterElement() shouldn't throw exception when selecting an element in focused editor #3: ${e}`);
+  }
+
+  editor.focus();
+  try {
+    getHTMLEditor().setCaretAfterElement(editor);
+    ok(false, "nsIHTMLEditor.setCaretAfterElement() should throw an exception if given node is the editing host");
+  } catch (e) {
+    ok(true, `nsIHTMLEditor.setCaretAfterElement() should throw an exception if given node is the editing host: ${e}`);
+  }
+
+  editor.focus();
+  try {
+    getHTMLEditor().setCaretAfterElement(editor.parentElement);
+    ok(false, "nsIHTMLEditor.setCaretAfterElement() should throw an exception if given node is outside of the editing host");
+  } catch (e) {
+    ok(true, `nsIHTMLEditor.setCaretAfterElement() should throw an exception if given node is outside of the editing host: ${e}`);
+  }
+
+  selection.removeAllRanges();
+  editor.blur();
+  try {
+    getHTMLEditor().setCaretAfterElement(editor.firstChild.nextSibling.firstChild);
+    ok(false, "nsIHTMLEditor.setCaretAfterElement() should throw an exception if there is no active editing host");
+  } catch (e) {
+    ok(true, `nsIHTMLEditor.setCaretAfterElement() should throw an exception if there is no active editing host: ${e}`);
+  }
+
+  editor.focus();
+  editor.firstChild.firstChild.nextSibling.nextSibling.setAttribute("contenteditable", "false");
+  try {
+    getHTMLEditor().setCaretAfterElement(editor.firstChild.firstChild.nextSibling.nextSibling);
+    ok(selection.isCollapsed,
+       "nsIHTMLEditor.setCaretAfterElement() should collapse Selection #4");
+    is(selection.anchorNode, editor.firstChild,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor node to parent of <i contenteditable=\"false\"> element in the first paragraph");
+    is(selection.anchorOffset, 3,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor offset to the index of <i contenteditable=\"false\"> element + 1 in the first paragraph");
+  } catch (e) {
+    ok(false, `nsIHTMLEditor.setCaretAfterElement() shouldn't throw exception when selecting an element in focused editor #4: ${e}`);
+  }
+
+  editor.focus();
+  try {
+    getHTMLEditor().setCaretAfterElement(editor.firstChild.firstChild.nextSibling);
+    ok(selection.isCollapsed,
+       "nsIHTMLEditor.setCaretAfterElement() should collapse Selection #5");
+    is(selection.anchorNode, editor.firstChild,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor node to parent of <b> element in the first paragraph");
+    is(selection.anchorOffset, 2,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor offset to the index of <b> element in the first paragraph");
+  } catch (e) {
+    ok(false, `nsIHTMLEditor.setCaretAfterElement() shouldn't throw exception when selecting an element in focused editor #5: ${e}`);
+  }
+
+  editor.focus();
+  editor.firstChild.nextSibling.setAttribute("contenteditable", "false");
+  try {
+    getHTMLEditor().setCaretAfterElement(editor.firstChild.nextSibling.firstChild.nextSibling);
+    ok(selection.isCollapsed,
+       "nsIHTMLEditor.setCaretAfterElement() should collapse Selection #6");
+    is(selection.anchorNode, editor.firstChild.nextSibling,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor node to parent of <i> element in the second paragraph which is not editable");
+    is(selection.anchorOffset, 2,
+       "nsIHTMLEditor.setCaretAfterElement() should set anchor offset to the index of <i> element + 1 in the second paragraph which is not editable");
+  } catch (e) {
+    ok(false, `nsIHTMLEditor.selectElement() shouldn't throw exception when selecting an element in focused editor #6: ${e}`);
+  }
+
+  SimpleTest.finish();
+});
+
+function getHTMLEditor() {
+  var Ci = SpecialPowers.Ci;
+  var editingSession = SpecialPowers.wrap(window).docShell.editingSession;
+  return editingSession.getEditorForWindow(window).QueryInterface(Ci.nsIHTMLEditor);
+}
+
+</script>
+</body>
+
+</html>