summaryrefslogtreecommitdiffstats
path: root/browser/components/urlbar/tests/unit/test_UrlbarQueryContext_restrictSource.js
blob: 0aadb8aef689a117243d0bf4dc56a1ab37cca7e5 (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
/* 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/. */

/**
 * Test for restrictions set through UrlbarQueryContext.sources.
 */

testEngine_setup();

add_task(async function test_restrictions() {
  await PlacesTestUtils.addVisits([
    { uri: "http://history.com/", title: "match" },
  ]);
  await PlacesUtils.bookmarks.insert({
    url: "http://bookmark.com/",
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    title: "match",
  });
  await UrlbarProviderOpenTabs.registerOpenTab(
    "http://openpagematch.com/",
    0,
    false
  );

  info("Bookmark restrict");
  let results = await get_results({
    sources: [UrlbarUtils.RESULT_SOURCE.BOOKMARKS],
    searchString: "match",
  });
  // Skip the heuristic result.
  Assert.deepEqual(
    results.filter(r => !r.heuristic).map(r => r.payload.url),
    ["http://bookmark.com/"]
  );

  info("History restrict");
  results = await get_results({
    sources: [UrlbarUtils.RESULT_SOURCE.HISTORY],
    searchString: "match",
  });
  // Skip the heuristic result.
  Assert.deepEqual(
    results.filter(r => !r.heuristic).map(r => r.payload.url),
    ["http://history.com/"]
  );

  info("tabs restrict");
  results = await get_results({
    sources: [UrlbarUtils.RESULT_SOURCE.TABS],
    searchString: "match",
  });
  // Skip the heuristic result.
  Assert.deepEqual(
    results.filter(r => !r.heuristic).map(r => r.payload.url),
    ["http://openpagematch.com/"]
  );

  info("search restrict");
  results = await get_results({
    sources: [UrlbarUtils.RESULT_SOURCE.SEARCH],
    searchString: "match",
  });
  Assert.ok(
    !results.some(r => r.payload.engine != SUGGESTIONS_ENGINE_NAME),
    "All the results should be search results"
  );

  info("search restrict should ignore restriction token");
  results = await get_results({
    sources: [UrlbarUtils.RESULT_SOURCE.SEARCH],
    searchString: `${UrlbarTokenizer.RESTRICT.BOOKMARKS} match`,
  });
  Assert.ok(
    !results.some(r => r.payload.engine != SUGGESTIONS_ENGINE_NAME),
    "All the results should be search results"
  );
  Assert.equal(
    results[0].payload.query,
    `${UrlbarTokenizer.RESTRICT.BOOKMARKS} match`,
    "The restriction token should be ignored and not stripped"
  );

  info("search restrict with alias");
  await SearchTestUtils.installSearchExtension({
    name: "Test",
    keyword: "match",
  });
  results = await get_results({
    sources: [UrlbarUtils.RESULT_SOURCE.SEARCH],
    searchString: "match this",
  });
  Assert.ok(
    !results.some(r => r.payload.engine != SUGGESTIONS_ENGINE_NAME),
    "All the results should be search results and the alias should be ignored"
  );
  Assert.equal(
    results[0].payload.query,
    `match this`,
    "The restriction token should be ignored and not stripped"
  );

  info("search restrict with other engine");
  results = await get_results({
    sources: [UrlbarUtils.RESULT_SOURCE.SEARCH],
    searchString: "match",
    engineName: "Test",
  });
  Assert.ok(
    !results.some(r => r.payload.engine != "Test"),
    "All the results should be search results from the Test engine"
  );
});

async function get_results(test) {
  let controller = UrlbarTestUtils.newMockController();
  let options = {
    allowAutofill: false,
    isPrivate: false,
    maxResults: 10,
    sources: test.sources,
  };
  if (test.engineName) {
    options.searchMode = {
      source: UrlbarUtils.RESULT_SOURCE.SEARCH,
      engineName: test.engineName,
    };
  }
  let queryContext = createContext(test.searchString, options);
  await controller.startQuery(queryContext);
  return queryContext.results;
}