summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/browser/browser_autoFill_trimURLs.js
blob: 3e068d52a47fe9154619248a9eb44f0060e1d70c (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// This test ensures that autoFilled values are not trimmed, unless the user
// selects from the autocomplete popup.

"use strict";

add_setup(async function () {
  const PREF_TRIMURL = "browser.urlbar.trimURLs";
  const PREF_AUTOFILL = "browser.urlbar.autoFill";

  registerCleanupFunction(async function () {
    Services.prefs.clearUserPref(PREF_TRIMURL);
    Services.prefs.clearUserPref(PREF_AUTOFILL);
    await PlacesUtils.history.clear();
    gURLBar.handleRevert();
  });
  Services.prefs.setBoolPref(PREF_TRIMURL, true);
  Services.prefs.setBoolPref(PREF_AUTOFILL, true);

  await PlacesUtils.bookmarks.eraseEverything();
  await PlacesUtils.history.clear();

  // Adding a tab would hit switch-to-tab, so it's safer to just add a visit.
  await PlacesTestUtils.addVisits([
    {
      uri: "http://www.autofilltrimurl.com/whatever",
    },
    {
      uri: "https://www.secureautofillurl.com/whatever",
    },
  ]);
});

async function promiseSearch(searchtext) {
  gURLBar.focus();
  gURLBar.inputField.value = searchtext.substr(0, searchtext.length - 1);
  EventUtils.sendString(searchtext.substr(-1, 1));
  await UrlbarTestUtils.promiseSearchComplete(window);
}

async function promiseTestResult(test) {
  info(`Searching for '${test.search}'`);

  await promiseSearch(test.search);

  Assert.equal(
    gURLBar.inputField.value,
    test.autofilledValue,
    `Autofilled value is as expected for search '${test.search}'`
  );

  let result = await UrlbarTestUtils.getDetailsOfResultAt(window, 0);

  Assert.equal(
    result.displayed.title,
    test.resultListDisplayTitle,
    `Autocomplete result should have displayed title as expected for search '${test.search}'`
  );

  Assert.equal(
    result.displayed.action,
    test.resultListActionText,
    `Autocomplete action text should be as expected for search '${test.search}'`
  );

  Assert.equal(
    result.type,
    test.resultListType,
    `Autocomplete result should have searchengine for the type for search '${test.search}'`
  );

  Assert.equal(
    !!result.searchParams,
    !!test.searchParams,
    "Should have search params if expected"
  );
  if (test.searchParams) {
    let definedParams = {};
    for (let [k, v] of Object.entries(result.searchParams)) {
      if (v !== undefined) {
        definedParams[k] = v;
      }
    }
    Assert.deepEqual(
      definedParams,
      test.searchParams,
      "Shoud have the correct search params"
    );
  } else {
    Assert.equal(
      result.url,
      test.finalCompleteValue,
      "Should have the correct URL/finalCompleteValue"
    );
  }
}

const tests = [
  {
    search: "http://",
    autofilledValue: "http://",
    resultListDisplayTitle: "http://",
    resultListActionText: "Search with Google",
    resultListType: UrlbarUtils.RESULT_TYPE.SEARCH,
    searchParams: {
      engine: "Google",
      query: "http://",
    },
  },
  {
    search: "https://",
    autofilledValue: "https://",
    resultListDisplayTitle: "https://",
    resultListActionText: "Search with Google",
    resultListType: UrlbarUtils.RESULT_TYPE.SEARCH,
    searchParams: {
      engine: "Google",
      query: "https://",
    },
  },
  {
    search: "au",
    autofilledValue: "autofilltrimurl.com/",
    resultListDisplayTitle: "www.autofilltrimurl.com",
    resultListActionText: "Visit",
    resultListType: UrlbarUtils.RESULT_TYPE.URL,
    finalCompleteValue: "http://www.autofilltrimurl.com/",
  },
  {
    search: "http://au",
    autofilledValue: "http://autofilltrimurl.com/",
    resultListDisplayTitle: "www.autofilltrimurl.com",
    resultListActionText: "Visit",
    resultListType: UrlbarUtils.RESULT_TYPE.URL,
    finalCompleteValue: "http://www.autofilltrimurl.com/",
  },
  {
    search: "sec",
    autofilledValue: "secureautofillurl.com/",
    resultListDisplayTitle: "https://www.secureautofillurl.com",
    resultListActionText: "Visit",
    resultListType: UrlbarUtils.RESULT_TYPE.URL,
    finalCompleteValue: "https://www.secureautofillurl.com/",
  },
  {
    search: "https://sec",
    autofilledValue: "https://secureautofillurl.com/",
    resultListDisplayTitle: "https://www.secureautofillurl.com",
    resultListActionText: "Visit",
    resultListType: UrlbarUtils.RESULT_TYPE.URL,
    finalCompleteValue: "https://www.secureautofillurl.com/",
  },
];

add_task(async function autofill_tests() {
  for (let test of tests) {
    await promiseTestResult(test);
  }
});

add_task(async function autofill_complete_domain() {
  await promiseSearch("http://www.autofilltrimurl.com");
  Assert.equal(
    gURLBar.inputField.value,
    "http://www.autofilltrimurl.com/",
    "Should have the correct autofill value"
  );

  // Now ensure selecting from the popup correctly trims.
  Assert.equal(
    UrlbarTestUtils.getResultCount(window),
    2,
    "Should have the correct matches"
  );
  EventUtils.synthesizeKey("KEY_ArrowDown");
  Assert.equal(
    gURLBar.inputField.value,
    "www.autofilltrimurl.com/whatever",
    "Should have applied trim correctly"
  );
});