summaryrefslogtreecommitdiffstats
path: root/toolkit/components/places/tests/unit/test_PlacesSearchAutocompleteProvider.js
blob: 1280ce3e7976c652721f295d961be8d66dcfa98a (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
/* 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/. */

Cu.import("resource://gre/modules/PlacesSearchAutocompleteProvider.jsm");

function run_test() {
  // Tell the search service we are running in the US.  This also has the
  // desired side-effect of preventing our geoip lookup.
  Services.prefs.setBoolPref("browser.search.isUS", true);
  Services.prefs.setCharPref("browser.search.countryCode", "US");
  Services.prefs.setBoolPref("browser.search.geoSpecificDefaults", false);
  run_next_test();
}

add_task(function* search_engine_match() {
  let engine = yield promiseDefaultSearchEngine();
  let token = engine.getResultDomain();
  let match = yield PlacesSearchAutocompleteProvider.findMatchByToken(token.substr(0, 1));
  do_check_eq(match.url, engine.searchForm);
  do_check_eq(match.engineName, engine.name);
  do_check_eq(match.iconUrl, engine.iconURI ? engine.iconURI.spec : null);
});

add_task(function* no_match() {
  do_check_eq(null, yield PlacesSearchAutocompleteProvider.findMatchByToken("test"));
});

add_task(function* hide_search_engine_nomatch() {
  let engine = yield promiseDefaultSearchEngine();
  let token = engine.getResultDomain();
  let promiseTopic = promiseSearchTopic("engine-changed");
  Services.search.removeEngine(engine);
  yield promiseTopic;
  do_check_true(engine.hidden);
  do_check_eq(null, yield PlacesSearchAutocompleteProvider.findMatchByToken(token.substr(0, 1)));
});

add_task(function* add_search_engine_match() {
  let promiseTopic = promiseSearchTopic("engine-added");
  do_check_eq(null, yield PlacesSearchAutocompleteProvider.findMatchByToken("bacon"));
  Services.search.addEngineWithDetails("bacon", "", "pork", "Search Bacon",
                                       "GET", "http://www.bacon.moz/?search={searchTerms}");
  yield promiseTopic;
  let match = yield PlacesSearchAutocompleteProvider.findMatchByToken("bacon");
  do_check_eq(match.url, "http://www.bacon.moz");
  do_check_eq(match.engineName, "bacon");
  do_check_eq(match.iconUrl, null);
});

add_task(function* test_aliased_search_engine_match() {
  do_check_eq(null, yield PlacesSearchAutocompleteProvider.findMatchByAlias("sober"));
  // Lower case
  let match = yield PlacesSearchAutocompleteProvider.findMatchByAlias("pork");
  do_check_eq(match.engineName, "bacon");
  do_check_eq(match.alias, "pork");
  do_check_eq(match.iconUrl, null);
  // Upper case
  let match1 = yield PlacesSearchAutocompleteProvider.findMatchByAlias("PORK");
  do_check_eq(match1.engineName, "bacon");
  do_check_eq(match1.alias, "pork");
  do_check_eq(match1.iconUrl, null);
  // Cap case
  let match2 = yield PlacesSearchAutocompleteProvider.findMatchByAlias("Pork");
  do_check_eq(match2.engineName, "bacon");
  do_check_eq(match2.alias, "pork");
  do_check_eq(match2.iconUrl, null);
});

add_task(function* test_aliased_search_engine_match_upper_case_alias() {
  let promiseTopic = promiseSearchTopic("engine-added");
  do_check_eq(null, yield PlacesSearchAutocompleteProvider.findMatchByToken("patch"));
  Services.search.addEngineWithDetails("patch", "", "PR", "Search Patch",
                                       "GET", "http://www.patch.moz/?search={searchTerms}");
  yield promiseTopic;
  // lower case
  let match = yield PlacesSearchAutocompleteProvider.findMatchByAlias("pr");
  do_check_eq(match.engineName, "patch");
  do_check_eq(match.alias, "PR");
  do_check_eq(match.iconUrl, null);
  // Upper case
  let match1 = yield PlacesSearchAutocompleteProvider.findMatchByAlias("PR");
  do_check_eq(match1.engineName, "patch");
  do_check_eq(match1.alias, "PR");
  do_check_eq(match1.iconUrl, null);
  // Cap case
  let match2 = yield PlacesSearchAutocompleteProvider.findMatchByAlias("Pr");
  do_check_eq(match2.engineName, "patch");
  do_check_eq(match2.alias, "PR");
  do_check_eq(match2.iconUrl, null);
});

add_task(function* remove_search_engine_nomatch() {
  let engine = Services.search.getEngineByName("bacon");
  let promiseTopic = promiseSearchTopic("engine-removed");
  Services.search.removeEngine(engine);
  yield promiseTopic;
  do_check_eq(null, yield PlacesSearchAutocompleteProvider.findMatchByToken("bacon"));
});

add_task(function* test_parseSubmissionURL_basic() {
  // Most of the logic of parseSubmissionURL is tested in the search service
  // itself, thus we only do a sanity check of the wrapper here.
  let engine = yield promiseDefaultSearchEngine();
  let submissionURL = engine.getSubmission("terms").uri.spec;

  let result = PlacesSearchAutocompleteProvider.parseSubmissionURL(submissionURL);
  do_check_eq(result.engineName, engine.name);
  do_check_eq(result.terms, "terms");

  result = PlacesSearchAutocompleteProvider.parseSubmissionURL("http://example.org/");
  do_check_eq(result, null);
});

function promiseDefaultSearchEngine() {
  let deferred = Promise.defer();
  Services.search.init( () => {
    deferred.resolve(Services.search.defaultEngine);
  });
  return deferred.promise;
}

function promiseSearchTopic(expectedVerb) {
  let deferred = Promise.defer();
  Services.obs.addObserver( function observe(subject, topic, verb) {
    do_print("browser-search-engine-modified: " + verb);
    if (verb == expectedVerb) {
      Services.obs.removeObserver(observe, "browser-search-engine-modified");
      deferred.resolve();
    }
  }, "browser-search-engine-modified", false);
  return deferred.promise;
}