No bug - tagging b4d3227540c9ebc43d64aac6168fdca7019c22d8 with FIREFOX_BETA_126_BASE...
[gecko.git] / testing / modules / MockRegistry.sys.mjs
blobb64d1ce2a859a79e356e8d716df2f0f89304ff08
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 import { MockRegistrar } from "resource://testing-common/MockRegistrar.sys.mjs";
7 export class MockRegistry {
8   constructor() {
9     // Three level structure of Maps pointing to Maps pointing to Maps
10     // this.roots is the top of the structure and has ROOT_KEY_* values
11     // as keys.  Maps at the second level are the values of the first
12     // level Map, they have registry keys (also called paths) as keys.
13     // Third level maps are the values in second level maps, they have
14     // map registry names to corresponding values (which in this implementation
15     // are always strings).
16     this.roots = new Map([
17       [Ci.nsIWindowsRegKey.ROOT_KEY_LOCAL_MACHINE, new Map()],
18       [Ci.nsIWindowsRegKey.ROOT_KEY_CURRENT_USER, new Map()],
19       [Ci.nsIWindowsRegKey.ROOT_KEY_CLASSES_ROOT, new Map()],
20     ]);
22     let registry = this;
24     /**
25      * This is a mock nsIWindowsRegistry implementation. It only implements a
26      * subset of the interface used in tests.  In particular, only values
27      * of type string are supported.
28      */
29     function MockWindowsRegKey() {}
30     MockWindowsRegKey.prototype = {
31       values: null,
33       // --- Overridden nsISupports interface functions ---
34       QueryInterface: ChromeUtils.generateQI(["nsIWindowsRegKey"]),
36       // --- Overridden nsIWindowsRegKey interface functions ---
37       open(root, path) {
38         let rootKey = registry.getRoot(root);
39         if (!rootKey.has(path)) {
40           rootKey.set(path, new Map());
41         }
42         this.values = rootKey.get(path);
43       },
45       close() {
46         this.values = null;
47       },
49       get valueCount() {
50         if (!this.values) {
51           throw Components.Exception("", Cr.NS_ERROR_FAILURE);
52         }
53         return this.values.size;
54       },
56       hasValue(name) {
57         if (!this.values) {
58           return false;
59         }
60         return this.values.has(name);
61       },
63       getValueType() {
64         return Ci.nsIWindowsRegKey.TYPE_STRING;
65       },
67       getValueName(index) {
68         if (!this.values || index >= this.values.size) {
69           throw Components.Exception("", Cr.NS_ERROR_FAILURE);
70         }
71         let names = Array.from(this.values.keys());
72         return names[index];
73       },
75       readStringValue(name) {
76         if (!this.values) {
77           throw new Error("invalid registry path");
78         }
79         return this.values.get(name);
80       },
81     };
83     // See bug 1688838 - nsNotifyAddrListener::CheckAdaptersAddresses might
84     // attempt to use the registry off the main thread, so we disable that
85     // feature while the mock registry is active.
86     this.oldSuffixListPref = Services.prefs.getBoolPref(
87       "network.notify.dnsSuffixList"
88     );
89     Services.prefs.setBoolPref("network.notify.dnsSuffixList", false);
91     this.oldCheckForProxiesPref = Services.prefs.getBoolPref(
92       "network.notify.checkForProxies"
93     );
94     Services.prefs.setBoolPref("network.notify.checkForProxies", false);
96     this.oldCheckForNRPTPref = Services.prefs.getBoolPref(
97       "network.notify.checkForNRPT"
98     );
99     Services.prefs.setBoolPref("network.notify.checkForNRPT", false);
101     this.cid = MockRegistrar.register(
102       "@mozilla.org/windows-registry-key;1",
103       MockWindowsRegKey
104     );
105   }
107   shutdown() {
108     MockRegistrar.unregister(this.cid);
109     Services.prefs.setBoolPref(
110       "network.notify.dnsSuffixList",
111       this.oldSuffixListPref
112     );
113     Services.prefs.setBoolPref(
114       "network.notify.checkForProxies",
115       this.oldCheckForProxiesPref
116     );
117     Services.prefs.setBoolPref(
118       "network.notify.checkForNRPT",
119       this.oldCheckForNRPTPref
120     );
121     this.cid = null;
122   }
124   getRoot(root) {
125     if (!this.roots.has(root)) {
126       throw new Error(`No such root ${root}`);
127     }
128     return this.roots.get(root);
129   }
131   setValue(root, path, name, value) {
132     let rootKey = this.getRoot(root);
134     if (!rootKey.has(path)) {
135       rootKey.set(path, new Map());
136     }
138     let pathmap = rootKey.get(path);
139     if (value == null) {
140       pathmap.delete(name);
141     } else {
142       pathmap.set(name, value);
143     }
144   }