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.

     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/. */
     5 // Inspired by the Places infrastructure in head_bookmarks.js
     7 const Cc = Components.classes;
     8 const Ci = Components.interfaces;
     9 const Cr = Components.results;
    10 const Cu = Components.utils;
    12 Cu.import('resource://gre/modules/Services.jsm');
    13 Cu.import('resource://gre/modules/ContentPrefInstance.jsm');
    15 const CONTENT_PREFS_DB_FILENAME = "content-prefs.sqlite";
    16 const CONTENT_PREFS_BACKUP_DB_FILENAME = "content-prefs.sqlite.corrupt";
    18 var ContentPrefTest = {
    19   //**************************************************************************//
    20   // Convenience Getters
    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   },
    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   },
    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   },
    47   //**************************************************************************//
    48   // nsISupports
    50   interfaces: [Ci.nsIDirectoryServiceProvider, Ci.nsISupports],
    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   },
    59   //**************************************************************************//
    60   // nsIDirectoryServiceProvider
    62   getFile: function ContentPrefTest_getFile(property, persistent) {
    63     persistent.value = true;
    65     if (property == "ProfD")
    66       return this._dirSvc.get("CurProcD", Ci.nsIFile);
    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   },
    75   //**************************************************************************//
    76   // Utilities
    78   getURI: function ContentPrefTest_getURI(spec) {
    79     return this._ioSvc.newURI(spec, null, null);
    80   },
    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   },
   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   },
   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   },
   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   }
   136 };
   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 }
   147 ContentPrefTest.deleteDatabase();
   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 }
   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 }

mercurial