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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/components/contentprefs/tests/unit/head_contentPrefs.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,166 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +// Inspired by the Places infrastructure in head_bookmarks.js
     1.9 +
    1.10 +const Cc = Components.classes;
    1.11 +const Ci = Components.interfaces;
    1.12 +const Cr = Components.results;
    1.13 +const Cu = Components.utils;
    1.14 +
    1.15 +Cu.import('resource://gre/modules/Services.jsm');
    1.16 +Cu.import('resource://gre/modules/ContentPrefInstance.jsm');
    1.17 +
    1.18 +const CONTENT_PREFS_DB_FILENAME = "content-prefs.sqlite";
    1.19 +const CONTENT_PREFS_BACKUP_DB_FILENAME = "content-prefs.sqlite.corrupt";
    1.20 +
    1.21 +var ContentPrefTest = {
    1.22 +  //**************************************************************************//
    1.23 +  // Convenience Getters
    1.24 +
    1.25 +  __dirSvc: null,
    1.26 +  get _dirSvc() {
    1.27 +    if (!this.__dirSvc)
    1.28 +      this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"].
    1.29 +                      getService(Ci.nsIProperties);
    1.30 +    return this.__dirSvc;
    1.31 +  },
    1.32 +
    1.33 +  __consoleSvc: null,
    1.34 +  get _consoleSvc() {
    1.35 +    if (!this.__consoleSvc)
    1.36 +      this.__consoleSvc = Cc["@mozilla.org/consoleservice;1"].
    1.37 +                          getService(Ci.nsIConsoleService);
    1.38 +    return this.__consoleSvc;
    1.39 +  },
    1.40 +
    1.41 +  __ioSvc: null,
    1.42 +  get _ioSvc() {
    1.43 +    if (!this.__ioSvc)
    1.44 +      this.__ioSvc = Cc["@mozilla.org/network/io-service;1"].
    1.45 +                     getService(Ci.nsIIOService);
    1.46 +    return this.__ioSvc;
    1.47 +  },
    1.48 +
    1.49 +
    1.50 +  //**************************************************************************//
    1.51 +  // nsISupports
    1.52 +  
    1.53 +  interfaces: [Ci.nsIDirectoryServiceProvider, Ci.nsISupports],
    1.54 +
    1.55 +  QueryInterface: function ContentPrefTest_QueryInterface(iid) {
    1.56 +    if (!this.interfaces.some( function(v) { return iid.equals(v) } ))
    1.57 +      throw Cr.NS_ERROR_NO_INTERFACE;
    1.58 +    return this;
    1.59 +  },
    1.60 +
    1.61 +
    1.62 +  //**************************************************************************//
    1.63 +  // nsIDirectoryServiceProvider
    1.64 +
    1.65 +  getFile: function ContentPrefTest_getFile(property, persistent) {
    1.66 +    persistent.value = true;
    1.67 +
    1.68 +    if (property == "ProfD")
    1.69 +      return this._dirSvc.get("CurProcD", Ci.nsIFile);
    1.70 +
    1.71 +    // This causes extraneous errors to show up in the log when the directory
    1.72 +    // service asks us first for CurProcD and MozBinD.  I wish there was a way
    1.73 +    // to suppress those errors.
    1.74 +    throw Cr.NS_ERROR_FAILURE;
    1.75 +  },
    1.76 +
    1.77 +
    1.78 +  //**************************************************************************//
    1.79 +  // Utilities
    1.80 +
    1.81 +  getURI: function ContentPrefTest_getURI(spec) {
    1.82 +    return this._ioSvc.newURI(spec, null, null);
    1.83 +  },
    1.84 +
    1.85 +  /**
    1.86 +   * Get the profile directory.
    1.87 +   */
    1.88 +  getProfileDir: function ContentPrefTest_getProfileDir() {
    1.89 +    // do_get_profile can be only called from a parent process
    1.90 +    if (runningInParent) {
    1.91 +      return do_get_profile();
    1.92 +    }
    1.93 +    // if running in a content process, this just returns the path
    1.94 +    // profile was initialized in the ipc head file
    1.95 +    let env = Components.classes["@mozilla.org/process/environment;1"]
    1.96 +                        .getService(Components.interfaces.nsIEnvironment);
    1.97 +    // the python harness sets this in the environment for us
    1.98 +    let profd = env.get("XPCSHELL_TEST_PROFILE_DIR");
    1.99 +    let file = Components.classes["@mozilla.org/file/local;1"]
   1.100 +                         .createInstance(Components.interfaces.nsILocalFile);
   1.101 +    file.initWithPath(profd);
   1.102 +    return file;
   1.103 +  },
   1.104 +
   1.105 +  /**
   1.106 +   * Delete the content pref service's persistent datastore.  We do this before
   1.107 +   * and after running tests to make sure we start from scratch each time. We
   1.108 +   * also do it during the database creation, schema migration, and backup tests.
   1.109 +   */
   1.110 +  deleteDatabase: function ContentPrefTest_deleteDatabase() {
   1.111 +    var file = this.getProfileDir();
   1.112 +    file.append(CONTENT_PREFS_DB_FILENAME);
   1.113 +    if (file.exists())
   1.114 +      try { file.remove(false); } catch(e) { /* stupid windows box */ }
   1.115 +    return file;
   1.116 +  },
   1.117 +
   1.118 +  /**
   1.119 +   * Delete the backup of the content pref service's persistent datastore.
   1.120 +   * We do this during the database creation, schema migration, and backup tests.
   1.121 +   */
   1.122 +  deleteBackupDatabase: function ContentPrefTest_deleteBackupDatabase() {
   1.123 +    var file = this.getProfileDir();
   1.124 +    file.append(CONTENT_PREFS_BACKUP_DB_FILENAME);
   1.125 +    if (file.exists())
   1.126 +      file.remove(false);
   1.127 +    return file;
   1.128 +  },
   1.129 +
   1.130 +  /**
   1.131 +   * Log a message to the console and the test log.
   1.132 +   */
   1.133 +  log: function ContentPrefTest_log(message) {
   1.134 +    message = "*** ContentPrefTest: " + message;
   1.135 +    this._consoleSvc.logStringMessage(message);
   1.136 +    print(message);
   1.137 +  }
   1.138 +
   1.139 +};
   1.140 +
   1.141 +let gInPrivateBrowsing = false;
   1.142 +function enterPBMode() {
   1.143 +  gInPrivateBrowsing = true;
   1.144 +}
   1.145 +function exitPBMode() {
   1.146 +  gInPrivateBrowsing = false;
   1.147 +  Services.obs.notifyObservers(null, "last-pb-context-exited", null);
   1.148 +}
   1.149 +
   1.150 +ContentPrefTest.deleteDatabase();
   1.151 +
   1.152 +function inChildProcess() {
   1.153 +  var appInfo = Cc["@mozilla.org/xre/app-info;1"];
   1.154 +  if (!appInfo || appInfo.getService(Ci.nsIXULRuntime).processType ==
   1.155 +      Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT) {
   1.156 +    return false;
   1.157 +  }
   1.158 +  return true;
   1.159 +}
   1.160 +
   1.161 +// Turn on logging for the content preferences service so we can troubleshoot
   1.162 +// problems with the tests. Note that we cannot do this in a child process
   1.163 +// without crashing (but we don't need it anyhow)
   1.164 +if (!inChildProcess()) {
   1.165 +  var prefBranch = Cc["@mozilla.org/preferences-service;1"].
   1.166 +                   getService(Ci.nsIPrefBranch);
   1.167 +  prefBranch.setBoolPref("browser.preferences.content.log", true);
   1.168 +}
   1.169 +

mercurial