summaryrefslogtreecommitdiffstats
path: root/dom/tests/mochitest/chrome/selectAtPoint.html
blob: 8c625e6f7b436d92890b370166d3ab5a64cd412d (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE HTML>
<html>
<head>
<title>nsIDOMWindowUtils::selectAtPoint 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">

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

  function getCharacterDims() {
    let span = document.getElementById("measure");
    let rect = span.getBoundingClientRect();
    return { width: rect.right - rect.left,
             height: rect.bottom - rect.top }; 
  }

  function setStart(aDWU, aX, aY, aSelectType)
  {
    // Clear any existing selection
    let selection = document.getSelection();
    selection.removeAllRanges();

    // Select text
    let result = aDWU.selectAtPoint(aX, aY, aSelectType);
    ok(result == true, "selectAtPoint secceeded?");
  }

  function setEnd(aDWU, aX, aY, aSelectType)
  {
    // Select text
    let result = aDWU.selectAtPoint(aX, aY, aSelectType);
    ok(result == true, "selectAtPoint secceeded?");
  }

  function setSingle(aDWU, aX, aY, aSelectType, aSelectTypeStr, aExpectedSelectionText) {
    // Clear any existing selection
    let selection = document.getSelection();
    selection.removeAllRanges();

    // Select text
    let result = aDWU.selectAtPoint(aX, aY, aSelectType);
    ok(result == true, "selectAtPoint secceeded?");
  }

  function checkSelection(aDoc, aSelectTypeStr, aExpectedSelectionText) {
    // Retrieve text selected
    let selection = aDoc.getSelection();
    let text = selection.toString();

    // Test
    let result = (text == aExpectedSelectionText);
    ok(result, aSelectTypeStr + " selection text matches?");
    if (!result) {
      dumpLn(aSelectTypeStr + " selection text:", "[" + text + "] expected:[" + aExpectedSelectionText + "]" );
    }
  }

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

    let os = Components.classes["@mozilla.org/xre/app-info;1"]
                       .getService(Ci.nsIXULRuntime).OS;
    let isLinux = (os == "Linux");
    let isMac = (os == "Darwin");
    let isWindows = (os == "WINNT");

    if (!isLinux && !isMac && !isWindows) {
      done();
      return;
    }

    window.scrollTo(0, 0);

    // Trick to get character spacing - get the bounds around a
    // single character trapped in a div.
    let charDims = getCharacterDims();
    // dumpLn("character dims:", charDims.width, charDims.height);

    //
    // Root frame selection
    //

    // First div in the main page

    let div = document.getElementById("div1");
    let rect = div.getBoundingClientRect();

    // Centered on the first character in the sentence div
    let targetPoint = { xPos: rect.left + (charDims.width / 2),
                        yPos: rect.top + (charDims.height / 2) };

    setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_WORDNOSPACE);
    checkSelection(document, "SELECT_WORDNOSPACE", "ttestselection1");
    setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_WORD);
    if (isLinux || isMac) {
      checkSelection(document, "SELECT_WORD", "ttestselection1");
    } else if (isWindows) {
      checkSelection(document, "SELECT_WORD", "ttestselection1 ");
    }
    setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_PARAGRAPH);
    checkSelection(document, "SELECT_PARAGRAPH", "ttestselection1 Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut. Ne labore incorrupte vix. Cu copiosae postulant tincidunt ius, in illud appetere contentiones eos. Ei munere officiis assentior pro, nibh decore ius at.");

    // Centered on the second character in the sentence div
    targetPoint = { xPos: rect.left + (charDims.width + (charDims.width / 2)),
                    yPos: rect.top + (charDims.height / 2) };
    setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_CHARACTER);
    checkSelection(document, "SELECT_CHARACTER", "te");
    setSingle(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_CLUSTER);
    checkSelection(document, "SELECT_CLUSTER", "te");

    // Separate character blocks in a word 't(te)s(ts)election1'
    targetPoint = { xPos: rect.left + (charDims.width + (charDims.width / 2)),
                    yPos: rect.top + (charDims.height / 2) };
    setStart(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_CHARACTER);
    targetPoint = { xPos: rect.left + ((charDims.width * 4) + (charDims.width / 2)),
                    yPos: rect.top + (charDims.height / 2) };
    setEnd(dwu, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_CHARACTER);
    if (isLinux || isMac) {
      // XXX I think this is a bug, the right hand selection is 4.5 characters over with a
      // monspaced font. what we want: t(te)s(ts)election1 what we get: t(te)st(se)lection1
      checkSelection(document, "split selection", "tese");
    } else if (isWindows) {
      checkSelection(document, "split selection", "tets");
    }

    // Trying to select where there's no text, should fail but not throw
    let result = dwu.selectAtPoint(rect.left - 20, rect.top - 20, Ci.nsIDOMWindowUtils.SELECT_CHARACTER, false);
    ok(result == false, "couldn't select?");

    // Second div in the main page

    div = document.getElementById("div2");
    rect = div.getBoundingClientRect();

    // Centered on the first line, first character in the paragraph div
    targetPoint = { xPos: rect.left + (charDims.width / 2),
                    yPos: rect.top + (charDims.height / 2) };
    setSingle(dwu, targetPoint.xPos + 50, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_PARAGRAPH);
    checkSelection(document, "SELECT_PARAGRAPH", "Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut. Ne labore incorrupte vix. Cu copiosae postulant tincidunt ius, in illud appetere contentiones eos.");

    //
    // Inner IFRAME selection tests
    //

    let frame = document.getElementById("frame1");
    let dwuFrame = frame.contentDocument
                        .defaultView
                        .QueryInterface(Ci.nsIInterfaceRequestor)
                        .getInterface(Ci.nsIDOMWindowUtils);

    frame.contentWindow.scrollTo(0, 0);

    rect = frame.getBoundingClientRect();

    targetPoint = { xPos: charDims.width / 2,
                    yPos: charDims.height / 2 };
    setSingle(dwuFrame, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_WORDNOSPACE);
    checkSelection(frame.contentWindow.document, "SELECT_WORDNOSPACE", "ttestselection2");
    setSingle(dwuFrame, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_WORD);
    if (isLinux || isMac) {
      checkSelection(frame.contentWindow.document, "SELECT_WORD", "ttestselection2");
    } else if (isWindows) {
      checkSelection(frame.contentWindow.document, "SELECT_WORD", "ttestselection2 ");
    }
    setSingle(dwuFrame, targetPoint.xPos, targetPoint.yPos, Ci.nsIDOMWindowUtils.SELECT_PARAGRAPH);
    checkSelection(frame.contentWindow.document, "SELECT_PARAGRAPH", "ttestselection2 Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut.");

    // Outside the frame should throw. This is a failure in coordinate setup of
    // nsDOMWindowUtils::SelectAtPoint.
    let thr = false;
    try {
      dwuFrame.selectAtPoint(rect.right + 50, rect.top, Ci.nsIDOMWindowUtils.SELECT_WORD, false);
    } catch (ex)  { thr = true; }
    ok(thr == true, "selectAtPoint expected throw?");
    
    done();
  }

  let frameLoad = false;
  let pageLoad = false;
  let painted = false;
  function testReady() {
    if (frameLoad && pageLoad && painted) 
      doTest();
  }

  function onFrameLoad() {
    // Exit the onload handler before trying the test, because we need
    // to ensure that paint unsupression has happened.
    setTimeout(function() {
      frameLoad = true;
      testReady();
    }, 0);
  }

  function onPageLoad() {
    // Exit the onload handler before trying the test, because we need
    // to ensure that paint unsupression has happened
    // XXXroc why do we need to separately test for the loading of the frame
    // and a paint? That should not be necessary for this test.
    setTimeout(function() {
      pageLoad = true;
      testReady();
    }, 0);
  }

  function onPaint() {
    window.removeEventListener("MozAfterPaint", onPaint, false);
    painted = true;
    testReady();
  }

  window.addEventListener("MozAfterPaint", onPaint, false);
</script>

<style type="text/css">

body {
  font-family: monospace;
  margin-left: 40px;
  margin-top: 40px;
  padding: 0;
}

#div1 {
  border: 1px solid red;
  width: 400px;
  height: 100px;
}

#frame1 {
  display: block;
  height: 100px;
  width: 300px;
  border: 1px solid blue;
  padding: 0;
  margin: 0;
}

#div2 {
  border: 1px solid green;
}

#measure {
  padding: 0px;
  margin: 0px;
  border: 1px solid red;
}

</style>
</head>
<body id="body" onload="onPageLoad();">

<div id="div1">ttestselection1 Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut. Ne labore incorrupte vix. Cu copiosae postulant tincidunt ius, in illud appetere contentiones eos. Ei munere officiis assentior pro, nibh decore ius at.</div>

<br />

<iframe id="frame1" src="data:text/html,<html><body style='margin: 0; padding: 0; font-family: monospace;' onload='window.parent.onFrameLoad();'><div id='sel2'>ttestselection2 Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut.</div><br/><br/></body></html>"></iframe>

<br/>

<div id="div2">Lorem ipsum dolor sit amet, at duo debet graeci, vivendum vulputate per ut. Ne labore incorrupte vix. Cu copiosae postulant tincidunt ius, in illud appetere contentiones eos.</div>

<br />

<span id="measure">t</span>

</body>
</html>