summaryrefslogtreecommitdiffstats
path: root/toolkit/components/search/tests/xpcshell/test_hidden.js
blob: b784f36242aa30db2ea1fb082097415dc3121bca (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

const kUrlPref = "geoSpecificDefaults.url";

function run_test() {
  removeMetadata();
  removeCacheFile();

  do_load_manifest("data/chrome.manifest");

  configureToLoadJarEngines();

  // Geo specific defaults won't be fetched if there's no country code.
  Services.prefs.setCharPref("browser.search.geoip.url",
                             'data:application/json,{"country_code": "US"}');

  // Make 'hidden' the only visible engine.
 let url = "data:application/json,{\"interval\": 31536000, \"settings\": {\"searchDefault\": \"hidden\", \"visibleDefaultEngines\": [\"hidden\"]}}";
  Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF).setCharPref(kUrlPref, url);

  do_check_false(Services.search.isInitialized);

  run_next_test();
}

add_task(function* async_init() {
  let commitPromise = promiseAfterCache()
  yield asyncInit();

  let engines = Services.search.getEngines();
  do_check_eq(engines.length, 1);

  // The default test jar engine has been hidden.
  let engine = Services.search.getEngineByName("bug645970");
  do_check_eq(engine, null);

  // The hidden engine is visible.
  engine = Services.search.getEngineByName("hidden");
  do_check_neq(engine, null);

  // The next test does a sync init, which won't do the geoSpecificDefaults XHR,
  // so it depends on the metadata having been written to disk.
  yield commitPromise;
});

add_task(function* sync_init() {
  let unInitPromise = waitForSearchNotification("uninit-complete");
  let reInitPromise = asyncReInit();
  yield unInitPromise;
  do_check_false(Services.search.isInitialized);

  // Synchronously check the current default engine, to force a sync init.
  do_check_eq(Services.search.currentEngine.name, "hidden");
  do_check_true(Services.search.isInitialized);

  let engines = Services.search.getEngines();
  do_check_eq(engines.length, 1);

  // The default test jar engine has been hidden.
  let engine = Services.search.getEngineByName("bug645970");
  do_check_eq(engine, null);

  // The hidden engine is visible.
  engine = Services.search.getEngineByName("hidden");
  do_check_neq(engine, null);

  yield reInitPromise;
});

add_task(function* invalid_engine() {
  // Trigger a new request.
  yield forceExpiration();

  // Set the visibleDefaultEngines list to something that contains a non-existent engine.
  // This should cause the search service to ignore the list altogether and fallback to
  // local defaults.
  let url = "data:application/json,{\"interval\": 31536000, \"settings\": {\"searchDefault\": \"hidden\", \"visibleDefaultEngines\": [\"hidden\", \"bogus\"]}}";
  Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF).setCharPref(kUrlPref, url);

  yield asyncReInit();

  let engines = Services.search.getEngines();
  do_check_eq(engines.length, 1);

  // The default test jar engine is visible.
  let engine = Services.search.getEngineByName("bug645970");
  do_check_neq(engine, null);

  // The hidden engine is... hidden.
  engine = Services.search.getEngineByName("hidden");
  do_check_eq(engine, null);
});