summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/import/test/unit/resources/mock_windows_reg_factory.js
blob: 19583d5007dd4fbf2e313f34ac8ccf0bf83fae32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var { MockRegistrar } = ChromeUtils.importESModule(
  "resource://testing-common/MockRegistrar.sys.mjs"
);

var gCid;

function MockWindowsRegKey(registryData) {
  this._registryData = registryData;
}

MockWindowsRegKey.prototype = {
  QueryInterface: ChromeUtils.generateQI(["nsIWindowsRegKey"]),

  open(aRootKey, aRelPath, aMode) {
    if (!this._registryData[aRelPath]) {
      throw Components.Exception("", Cr.NS_ERROR_FAILURE);
    }
    this._keyPath = aRelPath;
  },

  close() {},

  openChild(aRelPath, aMode) {
    if (
      !this._registryData[this._keyPath] ||
      !this._registryData[this._keyPath][aRelPath]
    ) {
      throw Components.Exception("", Cr.NS_ERROR_FAILURE);
    }

    let child = new MockWindowsRegKey({});
    let newKeyPath = this._keyPath + "\\" + aRelPath;
    child._keyPath = newKeyPath;
    child._registryData[newKeyPath] =
      this._registryData[this._keyPath][aRelPath];
    return child;
  },

  get childCount() {
    return Object.keys(this._registryData[this._keyPath]).length;
  },

  getChildName(aIndex) {
    let keys = Object.keys(this._registryData[this._keyPath]);
    let keyAtIndex = keys[aIndex];
    if (!keyAtIndex) {
      throw Components.Exception("", Cr.NS_ERROR_FAILURE);
    }

    return keyAtIndex;
  },

  _readValue(aName) {
    if (
      !this._registryData[this._keyPath] ||
      !this._registryData[this._keyPath][aName]
    ) {
      throw Components.Exception("", Cr.NS_ERROR_FAILURE);
    }

    return this._registryData[this._keyPath][aName];
  },

  readIntValue(aName) {
    return this._readValue(aName);
  },

  readStringValue(aName) {
    return this._readValue(aName);
  },
};

/* exported setup_mock_registry, teardown_mock_registry */
function setup_mock_registry(mockRegistry) {
  gCid = MockRegistrar.register(
    "@mozilla.org/windows-registry-key;1",
    MockWindowsRegKey,
    [mockRegistry]
  );
}

function teardown_mock_registry() {
  MockRegistrar.unregister(gCid);
}