summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/chrome/queryCaretRectUnix.html
blob: fae5bb7ec2ab8f0db4fa4cd0315b0cde26fd6eab (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
<!DOCTYPE HTML>
<html>
<head>
<title>nsIDOMWindowUtils::sendQueryContentEvent w/QUERY_CARET_RECT test</title>
<script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">

<style>
  #text {
  position: absolute;
  left: 0em;
  top: 0em;
  font-size: 10pt;
  font-family: monospace;
  line-height: 20px;
  letter-spacing: 0px;
  margin-top:-1px; /* nix the text area border */
  overflow: hidden;
  width:800px;
  height:400px;
  }
  #div-hor {
  position: absolute;
  left: 0em;
  border-top:1px solid lightgray;
  width: 1000px;
  pointer-events:none;
  }
  #div-ver {
  position: absolute;
  top: 0em;
  border-left:1px solid lightgray;
  height: 500px;
  pointer-events:none;
 }
</style>

<script type="application/javascript;version=1.8">
  var SimpleTest = window.opener.SimpleTest;
  var Ci = Components.interfaces;

  function ok() { window.opener.ok.apply(window.opener, arguments); }
  function done() { window.opener.done.apply(window.opener, arguments); }

  function dumpLn() {
    for (let idx = 0; idx < arguments.length; idx++)
      dump(arguments[idx] + " ");
    dump("\n");
  }

  // drawn grid is 20x20
  function drawGrid() {
    for (var idx = 0; idx < 20; idx++) {
      var e = document.createElement("div");
      e.setAttribute("id", "div-hor");
      e.style.top = (idx*20) + "px";
      document.getElementById("container").appendChild(e);
    }
    for (var idx = 0; idx < 40; idx++) {
      var e = document.createElement("div");
      e.setAttribute("id", "div-ver");
      e.style.left = (idx*20) + "px";
      document.getElementById("container").appendChild(e);
    }
  }

  function getSelection(aElement) {
    if (aElement instanceof Ci.nsIDOMNSEditableElement) {
      return aElement.QueryInterface(Ci.nsIDOMNSEditableElement)
                     .editor.selection;
    }
    return null;
  }

  function testCaretPosition(aDomWinUtils, aOffset, aRectDims) {
    let rect = aDomWinUtils.sendQueryContentEvent(
                 aDomWinUtils.QUERY_CARET_RECT,
                 aOffset, 0, 0, 0,
                 aDomWinUtils.QUERY_CONTENT_FLAG_USE_XP_LINE_BREAK);
    ok(rect, "rect returned");
    ok(rect.succeeded, "call succeeded");

    info("left:" + rect.left + " top:" + rect.top);

    ok(rect.left > aRectDims.min.left, "rect.left > " + aRectDims.min.left);
    ok(rect.left < aRectDims.max.left, "rect.left < " + aRectDims.max.left);
    ok(rect.top > aRectDims.min.top, "rect.top > " + aRectDims.min.top);
    ok(rect.top < aRectDims.max.top, "rect.top < " + aRectDims.max.top);
  }

  function doTest() {
    let domWinUtils = window.QueryInterface(Ci.nsIInterfaceRequestor)
                            .getInterface(Ci.nsIDOMWindowUtils);

    let text = document.getElementById("text");

    text.focus();

    let textrect = text.getBoundingClientRect();

    let cp = document.caretPositionFromPoint(10, 395);
    let input = cp.offsetNode;
    let offset = cp.offset;
    input.selectionStart = input.selectionEnd = offset;

    let selection = getSelection(text);

    testCaretPosition(domWinUtils, input.selectionStart, {
      min: { left: 5, top: 380 },
      max: { left: 25, top: 390 },
    });

    testCaretPosition(domWinUtils, input.selectionEnd, {
      min: { left: 5, top: 380 },
      max: { left: 25, top: 390 },
    });

    testCaretPosition(domWinUtils, text.selectionStart, {
      min: { left: 5, top: 380 },
      max: { left: 25, top: 390 },
    });

    testCaretPosition(domWinUtils, text.selectionEnd, {
      min: { left: 5, top: 380 },
      max: { left: 25, top: 390 },
    });

    testCaretPosition(domWinUtils, selection.anchorOffset, {
      min: { left: 5, top: 380 },
      max: { left: 25, top: 390 },
    });

    testCaretPosition(domWinUtils, selection.focusOffset, {
      min: { left: 5, top: 380 },
      max: { left: 25, top: 390 },
    });

    cp = document.caretPositionFromPoint(395, 395);
    input = cp.offsetNode;
    offset = cp.offset;
    input.selectionStart = input.selectionEnd = offset;

    selection = getSelection(text);

    testCaretPosition(domWinUtils, input.selectionStart, {
      min: { left: 390, top: 380 },
      max: { left: 400, top: 390 },
    });

    testCaretPosition(domWinUtils, input.selectionEnd, {
      min: { left: 390, top: 380 },
      max: { left: 400, top: 390 },
    });

    testCaretPosition(domWinUtils, text.selectionStart, {
      min: { left: 390, top: 380 },
      max: { left: 400, top: 390 },
    });

    testCaretPosition(domWinUtils, text.selectionEnd, {
      min: { left: 390, top: 380 },
      max: { left: 400, top: 390 },
    });

    testCaretPosition(domWinUtils, selection.anchorOffset, {
      min: { left: 390, top: 380 },
      max: { left: 400, top: 390 },
    });

    testCaretPosition(domWinUtils, selection.focusOffset, {
      min: { left: 390, top: 380 },
      max: { left: 400, top: 390 },
    });

    done();
  }

</script>

<body onload="doTest()">
<textarea id="text" wrap="on">
Alice was beginning to get very tired of sitting by her sister on the bank, 
and of having nothing to do: once or twice she had peeped into the book 
her sister was reading, but it had no pictures or conversations in it, 
`and what is the use of a book,' thought Alice `without pictures or 
conversation?'

So she was considering in her own mind (as well as she could, for the 
hot day made her feel very sleepy and stupid), whether the pleasure of 
making a daisy-chain would be worth the trouble of getting up and 
picking the daisies, when suddenly a White Rabbit with pink In another 
moment down went Alice after it, never once considering how in the world
she was to get out again. 

The rabbit-hole went straight on like a tunnel for some way, and then 
dipped suddenly down, so suddenly that Alice had not a moment to think 
about stopping herself before she found herself falling down a very deep
well.

Either the well was very deep, or she fell very slowly, for she had 
plenty of time as she went down to look about her and to wonder what was
 going to happen next. First, she tried to look down and make out what 
she was coming to, but it was too dark to see anything; then she looked 
at the sides of the well, and noticed that they were filled with 
cupboards and book-shelves; here and there she saw maps and pictures 
hung upon pegs. She took down a jar from one of the shelves as she 
passed; it was labelled `ORANGE MARMALADE', but to her great 
disappointment it was empty: she did not like to drop the jar for fear 
of killing somebody, so managed to put it into one of the cupboards as 
she fell past it. 
</textarea>
<div id="container"></div>
</body>
</html>