Bumping gaia.json for 1 gaia revision(s) a=gaia-bump
[gecko.git] / testing / modules / AppData.jsm
blobafccfb7cf9f8bac2e86fc8defa08c0d3a3bcdc83
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 "use strict";
7 this.EXPORTED_SYMBOLS = [
8   "makeFakeAppDir",
9 ];
11 const {classes: Cc, interfaces: Ci, results: Cr, utils: Cu} = Components;
13 Cu.import("resource://gre/modules/osfile.jsm");
14 Cu.import("resource://gre/modules/Promise.jsm");
16 // Reference needed in order for fake app dir provider to be active.
17 let gFakeAppDirectoryProvider;
19 /**
20  * Installs a fake UAppData directory.
21  *
22  * This is needed by tests because a UAppData directory typically isn't
23  * present in the test environment.
24  *
25  * We create the new UAppData directory under the profile's directory
26  * because the profile directory is automatically cleaned as part of
27  * test shutdown.
28  *
29  * This returns a promise that will be resolved once the new directory
30  * is created and installed.
31  */
32 this.makeFakeAppDir = function () {
33   let dirMode = OS.Constants.libc.S_IRWXU;
34   let dirService = Cc["@mozilla.org/file/directory_service;1"]
35                      .getService(Ci.nsIProperties);
36   let baseFile = dirService.get("ProfD", Ci.nsIFile);
37   let appD = baseFile.clone();
38   appD.append("UAppData");
40   if (gFakeAppDirectoryProvider) {
41     return Promise.resolve(appD.path);
42   }
44   function makeDir(f) {
45     if (f.exists()) {
46       return;
47     }
49     dump("Creating directory: " + f.path + "\n");
50     f.create(Ci.nsIFile.DIRECTORY_TYPE, dirMode);
51   }
53   makeDir(appD);
55   let reportsD = appD.clone();
56   reportsD.append("Crash Reports");
58   let pendingD = reportsD.clone();
59   pendingD.append("pending");
60   let submittedD = reportsD.clone();
61   submittedD.append("submitted");
63   makeDir(reportsD);
64   makeDir(pendingD);
65   makeDir(submittedD);
67   let provider = {
68     getFile: function (prop, persistent) {
69       persistent.value = true;
70       if (prop == "UAppData") {
71         return appD.clone();
72       }
74       throw Cr.NS_ERROR_FAILURE;
75     },
77     QueryInterace: function (iid) {
78       if (iid.equals(Ci.nsIDirectoryServiceProvider) ||
79           iid.equals(Ci.nsISupports)) {
80         return this;
81       }
83       throw Cr.NS_ERROR_NO_INTERFACE;
84     },
85   };
87   // Register the new provider.
88   dirService.QueryInterface(Ci.nsIDirectoryService)
89             .registerProvider(provider);
91   // And undefine the old one.
92   try {
93     dirService.undefine("UAppData");
94   } catch (ex) {};
96   gFakeAppDirectoryProvider = provider;
98   dump("Successfully installed fake UAppDir\n");
99   return Promise.resolve(appD.path);