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

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

mercurial