Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | /* |
michael@0 | 5 | * Test that currentEngine and defaultEngine properties are updated when the |
michael@0 | 6 | * prefs are set independently. |
michael@0 | 7 | */ |
michael@0 | 8 | |
michael@0 | 9 | "use strict"; |
michael@0 | 10 | |
michael@0 | 11 | const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; |
michael@0 | 12 | |
michael@0 | 13 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 14 | |
michael@0 | 15 | let waitForEngines = { |
michael@0 | 16 | "Test search engine": 1, |
michael@0 | 17 | "A second test engine": 1 |
michael@0 | 18 | }; |
michael@0 | 19 | |
michael@0 | 20 | const PREF_BRANCH = "browser.search."; |
michael@0 | 21 | |
michael@0 | 22 | /** |
michael@0 | 23 | * Wrapper for nsIPrefBranch::setComplexValue. |
michael@0 | 24 | * @param aPrefName |
michael@0 | 25 | * The name of the pref to set. |
michael@0 | 26 | */ |
michael@0 | 27 | function setLocalizedPref(aPrefName, aValue) { |
michael@0 | 28 | let nsIPLS = Ci.nsIPrefLocalizedString; |
michael@0 | 29 | let branch = Services.prefs.getBranch(PREF_BRANCH); |
michael@0 | 30 | try { |
michael@0 | 31 | var pls = Cc["@mozilla.org/pref-localizedstring;1"]. |
michael@0 | 32 | createInstance(Ci.nsIPrefLocalizedString); |
michael@0 | 33 | pls.data = aValue; |
michael@0 | 34 | branch.setComplexValue(aPrefName, nsIPLS, pls); |
michael@0 | 35 | } catch (ex) {} |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | function search_observer(aSubject, aTopic, aData) { |
michael@0 | 39 | let engine = aSubject.QueryInterface(Ci.nsISearchEngine); |
michael@0 | 40 | do_print("Observer: " + aData + " for " + engine.name); |
michael@0 | 41 | |
michael@0 | 42 | if (aData != "engine-added") { |
michael@0 | 43 | return; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | // If the engine is defined in `waitForEngines`, remove it from the list |
michael@0 | 47 | if (waitForEngines[engine.name]) { |
michael@0 | 48 | delete waitForEngines[engine.name]; |
michael@0 | 49 | } else { |
michael@0 | 50 | // This engine is not one we're waiting for, so bail out early. |
michael@0 | 51 | return; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | // Only continue when both engines have been loaded. |
michael@0 | 55 | if (Object.keys(waitForEngines).length) { |
michael@0 | 56 | return; |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | let search = Services.search; |
michael@0 | 60 | |
michael@0 | 61 | let engine1Name = "Test search engine"; |
michael@0 | 62 | let engine2Name = "A second test engine"; |
michael@0 | 63 | let engine1 = search.getEngineByName(engine1Name); |
michael@0 | 64 | let engine2 = search.getEngineByName(engine2Name); |
michael@0 | 65 | |
michael@0 | 66 | // Initial sanity check: |
michael@0 | 67 | search.defaultEngine = engine1; |
michael@0 | 68 | do_check_eq(search.defaultEngine, engine1); |
michael@0 | 69 | search.currentEngine = engine1; |
michael@0 | 70 | do_check_eq(search.currentEngine, engine1); |
michael@0 | 71 | |
michael@0 | 72 | setLocalizedPref("defaultenginename", engine2Name); |
michael@0 | 73 | // Default engine should be synced with the pref |
michael@0 | 74 | do_check_eq(search.defaultEngine, engine2); |
michael@0 | 75 | // Current engine should've stayed the same |
michael@0 | 76 | do_check_eq(search.currentEngine, engine1); |
michael@0 | 77 | |
michael@0 | 78 | setLocalizedPref("selectedEngine", engine2Name); |
michael@0 | 79 | // Default engine should've stayed the same |
michael@0 | 80 | do_check_eq(search.defaultEngine, engine2); |
michael@0 | 81 | // Current engine should've been updated |
michael@0 | 82 | do_check_eq(search.currentEngine, engine2); |
michael@0 | 83 | |
michael@0 | 84 | // Test that setting the currentEngine to the original default engine clears |
michael@0 | 85 | // the selectedEngine pref, rather than setting it. To do this we need to |
michael@0 | 86 | // set the value of defaultenginename on the default branch. |
michael@0 | 87 | let defaultBranch = Services.prefs.getDefaultBranch(""); |
michael@0 | 88 | let prefName = PREF_BRANCH + "defaultenginename"; |
michael@0 | 89 | let prefVal = "data:text/plain," + prefName + "=" + engine1Name; |
michael@0 | 90 | defaultBranch.setCharPref(prefName, prefVal, true); |
michael@0 | 91 | search.currentEngine = engine1; |
michael@0 | 92 | // Current engine should've been updated |
michael@0 | 93 | do_check_eq(search.currentEngine, engine1); |
michael@0 | 94 | do_check_false(Services.prefs.prefHasUserValue("browser.search.selectedEngine")); |
michael@0 | 95 | |
michael@0 | 96 | // Test that setting the defaultEngine to the original default engine clears |
michael@0 | 97 | // the defaultenginename pref, rather than setting it. |
michael@0 | 98 | do_check_true(Services.prefs.prefHasUserValue("browser.search.defaultenginename")); |
michael@0 | 99 | search.defaultEngine = engine1; |
michael@0 | 100 | do_check_eq(search.defaultEngine, engine1); |
michael@0 | 101 | do_check_false(Services.prefs.prefHasUserValue("browser.search.defaultenginename")); |
michael@0 | 102 | |
michael@0 | 103 | do_test_finished(); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | function run_test() { |
michael@0 | 107 | removeMetadata(); |
michael@0 | 108 | updateAppInfo(); |
michael@0 | 109 | |
michael@0 | 110 | let httpServer = new HttpServer(); |
michael@0 | 111 | httpServer.start(-1); |
michael@0 | 112 | httpServer.registerDirectory("/", do_get_cwd()); |
michael@0 | 113 | let baseUrl = "http://localhost:" + httpServer.identity.primaryPort; |
michael@0 | 114 | |
michael@0 | 115 | do_register_cleanup(function cleanup() { |
michael@0 | 116 | httpServer.stop(function() {}); |
michael@0 | 117 | Services.obs.removeObserver(search_observer, "browser-search-engine-modified"); |
michael@0 | 118 | }); |
michael@0 | 119 | |
michael@0 | 120 | do_test_pending(); |
michael@0 | 121 | |
michael@0 | 122 | Services.obs.addObserver(search_observer, "browser-search-engine-modified", false); |
michael@0 | 123 | |
michael@0 | 124 | Services.search.addEngine(baseUrl + "/data/engine.xml", |
michael@0 | 125 | Ci.nsISearchEngine.DATA_XML, |
michael@0 | 126 | null, false); |
michael@0 | 127 | Services.search.addEngine(baseUrl + "/data/engine2.xml", |
michael@0 | 128 | Ci.nsISearchEngine.DATA_XML, |
michael@0 | 129 | null, false); |
michael@0 | 130 | } |