summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_tags_general.js
blob: c2c620c152dbe6e0a5135cbe730b1a0bf8da083a (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* 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/. */

testEngine_setup();

/**
 * Checks the results of a search for `searchTerm`.
 *
 * @param {Array} uris
 *   A 2-element array containing [{string} uri, {array} tags}], where `tags`
 *   is a comma-separated list of the tags expected to appear in the search.
 * @param {string} searchTerm
 *   The term to search for
 */
async function ensure_tag_results(uris, searchTerm) {
  print("Searching for '" + searchTerm + "'");
  let context = createContext(searchTerm, { isPrivate: false });
  let urlbarResults = [];
  for (let [uri, tags] of uris) {
    urlbarResults.push(
      makeBookmarkResult(context, {
        uri,
        title: "A title",
        tags,
      })
    );
  }
  await check_results({
    context,
    matches: [
      makeSearchResult(context, {
        engineName: SUGGESTIONS_ENGINE_NAME,
        heuristic: true,
      }),
      ...urlbarResults,
    ],
  });
}

var uri1 = "http://site.tld/1/aaa";
var uri2 = "http://site.tld/2/bbb";
var uri3 = "http://site.tld/3/aaa";
var uri4 = "http://site.tld/4/bbb";
var uri5 = "http://site.tld/5/aaa";
var uri6 = "http://site.tld/6/bbb";

var tests = [
  () =>
    ensure_tag_results(
      [
        [uri1, ["foo"]],
        [uri4, ["foo bar"]],
        [uri6, ["foo bar cheese"]],
      ],
      "foo"
    ),
  () => ensure_tag_results([[uri1, ["foo"]]], "foo aaa"),
  () =>
    ensure_tag_results(
      [
        [uri4, ["foo bar"]],
        [uri6, ["foo bar cheese"]],
      ],
      "foo bbb"
    ),
  () =>
    ensure_tag_results(
      [
        [uri2, ["bar"]],
        [uri4, ["foo bar"]],
        [uri5, ["bar cheese"]],
        [uri6, ["foo bar cheese"]],
      ],
      "bar"
    ),
  () => ensure_tag_results([[uri5, ["bar cheese"]]], "bar aaa"),
  () =>
    ensure_tag_results(
      [
        [uri2, ["bar"]],
        [uri4, ["foo bar"]],
        [uri6, ["foo bar cheese"]],
      ],
      "bar bbb"
    ),
  () =>
    ensure_tag_results(
      [
        [uri3, ["cheese"]],
        [uri5, ["bar cheese"]],
        [uri6, ["foo bar cheese"]],
      ],
      "cheese"
    ),
  () =>
    ensure_tag_results(
      [
        [uri3, ["cheese"]],
        [uri5, ["bar cheese"]],
      ],
      "chees aaa"
    ),
  () => ensure_tag_results([[uri6, ["foo bar cheese"]]], "chees bbb"),
  () =>
    ensure_tag_results(
      [
        [uri4, ["foo bar"]],
        [uri6, ["foo bar cheese"]],
      ],
      "fo bar"
    ),
  () => ensure_tag_results([], "fo bar aaa"),
  () =>
    ensure_tag_results(
      [
        [uri4, ["foo bar"]],
        [uri6, ["foo bar cheese"]],
      ],
      "fo bar bbb"
    ),
  () =>
    ensure_tag_results(
      [
        [uri4, ["foo bar"]],
        [uri6, ["foo bar cheese"]],
      ],
      "ba foo"
    ),
  () => ensure_tag_results([], "ba foo aaa"),
  () =>
    ensure_tag_results(
      [
        [uri4, ["foo bar"]],
        [uri6, ["foo bar cheese"]],
      ],
      "ba foo bbb"
    ),
  () =>
    ensure_tag_results(
      [
        [uri5, ["bar cheese"]],
        [uri6, ["foo bar cheese"]],
      ],
      "ba chee"
    ),
  () => ensure_tag_results([[uri5, ["bar cheese"]]], "ba chee aaa"),
  () => ensure_tag_results([[uri6, ["foo bar cheese"]]], "ba chee bbb"),
  () =>
    ensure_tag_results(
      [
        [uri5, ["bar cheese"]],
        [uri6, ["foo bar cheese"]],
      ],
      "cheese bar"
    ),
  () => ensure_tag_results([[uri5, ["bar cheese"]]], "cheese bar aaa"),
  () => ensure_tag_results([[uri6, ["foo bar cheese"]]], "chees bar bbb"),
  () => ensure_tag_results([[uri6, ["foo bar cheese"]]], "cheese bar foo"),
  () => ensure_tag_results([], "foo bar cheese aaa"),
  () => ensure_tag_results([[uri6, ["foo bar cheese"]]], "foo bar cheese bbb"),
];

/**
 * Properly tags a uri adding it to bookmarks.
 *
 * @param {string} url
 *        The URI to tag.
 * @param {Array} tags
 *        The tags to add.
 */
async function tagURI(url, tags) {
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url,
    title: "A title",
  });
  PlacesUtils.tagging.tagURI(Services.io.newURI(url), tags);
}

/**
 * Test history autocomplete
 */
add_task(async function test_history_autocomplete_tags() {
  // always search in history + bookmarks, no matter what the default is
  Services.prefs.setBoolPref("browser.urlbar.suggest.history", true);
  Services.prefs.setBoolPref("browser.urlbar.suggest.bookmarks", true);
  Services.prefs.setBoolPref("browser.urlbar.suggest.searches", false);
  registerCleanupFunction(() => {
    Services.prefs.clearUserPref("browser.urlbar.suggest.history");
    Services.prefs.clearUserPref("browser.urlbar.suggest.bookmarks");
    Services.prefs.clearUserPref("browser.urlbar.suggest.searches");
  });

  await tagURI(uri6, ["foo bar cheese"]);
  await tagURI(uri5, ["bar cheese"]);
  await tagURI(uri4, ["foo bar"]);
  await tagURI(uri3, ["cheese"]);
  await tagURI(uri2, ["bar"]);
  await tagURI(uri1, ["foo"]);

  for (let tagTest of tests) {
    await tagTest();
  }
});