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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
Components.utils.import("resource://gre/modules/osfile.jsm");
const kSelectedEnginePref = "browser.search.selectedEngine";
// Check that the default engine matches the defaultenginename pref
add_task(function* test_defaultEngine() {
yield asyncInit();
do_check_eq(Services.search.currentEngine.name, getDefaultEngineName());
});
// Giving prefs a user value shouldn't change the selected engine.
add_task(function* test_selectedEngine() {
let defaultEngineName = getDefaultEngineName();
// Test the selectedEngine pref.
Services.prefs.setCharPref(kSelectedEnginePref, kTestEngineName);
yield asyncReInit();
do_check_eq(Services.search.currentEngine.name, defaultEngineName);
Services.prefs.clearUserPref(kSelectedEnginePref);
// Test the defaultenginename pref.
Services.prefs.setCharPref(kDefaultenginenamePref, kTestEngineName);
yield asyncReInit();
do_check_eq(Services.search.currentEngine.name, defaultEngineName);
Services.prefs.clearUserPref(kDefaultenginenamePref);
});
// Setting the search engine should be persisted across restarts.
add_task(function* test_persistAcrossRestarts() {
// Set the engine through the API.
Services.search.currentEngine = Services.search.getEngineByName(kTestEngineName);
do_check_eq(Services.search.currentEngine.name, kTestEngineName);
yield promiseAfterCache();
// Check that the a hash was saved.
let metadata = yield promiseGlobalMetadata();
do_check_eq(metadata.hash.length, 44);
// Re-init and check the engine is still the same.
yield asyncReInit();
do_check_eq(Services.search.currentEngine.name, kTestEngineName);
// Cleanup (set the engine back to default).
Services.search.resetToOriginalDefaultEngine();
do_check_eq(Services.search.currentEngine.name, getDefaultEngineName());
});
// An engine set without a valid hash should be ignored.
add_task(function* test_ignoreInvalidHash() {
// Set the engine through the API.
Services.search.currentEngine = Services.search.getEngineByName(kTestEngineName);
do_check_eq(Services.search.currentEngine.name, kTestEngineName);
yield promiseAfterCache();
// Then mess with the file (make the hash invalid).
let metadata = yield promiseGlobalMetadata();
metadata.hash = "invalid";
yield promiseSaveGlobalMetadata(metadata);
// Re-init the search service, and check that the json file is ignored.
yield asyncReInit();
do_check_eq(Services.search.currentEngine.name, getDefaultEngineName());
});
// Resetting the engine to the default should remove the saved value.
add_task(function* test_settingToDefault() {
// Set the engine through the API.
Services.search.currentEngine = Services.search.getEngineByName(kTestEngineName);
do_check_eq(Services.search.currentEngine.name, kTestEngineName);
yield promiseAfterCache();
// Check that the current engine was saved.
let metadata = yield promiseGlobalMetadata();
do_check_eq(metadata.current, kTestEngineName);
// Then set the engine back to the default through the API.
Services.search.currentEngine =
Services.search.getEngineByName(getDefaultEngineName());
yield promiseAfterCache();
// Check that the current engine is no longer saved in the JSON file.
metadata = yield promiseGlobalMetadata();
do_check_eq(metadata.current, "");
});
add_task(function* test_resetToOriginalDefaultEngine() {
let defaultName = getDefaultEngineName();
do_check_eq(Services.search.currentEngine.name, defaultName);
Services.search.currentEngine =
Services.search.getEngineByName(kTestEngineName);
do_check_eq(Services.search.currentEngine.name, kTestEngineName);
yield promiseAfterCache();
Services.search.resetToOriginalDefaultEngine();
do_check_eq(Services.search.currentEngine.name, defaultName);
yield promiseAfterCache();
});
add_task(function* test_fallback_kept_after_restart() {
// Set current engine to a default engine that isn't the original default.
let builtInEngines = Services.search.getDefaultEngines();
let defaultName = getDefaultEngineName();
let nonDefaultBuiltInEngine;
for (let engine of builtInEngines) {
if (engine.name != defaultName) {
nonDefaultBuiltInEngine = engine;
break;
}
}
Services.search.currentEngine = nonDefaultBuiltInEngine;
do_check_eq(Services.search.currentEngine.name, nonDefaultBuiltInEngine.name);
yield promiseAfterCache();
// Remove that engine...
Services.search.removeEngine(nonDefaultBuiltInEngine);
// The engine being a default (built-in) one, it should be hidden
// rather than actually removed.
do_check_true(nonDefaultBuiltInEngine.hidden);
// Using the currentEngine getter should force a fallback to the
// original default engine.
do_check_eq(Services.search.currentEngine.name, defaultName);
// Restoring the default engines should unhide our built-in test
// engine, but not change the value of currentEngine.
Services.search.restoreDefaultEngines();
do_check_false(nonDefaultBuiltInEngine.hidden);
do_check_eq(Services.search.currentEngine.name, defaultName);
yield promiseAfterCache();
// After a restart, the currentEngine value should still be unchanged.
yield asyncReInit();
do_check_eq(Services.search.currentEngine.name, defaultName);
});
function run_test() {
removeMetadata();
removeCacheFile();
do_check_false(Services.search.isInitialized);
let engineDummyFile = gProfD.clone();
engineDummyFile.append("searchplugins");
engineDummyFile.append("test-search-engine.xml");
let engineDir = engineDummyFile.parent;
engineDir.create(Ci.nsIFile.DIRECTORY_TYPE, FileUtils.PERMS_DIRECTORY);
do_get_file("data/engine.xml").copyTo(engineDir, "engine.xml");
do_register_cleanup(function() {
removeMetadata();
removeCacheFile();
});
run_next_test();
}
|