|
1 <!DOCTYPE HTML> |
|
2 <html> |
|
3 <head> |
|
4 <title>Test for nsIDOMWindowUtils.sendQueryContentEvent() and .sendSelectionSetEvent()</title> |
|
5 <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script> |
|
6 <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/> |
|
7 </head> |
|
8 <body> |
|
9 <p id="display"></p> |
|
10 <div id="content" style="display: none"> |
|
11 |
|
12 </div> |
|
13 <div id="editor" contenteditable>abc<br>def</div> |
|
14 <pre id="test"> |
|
15 <script type="application/javascript"> |
|
16 |
|
17 var editor = document.getElementById("editor"); |
|
18 |
|
19 SimpleTest.waitForExplicitFinish(); |
|
20 |
|
21 const kIsWin = navigator.platform.indexOf("Win") == 0; |
|
22 const kIsMac = navigator.platform.indexOf("Mac") == 0; |
|
23 |
|
24 const kLineBreak = kIsWin ? "\r\n" : kIsMac ? "\r" : "\n"; |
|
25 |
|
26 var gUtils = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) |
|
27 .getInterface(Components.interfaces.nsIDOMWindowUtils); |
|
28 |
|
29 function escape(aStr) |
|
30 { |
|
31 var result = aStr.replace("\n", "\\n"); |
|
32 return result.replace("\r", "\\r"); |
|
33 } |
|
34 |
|
35 function runTests() |
|
36 { |
|
37 editor.focus(); |
|
38 |
|
39 // NOTE: For compatibility, calling without flags should work as with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK. |
|
40 |
|
41 // QueryTextContent |
|
42 var expectedStr = escape(("abc" + kLineBreak + "def").substr(2, 4)); |
|
43 var result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0, |
|
44 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); |
|
45 ok(result.succeeded, |
|
46 "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); |
|
47 is(escape(result.text), expectedStr, |
|
48 "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) got unexpected string"); |
|
49 |
|
50 result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0); |
|
51 ok(result.succeeded, |
|
52 "sendQueryContentEvent(QUERY_TEXT_CONTENT) should succeed"); |
|
53 is(escape(result.text), expectedStr, |
|
54 "sendQueryContentEvent(QUERY_TEXT_CONTENT) should return same string as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
55 |
|
56 expectedStr = escape(("abc\ndef").substr(2, 4)); |
|
57 result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_CONTENT, 2, 4, 0, 0, |
|
58 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); |
|
59 ok(result.succeeded, |
|
60 "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); |
|
61 is(escape(result.text), expectedStr, |
|
62 "sendQueryContentEvent(QUERY_TEXT_CONTENT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) got unexpected string"); |
|
63 |
|
64 // QueryCaretRect |
|
65 window.getSelection().collapse(editor.firstChild, 0); |
|
66 |
|
67 var caretNative = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6, 0, 0, 0, |
|
68 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); |
|
69 ok(caretNative.succeeded, |
|
70 "sendQueryContentEvent(QUERY_CARET_RECT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); |
|
71 var caretXP = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6 - kLineBreak.length + 1, 0, 0, 0, |
|
72 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); |
|
73 ok(caretXP.succeeded, |
|
74 "sendQueryContentEvent(QUERY_CARET_RECT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); |
|
75 |
|
76 is(caretXP.top, caretNative.top, |
|
77 "The caret top should be same"); |
|
78 is(caretXP.left, caretNative.left, |
|
79 "The caret left should be same"); |
|
80 |
|
81 result = gUtils.sendQueryContentEvent(gUtils.QUERY_CARET_RECT, 6, 0, 0, 0); |
|
82 ok(result.succeeded, |
|
83 "sendQueryContentEvent(QUERY_CARET_RECT) should succeed"); |
|
84 is(result.top, caretNative.top, |
|
85 "sendQueryContentEvent(QUERY_CARET_RECT) should return same top as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
86 is(result.left, caretNative.left, |
|
87 "sendQueryContentEvent(QUERY_CARET_RECT) should return same left as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
88 |
|
89 // QueryTextRect |
|
90 var textRectNative = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6, 1, 0, 0, |
|
91 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); |
|
92 ok(textRectNative.succeeded, |
|
93 "sendQueryContentEvent(QUERY_TEXT_RECT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); |
|
94 var textRectXP = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6 - kLineBreak.length + 1, 1, 0, 0, |
|
95 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); |
|
96 ok(textRectXP.succeeded, |
|
97 "sendQueryContentEvent(QUERY_TEXT_RECT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); |
|
98 |
|
99 is(textRectXP.top, textRectNative.top, |
|
100 "The text top should be same"); |
|
101 is(textRectXP.left, textRectNative.left, |
|
102 "The text left should be same"); |
|
103 is(textRectXP.height, textRectNative.height, |
|
104 "The text height should be same"); |
|
105 is(textRectXP.width, textRectNative.width, |
|
106 "The text width should be same"); |
|
107 |
|
108 result = gUtils.sendQueryContentEvent(gUtils.QUERY_TEXT_RECT, 6, 1, 0, 0); |
|
109 ok(result.succeeded, |
|
110 "sendQueryContentEvent(QUERY_TEXT_RECT) should succeed"); |
|
111 is(result.top, textRectNative.top, |
|
112 "sendQueryContentEvent(QUERY_TEXT_RECT) should return same top as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
113 is(result.left, textRectNative.left, |
|
114 "sendQueryContentEvent(QUERY_TEXT_RECT) should return same left as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
115 is(result.height, textRectNative.height, |
|
116 "sendQueryContentEvent(QUERY_TEXT_RECT) should return same height as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
117 is(result.width, textRectNative.width, |
|
118 "sendQueryContentEvent(QUERY_TEXT_RECT) should return same width as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
119 |
|
120 // QueryCharacterAtOffset |
|
121 result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectNative.left + 1, textRectNative.top + 1, |
|
122 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); |
|
123 ok(result.succeeded, |
|
124 "sendQueryContentEvent(QUERY_CHARACTER_AT_POINT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); |
|
125 is(result.top, textRectNative.top, |
|
126 "The character top is wrong"); |
|
127 is(result.left, textRectNative.left, |
|
128 "The character left is wrong"); |
|
129 is(result.height, textRectNative.height, |
|
130 "The character height is wrong"); |
|
131 is(result.width, textRectNative.width, |
|
132 "The character width is wrong"); |
|
133 is(result.offset, 6, |
|
134 "The character offset is wrong"); |
|
135 |
|
136 result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectNative.left + 1, textRectNative.top + 1); |
|
137 ok(result.succeeded, |
|
138 "sendQueryContentEvent(QUERY_CHARACTER_AT_POINT) should succeed"); |
|
139 is(result.top, textRectNative.top, |
|
140 "The character top should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
141 is(result.left, textRectNative.left, |
|
142 "The character left should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
143 is(result.height, textRectNative.height, |
|
144 "The character height should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
145 is(result.width, textRectNative.width, |
|
146 "The character width should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
147 is(result.offset, 6, |
|
148 "The character offset should be same as calling with QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK"); |
|
149 |
|
150 result = gUtils.sendQueryContentEvent(gUtils.QUERY_CHARACTER_AT_POINT, 0, 0, textRectXP.left + 1, textRectXP.top + 1, |
|
151 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); |
|
152 ok(result.succeeded, |
|
153 "sendQueryContentEvent(QUERY_CHARACTER_AT_POINT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); |
|
154 is(result.top, textRectXP.top, |
|
155 "The character top is wrong"); |
|
156 is(result.left, textRectXP.left, |
|
157 "The character left is wrong"); |
|
158 is(result.height, textRectXP.height, |
|
159 "The character height is wrong"); |
|
160 is(result.width, textRectXP.width, |
|
161 "The character width is wrong"); |
|
162 is(result.offset, 6 - kLineBreak.length + 1, |
|
163 "The character offset is wrong"); |
|
164 |
|
165 // SelectionSet and QuerySelectedText |
|
166 var selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK); |
|
167 ok(selectionSet, |
|
168 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); |
|
169 expectedStr = escape(("abc" + kLineBreak + "def").substr(0, 6)); |
|
170 |
|
171 result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0, |
|
172 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); |
|
173 ok(result.succeeded, |
|
174 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); |
|
175 ok(!result.reversed, |
|
176 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should set non-reversed selection"); |
|
177 is(escape(result.text), expectedStr, |
|
178 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) got unexpected string"); |
|
179 |
|
180 selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_XP_LINE_BREAK); |
|
181 ok(selectionSet, |
|
182 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK) should succeed"); |
|
183 expectedStr = escape(("abc\ndef").substr(0, 6)); |
|
184 |
|
185 result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0, |
|
186 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); |
|
187 ok(result.succeeded, |
|
188 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); |
|
189 ok(!result.reversed, |
|
190 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK) should set non-reversed selection"); |
|
191 is(escape(result.text), expectedStr, |
|
192 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) got unexpected string"); |
|
193 |
|
194 var selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK | gUtils.SELECTION_SET_FLAG_REVERSE); |
|
195 ok(selectionSet, |
|
196 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); |
|
197 |
|
198 result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0, |
|
199 gUtils.QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK); |
|
200 ok(result.succeeded, |
|
201 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_NATIVE_LINE_BREAK) should succeed"); |
|
202 ok(result.reversed, |
|
203 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_NATIVE_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should set reversed selection"); |
|
204 |
|
205 selectionSet = gUtils.sendSelectionSetEvent(0, 6, gUtils.SELECTION_SET_FLAG_USE_XP_LINE_BREAK | gUtils.SELECTION_SET_FLAG_REVERSE); |
|
206 ok(selectionSet, |
|
207 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should succeed"); |
|
208 |
|
209 result = gUtils.sendQueryContentEvent(gUtils.QUERY_SELECTED_TEXT, 0, 0, 0, 0, |
|
210 gUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK); |
|
211 ok(result.succeeded, |
|
212 "sendQueryContentEvent(QUERY_SELECTED_TEXT, QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK) should succeed"); |
|
213 ok(result.reversed, |
|
214 "sendSelectionSetEvent(0, 6, SELECTION_SET_FLAG_USE_XP_LINE_BREAK | SELECTION_SET_FLAG_REVERSE) should set reversed selection"); |
|
215 |
|
216 SimpleTest.finish(); |
|
217 } |
|
218 |
|
219 SimpleTest.waitForFocus(runTests); |
|
220 |
|
221 </script> |
|
222 </pre> |
|
223 </body> |
|
224 </html> |