editor/libeditor/base/tests/test_selection_move_commands.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/editor/libeditor/base/tests/test_selection_move_commands.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,228 @@
     1.4 +<?xml version="1.0"?>
     1.5 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
     1.6 +<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?>
     1.7 +<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
     1.8 +        xmlns:html="http://www.w3.org/1999/xhtml"
     1.9 +        title="Test for nsSelectionMoveCommands">
    1.10 +  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
    1.11 +
    1.12 +<script class="testbody" type="application/javascript">
    1.13 +<![CDATA[
    1.14 +
    1.15 +var prefs = Components.classes["@mozilla.org/preferences-service;1"]
    1.16 +                      .getService(Components.interfaces.nsIPrefBranch);
    1.17 +prefs.setBoolPref("general.smoothScroll", false);
    1.18 +
    1.19 +var winUtils = SpecialPowers.getDOMWindowUtils(window);
    1.20 +winUtils.advanceTimeAndRefresh(100);
    1.21 +
    1.22 +function runTest() {
    1.23 +  var tests = execTests();
    1.24 +  function execNext() {
    1.25 +    try {
    1.26 +      winUtils.advanceTimeAndRefresh(100);
    1.27 +      tests.next();
    1.28 +      
    1.29 +      winUtils.advanceTimeAndRefresh(100);
    1.30 +      setTimeout(execNext, 20);
    1.31 +    } catch (e) {}
    1.32 +  }
    1.33 +  execNext();
    1.34 +}
    1.35 +
    1.36 +function execTests() {
    1.37 +  var e = document.getElementById("edit");
    1.38 +  var doc = e.contentDocument;
    1.39 +  var win = e.contentWindow;
    1.40 +  var root = doc.documentElement;
    1.41 +  var body = doc.body;
    1.42 +
    1.43 +  body.style.fontSize='16px';
    1.44 +  body.style.lineHeight='16px';
    1.45 +  body.style.height='400px';
    1.46 +  body.style.padding='0px';
    1.47 +  body.style.margin='0px';
    1.48 +  body.style.borderWidth='0px';
    1.49 +
    1.50 +  var sel = win.getSelection();
    1.51 +  doc.designMode='on';
    1.52 +  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>";
    1.53 +  win.focus();
    1.54 +  // Flush out layout to make sure that the subdocument will be the size we
    1.55 +  // expect by the time we try to scroll it.
    1.56 +  is(body.getBoundingClientRect().height, 400,
    1.57 +     "Body height should be what we set it to");
    1.58 +  yield;
    1.59 +
    1.60 +  function doCommand(cmd) {
    1.61 +    var controller = document.commandDispatcher.getControllerForCommand(cmd);
    1.62 +    if (controller) {
    1.63 +      controller.doCommand(cmd);
    1.64 +    }
    1.65 +  }
    1.66 +
    1.67 +  function testScrollCommand(cmd, expectTop) {
    1.68 +    is(root.getBoundingClientRect().top, -expectTop, cmd);
    1.69 +  }
    1.70 +
    1.71 +  function testMoveCommand(cmd, expectNode, expectOffset) {
    1.72 +    doCommand(cmd);
    1.73 +    is(sel.isCollapsed, true, "collapsed after " + cmd);
    1.74 +    is(sel.anchorNode, expectNode, "node after " + cmd);
    1.75 +    is(sel.anchorOffset, expectOffset, "offset after " + cmd);
    1.76 +  }
    1.77 +
    1.78 +  function findChildNum(e, child) {
    1.79 +    var i = 0;
    1.80 +    var n = e.firstChild;
    1.81 +    while (n && n != child) {
    1.82 +      n = n.nextSibling;
    1.83 +      ++i;
    1.84 +    }
    1.85 +    if (!n)
    1.86 +      return -1;
    1.87 +    return i;
    1.88 +  }
    1.89 +
    1.90 +  function testPageMoveCommand(cmd, expectOffset) {
    1.91 +    doCommand(cmd);
    1.92 +    is(sel.isCollapsed, true, "collapsed after " + cmd);
    1.93 +    is(sel.anchorOffset, expectOffset, "offset after " + cmd);
    1.94 +    return findChildNum(body, sel.anchorNode);
    1.95 +  }
    1.96 +
    1.97 +  function testSelectCommand(cmd, expectNode, expectOffset) {
    1.98 +    var anchorNode = sel.anchorNode;
    1.99 +    var anchorOffset = sel.anchorOffset;
   1.100 +    doCommand(cmd);
   1.101 +    is(sel.isCollapsed, false, "not collapsed after " + cmd);
   1.102 +    is(sel.anchorNode, anchorNode, "anchor not moved after " + cmd);
   1.103 +    is(sel.anchorOffset, anchorOffset, "anchor not moved after " + cmd);
   1.104 +    is(sel.focusNode, expectNode, "node after " + cmd);
   1.105 +    is(sel.focusOffset, expectOffset, "offset after " + cmd);
   1.106 +  }
   1.107 +
   1.108 +  function testPageSelectCommand(cmd, expectOffset) {
   1.109 +    var anchorNode = sel.anchorNode;
   1.110 +    var anchorOffset = sel.anchorOffset;
   1.111 +    doCommand(cmd);
   1.112 +    is(sel.isCollapsed, false, "not collapsed after " + cmd);
   1.113 +    is(sel.anchorNode, anchorNode, "anchor not moved after " + cmd);
   1.114 +    is(sel.anchorOffset, anchorOffset, "anchor not moved after " + cmd);
   1.115 +    is(sel.focusOffset, expectOffset, "offset after " + cmd);
   1.116 +    return findChildNum(body, sel.focusNode);
   1.117 +  }
   1.118 +
   1.119 +  function node(i) {
   1.120 +    var n = body.firstChild;
   1.121 +    while (i > 0) {
   1.122 +      n = n.nextSibling;
   1.123 +      --i;
   1.124 +    }
   1.125 +    return n;
   1.126 +  }
   1.127 +
   1.128 +  doCommand("cmd_scrollBottom");
   1.129 +  yield;
   1.130 +  testScrollCommand("cmd_scrollBottom", root.scrollHeight - 100);
   1.131 +  doCommand("cmd_scrollTop");
   1.132 +  yield;
   1.133 +  testScrollCommand("cmd_scrollTop", 0);
   1.134 +
   1.135 +  doCommand("cmd_scrollPageDown");
   1.136 +  yield;
   1.137 +  var pageHeight = -root.getBoundingClientRect().top;
   1.138 +  ok(pageHeight > 0, "cmd_scrollPageDown works");
   1.139 +  ok(pageHeight <= 100, "cmd_scrollPageDown doesn't scroll too much");
   1.140 +  doCommand("cmd_scrollBottom");
   1.141 +  doCommand("cmd_scrollPageUp");
   1.142 +  yield;
   1.143 +  testScrollCommand("cmd_scrollPageUp", root.scrollHeight - 100 - pageHeight);
   1.144 +
   1.145 +  doCommand("cmd_scrollTop");
   1.146 +  doCommand("cmd_scrollLineDown");
   1.147 +  yield;
   1.148 +  var lineHeight = -root.getBoundingClientRect().top;
   1.149 +  ok(lineHeight > 0, "Can scroll by lines");
   1.150 +  doCommand("cmd_scrollBottom");
   1.151 +  doCommand("cmd_scrollLineUp");
   1.152 +  yield;
   1.153 +  testScrollCommand("cmd_scrollLineUp", root.scrollHeight - 100 - lineHeight);
   1.154 +
   1.155 +  var runSelectionTests = function(selectWordNextNode, selectWordNextOffset) {
   1.156 +    testMoveCommand("cmd_moveBottom", body, 23);
   1.157 +    testMoveCommand("cmd_moveTop", node(0), 0);
   1.158 +    testSelectCommand("cmd_selectBottom", body, 23);
   1.159 +    doCommand("cmd_moveBottom");
   1.160 +    testSelectCommand("cmd_selectTop", node(0), 0);
   1.161 +
   1.162 +    doCommand("cmd_moveTop");
   1.163 +    testMoveCommand("cmd_lineNext", node(2), 0);
   1.164 +    testMoveCommand("cmd_linePrevious", node(0), 0);
   1.165 +    testSelectCommand("cmd_selectLineNext", node(2), 0);
   1.166 +    doCommand("cmd_moveBottom");
   1.167 +    testSelectCommand("cmd_selectLinePrevious", node(20), 2);
   1.168 +
   1.169 +    doCommand("cmd_moveBottom");
   1.170 +    testMoveCommand("cmd_charPrevious", node(22), 1);
   1.171 +    testMoveCommand("cmd_charNext", node(22), 2);
   1.172 +    testSelectCommand("cmd_selectCharPrevious", node(22), 1);
   1.173 +    doCommand("cmd_moveTop");
   1.174 +    testSelectCommand("cmd_selectCharNext", node(0), 1);
   1.175 +
   1.176 +    doCommand("cmd_moveTop");
   1.177 +    testMoveCommand("cmd_endLine", body, 1);
   1.178 +    testMoveCommand("cmd_beginLine", node(0), 0);
   1.179 +    testSelectCommand("cmd_selectEndLine", body, 1);
   1.180 +    doCommand("cmd_moveBottom");
   1.181 +    testSelectCommand("cmd_selectBeginLine", node(22), 0);
   1.182 +
   1.183 +    doCommand("cmd_moveBottom");
   1.184 +    testMoveCommand("cmd_wordPrevious", node(22), 0);
   1.185 +    testMoveCommand("cmd_wordNext", body, 23);
   1.186 +    testSelectCommand("cmd_selectWordPrevious", node(22), 0);
   1.187 +    doCommand("cmd_moveTop");
   1.188 +    testSelectCommand("cmd_selectWordNext", selectWordNextNode, selectWordNextOffset);
   1.189 +
   1.190 +    doCommand("cmd_moveTop");
   1.191 +    var lineNum = testPageMoveCommand("cmd_movePageDown", 0);
   1.192 +    ok(lineNum > 0, "cmd_movePageDown works");
   1.193 +    doCommand("cmd_moveBottom");
   1.194 +    doCommand("cmd_beginLine");
   1.195 +    is(testPageMoveCommand("cmd_movePageUp", 0), 22 - lineNum, "cmd_movePageUp");
   1.196 +
   1.197 +    doCommand("cmd_moveTop");
   1.198 +    is(testPageSelectCommand("cmd_selectPageDown", 0), lineNum, "cmd_selectPageDown");
   1.199 +    doCommand("cmd_moveBottom");
   1.200 +    doCommand("cmd_beginLine");
   1.201 +    is(testPageSelectCommand("cmd_selectPageUp", 0), 22 - lineNum, "cmd_selectPageUp");
   1.202 +  }
   1.203 +
   1.204 +  try {
   1.205 +    prefs.setBoolPref("layout.word_select.eat_space_to_next_word", false);
   1.206 +    runSelectionTests(body, 1);
   1.207 +    prefs.setBoolPref("layout.word_select.eat_space_to_next_word", true);
   1.208 +    runSelectionTests(node(2), 0);
   1.209 +  } finally {
   1.210 +    prefs.clearUserPref("general.smoothScroll");
   1.211 +    prefs.clearUserPref("layout.word_select.eat_space_to_next_word");
   1.212 +  }
   1.213 +
   1.214 +  winUtils.restoreNormalRefresh();
   1.215 +  SimpleTest.finish();
   1.216 +}
   1.217 +
   1.218 +SimpleTest.waitForExplicitFinish();
   1.219 +addLoadEvent(runTest);
   1.220 +]]>
   1.221 +</script>
   1.222 +
   1.223 +<body  id="html_body" xmlns="http://www.w3.org/1999/xhtml">
   1.224 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=372685">Mozilla Bug 372685</a>
   1.225 +<p id="display"></p>
   1.226 +
   1.227 +<pre id="test">
   1.228 +</pre>
   1.229 +<iframe id="edit" width="200" height="100" src="about:blank"/>
   1.230 +</body>
   1.231 +</window>

mercurial