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 {
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()],
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.
29 function MockWindowsRegKey() {}
30 MockWindowsRegKey.prototype = {
33 // --- Overridden nsISupports interface functions ---
34 QueryInterface: ChromeUtils.generateQI(["nsIWindowsRegKey"]),
36 // --- Overridden nsIWindowsRegKey interface functions ---
38 let rootKey = registry.getRoot(root);
39 if (!rootKey.has(path)) {
40 rootKey.set(path, new Map());
42 this.values = rootKey.get(path);
51 throw Components.Exception("", Cr.NS_ERROR_FAILURE);
53 return this.values.size;
60 return this.values.has(name);
64 return Ci.nsIWindowsRegKey.TYPE_STRING;
68 if (!this.values || index >= this.values.size) {
69 throw Components.Exception("", Cr.NS_ERROR_FAILURE);
71 let names = Array.from(this.values.keys());
75 readStringValue(name) {
77 throw new Error("invalid registry path");
79 return this.values.get(name);
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"
89 Services.prefs.setBoolPref("network.notify.dnsSuffixList", false);
91 this.oldCheckForProxiesPref = Services.prefs.getBoolPref(
92 "network.notify.checkForProxies"
94 Services.prefs.setBoolPref("network.notify.checkForProxies", false);
96 this.oldCheckForNRPTPref = Services.prefs.getBoolPref(
97 "network.notify.checkForNRPT"
99 Services.prefs.setBoolPref("network.notify.checkForNRPT", false);
101 this.cid = MockRegistrar.register(
102 "@mozilla.org/windows-registry-key;1",
108 MockRegistrar.unregister(this.cid);
109 Services.prefs.setBoolPref(
110 "network.notify.dnsSuffixList",
111 this.oldSuffixListPref
113 Services.prefs.setBoolPref(
114 "network.notify.checkForProxies",
115 this.oldCheckForProxiesPref
117 Services.prefs.setBoolPref(
118 "network.notify.checkForNRPT",
119 this.oldCheckForNRPTPref
125 if (!this.roots.has(root)) {
126 throw new Error(`No such root ${root}`);
128 return this.roots.get(root);
131 setValue(root, path, name, value) {
132 let rootKey = this.getRoot(root);
134 if (!rootKey.has(path)) {
135 rootKey.set(path, new Map());
138 let pathmap = rootKey.get(path);
140 pathmap.delete(name);
142 pathmap.set(name, value);