michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // Inspired by the Places infrastructure in head_bookmarks.js michael@0: michael@0: const Cc = Components.classes; michael@0: const Ci = Components.interfaces; michael@0: const Cr = Components.results; michael@0: const Cu = Components.utils; michael@0: michael@0: Cu.import('resource://gre/modules/Services.jsm'); michael@0: Cu.import('resource://gre/modules/ContentPrefInstance.jsm'); michael@0: michael@0: const CONTENT_PREFS_DB_FILENAME = "content-prefs.sqlite"; michael@0: const CONTENT_PREFS_BACKUP_DB_FILENAME = "content-prefs.sqlite.corrupt"; michael@0: michael@0: var ContentPrefTest = { michael@0: //**************************************************************************// michael@0: // Convenience Getters michael@0: michael@0: __dirSvc: null, michael@0: get _dirSvc() { michael@0: if (!this.__dirSvc) michael@0: this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"]. michael@0: getService(Ci.nsIProperties); michael@0: return this.__dirSvc; michael@0: }, michael@0: michael@0: __consoleSvc: null, michael@0: get _consoleSvc() { michael@0: if (!this.__consoleSvc) michael@0: this.__consoleSvc = Cc["@mozilla.org/consoleservice;1"]. michael@0: getService(Ci.nsIConsoleService); michael@0: return this.__consoleSvc; michael@0: }, michael@0: michael@0: __ioSvc: null, michael@0: get _ioSvc() { michael@0: if (!this.__ioSvc) michael@0: this.__ioSvc = Cc["@mozilla.org/network/io-service;1"]. michael@0: getService(Ci.nsIIOService); michael@0: return this.__ioSvc; michael@0: }, michael@0: michael@0: michael@0: //**************************************************************************// michael@0: // nsISupports michael@0: michael@0: interfaces: [Ci.nsIDirectoryServiceProvider, Ci.nsISupports], michael@0: michael@0: QueryInterface: function ContentPrefTest_QueryInterface(iid) { michael@0: if (!this.interfaces.some( function(v) { return iid.equals(v) } )) michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: return this; michael@0: }, michael@0: michael@0: michael@0: //**************************************************************************// michael@0: // nsIDirectoryServiceProvider michael@0: michael@0: getFile: function ContentPrefTest_getFile(property, persistent) { michael@0: persistent.value = true; michael@0: michael@0: if (property == "ProfD") michael@0: return this._dirSvc.get("CurProcD", Ci.nsIFile); michael@0: michael@0: // This causes extraneous errors to show up in the log when the directory michael@0: // service asks us first for CurProcD and MozBinD. I wish there was a way michael@0: // to suppress those errors. michael@0: throw Cr.NS_ERROR_FAILURE; michael@0: }, michael@0: michael@0: michael@0: //**************************************************************************// michael@0: // Utilities michael@0: michael@0: getURI: function ContentPrefTest_getURI(spec) { michael@0: return this._ioSvc.newURI(spec, null, null); michael@0: }, michael@0: michael@0: /** michael@0: * Get the profile directory. michael@0: */ michael@0: getProfileDir: function ContentPrefTest_getProfileDir() { michael@0: // do_get_profile can be only called from a parent process michael@0: if (runningInParent) { michael@0: return do_get_profile(); michael@0: } michael@0: // if running in a content process, this just returns the path michael@0: // profile was initialized in the ipc head file michael@0: let env = Components.classes["@mozilla.org/process/environment;1"] michael@0: .getService(Components.interfaces.nsIEnvironment); michael@0: // the python harness sets this in the environment for us michael@0: let profd = env.get("XPCSHELL_TEST_PROFILE_DIR"); michael@0: let file = Components.classes["@mozilla.org/file/local;1"] michael@0: .createInstance(Components.interfaces.nsILocalFile); michael@0: file.initWithPath(profd); michael@0: return file; michael@0: }, michael@0: michael@0: /** michael@0: * Delete the content pref service's persistent datastore. We do this before michael@0: * and after running tests to make sure we start from scratch each time. We michael@0: * also do it during the database creation, schema migration, and backup tests. michael@0: */ michael@0: deleteDatabase: function ContentPrefTest_deleteDatabase() { michael@0: var file = this.getProfileDir(); michael@0: file.append(CONTENT_PREFS_DB_FILENAME); michael@0: if (file.exists()) michael@0: try { file.remove(false); } catch(e) { /* stupid windows box */ } michael@0: return file; michael@0: }, michael@0: michael@0: /** michael@0: * Delete the backup of the content pref service's persistent datastore. michael@0: * We do this during the database creation, schema migration, and backup tests. michael@0: */ michael@0: deleteBackupDatabase: function ContentPrefTest_deleteBackupDatabase() { michael@0: var file = this.getProfileDir(); michael@0: file.append(CONTENT_PREFS_BACKUP_DB_FILENAME); michael@0: if (file.exists()) michael@0: file.remove(false); michael@0: return file; michael@0: }, michael@0: michael@0: /** michael@0: * Log a message to the console and the test log. michael@0: */ michael@0: log: function ContentPrefTest_log(message) { michael@0: message = "*** ContentPrefTest: " + message; michael@0: this._consoleSvc.logStringMessage(message); michael@0: print(message); michael@0: } michael@0: michael@0: }; michael@0: michael@0: let gInPrivateBrowsing = false; michael@0: function enterPBMode() { michael@0: gInPrivateBrowsing = true; michael@0: } michael@0: function exitPBMode() { michael@0: gInPrivateBrowsing = false; michael@0: Services.obs.notifyObservers(null, "last-pb-context-exited", null); michael@0: } michael@0: michael@0: ContentPrefTest.deleteDatabase(); michael@0: michael@0: function inChildProcess() { michael@0: var appInfo = Cc["@mozilla.org/xre/app-info;1"]; michael@0: if (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType == michael@0: Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) { michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: // Turn on logging for the content preferences service so we can troubleshoot michael@0: // problems with the tests. Note that we cannot do this in a child process michael@0: // without crashing (but we don't need it anyhow) michael@0: if (!inChildProcess()) { michael@0: var prefBranch = Cc["@mozilla.org/preferences-service;1"]. michael@0: getService(Ci.nsIPrefBranch); michael@0: prefBranch.setBoolPref("browser.preferences.content.log", true); michael@0: } michael@0: