summaryrefslogtreecommitdiffstats
path: root/remote/marionette/test/xpcshell/test_json.js
blob: 479e1fe473d277df7932a9ff19036ff5d813753e (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
const { json } = ChromeUtils.importESModule(
  "chrome://remote/content/marionette/json.sys.mjs"
);
const { NodeCache } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/webdriver/NodeCache.sys.mjs"
);
const { ShadowRoot, WebElement, WebReference } = ChromeUtils.importESModule(
  "chrome://remote/content/marionette/element.sys.mjs"
);

function setupTest() {
  const browser = Services.appShell.createWindowlessBrowser(false);
  const nodeCache = new NodeCache();

  const htmlEl = browser.document.createElement("video");
  browser.document.body.appendChild(htmlEl);

  const svgEl = browser.document.createElementNS(
    "http://www.w3.org/2000/svg",
    "rect"
  );
  browser.document.body.appendChild(svgEl);

  const shadowRoot = htmlEl.openOrClosedShadowRoot;

  const iframeEl = browser.document.createElement("iframe");
  browser.document.body.appendChild(iframeEl);
  const childEl = iframeEl.contentDocument.createElement("div");

  return { browser, nodeCache, childEl, iframeEl, htmlEl, shadowRoot, svgEl };
}

add_task(function test_clone_generalTypes() {
  const { nodeCache } = setupTest();

  // null
  equal(json.clone(undefined, nodeCache), null);
  equal(json.clone(null, nodeCache), null);

  // primitives
  equal(json.clone(true, nodeCache), true);
  equal(json.clone(42, nodeCache), 42);
  equal(json.clone("foo", nodeCache), "foo");

  // toJSON
  equal(
    json.clone({
      toJSON() {
        return "foo";
      },
    }),
    "foo"
  );
});

add_task(function test_clone_ShadowRoot() {
  const { nodeCache, shadowRoot } = setupTest();

  const shadowRootRef = nodeCache.getOrCreateNodeReference(shadowRoot);
  deepEqual(
    json.clone(shadowRoot, nodeCache),
    WebReference.from(shadowRoot, shadowRootRef).toJSON()
  );
});

add_task(function test_clone_WebElement() {
  const { htmlEl, nodeCache, svgEl } = setupTest();

  const htmlElRef = nodeCache.getOrCreateNodeReference(htmlEl);
  deepEqual(
    json.clone(htmlEl, nodeCache),
    WebReference.from(htmlEl, htmlElRef).toJSON()
  );

  // Check an element with a different namespace
  const svgElRef = nodeCache.getOrCreateNodeReference(svgEl);
  deepEqual(
    json.clone(svgEl, nodeCache),
    WebReference.from(svgEl, svgElRef).toJSON()
  );
});

add_task(function test_clone_Sequences() {
  const { htmlEl, nodeCache } = setupTest();

  const htmlElRef = nodeCache.getOrCreateNodeReference(htmlEl);

  const input = [
    null,
    true,
    [],
    htmlEl,
    {
      toJSON() {
        return "foo";
      },
    },
    { bar: "baz" },
  ];

  const actual = json.clone(input, nodeCache);

  equal(actual[0], null);
  equal(actual[1], true);
  deepEqual(actual[2], []);
  deepEqual(actual[3], { [WebElement.Identifier]: htmlElRef });
  equal(actual[4], "foo");
  deepEqual(actual[5], { bar: "baz" });
});

add_task(function test_clone_objects() {
  const { htmlEl, nodeCache } = setupTest();

  const htmlElRef = nodeCache.getOrCreateNodeReference(htmlEl);

  const input = {
    null: null,
    boolean: true,
    array: [42],
    element: htmlEl,
    toJSON: {
      toJSON() {
        return "foo";
      },
    },
    object: { bar: "baz" },
  };

  const actual = json.clone(input, nodeCache);

  equal(actual.null, null);
  equal(actual.boolean, true);
  deepEqual(actual.array, [42]);
  deepEqual(actual.element, { [WebElement.Identifier]: htmlElRef });
  equal(actual.toJSON, "foo");
  deepEqual(actual.object, { bar: "baz" });
});

add_task(function test_clone_сyclicReference() {
  const { nodeCache } = setupTest();

  // object
  Assert.throws(() => {
    const obj = {};
    obj.reference = obj;
    json.clone(obj, nodeCache);
  }, /JavaScriptError/);

  // array
  Assert.throws(() => {
    const array = [];
    array.push(array);
    json.clone(array, nodeCache);
  }, /JavaScriptError/);

  // array in object
  Assert.throws(() => {
    const array = [];
    array.push(array);
    json.clone({ array }, nodeCache);
  }, /JavaScriptError/);

  // object in array
  Assert.throws(() => {
    const obj = {};
    obj.reference = obj;
    json.clone([obj], nodeCache);
  }, /JavaScriptError/);
});

add_task(function test_deserialize_generalTypes() {
  const { browser, nodeCache } = setupTest();
  const win = browser.document.ownerGlobal;

  // null
  equal(json.deserialize(undefined, nodeCache, win), undefined);
  equal(json.deserialize(null, nodeCache, win), null);

  // primitives
  equal(json.deserialize(true, nodeCache, win), true);
  equal(json.deserialize(42, nodeCache, win), 42);
  equal(json.deserialize("foo", nodeCache, win), "foo");
});

add_task(function test_deserialize_ShadowRoot() {
  const { browser, nodeCache, shadowRoot } = setupTest();
  const win = browser.document.ownerGlobal;

  // Fails to resolve for unknown elements
  const unknownShadowRootId = { [ShadowRoot.Identifier]: "foo" };
  Assert.throws(() => {
    json.deserialize(unknownShadowRootId, nodeCache, win);
  }, /NoSuchShadowRootError/);

  const shadowRootRef = nodeCache.getOrCreateNodeReference(shadowRoot);
  const shadowRootEl = { [ShadowRoot.Identifier]: shadowRootRef };

  // Fails to resolve for missing window reference
  Assert.throws(() => json.deserialize(shadowRootEl, nodeCache), /TypeError/);

  // Previously seen element is associated with original web element reference
  const root = json.deserialize(shadowRootEl, nodeCache, win);
  deepEqual(root, shadowRoot);
  deepEqual(root, nodeCache.getNode(browser.browsingContext, shadowRootRef));
});

add_task(function test_deserialize_WebElement() {
  const { browser, htmlEl, nodeCache } = setupTest();
  const win = browser.document.ownerGlobal;

  // Fails to resolve for unknown elements
  const unknownWebElId = { [WebElement.Identifier]: "foo" };
  Assert.throws(() => {
    json.deserialize(unknownWebElId, nodeCache, win);
  }, /NoSuchElementError/);

  const htmlElRef = nodeCache.getOrCreateNodeReference(htmlEl);
  const htmlWebEl = { [WebElement.Identifier]: htmlElRef };

  // Fails to resolve for missing window reference
  Assert.throws(() => json.deserialize(htmlWebEl, nodeCache), /TypeError/);

  // Previously seen element is associated with original web element reference
  const el = json.deserialize(htmlWebEl, nodeCache, win);
  deepEqual(el, htmlEl);
  deepEqual(el, nodeCache.getNode(browser.browsingContext, htmlElRef));
});

add_task(function test_deserialize_Sequences() {
  const { browser, htmlEl, nodeCache } = setupTest();
  const win = browser.document.ownerGlobal;

  const htmlElRef = nodeCache.getOrCreateNodeReference(htmlEl);

  const input = [
    null,
    true,
    [42],
    { [WebElement.Identifier]: htmlElRef },
    { bar: "baz" },
  ];

  const actual = json.deserialize(input, nodeCache, win);

  equal(actual[0], null);
  equal(actual[1], true);
  deepEqual(actual[2], [42]);
  deepEqual(actual[3], htmlEl);
  deepEqual(actual[4], { bar: "baz" });
});

add_task(function test_deserialize_objects() {
  const { browser, htmlEl, nodeCache } = setupTest();
  const win = browser.document.ownerGlobal;

  const htmlElRef = nodeCache.getOrCreateNodeReference(htmlEl);

  const input = {
    null: null,
    boolean: true,
    array: [42],
    element: { [WebElement.Identifier]: htmlElRef },
    object: { bar: "baz" },
  };

  const actual = json.deserialize(input, nodeCache, win);

  equal(actual.null, null);
  equal(actual.boolean, true);
  deepEqual(actual.array, [42]);
  deepEqual(actual.element, htmlEl);
  deepEqual(actual.object, { bar: "baz" });

  nodeCache.clear({ all: true });
});