summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/browser/browser_bug1198465.js
blob: 52a3705ac4a8d4b1220d23cbdc8cfca73a116d24 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

var kPrefName = "accessibility.typeaheadfind.prefillwithselection";
var kEmptyURI = "data:text/html,";

// This pref is false by default in OSX; ensure the test still works there.
Services.prefs.setBoolPref(kPrefName, true);

registerCleanupFunction(function () {
  Services.prefs.clearUserPref(kPrefName);
});

add_task(async function () {
  let aTab = await BrowserTestUtils.openNewForegroundTab(gBrowser, kEmptyURI);
  ok(!gFindBarInitialized, "findbar isn't initialized yet");

  // Note: the use case here is when the user types directly in the findbar
  // _before_ it's prefilled with a text selection in the page.

  // So `yield BrowserTestUtils.sendChar()` can't be used here:
  //  - synthesizing a key in the browser won't actually send it to the
  //    findbar; the findbar isn't part of the browser content.
  //  - we need to _not_ wait for _startFindDeferred to be resolved; yielding
  //    a synthesized keypress on the browser implicitely happens after the
  //    browser has dispatched its return message with the prefill value for
  //    the findbar, which essentially nulls these tests.

  // The parent-side of the sidebar initialization is also async, so we do
  // need to wait for that. We verify a bit further down that _startFindDeferred
  // hasn't been resolved yet.
  await gFindBarPromise;

  let findBar = gFindBar;
  is(findBar._findField.value, "", "findbar is empty");

  // Test 1
  //  Any input in the findbar should erase a previous search.

  findBar._findField.value = "xy";
  findBar.startFind();
  is(findBar._findField.value, "xy", "findbar should have xy initial query");
  is(findBar._findField, document.activeElement, "findbar is now focused");

  EventUtils.sendChar("z", window);
  is(findBar._findField.value, "z", "z erases xy");

  findBar._findField.value = "";
  ok(!findBar._findField.value, "erase findbar after first test");

  // Test 2
  //  Prefilling the findbar should be ignored if a search has been run.

  findBar.startFind();
  ok(findBar._startFindDeferred, "prefilled value hasn't been fetched yet");
  is(findBar._findField, document.activeElement, "findbar is still focused");

  EventUtils.sendChar("a", window);
  EventUtils.sendChar("b", window);
  is(findBar._findField.value, "ab", "initial ab typed in the findbar");

  // This resolves _startFindDeferred if it's still pending; let's just skip
  // over waiting for the browser's return message that should do this as it
  // doesn't really matter.
  findBar.onCurrentSelection("foo", true);
  ok(!findBar._startFindDeferred, "prefilled value fetched");
  is(findBar._findField.value, "ab", "ab kept instead of prefill value");

  EventUtils.sendChar("c", window);
  is(findBar._findField.value, "abc", "c is appended after ab");

  // Clear the findField value to make the test  run successfully
  // for multiple runs in the same browser session.
  findBar._findField.value = "";
  BrowserTestUtils.removeTab(aTab);
});