No bug - tagging b4d3227540c9ebc43d64aac6168fdca7019c22d8 with FIREFOX_BETA_126_BASE...
[gecko.git] / testing / modules / AppData.sys.mjs
blob0eaa3ba722008d34313d08805732213fa677d9a8
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 // Reference needed in order for fake app dir provider to be active.
6 var gFakeAppDirectoryProvider;
8 /**
9  * Installs a fake UAppData directory.
10  *
11  * This is needed by tests because a UAppData directory typically isn't
12  * present in the test environment.
13  *
14  * We create the new UAppData directory under the profile's directory
15  * because the profile directory is automatically cleaned as part of
16  * test shutdown.
17  *
18  * This returns a promise that will be resolved once the new directory
19  * is created and installed.
20  */
21 export var makeFakeAppDir = function () {
22   let dirMode = 0o700;
23   let baseFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
24   let appD = baseFile.clone();
25   appD.append("UAppData");
27   if (gFakeAppDirectoryProvider) {
28     return Promise.resolve(appD.path);
29   }
31   function makeDir(f) {
32     if (f.exists()) {
33       return;
34     }
36     dump("Creating directory: " + f.path + "\n");
37     f.create(Ci.nsIFile.DIRECTORY_TYPE, dirMode);
38   }
40   makeDir(appD);
42   let reportsD = appD.clone();
43   reportsD.append("Crash Reports");
45   let pendingD = reportsD.clone();
46   pendingD.append("pending");
47   let submittedD = reportsD.clone();
48   submittedD.append("submitted");
50   makeDir(reportsD);
51   makeDir(pendingD);
52   makeDir(submittedD);
54   let provider = {
55     getFile(prop, persistent) {
56       persistent.value = true;
57       if (prop == "UAppData") {
58         return appD.clone();
59       }
61       throw Components.Exception("", Cr.NS_ERROR_FAILURE);
62     },
64     QueryInterace(iid) {
65       if (
66         iid.equals(Ci.nsIDirectoryServiceProvider) ||
67         iid.equals(Ci.nsISupports)
68       ) {
69         return this;
70       }
72       throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
73     },
74   };
76   // Register the new provider.
77   Services.dirsvc.registerProvider(provider);
79   // And undefine the old one.
80   try {
81     Services.dirsvc.undefine("UAppData");
82   } catch (ex) {}
84   gFakeAppDirectoryProvider = provider;
86   dump("Successfully installed fake UAppDir\n");
87   return Promise.resolve(appD.path);