Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
1 /* Any copyright is dedicated to the Public Domain.
2 * http://creativecommons.org/publicdomain/zero/1.0/ */
4 /*
5 * Test that currentEngine and defaultEngine properties are updated when the
6 * prefs are set independently.
7 */
9 "use strict";
11 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
13 Cu.import("resource://testing-common/httpd.js");
15 let waitForEngines = {
16 "Test search engine": 1,
17 "A second test engine": 1
18 };
20 const PREF_BRANCH = "browser.search.";
22 /**
23 * Wrapper for nsIPrefBranch::setComplexValue.
24 * @param aPrefName
25 * The name of the pref to set.
26 */
27 function setLocalizedPref(aPrefName, aValue) {
28 let nsIPLS = Ci.nsIPrefLocalizedString;
29 let branch = Services.prefs.getBranch(PREF_BRANCH);
30 try {
31 var pls = Cc["@mozilla.org/pref-localizedstring;1"].
32 createInstance(Ci.nsIPrefLocalizedString);
33 pls.data = aValue;
34 branch.setComplexValue(aPrefName, nsIPLS, pls);
35 } catch (ex) {}
36 }
38 function search_observer(aSubject, aTopic, aData) {
39 let engine = aSubject.QueryInterface(Ci.nsISearchEngine);
40 do_print("Observer: " + aData + " for " + engine.name);
42 if (aData != "engine-added") {
43 return;
44 }
46 // If the engine is defined in `waitForEngines`, remove it from the list
47 if (waitForEngines[engine.name]) {
48 delete waitForEngines[engine.name];
49 } else {
50 // This engine is not one we're waiting for, so bail out early.
51 return;
52 }
54 // Only continue when both engines have been loaded.
55 if (Object.keys(waitForEngines).length) {
56 return;
57 }
59 let search = Services.search;
61 let engine1Name = "Test search engine";
62 let engine2Name = "A second test engine";
63 let engine1 = search.getEngineByName(engine1Name);
64 let engine2 = search.getEngineByName(engine2Name);
66 // Initial sanity check:
67 search.defaultEngine = engine1;
68 do_check_eq(search.defaultEngine, engine1);
69 search.currentEngine = engine1;
70 do_check_eq(search.currentEngine, engine1);
72 setLocalizedPref("defaultenginename", engine2Name);
73 // Default engine should be synced with the pref
74 do_check_eq(search.defaultEngine, engine2);
75 // Current engine should've stayed the same
76 do_check_eq(search.currentEngine, engine1);
78 setLocalizedPref("selectedEngine", engine2Name);
79 // Default engine should've stayed the same
80 do_check_eq(search.defaultEngine, engine2);
81 // Current engine should've been updated
82 do_check_eq(search.currentEngine, engine2);
84 // Test that setting the currentEngine to the original default engine clears
85 // the selectedEngine pref, rather than setting it. To do this we need to
86 // set the value of defaultenginename on the default branch.
87 let defaultBranch = Services.prefs.getDefaultBranch("");
88 let prefName = PREF_BRANCH + "defaultenginename";
89 let prefVal = "data:text/plain," + prefName + "=" + engine1Name;
90 defaultBranch.setCharPref(prefName, prefVal, true);
91 search.currentEngine = engine1;
92 // Current engine should've been updated
93 do_check_eq(search.currentEngine, engine1);
94 do_check_false(Services.prefs.prefHasUserValue("browser.search.selectedEngine"));
96 // Test that setting the defaultEngine to the original default engine clears
97 // the defaultenginename pref, rather than setting it.
98 do_check_true(Services.prefs.prefHasUserValue("browser.search.defaultenginename"));
99 search.defaultEngine = engine1;
100 do_check_eq(search.defaultEngine, engine1);
101 do_check_false(Services.prefs.prefHasUserValue("browser.search.defaultenginename"));
103 do_test_finished();
104 }
106 function run_test() {
107 removeMetadata();
108 updateAppInfo();
110 let httpServer = new HttpServer();
111 httpServer.start(-1);
112 httpServer.registerDirectory("/", do_get_cwd());
113 let baseUrl = "http://localhost:" + httpServer.identity.primaryPort;
115 do_register_cleanup(function cleanup() {
116 httpServer.stop(function() {});
117 Services.obs.removeObserver(search_observer, "browser-search-engine-modified");
118 });
120 do_test_pending();
122 Services.obs.addObserver(search_observer, "browser-search-engine-modified", false);
124 Services.search.addEngine(baseUrl + "/data/engine.xml",
125 Ci.nsISearchEngine.DATA_XML,
126 null, false);
127 Services.search.addEngine(baseUrl + "/data/engine2.xml",
128 Ci.nsISearchEngine.DATA_XML,
129 null, false);
130 }