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

const NS_APP_USER_SEARCH_DIR  = "UsrSrchPlugns";

const kTestEngineShortName = "engine";
const kWhiteListPrefName = "reset.whitelist";

function run_test() {
  // Copy an engine to [profile]/searchplugin/
  let dir = Services.dirsvc.get(NS_APP_USER_SEARCH_DIR, Ci.nsIFile);
  if (!dir.exists())
    dir.create(dir.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
  do_get_file("data/engine.xml").copyTo(dir, kTestEngineShortName + ".xml");

  let file = dir.clone();
  file.append(kTestEngineShortName + ".xml");
  do_check_true(file.exists());

  do_check_false(Services.search.isInitialized);

  Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF)
          .setBoolPref("reset.enabled", true);

  run_next_test();
}

function* removeLoadPathHash() {
  // Remove the loadPathHash and re-initialize the search service.
  let cache = yield promiseCacheData();
  for (let engine of cache.engines) {
    if (engine._shortName == kTestEngineShortName) {
      delete engine._metaData["loadPathHash"];
      break;
    }
  }
  yield promiseSaveCacheData(cache);
  yield asyncReInit();
}

add_task(function* test_no_prompt_when_valid_loadPathHash() {
  yield asyncInit();

  // test the engine is loaded ok.
  let engine = Services.search.getEngineByName(kTestEngineName);
  do_check_neq(engine, null);

  yield promiseAfterCache();

  // The test engine has been found in the profile directory and imported,
  // so it shouldn't have a loadPathHash.
  let metadata = yield promiseEngineMetadata();
  do_check_true(kTestEngineShortName in metadata);
  do_check_false("loadPathHash" in metadata[kTestEngineShortName]);

  // After making it the currentEngine with the search service API,
  // the test engine should have a valid loadPathHash.
  Services.search.currentEngine = engine;
  yield promiseAfterCache();
  metadata = yield promiseEngineMetadata();
  do_check_true("loadPathHash" in metadata[kTestEngineShortName]);
  let loadPathHash = metadata[kTestEngineShortName].loadPathHash;
  do_check_eq(typeof loadPathHash, "string");
  do_check_eq(loadPathHash.length, 44);

  // A search should not cause the search reset prompt.
  let submission =
    Services.search.currentEngine.getSubmission("foo", null, "searchbar");
  do_check_eq(submission.uri.spec,
              "http://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&aq=t");
});

add_task(function* test_promptURLs() {
  yield removeLoadPathHash();

  // The default should still be the test engine.
  let currentEngine = Services.search.currentEngine;
  do_check_eq(currentEngine.name, kTestEngineName);
  // but the submission url should be about:searchreset
  let url = (data, purpose) =>
    currentEngine.getSubmission(data, null, purpose).uri.spec;
  do_check_eq(url("foo", "searchbar"),
              "about:searchreset?data=foo&purpose=searchbar");
  do_check_eq(url("foo"), "about:searchreset?data=foo");
  do_check_eq(url("", "searchbar"), "about:searchreset?purpose=searchbar");
  do_check_eq(url(""), "about:searchreset");
  do_check_eq(url("", ""), "about:searchreset");

  // Calling the currentEngine setter for the same engine should
  // prevent further prompts.
  Services.search.currentEngine = Services.search.currentEngine;
  do_check_eq(url("foo", "searchbar"),
              "http://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&aq=t");

  // And the loadPathHash should be back.
  yield promiseAfterCache();
  let metadata = yield promiseEngineMetadata();
  do_check_true("loadPathHash" in metadata[kTestEngineShortName]);
  let loadPathHash = metadata[kTestEngineShortName].loadPathHash;
  do_check_eq(typeof loadPathHash, "string");
  do_check_eq(loadPathHash.length, 44);
});

add_task(function* test_whitelist() {
  yield removeLoadPathHash();

  // The default should still be the test engine.
  let currentEngine = Services.search.currentEngine;
  do_check_eq(currentEngine.name, kTestEngineName);
  let expectPrompt = shouldPrompt => {
    let expectedURL =
      shouldPrompt ? "about:searchreset?data=foo&purpose=searchbar"
                   : "http://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&aq=t";
    let url = currentEngine.getSubmission("foo", null, "searchbar").uri.spec;
    do_check_eq(url, expectedURL);
  };
  expectPrompt(true);

  // Unless we whitelist our test engine.
  let branch = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF);
  let initialWhiteList = branch.getCharPref(kWhiteListPrefName);
  branch.setCharPref(kWhiteListPrefName, "example.com,test.tld");
  expectPrompt(true);
  branch.setCharPref(kWhiteListPrefName, "www.google.com");
  expectPrompt(false);
  branch.setCharPref(kWhiteListPrefName, "example.com,www.google.com,test.tld");
  expectPrompt(false);

  // The loadPathHash should not be back after the prompt was skipped due to the
  // whitelist.
  yield asyncReInit();
  let metadata = yield promiseEngineMetadata();
  do_check_false("loadPathHash" in metadata[kTestEngineShortName]);

  branch.setCharPref(kWhiteListPrefName, initialWhiteList);
  expectPrompt(true);
});