Bug 1271119 - Port test_selection_move_commands.xul from chrome to plain; r?masayuki draft
authorAryeh Gregor <ayg@aryeh.name>
Tue, 30 Aug 2016 18:01:33 +0300
changeset 430254 210e7dcb484a9ebbb68144964c893c05efa985b7
parent 429802 3a32dae52b8d5b3de7bc060e00b4d7f3b801d505
child 430255 1e236ced983c072155a6c499c88798b6f762145b
push id33788
push userayg@aryeh.name
push dateThu, 27 Oct 2016 12:44:14 +0000
reviewersmasayuki
bugs1271119, 1299574
milestone52.0a1
Bug 1271119 - Port test_selection_move_commands.xul from chrome to plain; r?masayuki Filed bug 1299574 for the Android todo. (Chrome tests in this directory are disabled for Android, so all this porting enables them.) MozReview-Commit-ID: 6FAioabxq0a
editor/libeditor/tests/chrome.ini
editor/libeditor/tests/mochitest.ini
editor/libeditor/tests/test_selection_move_commands.html
editor/libeditor/tests/test_selection_move_commands.xul
--- a/editor/libeditor/tests/chrome.ini
+++ b/editor/libeditor/tests/chrome.ini
@@ -36,12 +36,11 @@ subsuite = clipboard
 [test_bug1248185.html]
 [test_bug1250010.html]
 [test_bug1257363.html]
 [test_composition_event_created_in_chrome.html]
 [test_contenteditable_text_input_handling.html]
 [test_dragdrop.html]
 skip-if = buildapp == 'mulet'
 [test_htmleditor_keyevent_handling.html]
-[test_selection_move_commands.xul]
 [test_set_document_title_transaction.html]
 [test_texteditor_keyevent_handling.html]
 skip-if = (debug && os=='win') || (os == 'linux') # Bug 1116205, leaks on windows debug, fails delete key on linux
--- a/editor/libeditor/tests/mochitest.ini
+++ b/editor/libeditor/tests/mochitest.ini
@@ -182,8 +182,9 @@ subsuite = clipboard
 [test_backspace_vs.html]
 [test_css_chrome_load_access.html]
 skip-if = toolkit == 'android' # chrome urls not available due to packaging
 [test_bug1247483.html]
 subsuite = clipboard
 skip-if = toolkit == 'android'
 [test_bug1258085.html]
 [test_bug1268736.html]
+[test_selection_move_commands.html]
rename from editor/libeditor/tests/test_selection_move_commands.xul
rename to editor/libeditor/tests/test_selection_move_commands.html
--- a/editor/libeditor/tests/test_selection_move_commands.xul
+++ b/editor/libeditor/tests/test_selection_move_commands.html
@@ -1,41 +1,29 @@
-<?xml version="1.0"?>
-<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
-<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
-<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
-        xmlns:html="http://www.w3.org/1999/xhtml"
-        title="Test for nsSelectionMoveCommands">
-  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+<!doctype html>
+<title>Test for nsSelectionMoveCommands</title>
+<link rel=stylesheet href="/tests/SimpleTest/test.css">
+<script src="/tests/SimpleTest/SimpleTest.js"></script>
+<script src="/tests/SimpleTest/SpawnTask.js"></script>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=372685">Mozilla Bug 372685</a>
 
-<script class="testbody" type="application/javascript">
-<![CDATA[
+<iframe id="edit" width="200" height="100" src="about:blank"></iframe>
 
-var prefs = Components.classes["@mozilla.org/preferences-service;1"]
-                      .getService(Components.interfaces.nsIPrefBranch);
-prefs.setBoolPref("general.smoothScroll", false);
+<script>
+SimpleTest.waitForExplicitFinish();
+SimpleTest.requestFlakyTimeout("Legacy test, possibly no good reason");
 
 var winUtils = SpecialPowers.getDOMWindowUtils(window);
-winUtils.advanceTimeAndRefresh(100);
 
-function runTest() {
-  var tests = execTests();
-  function execNext() {
-    try {
-      winUtils.advanceTimeAndRefresh(100);
-      tests.next();
-      
-      winUtils.advanceTimeAndRefresh(100);
-      setTimeout(execNext, 20);
-    } catch (e) {}
-  }
-  execNext();
+function* setup() {
+  yield SpecialPowers.pushPrefEnv({set: [["general.smoothScroll", false]]});
+  winUtils.advanceTimeAndRefresh(100);
 }
 
-function execTests() {
+function* runTests() {
   var e = document.getElementById("edit");
   var doc = e.contentDocument;
   var win = e.contentWindow;
   var root = doc.documentElement;
   var body = doc.body;
 
   body.style.fontSize='16px';
   body.style.lineHeight='16px';
@@ -49,32 +37,32 @@ function execTests() {
   body.innerHTML = "1<br>2<br>3<br>4<br>5<br>6<br>7<br>8<br>9<br>10<br>11<br>12<br>";
   win.focus();
   // Flush out layout to make sure that the subdocument will be the size we
   // expect by the time we try to scroll it.
   is(body.getBoundingClientRect().height, 400,
      "Body height should be what we set it to");
   yield;
 
-  function doCommand(cmd) {
-    var controller = document.commandDispatcher.getControllerForCommand(cmd);
-    if (controller) {
-      controller.doCommand(cmd);
-    }
-  }
-
   function testScrollCommand(cmd, expectTop) {
     // http://dev.w3.org/csswg/cssom-view/#dom-element-getboundingclientrect
     // doesn't explicitly rule out -0 here, but for now assume that only
     // positive zeroes are permitted.
-    is(root.getBoundingClientRect().top, -expectTop + 0, cmd);
+    if (navigator.appVersion.indexOf("Android") != -1 && expectTop != 0) {
+      // Android doesn't get the values exactly correct for some reason
+      todo_is(root.getBoundingClientRect().top, -expectTop + 0, cmd);
+      ok(Math.abs(root.getBoundingClientRect().top + expectTop) < 0.2,
+         cmd + " (approximately)");
+    } else {
+      is(root.getBoundingClientRect().top, -expectTop + 0, cmd);
+    }
   }
 
   function testMoveCommand(cmd, expectNode, expectOffset) {
-    doCommand(cmd);
+    SpecialPowers.doCommand(window, cmd);
     is(sel.isCollapsed, true, "collapsed after " + cmd);
     is(sel.anchorNode, expectNode, "node after " + cmd);
     is(sel.anchorOffset, expectOffset, "offset after " + cmd);
   }
 
   function findChildNum(e, child) {
     var i = 0;
     var n = e.firstChild;
@@ -83,149 +71,149 @@ function execTests() {
       ++i;
     }
     if (!n)
       return -1;
     return i;
   }
 
   function testPageMoveCommand(cmd, expectOffset) {
-    doCommand(cmd);
+    SpecialPowers.doCommand(window, cmd);
     is(sel.isCollapsed, true, "collapsed after " + cmd);
     is(sel.anchorOffset, expectOffset, "offset after " + cmd);
     return findChildNum(body, sel.anchorNode);
   }
 
   function testSelectCommand(cmd, expectNode, expectOffset) {
     var anchorNode = sel.anchorNode;
     var anchorOffset = sel.anchorOffset;
-    doCommand(cmd);
+    SpecialPowers.doCommand(window, cmd);
     is(sel.isCollapsed, false, "not collapsed after " + cmd);
     is(sel.anchorNode, anchorNode, "anchor not moved after " + cmd);
     is(sel.anchorOffset, anchorOffset, "anchor not moved after " + cmd);
     is(sel.focusNode, expectNode, "node after " + cmd);
     is(sel.focusOffset, expectOffset, "offset after " + cmd);
   }
 
   function testPageSelectCommand(cmd, expectOffset) {
     var anchorNode = sel.anchorNode;
     var anchorOffset = sel.anchorOffset;
-    doCommand(cmd);
+    SpecialPowers.doCommand(window, cmd);
     is(sel.isCollapsed, false, "not collapsed after " + cmd);
     is(sel.anchorNode, anchorNode, "anchor not moved after " + cmd);
     is(sel.anchorOffset, anchorOffset, "anchor not moved after " + cmd);
     is(sel.focusOffset, expectOffset, "offset after " + cmd);
     return findChildNum(body, sel.focusNode);
   }
 
   function node(i) {
     var n = body.firstChild;
     while (i > 0) {
       n = n.nextSibling;
       --i;
     }
     return n;
   }
 
-  doCommand("cmd_scrollBottom");
+  SpecialPowers.doCommand(window, "cmd_scrollBottom");
   yield;
   testScrollCommand("cmd_scrollBottom", root.scrollHeight - 100);
-  doCommand("cmd_scrollTop");
+  SpecialPowers.doCommand(window, "cmd_scrollTop");
   yield;
   testScrollCommand("cmd_scrollTop", 0);
 
-  doCommand("cmd_scrollPageDown");
+  SpecialPowers.doCommand(window, "cmd_scrollPageDown");
   yield;
   var pageHeight = -root.getBoundingClientRect().top;
   ok(pageHeight > 0, "cmd_scrollPageDown works");
   ok(pageHeight <= 100, "cmd_scrollPageDown doesn't scroll too much");
-  doCommand("cmd_scrollBottom");
-  doCommand("cmd_scrollPageUp");
+  SpecialPowers.doCommand(window, "cmd_scrollBottom");
+  SpecialPowers.doCommand(window, "cmd_scrollPageUp");
   yield;
   testScrollCommand("cmd_scrollPageUp", root.scrollHeight - 100 - pageHeight);
 
-  doCommand("cmd_scrollTop");
-  doCommand("cmd_scrollLineDown");
+  SpecialPowers.doCommand(window, "cmd_scrollTop");
+  SpecialPowers.doCommand(window, "cmd_scrollLineDown");
   yield;
   var lineHeight = -root.getBoundingClientRect().top;
   ok(lineHeight > 0, "Can scroll by lines");
-  doCommand("cmd_scrollBottom");
-  doCommand("cmd_scrollLineUp");
+  SpecialPowers.doCommand(window, "cmd_scrollBottom");
+  SpecialPowers.doCommand(window, "cmd_scrollLineUp");
   yield;
   testScrollCommand("cmd_scrollLineUp", root.scrollHeight - 100 - lineHeight);
 
   var runSelectionTests = function(selectWordNextNode, selectWordNextOffset) {
     testMoveCommand("cmd_moveBottom", body, 23);
     testMoveCommand("cmd_moveTop", node(0), 0);
     testSelectCommand("cmd_selectBottom", body, 23);
-    doCommand("cmd_moveBottom");
+    SpecialPowers.doCommand(window, "cmd_moveBottom");
     testSelectCommand("cmd_selectTop", node(0), 0);
 
-    doCommand("cmd_moveTop");
+    SpecialPowers.doCommand(window, "cmd_moveTop");
     testMoveCommand("cmd_lineNext", node(2), 0);
     testMoveCommand("cmd_linePrevious", node(0), 0);
     testSelectCommand("cmd_selectLineNext", node(2), 0);
-    doCommand("cmd_moveBottom");
+    SpecialPowers.doCommand(window, "cmd_moveBottom");
     testSelectCommand("cmd_selectLinePrevious", node(20), 2);
 
-    doCommand("cmd_moveBottom");
+    SpecialPowers.doCommand(window, "cmd_moveBottom");
     testMoveCommand("cmd_charPrevious", node(22), 1);
     testMoveCommand("cmd_charNext", node(22), 2);
     testSelectCommand("cmd_selectCharPrevious", node(22), 1);
-    doCommand("cmd_moveTop");
+    SpecialPowers.doCommand(window, "cmd_moveTop");
     testSelectCommand("cmd_selectCharNext", node(0), 1);
 
-    doCommand("cmd_moveTop");
+    SpecialPowers.doCommand(window, "cmd_moveTop");
     testMoveCommand("cmd_endLine", node(0), 1);
     testMoveCommand("cmd_beginLine", node(0), 0);
     testSelectCommand("cmd_selectEndLine", node(0), 1);
-    doCommand("cmd_moveBottom");
+    SpecialPowers.doCommand(window, "cmd_moveBottom");
     testSelectCommand("cmd_selectBeginLine", node(22), 0);
 
-    doCommand("cmd_moveBottom");
+    SpecialPowers.doCommand(window, "cmd_moveBottom");
     testMoveCommand("cmd_wordPrevious", node(22), 0);
     testMoveCommand("cmd_wordNext", body, 23);
     testSelectCommand("cmd_selectWordPrevious", node(22), 0);
-    doCommand("cmd_moveTop");
+    SpecialPowers.doCommand(window, "cmd_moveTop");
     testSelectCommand("cmd_selectWordNext", selectWordNextNode, selectWordNextOffset);
 
-    doCommand("cmd_moveTop");
+    SpecialPowers.doCommand(window, "cmd_moveTop");
     var lineNum = testPageMoveCommand("cmd_movePageDown", 0);
     ok(lineNum > 0, "cmd_movePageDown works");
-    doCommand("cmd_moveBottom");
-    doCommand("cmd_beginLine");
+    SpecialPowers.doCommand(window, "cmd_moveBottom");
+    SpecialPowers.doCommand(window, "cmd_beginLine");
     is(testPageMoveCommand("cmd_movePageUp", 0), 22 - lineNum, "cmd_movePageUp");
 
-    doCommand("cmd_moveTop");
+    SpecialPowers.doCommand(window, "cmd_moveTop");
     is(testPageSelectCommand("cmd_selectPageDown", 0), lineNum, "cmd_selectPageDown");
-    doCommand("cmd_moveBottom");
-    doCommand("cmd_beginLine");
+    SpecialPowers.doCommand(window, "cmd_moveBottom");
+    SpecialPowers.doCommand(window, "cmd_beginLine");
     is(testPageSelectCommand("cmd_selectPageUp", 0), 22 - lineNum, "cmd_selectPageUp");
   }
 
-  try {
-    prefs.setBoolPref("layout.word_select.eat_space_to_next_word", false);
-    runSelectionTests(body, 1);
-    prefs.setBoolPref("layout.word_select.eat_space_to_next_word", true);
-    runSelectionTests(node(2), 0);
-  } finally {
-    prefs.clearUserPref("general.smoothScroll");
-    prefs.clearUserPref("layout.word_select.eat_space_to_next_word");
-  }
+  yield SpecialPowers.pushPrefEnv({set: [["layout.word_select.eat_space_to_next_word", false]]});
+  runSelectionTests(body, 1);
+  yield SpecialPowers.pushPrefEnv({set: [["layout.word_select.eat_space_to_next_word", true]]});
+  runSelectionTests(node(2), 0);
+}
 
+function cleanup() {
   winUtils.restoreNormalRefresh();
   SimpleTest.finish();
 }
 
-SimpleTest.waitForExplicitFinish();
-addLoadEvent(runTest);
-]]>
-</script>
+function* testRunner() {
+  let curTest = runTests();
+  while (true) {
+    winUtils.advanceTimeAndRefresh(100);
+    if (curTest.next().done) {
+      break;
+    }
+    winUtils.advanceTimeAndRefresh(100);
+    yield new Promise(resolve => setTimeout(resolve, 20));
+  }
+}
 
-<body  id="html_body" xmlns="http://www.w3.org/1999/xhtml">
-<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=372685">Mozilla Bug 372685</a>
-<p id="display"></p>
-
-<pre id="test">
-</pre>
-<iframe id="edit" width="200" height="100" src="about:blank"/>
-</body>
-</window>
+spawn_task(setup)
+  .then(() => spawn_task(testRunner))
+  .then(() => spawn_task(cleanup))
+  .catch(err => ok(false, err));
+</script>