summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/tests/xpcshell/test_Integration.js
blob: 8213e32592f4f43f5b34fc45264bf37ef3452c77 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/*
 * Tests the Integration.sys.mjs module.
 */

"use strict";
const { Integration } = ChromeUtils.importESModule(
  "resource://gre/modules/Integration.sys.mjs"
);

const TestIntegration = {
  value: "value",

  get valueFromThis() {
    return this.value;
  },

  get property() {
    return this._property;
  },

  set property(value) {
    this._property = value;
  },

  method(argument) {
    this.methodArgument = argument;
    return "method" + argument;
  },

  async asyncMethod(argument) {
    this.asyncMethodArgument = argument;
    return "asyncMethod" + argument;
  },
};

let overrideFn = base => ({
  value: "overridden-value",

  get property() {
    return "overridden-" + base.__lookupGetter__("property").call(this);
  },

  set property(value) {
    base.__lookupSetter__("property").call(this, "overridden-" + value);
  },

  method() {
    return "overridden-" + base.method.apply(this, arguments);
  },

  async asyncMethod() {
    return "overridden-" + (await base.asyncMethod.apply(this, arguments));
  },
});

let superOverrideFn = base => {
  let nextLevel = {
    value: "overridden-value",

    get property() {
      return "overridden-" + super.property;
    },

    set property(value) {
      super.property = "overridden-" + value;
    },

    method() {
      return "overridden-" + super.method(...arguments);
    },

    async asyncMethod() {
      // We cannot use the "super" keyword in methods defined using "Task.async".
      return "overridden-" + (await base.asyncMethod.apply(this, arguments));
    },
  };
  Object.setPrototypeOf(nextLevel, base);
  return nextLevel;
};

/**
 * Fails the test if the results of method invocations on the combined object
 * don't match the expected results based on how many overrides are registered.
 *
 * @param combined
 *        The combined object based on the TestIntegration root.
 * @param overridesCount
 *        Zero if the root object is not overridden, or a higher value to test
 *        the presence of one or more integration overrides.
 */
async function assertCombinedResults(combined, overridesCount) {
  let expectedValue = overridesCount > 0 ? "overridden-value" : "value";
  let prefix = "overridden-".repeat(overridesCount);

  Assert.equal(combined.value, expectedValue);
  Assert.equal(combined.valueFromThis, expectedValue);

  combined.property = "property";
  Assert.equal(combined.property, prefix.repeat(2) + "property");

  combined.methodArgument = "";
  Assert.equal(combined.method("-argument"), prefix + "method-argument");
  Assert.equal(combined.methodArgument, "-argument");

  combined.asyncMethodArgument = "";
  Assert.equal(
    await combined.asyncMethod("-argument"),
    prefix + "asyncMethod-argument"
  );
  Assert.equal(combined.asyncMethodArgument, "-argument");
}

/**
 * Fails the test if the results of method invocations on the combined object
 * for the "testModule" integration point don't match the expected results based
 * on how many overrides are registered.
 *
 * @param overridesCount
 *        Zero if the root object is not overridden, or a higher value to test
 *        the presence of one or more integration overrides.
 */
async function assertCurrentCombinedResults(overridesCount) {
  let combined = Integration.testModule.getCombined(TestIntegration);
  await assertCombinedResults(combined, overridesCount);
}

/**
 * Checks the initial state with no integration override functions registered.
 */
add_task(async function test_base() {
  await assertCurrentCombinedResults(0);
});

/**
 * Registers and unregisters an integration override function.
 */
add_task(async function test_override() {
  Integration.testModule.register(overrideFn);
  await assertCurrentCombinedResults(1);

  // Registering the same function more than once has no effect.
  Integration.testModule.register(overrideFn);
  await assertCurrentCombinedResults(1);

  Integration.testModule.unregister(overrideFn);
  await assertCurrentCombinedResults(0);
});

/**
 * Registers and unregisters more than one integration override function, of
 * which one uses the prototype and the "super" keyword to access the base.
 */
add_task(async function test_override_super_multiple() {
  Integration.testModule.register(overrideFn);
  Integration.testModule.register(superOverrideFn);
  await assertCurrentCombinedResults(2);

  Integration.testModule.unregister(overrideFn);
  await assertCurrentCombinedResults(1);

  Integration.testModule.unregister(superOverrideFn);
  await assertCurrentCombinedResults(0);
});

/**
 * Registers an integration override function that throws an exception, and
 * ensures that this does not block other functions from being registered.
 */
add_task(async function test_override_error() {
  let errorOverrideFn = base => {
    throw new Error("Expected error.");
  };

  Integration.testModule.register(errorOverrideFn);
  Integration.testModule.register(overrideFn);
  await assertCurrentCombinedResults(1);

  Integration.testModule.unregister(errorOverrideFn);
  Integration.testModule.unregister(overrideFn);
  await assertCurrentCombinedResults(0);
});

/**
 * Checks that state saved using the "this" reference is preserved as a shallow
 * copy when registering new integration override functions.
 */
add_task(async function test_state_preserved() {
  let valueObject = { toString: () => "toString" };

  let combined = Integration.testModule.getCombined(TestIntegration);
  combined.property = valueObject;
  Assert.ok(combined.property === valueObject);

  Integration.testModule.register(overrideFn);
  combined = Integration.testModule.getCombined(TestIntegration);
  Assert.equal(combined.property, "overridden-toString");

  Integration.testModule.unregister(overrideFn);
  combined = Integration.testModule.getCombined(TestIntegration);
  Assert.ok(combined.property === valueObject);
});

/**
 * Checks that the combined integration objects cannot be used with XPCOM.
 *
 * This is limited by the fact that interfaces with the "[function]" annotation,
 * for example nsIObserver, do not call the QueryInterface implementation.
 */
add_task(async function test_xpcom_throws() {
  let combined = Integration.testModule.getCombined(TestIntegration);

  // This calls QueryInterface because it looks for nsISupportsWeakReference.
  Assert.throws(
    () => Services.obs.addObserver(combined, "test-topic", true),
    /NS_NOINTERFACE/
  );
});

/**
 * Checks that getters defined by defineESModuleGetter are able to retrieve the
 * latest version of the combined integration object.
 */
add_task(async function test_defineESModuleGetter() {
  let objectForGetters = {};

  Integration.testModule.defineESModuleGetter(
    objectForGetters,
    "TestIntegration",
    "resource://testing-common/TestIntegration.sys.mjs"
  );

  Integration.testModule.register(overrideFn);
  await assertCombinedResults(objectForGetters.TestIntegration, 1);

  Integration.testModule.unregister(overrideFn);
  await assertCombinedResults(objectForGetters.TestIntegration, 0);
});