dom/tests/mochitest/chrome/queryCaretRectUnix.html

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:0217c6028ece
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <title>nsIDOMWindowUtils::sendQueryContentEvent w/QUERY_CARET_RECT test</title>
5 <script type="text/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
8 <style>
9 #text {
10 position: absolute;
11 left: 0em;
12 top: 0em;
13 font-size: 10pt;
14 font-family: monospace;
15 line-height: 20px;
16 letter-spacing: 0px;
17 margin-top:-1px; /* nix the text area border */
18 overflow: hidden;
19 width:800px;
20 height:400px;
21 }
22 #div-hor {
23 position: absolute;
24 left: 0em;
25 border-top:1px solid lightgray;
26 width: 1000px;
27 pointer-events:none;
28 }
29 #div-ver {
30 position: absolute;
31 top: 0em;
32 border-left:1px solid lightgray;
33 height: 500px;
34 pointer-events:none;
35 }
36 </style>
37
38 <script type="application/javascript;version=1.8">
39 let SimpleTest = window.opener.SimpleTest;
40 let Ci = Components.interfaces;
41
42 function ok() { window.opener.ok.apply(window.opener, arguments); }
43 function done() { window.opener.done.apply(window.opener, arguments); }
44
45 function dumpLn() {
46 for (let idx = 0; idx < arguments.length; idx++)
47 dump(arguments[idx] + " ");
48 dump("\n");
49 }
50
51 // drawn grid is 20x20
52 function drawGrid() {
53 for (var idx = 0; idx < 20; idx++) {
54 var e = document.createElement("div");
55 e.setAttribute("id", "div-hor");
56 e.style.top = (idx*20) + "px";
57 document.getElementById("container").appendChild(e);
58 }
59 for (var idx = 0; idx < 40; idx++) {
60 var e = document.createElement("div");
61 e.setAttribute("id", "div-ver");
62 e.style.left = (idx*20) + "px";
63 document.getElementById("container").appendChild(e);
64 }
65 }
66
67 function getSelection(aElement) {
68 if (aElement instanceof Ci.nsIDOMNSEditableElement) {
69 return aElement.QueryInterface(Ci.nsIDOMNSEditableElement)
70 .editor.selection;
71 }
72 return null;
73 }
74
75 function testCaretPosition(aDomWinUtils, aOffset, aRectDims) {
76 let rect = aDomWinUtils.sendQueryContentEvent(
77 aDomWinUtils.QUERY_CARET_RECT,
78 aOffset, 0, 0, 0,
79 aDomWinUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
80 ok(rect, "rect returned");
81 ok(rect.succeeded, "call succeeded");
82
83 info("left:" + rect.left + " top:" + rect.top);
84
85 ok(rect.left > aRectDims.min.left, "rect.left > " + aRectDims.min.left);
86 ok(rect.left < aRectDims.max.left, "rect.left < " + aRectDims.max.left);
87 ok(rect.top > aRectDims.min.top, "rect.top > " + aRectDims.min.top);
88 ok(rect.top < aRectDims.max.top, "rect.top < " + aRectDims.max.top);
89 }
90
91 function doTest() {
92 let domWinUtils = window.QueryInterface(Ci.nsIInterfaceRequestor)
93 .getInterface(Ci.nsIDOMWindowUtils);
94
95 let text = document.getElementById("text");
96
97 text.focus();
98
99 let textrect = text.getBoundingClientRect();
100
101 let cp = document.caretPositionFromPoint(10, 395);
102 let input = cp.offsetNode;
103 let offset = cp.offset;
104 input.selectionStart = input.selectionEnd = offset;
105
106 let selection = getSelection(text);
107
108 testCaretPosition(domWinUtils, input.selectionStart, {
109 min: { left: 5, top: 380 },
110 max: { left: 25, top: 390 },
111 });
112
113 testCaretPosition(domWinUtils, input.selectionEnd, {
114 min: { left: 5, top: 380 },
115 max: { left: 25, top: 390 },
116 });
117
118 testCaretPosition(domWinUtils, text.selectionStart, {
119 min: { left: 5, top: 380 },
120 max: { left: 25, top: 390 },
121 });
122
123 testCaretPosition(domWinUtils, text.selectionEnd, {
124 min: { left: 5, top: 380 },
125 max: { left: 25, top: 390 },
126 });
127
128 testCaretPosition(domWinUtils, selection.anchorOffset, {
129 min: { left: 5, top: 380 },
130 max: { left: 25, top: 390 },
131 });
132
133 testCaretPosition(domWinUtils, selection.focusOffset, {
134 min: { left: 5, top: 380 },
135 max: { left: 25, top: 390 },
136 });
137
138 cp = document.caretPositionFromPoint(395, 395);
139 input = cp.offsetNode;
140 offset = cp.offset;
141 input.selectionStart = input.selectionEnd = offset;
142
143 let selection = getSelection(text);
144
145 testCaretPosition(domWinUtils, input.selectionStart, {
146 min: { left: 390, top: 380 },
147 max: { left: 400, top: 390 },
148 });
149
150 testCaretPosition(domWinUtils, input.selectionEnd, {
151 min: { left: 390, top: 380 },
152 max: { left: 400, top: 390 },
153 });
154
155 testCaretPosition(domWinUtils, text.selectionStart, {
156 min: { left: 390, top: 380 },
157 max: { left: 400, top: 390 },
158 });
159
160 testCaretPosition(domWinUtils, text.selectionEnd, {
161 min: { left: 390, top: 380 },
162 max: { left: 400, top: 390 },
163 });
164
165 testCaretPosition(domWinUtils, selection.anchorOffset, {
166 min: { left: 390, top: 380 },
167 max: { left: 400, top: 390 },
168 });
169
170 testCaretPosition(domWinUtils, selection.focusOffset, {
171 min: { left: 390, top: 380 },
172 max: { left: 400, top: 390 },
173 });
174
175 done();
176 }
177
178 </script>
179
180 <body onload="doTest()">
181 <textarea id="text" wrap="on">
182 Alice was beginning to get very tired of sitting by her sister on the bank,
183 and of having nothing to do: once or twice she had peeped into the book
184 her sister was reading, but it had no pictures or conversations in it,
185 `and what is the use of a book,' thought Alice `without pictures or
186 conversation?'
187
188 So she was considering in her own mind (as well as she could, for the
189 hot day made her feel very sleepy and stupid), whether the pleasure of
190 making a daisy-chain would be worth the trouble of getting up and
191 picking the daisies, when suddenly a White Rabbit with pink In another
192 moment down went Alice after it, never once considering how in the world
193 she was to get out again.
194
195 The rabbit-hole went straight on like a tunnel for some way, and then
196 dipped suddenly down, so suddenly that Alice had not a moment to think
197 about stopping herself before she found herself falling down a very deep
198 well.
199
200 Either the well was very deep, or she fell very slowly, for she had
201 plenty of time as she went down to look about her and to wonder what was
202 going to happen next. First, she tried to look down and make out what
203 she was coming to, but it was too dark to see anything; then she looked
204 at the sides of the well, and noticed that they were filled with
205 cupboards and book-shelves; here and there she saw maps and pictures
206 hung upon pegs. She took down a jar from one of the shelves as she
207 passed; it was labelled `ORANGE MARMALADE', but to her great
208 disappointment it was empty: she did not like to drop the jar for fear
209 of killing somebody, so managed to put it into one of the cupboards as
210 she fell past it.
211 </textarea>
212 <div id="container"></div>
213 </body>
214 </html>

mercurial