toolkit/components/contentprefs/tests/unit/head_contentPrefs.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 // Inspired by the Places infrastructure in head_bookmarks.js
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cr = Components.results;
michael@0 10 const Cu = Components.utils;
michael@0 11
michael@0 12 Cu.import('resource://gre/modules/Services.jsm');
michael@0 13 Cu.import('resource://gre/modules/ContentPrefInstance.jsm');
michael@0 14
michael@0 15 const CONTENT_PREFS_DB_FILENAME = "content-prefs.sqlite";
michael@0 16 const CONTENT_PREFS_BACKUP_DB_FILENAME = "content-prefs.sqlite.corrupt";
michael@0 17
michael@0 18 var ContentPrefTest = {
michael@0 19 //**************************************************************************//
michael@0 20 // Convenience Getters
michael@0 21
michael@0 22 __dirSvc: null,
michael@0 23 get _dirSvc() {
michael@0 24 if (!this.__dirSvc)
michael@0 25 this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"].
michael@0 26 getService(Ci.nsIProperties);
michael@0 27 return this.__dirSvc;
michael@0 28 },
michael@0 29
michael@0 30 __consoleSvc: null,
michael@0 31 get _consoleSvc() {
michael@0 32 if (!this.__consoleSvc)
michael@0 33 this.__consoleSvc = Cc["@mozilla.org/consoleservice;1"].
michael@0 34 getService(Ci.nsIConsoleService);
michael@0 35 return this.__consoleSvc;
michael@0 36 },
michael@0 37
michael@0 38 __ioSvc: null,
michael@0 39 get _ioSvc() {
michael@0 40 if (!this.__ioSvc)
michael@0 41 this.__ioSvc = Cc["@mozilla.org/network/io-service;1"].
michael@0 42 getService(Ci.nsIIOService);
michael@0 43 return this.__ioSvc;
michael@0 44 },
michael@0 45
michael@0 46
michael@0 47 //**************************************************************************//
michael@0 48 // nsISupports
michael@0 49
michael@0 50 interfaces: [Ci.nsIDirectoryServiceProvider, Ci.nsISupports],
michael@0 51
michael@0 52 QueryInterface: function ContentPrefTest_QueryInterface(iid) {
michael@0 53 if (!this.interfaces.some( function(v) { return iid.equals(v) } ))
michael@0 54 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 55 return this;
michael@0 56 },
michael@0 57
michael@0 58
michael@0 59 //**************************************************************************//
michael@0 60 // nsIDirectoryServiceProvider
michael@0 61
michael@0 62 getFile: function ContentPrefTest_getFile(property, persistent) {
michael@0 63 persistent.value = true;
michael@0 64
michael@0 65 if (property == "ProfD")
michael@0 66 return this._dirSvc.get("CurProcD", Ci.nsIFile);
michael@0 67
michael@0 68 // This causes extraneous errors to show up in the log when the directory
michael@0 69 // service asks us first for CurProcD and MozBinD. I wish there was a way
michael@0 70 // to suppress those errors.
michael@0 71 throw Cr.NS_ERROR_FAILURE;
michael@0 72 },
michael@0 73
michael@0 74
michael@0 75 //**************************************************************************//
michael@0 76 // Utilities
michael@0 77
michael@0 78 getURI: function ContentPrefTest_getURI(spec) {
michael@0 79 return this._ioSvc.newURI(spec, null, null);
michael@0 80 },
michael@0 81
michael@0 82 /**
michael@0 83 * Get the profile directory.
michael@0 84 */
michael@0 85 getProfileDir: function ContentPrefTest_getProfileDir() {
michael@0 86 // do_get_profile can be only called from a parent process
michael@0 87 if (runningInParent) {
michael@0 88 return do_get_profile();
michael@0 89 }
michael@0 90 // if running in a content process, this just returns the path
michael@0 91 // profile was initialized in the ipc head file
michael@0 92 let env = Components.classes["@mozilla.org/process/environment;1"]
michael@0 93 .getService(Components.interfaces.nsIEnvironment);
michael@0 94 // the python harness sets this in the environment for us
michael@0 95 let profd = env.get("XPCSHELL_TEST_PROFILE_DIR");
michael@0 96 let file = Components.classes["@mozilla.org/file/local;1"]
michael@0 97 .createInstance(Components.interfaces.nsILocalFile);
michael@0 98 file.initWithPath(profd);
michael@0 99 return file;
michael@0 100 },
michael@0 101
michael@0 102 /**
michael@0 103 * Delete the content pref service's persistent datastore. We do this before
michael@0 104 * and after running tests to make sure we start from scratch each time. We
michael@0 105 * also do it during the database creation, schema migration, and backup tests.
michael@0 106 */
michael@0 107 deleteDatabase: function ContentPrefTest_deleteDatabase() {
michael@0 108 var file = this.getProfileDir();
michael@0 109 file.append(CONTENT_PREFS_DB_FILENAME);
michael@0 110 if (file.exists())
michael@0 111 try { file.remove(false); } catch(e) { /* stupid windows box */ }
michael@0 112 return file;
michael@0 113 },
michael@0 114
michael@0 115 /**
michael@0 116 * Delete the backup of the content pref service's persistent datastore.
michael@0 117 * We do this during the database creation, schema migration, and backup tests.
michael@0 118 */
michael@0 119 deleteBackupDatabase: function ContentPrefTest_deleteBackupDatabase() {
michael@0 120 var file = this.getProfileDir();
michael@0 121 file.append(CONTENT_PREFS_BACKUP_DB_FILENAME);
michael@0 122 if (file.exists())
michael@0 123 file.remove(false);
michael@0 124 return file;
michael@0 125 },
michael@0 126
michael@0 127 /**
michael@0 128 * Log a message to the console and the test log.
michael@0 129 */
michael@0 130 log: function ContentPrefTest_log(message) {
michael@0 131 message = "*** ContentPrefTest: " + message;
michael@0 132 this._consoleSvc.logStringMessage(message);
michael@0 133 print(message);
michael@0 134 }
michael@0 135
michael@0 136 };
michael@0 137
michael@0 138 let gInPrivateBrowsing = false;
michael@0 139 function enterPBMode() {
michael@0 140 gInPrivateBrowsing = true;
michael@0 141 }
michael@0 142 function exitPBMode() {
michael@0 143 gInPrivateBrowsing = false;
michael@0 144 Services.obs.notifyObservers(null, "last-pb-context-exited", null);
michael@0 145 }
michael@0 146
michael@0 147 ContentPrefTest.deleteDatabase();
michael@0 148
michael@0 149 function inChildProcess() {
michael@0 150 var appInfo = Cc["@mozilla.org/xre/app-info;1"];
michael@0 151 if (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType ==
michael@0 152 Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
michael@0 153 return false;
michael@0 154 }
michael@0 155 return true;
michael@0 156 }
michael@0 157
michael@0 158 // Turn on logging for the content preferences service so we can troubleshoot
michael@0 159 // problems with the tests. Note that we cannot do this in a child process
michael@0 160 // without crashing (but we don't need it anyhow)
michael@0 161 if (!inChildProcess()) {
michael@0 162 var prefBranch = Cc["@mozilla.org/preferences-service;1"].
michael@0 163 getService(Ci.nsIPrefBranch);
michael@0 164 prefBranch.setBoolPref("browser.preferences.content.log", true);
michael@0 165 }
michael@0 166

mercurial