Bug 1700051: part 36) Reduce accessibility of `SoftText::mBegin` to `private`. r...
[gecko.git] / testing / modules / AppData.jsm
blob7d27ef1f2afe88ee5ed08c0499d99e313ee68c4c
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 var EXPORTED_SYMBOLS = ["makeFakeAppDir"];
9 const { OS } = ChromeUtils.import("resource://gre/modules/osfile.jsm");
10 const { Promise } = ChromeUtils.import("resource://gre/modules/Promise.jsm");
11 const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
13 // Reference needed in order for fake app dir provider to be active.
14 var gFakeAppDirectoryProvider;
16 /**
17  * Installs a fake UAppData directory.
18  *
19  * This is needed by tests because a UAppData directory typically isn't
20  * present in the test environment.
21  *
22  * We create the new UAppData directory under the profile's directory
23  * because the profile directory is automatically cleaned as part of
24  * test shutdown.
25  *
26  * This returns a promise that will be resolved once the new directory
27  * is created and installed.
28  */
29 var makeFakeAppDir = function() {
30   let dirMode = OS.Constants.libc.S_IRWXU;
31   let baseFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
32   let appD = baseFile.clone();
33   appD.append("UAppData");
35   if (gFakeAppDirectoryProvider) {
36     return Promise.resolve(appD.path);
37   }
39   function makeDir(f) {
40     if (f.exists()) {
41       return;
42     }
44     dump("Creating directory: " + f.path + "\n");
45     f.create(Ci.nsIFile.DIRECTORY_TYPE, dirMode);
46   }
48   makeDir(appD);
50   let reportsD = appD.clone();
51   reportsD.append("Crash Reports");
53   let pendingD = reportsD.clone();
54   pendingD.append("pending");
55   let submittedD = reportsD.clone();
56   submittedD.append("submitted");
58   makeDir(reportsD);
59   makeDir(pendingD);
60   makeDir(submittedD);
62   let provider = {
63     getFile(prop, persistent) {
64       persistent.value = true;
65       if (prop == "UAppData") {
66         return appD.clone();
67       }
69       throw Components.Exception("", Cr.NS_ERROR_FAILURE);
70     },
72     QueryInterace(iid) {
73       if (
74         iid.equals(Ci.nsIDirectoryServiceProvider) ||
75         iid.equals(Ci.nsISupports)
76       ) {
77         return this;
78       }
80       throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
81     },
82   };
84   // Register the new provider.
85   Services.dirsvc.registerProvider(provider);
87   // And undefine the old one.
88   try {
89     Services.dirsvc.undefine("UAppData");
90   } catch (ex) {}
92   gFakeAppDirectoryProvider = provider;
94   dump("Successfully installed fake UAppDir\n");
95   return Promise.resolve(appD.path);