aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.12/test/test-selection.js
blob: 9985ca498e80a7179b7b8489a7a484c4f2705009 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

let timer = require("sdk/timers");
let {Cc,Ci} = require("chrome");

// Arbitrary delay needed to avoid weird behavior.
// TODO: We need to find all uses of this and replace them
// with more deterministic solutions.
const ARB_DELAY = 100;

// Select all divs elements in an HTML document
function selectAllDivs(window) {
  let divs = window.document.getElementsByTagName("div");
  let s = window.getSelection();
  if (s.rangeCount > 0)
    s.removeAllRanges();
  for (let i = 0; i < divs.length; i++) {
    let range = window.document.createRange();
    range.selectNode(divs[i]);
    s.addRange(range);
  }
}

function selectTextarea(window, from, to) {
  let textarea = window.document.getElementsByTagName("textarea")[0];

  from = from || 0;
  to = to || textarea.value.length;

  textarea.setSelectionRange(from, to);
  textarea.focus();
}

function primeTestCase(html, test, callback) {
  let tabBrowser = require("sdk/deprecated/tab-browser");
  let dataURL = "data:text/html;charset=utf-8," + encodeURI(html);
  let tracker = tabBrowser.whenContentLoaded(
    function(window) {
      if (window.document.location.href != dataURL)
        return;
      callback(window, test);
      timer.setTimeout(function() {
          tracker.unload();
          test.done();
          window.close();
        },
        ARB_DELAY);
    }
  );
  tabBrowser.addTab(dataURL);
}

const DIV1 = '<div id="foo">bar</div>';
const DIV2 = '<div>noodles</div>';
const HTML_MULTIPLE = '<html><body>' + DIV1 + DIV2 + '</body></html>';
const HTML_SINGLE = '<html><body>' + DIV1 + '</body></html>';

// Tests of contiguous

exports.testContiguousMultiple = function testContiguousMultiple(test) {
  let selection = require("sdk/selection");
  primeTestCase(HTML_MULTIPLE, test, function(window, test) {
    selectAllDivs(window);
    test.assertEqual(selection.isContiguous, false,
      "selection.isContiguous multiple works.");
  });

  test.waitUntilDone(5000);
};

exports.testContiguousSingle = function testContiguousSingle(test) {
  let selection = require("sdk/selection");
  primeTestCase(HTML_SINGLE, test, function(window, test) {
    selectAllDivs(window);
    test.assertEqual(selection.isContiguous, true,
      "selection.isContiguous single works.");
  });

  test.waitUntilDone(5000);
};

exports.testContiguousWithoutSelection =
  function testContiguousWithoutSelection(test) {
    let selection = require("sdk/selection");
    primeTestCase(HTML_SINGLE, test, function(window, test) {
      test.assertEqual(selection.isContiguous, false,
        "selection.isContiguous without selection works.");
    });

    test.waitUntilDone(5000);
};

/**
 * Test that setting the contiguous property has no effect.
 */
/*exports.testSetContiguous = function testSetContiguous(test) {
  let selection = require("sdk/selection");
  primeTestCase(HTML_MULTIPLE, test, function(window, test) {
    selectAllDivs(window);
    try {
      selection.isContiguous = true;
      test.assertEqual(selection.isContiguous, false,
        "setting selection.isContiguous doesn't work (as expected).");
    }
    catch (e) {
      test.pass("setting selection.isContiguous doesn't work (as expected).");
    }
  });

  test.waitUntilDone(5000);
};*/


// HTML tests

exports.testGetHTMLSingleSelection = function testGetHTMLSingleSelection(test) {
  let selection = require("sdk/selection");
  primeTestCase(HTML_SINGLE, test, function(window, test) {
    selectAllDivs(window);
    test.assertEqual(selection.html, DIV1, "get html selection works");
  });

  test.waitUntilDone(5000);
};

/* Myk's comments: This is fine.  However, it reminds me to figure out and
   specify whether iteration is ordered.  If so, we'll want to change this
   test in the future to test that the discontiguous selections are returned in
   the appropriate order. In the meantime, add a comment to that effect here */
exports.testGetHTMLMultipleSelection =
  function testGetHTMLMultipleSelection(test) {
    let selection = require("sdk/selection");
    primeTestCase(HTML_MULTIPLE, test, function(window, test) {
      selectAllDivs(window);
      let assertions = false;
      for each (let i in selection) {
        test.assertEqual(true, [DIV1, DIV2].some(function(t) t == i.html),
          "get multiple selection html works");
        assertions = true;
      }
      // Ensure we ran at least one assertEqual()
      test.assert(assertions, "No assertions were called");
    });

    test.waitUntilDone(5000);
};

exports.testGetHTMLNull = function testGetHTMLNull(test) {
  let selection = require("sdk/selection");
  primeTestCase(HTML_SINGLE, test, function(window, test) {
    test.assertEqual(selection.html, null, "get html null works");
  });

  test.waitUntilDone(5000);
};

exports.testGetHTMLWeird = function testGetHTMLWeird(test) {
  let selection = require("sdk/selection");
  // If the getter is used when there are contiguous selections, the first
  // selection should be returned
  primeTestCase(HTML_MULTIPLE, test, function(window, test) {
    selectAllDivs(window);
    test.assertEqual(selection.html, DIV1, "get html weird works");
  });

  test.waitUntilDone(5000);
};

exports.testGetHTMLNullInTextareaSelection =
  function testGetHTMLNullInTextareaSelection(test) {
      let selection = require("sdk/selection");

      primeTestCase(TEXT_FIELD, test, function(window, test) {
        selectTextarea(window);

        test.assertEqual(selection.html, null, "get html null in textarea works")
      });

      test.waitUntilDone(5000);
};

const REPLACEMENT_HTML = "<b>Lorem ipsum dolor sit amet</b>";

exports.testSetHTMLSelection = function testSetHTMLSelection(test) {
  let selection = require("sdk/selection");
  primeTestCase(HTML_SINGLE, test, function(window, test) {
    selectAllDivs(window);
    selection.html = REPLACEMENT_HTML;
    test.assertEqual(selection.html, "<span>" + REPLACEMENT_HTML +
      "</span>", "selection html works");
  });

  test.waitUntilDone(5000);
};

exports.testSetHTMLException = function testSetHTMLException(test) {
  let selection = require("sdk/selection");
  primeTestCase(HTML_SINGLE, test, function(window, test) {
    try {
      selection.html = REPLACEMENT_HTML;
      test.fail("set HTML throws when there's no selection.");
    }
    catch (e) {
      test.pass("set HTML throws when there's no selection.");
    }
  });

  test.waitUntilDone(5000);
};

const TEXT1 = "foo";
const TEXT2 = "noodles";
const TEXT_MULTIPLE = "<html><body><div>" + TEXT1 + "</div><div>" + TEXT2 +
  "</div></body></html>";
const TEXT_SINGLE = "<html><body><div>" + TEXT1 + "</div></body></html>";
const TEXT_FIELD = "<html><body><textarea>" + TEXT1 + "</textarea></body></html>";

// Text tests

exports.testSetHTMLinTextareaSelection =
  function testSetHTMLinTextareaSelection(test) {
    let selection = require("sdk/selection");

    primeTestCase(TEXT_FIELD, test, function(window, test) {
      selectTextarea(window);

      // HTML string is set as plain text in textareas, that's because
      // `selection.html` and `selection.text` are basically aliases when a
      // value is set. See bug 677269
      selection.html = REPLACEMENT_HTML;

      test.assertEqual(selection.text, REPLACEMENT_HTML,
        "set selection html in textarea works");
    });

    test.waitUntilDone(5000);
};

exports.testGetTextSingleSelection =
  function testGetTextSingleSelection(test) {
  let selection = require("sdk/selection");
    primeTestCase(TEXT_SINGLE, test, function(window, test) {
      selectAllDivs(window);
      test.assertEqual(selection.text, TEXT1, "get text selection works");
    });

    test.waitUntilDone(5000);
};

exports.testGetTextMultipleSelection =
  function testGetTextMultipleSelection(test) {
  let selection = require("sdk/selection");
    primeTestCase(TEXT_MULTIPLE, test, function(window, test) {
      selectAllDivs(window);
      let assertions = false;
      for each (let i in selection) {
        test.assertEqual(true, [TEXT1, TEXT2].some(function(t) t == i.text),
          "get multiple selection text works");
        assertions = true;
      }
      // Ensure we ran at least one assertEqual()
      test.assert(assertions, "No assertions were called");
    });

    test.waitUntilDone(5000);
};

exports.testGetTextNull = function testGetTextNull(test) {
  let selection = require("sdk/selection");
  primeTestCase(TEXT_SINGLE, test, function(window, test) {
    test.assertEqual(selection.text, null, "get text null works");
  });

  test.waitUntilDone(5000);
};

exports.testGetTextWeird = function testGetTextWeird(test) {
  let selection = require("sdk/selection");
  // If the getter is used when there are contiguous selections, the first
  // selection should be returned
  primeTestCase(TEXT_MULTIPLE, test, function(window, test) {
    selectAllDivs(window);
    test.assertEqual(selection.text, TEXT1, "get text weird works");
  });

  test.waitUntilDone(5000);
};

exports.testGetTextNullInTextareaSelection =
  function testGetTextInTextareaSelection(test) {
    let selection = require("sdk/selection");

    primeTestCase(TEXT_FIELD, test, function(window, test) {
      test.assertEqual(selection.text, null, "get text null in textarea works")
    });

    test.waitUntilDone(5000);
};

exports.testGetTextInTextareaSelection =
  function testGetTextInTextareaSelection(test) {
    let selection = require("sdk/selection");

    primeTestCase(TEXT_FIELD, test, function(window, test) {
      selectTextarea(window);

      test.assertEqual(selection.text, TEXT1, "get text null in textarea works")
    });

    test.waitUntilDone(5000);
};

const REPLACEMENT_TEXT = "Lorem ipsum dolor sit amet";

exports.testSetTextSelection = function testSetTextSelection(test) {
  let selection = require("sdk/selection");
  primeTestCase(TEXT_SINGLE, test, function(window, test) {
    selectAllDivs(window);
    selection.text = REPLACEMENT_TEXT;
    test.assertEqual(selection.text, REPLACEMENT_TEXT, "selection text works");
  });

  test.waitUntilDone(5000);
};

exports.testSetHTMLException = function testSetHTMLException(test) {
  let selection = require("sdk/selection");
  primeTestCase(TEXT_SINGLE, test, function(window, test) {
    try {
      selection.text = REPLACEMENT_TEXT;
      test.fail("set HTML throws when there's no selection.");
    }
    catch (e) {
      test.pass("set HTML throws when there's no selection.");
    }
  });

  test.waitUntilDone(5000);
};

exports.testSetTextInTextareaSelection =
  function testSetTextInTextareaSelection(test) {
    let selection = require("sdk/selection");

    primeTestCase(TEXT_FIELD, test, function(window, test) {
      selectTextarea(window);

      selection.text = REPLACEMENT_TEXT;

      test.assertEqual(selection.text, REPLACEMENT_TEXT,
        "set selection text in textarea works");
    });

    test.waitUntilDone(5000);
};

// Iterator tests

exports.testIterator = function testIterator(test) {
  let selection = require("sdk/selection");
  let selectionCount = 0;
  primeTestCase(TEXT_MULTIPLE, test, function(window, test) {
    selectAllDivs(window);
    for each (let i in selection)
      selectionCount++;
    test.assertEqual(2, selectionCount, "iterator works.");
  });

  test.waitUntilDone(5000);
};

exports.testIteratorWithTextareaSelection =
  function testIteratorWithTextareaSelection(test) {
    let selection = require("sdk/selection");
    let selectionCount = 0;

    primeTestCase(TEXT_FIELD, test, function(window, test) {
      selectTextarea(window);

      for each (let i in selection)
        selectionCount++;

      test.assertEqual(1, selectionCount, "iterator works in textarea.");
    });

    test.waitUntilDone(5000);
};

/* onSelect tests */

/*
function sendSelectionSetEvent(window) {
  const Ci = Components['interfaces'];
  let utils = window.QueryInterface(Ci.nsIInterfaceRequestor).
                                    getInterface(Ci.nsIDOMWindowUtils);
  if (!utils.sendSelectionSetEvent(0, 1, false))
    dump("**** sendSelectionSetEvent did not select anything\n");
  else
    dump("**** sendSelectionSetEvent succeeded\n");
}

// testOnSelect() requires nsIDOMWindowUtils, which is only available in
// Firefox 3.7+.
exports.testOnSelect = function testOnSelect(test) {
  let selection = require("sdk/selection");
  let callbackCount = 0;
  primeTestCase(TEXT_SINGLE, test, function(window, test) {
    selection.onSelect = function() {callbackCount++};
    // Now simulate the user selecting stuff
    sendSelectionSetEvent(window);
    selection.text = REPLACEMENT_TEXT;
    test.assertEqual(1, callbackCount, "onSelect text listener works.");
    //test.pass();
    //test.done();
  });

  test.waitUntilDone(5000);
};

// testOnSelectExceptionNoBubble() requires nsIDOMWindowUtils, which is only
// available in Firefox 3.7+.
exports.testOnSelectExceptionNoBubble =
  function testOnSelectTextSelection(test) {
    let selection = require("sdk/selection");
    primeTestCase(HTML_SINGLE, test, function(window, test) {
      selection.onSelect = function() {
        throw new Error("Exception thrown in testOnSelectExceptionNoBubble");
      };
      // Now simulate the user selecting stuff
      sendSelectionSetEvent(window);
      test.pass("onSelect catches exceptions.");
    });

    test.waitUntilDone(5000);
};
*/

// If the module doesn't support the app we're being run in, require() will
// throw.  In that case, remove all tests above from exports, and add one dummy
// test that passes.
try {
  require("sdk/selection");
}
catch (err) {
  // This bug should be mentioned in the error message.
  let bug = "https://bugzilla.mozilla.org/show_bug.cgi?id=560716";
  if (err.message.indexOf(bug) < 0)
    throw err;

  module.exports = {
    testAppNotSupported: function (test) {
      test.pass("the selection module does not support this application.");
    }
  }
}