testing/modules/AppData.jsm

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/testing/modules/AppData.jsm	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     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 +"use strict";
     1.9 +
    1.10 +this.EXPORTED_SYMBOLS = [
    1.11 +  "makeFakeAppDir",
    1.12 +];
    1.13 +
    1.14 +const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
    1.15 +
    1.16 +Cu.import("resource://gre/modules/osfile.jsm");
    1.17 +Cu.import("resource://gre/modules/Promise.jsm");
    1.18 +
    1.19 +// Reference needed in order for fake app dir provider to be active.
    1.20 +let gFakeAppDirectoryProvider;
    1.21 +
    1.22 +/**
    1.23 + * Installs a fake UAppData directory.
    1.24 + *
    1.25 + * This is needed by tests because a UAppData directory typically isn't
    1.26 + * present in the test environment.
    1.27 + *
    1.28 + * We create the new UAppData directory under the profile's directory
    1.29 + * because the profile directory is automatically cleaned as part of
    1.30 + * test shutdown.
    1.31 + *
    1.32 + * This returns a promise that will be resolved once the new directory
    1.33 + * is created and installed.
    1.34 + */
    1.35 +this.makeFakeAppDir = function () {
    1.36 +  let dirMode = OS.Constants.libc.S_IRWXU;
    1.37 +  let dirService = Cc["@mozilla.org/file/directory_service;1"]
    1.38 +                     .getService(Ci.nsIProperties);
    1.39 +  let baseFile = dirService.get("ProfD", Ci.nsIFile);
    1.40 +  let appD = baseFile.clone();
    1.41 +  appD.append("UAppData");
    1.42 +
    1.43 +  if (gFakeAppDirectoryProvider) {
    1.44 +    return Promise.resolve(appD.path);
    1.45 +  }
    1.46 +
    1.47 +  function makeDir(f) {
    1.48 +    if (f.exists()) {
    1.49 +      return;
    1.50 +    }
    1.51 +
    1.52 +    dump("Creating directory: " + f.path + "\n");
    1.53 +    f.create(Ci.nsIFile.DIRECTORY_TYPE, dirMode);
    1.54 +  }
    1.55 +
    1.56 +  makeDir(appD);
    1.57 +
    1.58 +  let reportsD = appD.clone();
    1.59 +  reportsD.append("Crash Reports");
    1.60 +
    1.61 +  let pendingD = reportsD.clone();
    1.62 +  pendingD.append("pending");
    1.63 +  let submittedD = reportsD.clone();
    1.64 +  submittedD.append("submitted");
    1.65 +
    1.66 +  makeDir(reportsD);
    1.67 +  makeDir(pendingD);
    1.68 +  makeDir(submittedD);
    1.69 +
    1.70 +  let provider = {
    1.71 +    getFile: function (prop, persistent) {
    1.72 +      persistent.value = true;
    1.73 +      if (prop == "UAppData") {
    1.74 +        return appD.clone();
    1.75 +      }
    1.76 +
    1.77 +      throw Cr.NS_ERROR_FAILURE;
    1.78 +    },
    1.79 +
    1.80 +    QueryInterace: function (iid) {
    1.81 +      if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
    1.82 +          iid.equals(Ci.nsISupports)) {
    1.83 +        return this;
    1.84 +      }
    1.85 +
    1.86 +      throw Cr.NS_ERROR_NO_INTERFACE;
    1.87 +    },
    1.88 +  };
    1.89 +
    1.90 +  // Register the new provider.
    1.91 +  dirService.QueryInterface(Ci.nsIDirectoryService)
    1.92 +            .registerProvider(provider);
    1.93 +
    1.94 +  // And undefine the old one.
    1.95 +  try {
    1.96 +    dirService.undefine("UAppData");
    1.97 +  } catch (ex) {};
    1.98 +
    1.99 +  gFakeAppDirectoryProvider = provider;
   1.100 +
   1.101 +  dump("Successfully installed fake UAppDir\n");
   1.102 +  return Promise.resolve(appD.path);
   1.103 +};
   1.104 +

mercurial