summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_selection_move_commands.html
blob: e217f8fdfbded42e30a5ae65cbc84c033c2f536e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!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=454004">Mozilla Bug 454004</a>

<iframe id="edit" width="200" height="100" src="about:blank"></iframe>

<script>
SimpleTest.waitForExplicitFinish();
SimpleTest.requestFlakyTimeout("Legacy test, possibly no good reason");

var winUtils = SpecialPowers.getDOMWindowUtils(window);

function* setup() {
  yield SpecialPowers.pushPrefEnv({set: [["general.smoothScroll", false]]});
  winUtils.advanceTimeAndRefresh(100);
}

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';
  body.style.height='400px';
  body.style.padding='0px';
  body.style.margin='0px';
  body.style.borderWidth='0px';

  var sel = win.getSelection();
  doc.designMode='on';
  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 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.
    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) {
    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;
    while (n && n != child) {
      n = n.nextSibling;
      ++i;
    }
    if (!n)
      return -1;
    return i;
  }

  function testPageMoveCommand(cmd, expectOffset) {
    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;
    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;
    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;
  }

  SpecialPowers.doCommand(window, "cmd_scrollBottom");
  yield;
  testScrollCommand("cmd_scrollBottom", root.scrollHeight - 100);
  SpecialPowers.doCommand(window, "cmd_scrollTop");
  yield;
  testScrollCommand("cmd_scrollTop", 0);

  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");
  SpecialPowers.doCommand(window, "cmd_scrollBottom");
  SpecialPowers.doCommand(window, "cmd_scrollPageUp");
  yield;
  testScrollCommand("cmd_scrollPageUp", root.scrollHeight - 100 - pageHeight);

  SpecialPowers.doCommand(window, "cmd_scrollTop");
  SpecialPowers.doCommand(window, "cmd_scrollLineDown");
  yield;
  var lineHeight = -root.getBoundingClientRect().top;
  ok(lineHeight > 0, "Can scroll by lines");
  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);
    SpecialPowers.doCommand(window, "cmd_moveBottom");
    testSelectCommand("cmd_selectTop", node(0), 0);

    SpecialPowers.doCommand(window, "cmd_moveTop");
    testMoveCommand("cmd_lineNext", node(2), 0);
    testMoveCommand("cmd_linePrevious", node(0), 0);
    testSelectCommand("cmd_selectLineNext", node(2), 0);
    SpecialPowers.doCommand(window, "cmd_moveBottom");
    testSelectCommand("cmd_selectLinePrevious", node(20), 2);

    SpecialPowers.doCommand(window, "cmd_moveBottom");
    testMoveCommand("cmd_charPrevious", node(22), 1);
    testMoveCommand("cmd_charNext", node(22), 2);
    testSelectCommand("cmd_selectCharPrevious", node(22), 1);
    SpecialPowers.doCommand(window, "cmd_moveTop");
    testSelectCommand("cmd_selectCharNext", node(0), 1);

    SpecialPowers.doCommand(window, "cmd_moveTop");
    testMoveCommand("cmd_endLine", node(0), 1);
    testMoveCommand("cmd_beginLine", node(0), 0);
    testSelectCommand("cmd_selectEndLine", node(0), 1);
    SpecialPowers.doCommand(window, "cmd_moveBottom");
    testSelectCommand("cmd_selectBeginLine", node(22), 0);

    SpecialPowers.doCommand(window, "cmd_moveBottom");
    testMoveCommand("cmd_wordPrevious", node(22), 0);
    testMoveCommand("cmd_wordNext", body, 23);
    testSelectCommand("cmd_selectWordPrevious", node(22), 0);
    SpecialPowers.doCommand(window, "cmd_moveTop");
    testSelectCommand("cmd_selectWordNext", selectWordNextNode, selectWordNextOffset);

    SpecialPowers.doCommand(window, "cmd_moveTop");
    var lineNum = testPageMoveCommand("cmd_movePageDown", 0);
    ok(lineNum > 0, "cmd_movePageDown works");
    SpecialPowers.doCommand(window, "cmd_moveBottom");
    SpecialPowers.doCommand(window, "cmd_beginLine");
    is(testPageMoveCommand("cmd_movePageUp", 0), 22 - lineNum, "cmd_movePageUp");

    SpecialPowers.doCommand(window, "cmd_moveTop");
    is(testPageSelectCommand("cmd_selectPageDown", 0), lineNum, "cmd_selectPageDown");
    SpecialPowers.doCommand(window, "cmd_moveBottom");
    SpecialPowers.doCommand(window, "cmd_beginLine");
    is(testPageSelectCommand("cmd_selectPageUp", 0), 22 - lineNum, "cmd_selectPageUp");
  }

  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();
}

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));
  }
}

spawn_task(setup)
  .then(() => spawn_task(testRunner))
  .then(() => spawn_task(cleanup))
  .catch(err => ok(false, err));
</script>