Bug 1890689 Don't pretend to pre-buffer with DynamicResampler r=pehrsons
[gecko.git] / browser / modules / test / unit / test_FirefoxBridgeExtensionUtils.js
blob77db0d82866802893734a353f43cff8df5e232cb
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 const { FirefoxBridgeExtensionUtils } = ChromeUtils.importESModule(
7   "resource:///modules/FirefoxBridgeExtensionUtils.sys.mjs"
8 );
10 const FIREFOX_SHELL_OPEN_COMMAND_PATH = "firefox\\shell\\open\\command";
11 const FIREFOX_PRIVATE_SHELL_OPEN_COMMAND_PATH =
12   "firefox-private\\shell\\open\\command";
14 class StubbedRegistryKey {
15   #children;
16   #originalChildren;
17   #closeCalled;
18   #deletedChildren;
19   #openedForRead;
20   #values;
22   constructor(children, values) {
23     this.#children = children;
24     this.#values = values;
25     this.#originalChildren = new Map(children);
27     this.#closeCalled = false;
28     this.#openedForRead = false;
29     this.#deletedChildren = new Set([]);
30   }
32   get ACCESS_READ() {
33     return 0;
34   }
36   reset() {
37     this.#closeCalled = false;
38     this.#deletedChildren = new Set([]);
39     this.#children = new Map(this.#originalChildren);
40   }
42   open(_accessLevel) {
43     this.#openedForRead = true;
44   }
46   get wasOpenedForRead() {
47     return this.#openedForRead;
48   }
50   openChild(path, accessLevel) {
51     const result = this.#children.get(path);
52     result?.open(accessLevel);
53     return result;
54   }
56   hasChild(path) {
57     return this.#children.has(path);
58   }
60   close() {
61     this.#closeCalled = true;
62   }
64   removeChild(path) {
65     this.#deletedChildren.add(path);
67     // delete the actual child if it's in there
68     this.#children.delete(path);
69   }
71   isChildDeleted(path) {
72     return this.#deletedChildren.has(path);
73   }
75   getChildName(index) {
76     let i = 0;
77     for (const [key] of this.#children) {
78       if (i == index) {
79         return key;
80       }
81       i++;
82     }
84     return undefined;
85   }
87   readStringValue(name) {
88     return this.#values.get(name);
89   }
91   get childCount() {
92     return this.#children.size;
93   }
95   getValueType(entryName) {
96     if (typeof this.readStringValue(entryName) == "string") {
97       return Ci.nsIWindowsRegKey.TYPE_STRING;
98     }
100     throw new Error(`${entryName} not found in registry`);
101   }
103   get wasCloseCalled() {
104     return this.#closeCalled;
105   }
107   getValueName(index) {
108     let i = 0;
109     for (const [key] of this.#values) {
110       if (i == index) {
111         return key;
112       }
113       i++;
114     }
116     return undefined;
117   }
119   get valueCount() {
120     return this.#values.size;
121   }
124 class StubbedDeleteBridgeProtocolRegistryEntryHelper {
125   #applicationPath;
126   #registryRootKey;
128   constructor({ applicationPath, registryRootKey }) {
129     this.#applicationPath = applicationPath;
130     this.#registryRootKey = registryRootKey;
131   }
133   getApplicationPath() {
134     return this.#applicationPath;
135   }
137   openRegistryRoot() {
138     return this.#registryRootKey;
139   }
141   deleteRegistryTree(root, toDeletePath) {
142     // simplify this for tests
143     root.removeChild(toDeletePath);
144   }
147 add_task(async function test_DeleteWhenSameFirefoxInstall() {
148   const applicationPath = "testPath";
150   const firefoxEntries = new Map();
151   firefoxEntries.set("", `\"${applicationPath}\" -osint -url \"%1\"`);
153   const firefoxProtocolRegKey = new StubbedRegistryKey(
154     new Map(),
155     firefoxEntries
156   );
158   const firefoxPrivateEntries = new Map();
159   firefoxPrivateEntries.set(
160     "",
161     `\"${applicationPath}\" -osint -private-window \"%1\"`
162   );
163   const firefoxPrivateProtocolRegKey = new StubbedRegistryKey(
164     new Map(),
165     firefoxPrivateEntries
166   );
168   const children = new Map();
169   children.set(FIREFOX_SHELL_OPEN_COMMAND_PATH, firefoxProtocolRegKey);
170   children.set(
171     FIREFOX_PRIVATE_SHELL_OPEN_COMMAND_PATH,
172     firefoxPrivateProtocolRegKey
173   );
175   const registryRootKey = new StubbedRegistryKey(children, new Map());
177   const stubbedDeleteBridgeProtocolRegistryHelper =
178     new StubbedDeleteBridgeProtocolRegistryEntryHelper({
179       applicationPath,
180       registryRootKey,
181     });
183   FirefoxBridgeExtensionUtils.maybeDeleteBridgeProtocolRegistryEntries(
184     stubbedDeleteBridgeProtocolRegistryHelper
185   );
187   ok(registryRootKey.wasCloseCalled, "Root key closed");
189   ok(firefoxProtocolRegKey.wasOpenedForRead, "Firefox key opened");
190   ok(firefoxProtocolRegKey.wasCloseCalled, "Firefox key closed");
191   ok(
192     registryRootKey.isChildDeleted("firefox"),
193     "Firefox protocol registry entry deleted"
194   );
196   ok(
197     firefoxPrivateProtocolRegKey.wasOpenedForRead,
198     "Firefox private key opened"
199   );
200   ok(firefoxPrivateProtocolRegKey.wasCloseCalled, "Firefox private key closed");
201   ok(
202     registryRootKey.isChildDeleted("firefox-private"),
203     "Firefox private protocol registry entry deleted"
204   );
207 add_task(async function test_DeleteWhenDifferentFirefoxInstall() {
208   const applicationPath = "testPath";
209   const badApplicationPath = "testPath2";
211   const firefoxEntries = new Map();
212   firefoxEntries.set("", `\"${badApplicationPath}\" -osint -url \"%1\"`);
214   const firefoxProtocolRegKey = new StubbedRegistryKey(
215     new Map(),
216     firefoxEntries
217   );
219   const firefoxPrivateEntries = new Map();
220   firefoxPrivateEntries.set(
221     "",
222     `\"${badApplicationPath}\" -osint -private-window \"%1\"`
223   );
224   const firefoxPrivateProtocolRegKey = new StubbedRegistryKey(
225     new Map(),
226     firefoxPrivateEntries
227   );
229   const children = new Map();
230   children.set(FIREFOX_SHELL_OPEN_COMMAND_PATH, firefoxProtocolRegKey);
231   children.set(
232     FIREFOX_PRIVATE_SHELL_OPEN_COMMAND_PATH,
233     firefoxPrivateProtocolRegKey
234   );
236   const registryRootKey = new StubbedRegistryKey(children, new Map());
238   const stubbedDeleteBridgeProtocolRegistryHelper =
239     new StubbedDeleteBridgeProtocolRegistryEntryHelper({
240       applicationPath,
241       registryRootKey,
242     });
244   FirefoxBridgeExtensionUtils.maybeDeleteBridgeProtocolRegistryEntries(
245     stubbedDeleteBridgeProtocolRegistryHelper
246   );
248   ok(registryRootKey.wasCloseCalled, "Root key closed");
250   ok(firefoxProtocolRegKey.wasOpenedForRead, "Firefox key opened");
251   ok(firefoxProtocolRegKey.wasCloseCalled, "Firefox key closed");
252   ok(
253     !registryRootKey.isChildDeleted("firefox"),
254     "Firefox protocol registry entry not deleted"
255   );
257   ok(
258     firefoxPrivateProtocolRegKey.wasOpenedForRead,
259     "Firefox private key opened"
260   );
261   ok(firefoxPrivateProtocolRegKey.wasCloseCalled, "Firefox private key closed");
262   ok(
263     !registryRootKey.isChildDeleted("firefox-private"),
264     "Firefox private protocol registry entry not deleted"
265   );
268 add_task(async function test_DeleteWhenNoRegistryEntries() {
269   const applicationPath = "testPath";
271   const firefoxPrivateEntries = new Map();
272   const firefoxPrivateProtocolRegKey = new StubbedRegistryKey(
273     new Map(),
274     firefoxPrivateEntries
275   );
277   const children = new Map();
278   children.set(
279     FIREFOX_PRIVATE_SHELL_OPEN_COMMAND_PATH,
280     firefoxPrivateProtocolRegKey
281   );
283   const registryRootKey = new StubbedRegistryKey(children, new Map());
285   const stubbedDeleteBridgeProtocolRegistryHelper =
286     new StubbedDeleteBridgeProtocolRegistryEntryHelper({
287       applicationPath,
288       registryRootKey,
289     });
291   FirefoxBridgeExtensionUtils.maybeDeleteBridgeProtocolRegistryEntries(
292     stubbedDeleteBridgeProtocolRegistryHelper
293   );
295   ok(registryRootKey.wasCloseCalled, "Root key closed");
297   ok(
298     firefoxPrivateProtocolRegKey.wasOpenedForRead,
299     "Firefox private key opened"
300   );
301   ok(firefoxPrivateProtocolRegKey.wasCloseCalled, "Firefox private key closed");
302   ok(
303     !registryRootKey.isChildDeleted("firefox"),
304     "Firefox protocol registry entry deleted when it shouldn't be"
305   );
306   ok(
307     !registryRootKey.isChildDeleted("firefox-private"),
308     "Firefox private protocol registry deleted when it shouldn't be"
309   );
312 add_task(async function test_DeleteWhenUnexpectedRegistryEntries() {
313   const applicationPath = "testPath";
315   const firefoxEntries = new Map();
316   firefoxEntries.set("", `\"${applicationPath}\" -osint -url \"%1\"`);
317   firefoxEntries.set("extraEntry", "extraValue");
318   const firefoxProtocolRegKey = new StubbedRegistryKey(
319     new Map(),
320     firefoxEntries
321   );
323   const children = new Map();
324   children.set(FIREFOX_SHELL_OPEN_COMMAND_PATH, firefoxProtocolRegKey);
326   const registryRootKey = new StubbedRegistryKey(children, new Map());
328   const stubbedDeleteBridgeProtocolRegistryHelper =
329     new StubbedDeleteBridgeProtocolRegistryEntryHelper({
330       applicationPath,
331       registryRootKey,
332     });
334   FirefoxBridgeExtensionUtils.maybeDeleteBridgeProtocolRegistryEntries(
335     stubbedDeleteBridgeProtocolRegistryHelper
336   );
338   ok(registryRootKey.wasCloseCalled, "Root key closed");
340   ok(firefoxProtocolRegKey.wasOpenedForRead, "Firefox key opened");
341   ok(firefoxProtocolRegKey.wasCloseCalled, "Firefox key closed");
342   ok(
343     !registryRootKey.isChildDeleted("firefox"),
344     "Firefox protocol registry entry deleted when it shouldn't be"
345   );
346   ok(
347     !registryRootKey.isChildDeleted("firefox-private"),
348     "Firefox private protocol registry deleted when it shouldn't be"
349   );