|
1 <?xml version="1.0"?> |
|
2 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> |
|
3 <?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css" type="text/css"?> |
|
4 <window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" |
|
5 xmlns:html="http://www.w3.org/1999/xhtml" |
|
6 title="Test for nsSelectionMoveCommands"> |
|
7 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
8 |
|
9 <script class="testbody" type="application/javascript"> |
|
10 <![CDATA[ |
|
11 |
|
12 var prefs = Components.classes["@mozilla.org/preferences-service;1"] |
|
13 .getService(Components.interfaces.nsIPrefBranch); |
|
14 prefs.setBoolPref("general.smoothScroll", false); |
|
15 |
|
16 var winUtils = SpecialPowers.getDOMWindowUtils(window); |
|
17 winUtils.advanceTimeAndRefresh(100); |
|
18 |
|
19 function runTest() { |
|
20 var tests = execTests(); |
|
21 function execNext() { |
|
22 try { |
|
23 winUtils.advanceTimeAndRefresh(100); |
|
24 tests.next(); |
|
25 |
|
26 winUtils.advanceTimeAndRefresh(100); |
|
27 setTimeout(execNext, 20); |
|
28 } catch (e) {} |
|
29 } |
|
30 execNext(); |
|
31 } |
|
32 |
|
33 function execTests() { |
|
34 var e = document.getElementById("edit"); |
|
35 var doc = e.contentDocument; |
|
36 var win = e.contentWindow; |
|
37 var root = doc.documentElement; |
|
38 var body = doc.body; |
|
39 |
|
40 body.style.fontSize='16px'; |
|
41 body.style.lineHeight='16px'; |
|
42 body.style.height='400px'; |
|
43 body.style.padding='0px'; |
|
44 body.style.margin='0px'; |
|
45 body.style.borderWidth='0px'; |
|
46 |
|
47 var sel = win.getSelection(); |
|
48 doc.designMode='on'; |
|
49 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>"; |
|
50 win.focus(); |
|
51 // Flush out layout to make sure that the subdocument will be the size we |
|
52 // expect by the time we try to scroll it. |
|
53 is(body.getBoundingClientRect().height, 400, |
|
54 "Body height should be what we set it to"); |
|
55 yield; |
|
56 |
|
57 function doCommand(cmd) { |
|
58 var controller = document.commandDispatcher.getControllerForCommand(cmd); |
|
59 if (controller) { |
|
60 controller.doCommand(cmd); |
|
61 } |
|
62 } |
|
63 |
|
64 function testScrollCommand(cmd, expectTop) { |
|
65 is(root.getBoundingClientRect().top, -expectTop, cmd); |
|
66 } |
|
67 |
|
68 function testMoveCommand(cmd, expectNode, expectOffset) { |
|
69 doCommand(cmd); |
|
70 is(sel.isCollapsed, true, "collapsed after " + cmd); |
|
71 is(sel.anchorNode, expectNode, "node after " + cmd); |
|
72 is(sel.anchorOffset, expectOffset, "offset after " + cmd); |
|
73 } |
|
74 |
|
75 function findChildNum(e, child) { |
|
76 var i = 0; |
|
77 var n = e.firstChild; |
|
78 while (n && n != child) { |
|
79 n = n.nextSibling; |
|
80 ++i; |
|
81 } |
|
82 if (!n) |
|
83 return -1; |
|
84 return i; |
|
85 } |
|
86 |
|
87 function testPageMoveCommand(cmd, expectOffset) { |
|
88 doCommand(cmd); |
|
89 is(sel.isCollapsed, true, "collapsed after " + cmd); |
|
90 is(sel.anchorOffset, expectOffset, "offset after " + cmd); |
|
91 return findChildNum(body, sel.anchorNode); |
|
92 } |
|
93 |
|
94 function testSelectCommand(cmd, expectNode, expectOffset) { |
|
95 var anchorNode = sel.anchorNode; |
|
96 var anchorOffset = sel.anchorOffset; |
|
97 doCommand(cmd); |
|
98 is(sel.isCollapsed, false, "not collapsed after " + cmd); |
|
99 is(sel.anchorNode, anchorNode, "anchor not moved after " + cmd); |
|
100 is(sel.anchorOffset, anchorOffset, "anchor not moved after " + cmd); |
|
101 is(sel.focusNode, expectNode, "node after " + cmd); |
|
102 is(sel.focusOffset, expectOffset, "offset after " + cmd); |
|
103 } |
|
104 |
|
105 function testPageSelectCommand(cmd, expectOffset) { |
|
106 var anchorNode = sel.anchorNode; |
|
107 var anchorOffset = sel.anchorOffset; |
|
108 doCommand(cmd); |
|
109 is(sel.isCollapsed, false, "not collapsed after " + cmd); |
|
110 is(sel.anchorNode, anchorNode, "anchor not moved after " + cmd); |
|
111 is(sel.anchorOffset, anchorOffset, "anchor not moved after " + cmd); |
|
112 is(sel.focusOffset, expectOffset, "offset after " + cmd); |
|
113 return findChildNum(body, sel.focusNode); |
|
114 } |
|
115 |
|
116 function node(i) { |
|
117 var n = body.firstChild; |
|
118 while (i > 0) { |
|
119 n = n.nextSibling; |
|
120 --i; |
|
121 } |
|
122 return n; |
|
123 } |
|
124 |
|
125 doCommand("cmd_scrollBottom"); |
|
126 yield; |
|
127 testScrollCommand("cmd_scrollBottom", root.scrollHeight - 100); |
|
128 doCommand("cmd_scrollTop"); |
|
129 yield; |
|
130 testScrollCommand("cmd_scrollTop", 0); |
|
131 |
|
132 doCommand("cmd_scrollPageDown"); |
|
133 yield; |
|
134 var pageHeight = -root.getBoundingClientRect().top; |
|
135 ok(pageHeight > 0, "cmd_scrollPageDown works"); |
|
136 ok(pageHeight <= 100, "cmd_scrollPageDown doesn't scroll too much"); |
|
137 doCommand("cmd_scrollBottom"); |
|
138 doCommand("cmd_scrollPageUp"); |
|
139 yield; |
|
140 testScrollCommand("cmd_scrollPageUp", root.scrollHeight - 100 - pageHeight); |
|
141 |
|
142 doCommand("cmd_scrollTop"); |
|
143 doCommand("cmd_scrollLineDown"); |
|
144 yield; |
|
145 var lineHeight = -root.getBoundingClientRect().top; |
|
146 ok(lineHeight > 0, "Can scroll by lines"); |
|
147 doCommand("cmd_scrollBottom"); |
|
148 doCommand("cmd_scrollLineUp"); |
|
149 yield; |
|
150 testScrollCommand("cmd_scrollLineUp", root.scrollHeight - 100 - lineHeight); |
|
151 |
|
152 var runSelectionTests = function(selectWordNextNode, selectWordNextOffset) { |
|
153 testMoveCommand("cmd_moveBottom", body, 23); |
|
154 testMoveCommand("cmd_moveTop", node(0), 0); |
|
155 testSelectCommand("cmd_selectBottom", body, 23); |
|
156 doCommand("cmd_moveBottom"); |
|
157 testSelectCommand("cmd_selectTop", node(0), 0); |
|
158 |
|
159 doCommand("cmd_moveTop"); |
|
160 testMoveCommand("cmd_lineNext", node(2), 0); |
|
161 testMoveCommand("cmd_linePrevious", node(0), 0); |
|
162 testSelectCommand("cmd_selectLineNext", node(2), 0); |
|
163 doCommand("cmd_moveBottom"); |
|
164 testSelectCommand("cmd_selectLinePrevious", node(20), 2); |
|
165 |
|
166 doCommand("cmd_moveBottom"); |
|
167 testMoveCommand("cmd_charPrevious", node(22), 1); |
|
168 testMoveCommand("cmd_charNext", node(22), 2); |
|
169 testSelectCommand("cmd_selectCharPrevious", node(22), 1); |
|
170 doCommand("cmd_moveTop"); |
|
171 testSelectCommand("cmd_selectCharNext", node(0), 1); |
|
172 |
|
173 doCommand("cmd_moveTop"); |
|
174 testMoveCommand("cmd_endLine", body, 1); |
|
175 testMoveCommand("cmd_beginLine", node(0), 0); |
|
176 testSelectCommand("cmd_selectEndLine", body, 1); |
|
177 doCommand("cmd_moveBottom"); |
|
178 testSelectCommand("cmd_selectBeginLine", node(22), 0); |
|
179 |
|
180 doCommand("cmd_moveBottom"); |
|
181 testMoveCommand("cmd_wordPrevious", node(22), 0); |
|
182 testMoveCommand("cmd_wordNext", body, 23); |
|
183 testSelectCommand("cmd_selectWordPrevious", node(22), 0); |
|
184 doCommand("cmd_moveTop"); |
|
185 testSelectCommand("cmd_selectWordNext", selectWordNextNode, selectWordNextOffset); |
|
186 |
|
187 doCommand("cmd_moveTop"); |
|
188 var lineNum = testPageMoveCommand("cmd_movePageDown", 0); |
|
189 ok(lineNum > 0, "cmd_movePageDown works"); |
|
190 doCommand("cmd_moveBottom"); |
|
191 doCommand("cmd_beginLine"); |
|
192 is(testPageMoveCommand("cmd_movePageUp", 0), 22 - lineNum, "cmd_movePageUp"); |
|
193 |
|
194 doCommand("cmd_moveTop"); |
|
195 is(testPageSelectCommand("cmd_selectPageDown", 0), lineNum, "cmd_selectPageDown"); |
|
196 doCommand("cmd_moveBottom"); |
|
197 doCommand("cmd_beginLine"); |
|
198 is(testPageSelectCommand("cmd_selectPageUp", 0), 22 - lineNum, "cmd_selectPageUp"); |
|
199 } |
|
200 |
|
201 try { |
|
202 prefs.setBoolPref("layout.word_select.eat_space_to_next_word", false); |
|
203 runSelectionTests(body, 1); |
|
204 prefs.setBoolPref("layout.word_select.eat_space_to_next_word", true); |
|
205 runSelectionTests(node(2), 0); |
|
206 } finally { |
|
207 prefs.clearUserPref("general.smoothScroll"); |
|
208 prefs.clearUserPref("layout.word_select.eat_space_to_next_word"); |
|
209 } |
|
210 |
|
211 winUtils.restoreNormalRefresh(); |
|
212 SimpleTest.finish(); |
|
213 } |
|
214 |
|
215 SimpleTest.waitForExplicitFinish(); |
|
216 addLoadEvent(runTest); |
|
217 ]]> |
|
218 </script> |
|
219 |
|
220 <body id="html_body" xmlns="http://www.w3.org/1999/xhtml"> |
|
221 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=372685">Mozilla Bug 372685</a> |
|
222 <p id="display"></p> |
|
223 |
|
224 <pre id="test"> |
|
225 </pre> |
|
226 <iframe id="edit" width="200" height="100" src="about:blank"/> |
|
227 </body> |
|
228 </window> |