michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = [ michael@0: "makeFakeAppDir", michael@0: ]; michael@0: michael@0: const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components; michael@0: michael@0: Cu.import("resource://gre/modules/osfile.jsm"); michael@0: Cu.import("resource://gre/modules/Promise.jsm"); michael@0: michael@0: // Reference needed in order for fake app dir provider to be active. michael@0: let gFakeAppDirectoryProvider; michael@0: michael@0: /** michael@0: * Installs a fake UAppData directory. michael@0: * michael@0: * This is needed by tests because a UAppData directory typically isn't michael@0: * present in the test environment. michael@0: * michael@0: * We create the new UAppData directory under the profile's directory michael@0: * because the profile directory is automatically cleaned as part of michael@0: * test shutdown. michael@0: * michael@0: * This returns a promise that will be resolved once the new directory michael@0: * is created and installed. michael@0: */ michael@0: this.makeFakeAppDir = function () { michael@0: let dirMode = OS.Constants.libc.S_IRWXU; michael@0: let dirService = Cc["@mozilla.org/file/directory_service;1"] michael@0: .getService(Ci.nsIProperties); michael@0: let baseFile = dirService.get("ProfD", Ci.nsIFile); michael@0: let appD = baseFile.clone(); michael@0: appD.append("UAppData"); michael@0: michael@0: if (gFakeAppDirectoryProvider) { michael@0: return Promise.resolve(appD.path); michael@0: } michael@0: michael@0: function makeDir(f) { michael@0: if (f.exists()) { michael@0: return; michael@0: } michael@0: michael@0: dump("Creating directory: " + f.path + "\n"); michael@0: f.create(Ci.nsIFile.DIRECTORY_TYPE, dirMode); michael@0: } michael@0: michael@0: makeDir(appD); michael@0: michael@0: let reportsD = appD.clone(); michael@0: reportsD.append("Crash Reports"); michael@0: michael@0: let pendingD = reportsD.clone(); michael@0: pendingD.append("pending"); michael@0: let submittedD = reportsD.clone(); michael@0: submittedD.append("submitted"); michael@0: michael@0: makeDir(reportsD); michael@0: makeDir(pendingD); michael@0: makeDir(submittedD); michael@0: michael@0: let provider = { michael@0: getFile: function (prop, persistent) { michael@0: persistent.value = true; michael@0: if (prop == "UAppData") { michael@0: return appD.clone(); michael@0: } michael@0: michael@0: throw Cr.NS_ERROR_FAILURE; michael@0: }, michael@0: michael@0: QueryInterace: function (iid) { michael@0: if (iid.equals(Ci.nsIDirectoryServiceProvider) || michael@0: iid.equals(Ci.nsISupports)) { michael@0: return this; michael@0: } michael@0: michael@0: throw Cr.NS_ERROR_NO_INTERFACE; michael@0: }, michael@0: }; michael@0: michael@0: // Register the new provider. michael@0: dirService.QueryInterface(Ci.nsIDirectoryService) michael@0: .registerProvider(provider); michael@0: michael@0: // And undefine the old one. michael@0: try { michael@0: dirService.undefine("UAppData"); michael@0: } catch (ex) {}; michael@0: michael@0: gFakeAppDirectoryProvider = provider; michael@0: michael@0: dump("Successfully installed fake UAppDir\n"); michael@0: return Promise.resolve(appD.path); michael@0: }; michael@0: