testing/modules/AppData.jsm

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 "use strict";
michael@0 6
michael@0 7 this.EXPORTED_SYMBOLS = [
michael@0 8 "makeFakeAppDir",
michael@0 9 ];
michael@0 10
michael@0 11 const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
michael@0 12
michael@0 13 Cu.import("resource://gre/modules/osfile.jsm");
michael@0 14 Cu.import("resource://gre/modules/Promise.jsm");
michael@0 15
michael@0 16 // Reference needed in order for fake app dir provider to be active.
michael@0 17 let gFakeAppDirectoryProvider;
michael@0 18
michael@0 19 /**
michael@0 20 * Installs a fake UAppData directory.
michael@0 21 *
michael@0 22 * This is needed by tests because a UAppData directory typically isn't
michael@0 23 * present in the test environment.
michael@0 24 *
michael@0 25 * We create the new UAppData directory under the profile's directory
michael@0 26 * because the profile directory is automatically cleaned as part of
michael@0 27 * test shutdown.
michael@0 28 *
michael@0 29 * This returns a promise that will be resolved once the new directory
michael@0 30 * is created and installed.
michael@0 31 */
michael@0 32 this.makeFakeAppDir = function () {
michael@0 33 let dirMode = OS.Constants.libc.S_IRWXU;
michael@0 34 let dirService = Cc["@mozilla.org/file/directory_service;1"]
michael@0 35 .getService(Ci.nsIProperties);
michael@0 36 let baseFile = dirService.get("ProfD", Ci.nsIFile);
michael@0 37 let appD = baseFile.clone();
michael@0 38 appD.append("UAppData");
michael@0 39
michael@0 40 if (gFakeAppDirectoryProvider) {
michael@0 41 return Promise.resolve(appD.path);
michael@0 42 }
michael@0 43
michael@0 44 function makeDir(f) {
michael@0 45 if (f.exists()) {
michael@0 46 return;
michael@0 47 }
michael@0 48
michael@0 49 dump("Creating directory: " + f.path + "\n");
michael@0 50 f.create(Ci.nsIFile.DIRECTORY_TYPE, dirMode);
michael@0 51 }
michael@0 52
michael@0 53 makeDir(appD);
michael@0 54
michael@0 55 let reportsD = appD.clone();
michael@0 56 reportsD.append("Crash Reports");
michael@0 57
michael@0 58 let pendingD = reportsD.clone();
michael@0 59 pendingD.append("pending");
michael@0 60 let submittedD = reportsD.clone();
michael@0 61 submittedD.append("submitted");
michael@0 62
michael@0 63 makeDir(reportsD);
michael@0 64 makeDir(pendingD);
michael@0 65 makeDir(submittedD);
michael@0 66
michael@0 67 let provider = {
michael@0 68 getFile: function (prop, persistent) {
michael@0 69 persistent.value = true;
michael@0 70 if (prop == "UAppData") {
michael@0 71 return appD.clone();
michael@0 72 }
michael@0 73
michael@0 74 throw Cr.NS_ERROR_FAILURE;
michael@0 75 },
michael@0 76
michael@0 77 QueryInterace: function (iid) {
michael@0 78 if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
michael@0 79 iid.equals(Ci.nsISupports)) {
michael@0 80 return this;
michael@0 81 }
michael@0 82
michael@0 83 throw Cr.NS_ERROR_NO_INTERFACE;
michael@0 84 },
michael@0 85 };
michael@0 86
michael@0 87 // Register the new provider.
michael@0 88 dirService.QueryInterface(Ci.nsIDirectoryService)
michael@0 89 .registerProvider(provider);
michael@0 90
michael@0 91 // And undefine the old one.
michael@0 92 try {
michael@0 93 dirService.undefine("UAppData");
michael@0 94 } catch (ex) {};
michael@0 95
michael@0 96 gFakeAppDirectoryProvider = provider;
michael@0 97
michael@0 98 dump("Successfully installed fake UAppDir\n");
michael@0 99 return Promise.resolve(appD.path);
michael@0 100 };
michael@0 101

mercurial