summaryrefslogtreecommitdiffstats
path: root/remote/marionette/test/xpcshell/test_element.js
blob: 7b2379b1abf0fc2154db68913ecb621d85dba00d (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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const { element, ShadowRoot, WebElement, WebFrame, WebReference, WebWindow } =
  ChromeUtils.importESModule(
    "chrome://remote/content/marionette/element.sys.mjs"
  );
const { NodeCache } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/webdriver/NodeCache.sys.mjs"
);

const MemoryReporter = Cc["@mozilla.org/memory-reporter-manager;1"].getService(
  Ci.nsIMemoryReporterManager
);

class MockElement {
  constructor(tagName, attrs = {}) {
    this.tagName = tagName;
    this.localName = tagName;

    this.isConnected = false;
    this.ownerGlobal = {
      document: {
        isActive() {
          return true;
        },
      },
    };

    for (let attr in attrs) {
      this[attr] = attrs[attr];
    }
  }

  get nodeType() {
    return 1;
  }

  get ELEMENT_NODE() {
    return 1;
  }

  // this is a severely limited CSS selector
  // that only supports lists of tag names
  matches(selector) {
    let tags = selector.split(",");
    return tags.includes(this.localName);
  }
}

class MockXULElement extends MockElement {
  constructor(tagName, attrs = {}) {
    super(tagName, attrs);
    this.namespaceURI = XUL_NS;

    if (typeof this.ownerDocument == "undefined") {
      this.ownerDocument = {};
    }
    if (typeof this.ownerDocument.documentElement == "undefined") {
      this.ownerDocument.documentElement = { namespaceURI: XUL_NS };
    }
  }
}

const xulEl = new MockXULElement("text");

const domElInPrivilegedDocument = new MockElement("input", {
  nodePrincipal: { isSystemPrincipal: true },
});
const xulElInPrivilegedDocument = new MockXULElement("text", {
  nodePrincipal: { isSystemPrincipal: true },
});

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

  browser.document.body.innerHTML = `
    <div id="foo" style="margin: 50px">
      <iframe></iframe>
      <video></video>
      <svg xmlns="http://www.w3.org/2000/svg"></svg>
      <textarea></textarea>
    </div>
  `;

  const divEl = browser.document.querySelector("div");
  const svgEl = browser.document.querySelector("svg");
  const textareaEl = browser.document.querySelector("textarea");
  const videoEl = browser.document.querySelector("video");

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

  const shadowRoot = videoEl.openOrClosedShadowRoot;

  return {
    browser,
    nodeCache: new NodeCache(),
    childEl,
    divEl,
    iframeEl,
    shadowRoot,
    svgEl,
    textareaEl,
    videoEl,
  };
}

add_task(function test_findClosest() {
  const { divEl, videoEl } = setupTest();

  equal(element.findClosest(divEl, "foo"), null);
  equal(element.findClosest(videoEl, "div"), divEl);
});

add_task(function test_isSelected() {
  const { browser, divEl } = setupTest();

  const checkbox = browser.document.createElement("input");
  checkbox.setAttribute("type", "checkbox");

  ok(!element.isSelected(checkbox));
  checkbox.checked = true;
  ok(element.isSelected(checkbox));

  // selected is not a property of <input type=checkbox>
  checkbox.selected = true;
  checkbox.checked = false;
  ok(!element.isSelected(checkbox));

  const option = browser.document.createElement("option");

  ok(!element.isSelected(option));
  option.selected = true;
  ok(element.isSelected(option));

  // checked is not a property of <option>
  option.checked = true;
  option.selected = false;
  ok(!element.isSelected(option));

  // anything else should not be selected
  for (const type of [undefined, null, "foo", true, [], {}, divEl]) {
    ok(!element.isSelected(type));
  }
});

add_task(function test_isElement() {
  const { divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(element.isElement(divEl));
  ok(element.isElement(svgEl));
  ok(element.isElement(xulEl));
  ok(element.isElement(domElInPrivilegedDocument));
  ok(element.isElement(xulElInPrivilegedDocument));

  ok(!element.isElement(shadowRoot));
  ok(!element.isElement(divEl.ownerGlobal));
  ok(!element.isElement(iframeEl.contentWindow));

  for (const type of [true, 42, {}, [], undefined, null]) {
    ok(!element.isElement(type));
  }
});

add_task(function test_isDOMElement() {
  const { divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(element.isDOMElement(divEl));
  ok(element.isDOMElement(svgEl));
  ok(element.isDOMElement(domElInPrivilegedDocument));

  ok(!element.isDOMElement(shadowRoot));
  ok(!element.isDOMElement(divEl.ownerGlobal));
  ok(!element.isDOMElement(iframeEl.contentWindow));
  ok(!element.isDOMElement(xulEl));
  ok(!element.isDOMElement(xulElInPrivilegedDocument));

  for (const type of [true, 42, "foo", {}, [], undefined, null]) {
    ok(!element.isDOMElement(type));
  }
});

add_task(function test_isXULElement() {
  const { divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(element.isXULElement(xulEl));
  ok(element.isXULElement(xulElInPrivilegedDocument));

  ok(!element.isXULElement(divEl));
  ok(!element.isXULElement(domElInPrivilegedDocument));
  ok(!element.isXULElement(svgEl));
  ok(!element.isXULElement(shadowRoot));
  ok(!element.isXULElement(divEl.ownerGlobal));
  ok(!element.isXULElement(iframeEl.contentWindow));

  for (const type of [true, 42, "foo", {}, [], undefined, null]) {
    ok(!element.isXULElement(type));
  }
});

add_task(function test_isDOMWindow() {
  const { divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(element.isDOMWindow(divEl.ownerGlobal));
  ok(element.isDOMWindow(iframeEl.contentWindow));

  ok(!element.isDOMWindow(divEl));
  ok(!element.isDOMWindow(svgEl));
  ok(!element.isDOMWindow(shadowRoot));
  ok(!element.isDOMWindow(domElInPrivilegedDocument));
  ok(!element.isDOMWindow(xulEl));
  ok(!element.isDOMWindow(xulElInPrivilegedDocument));

  for (const type of [true, 42, {}, [], undefined, null]) {
    ok(!element.isDOMWindow(type));
  }
});

add_task(function test_isShadowRoot() {
  const { browser, divEl, iframeEl, shadowRoot, svgEl } = setupTest();

  ok(element.isShadowRoot(shadowRoot));

  ok(!element.isShadowRoot(divEl));
  ok(!element.isShadowRoot(svgEl));
  ok(!element.isShadowRoot(divEl.ownerGlobal));
  ok(!element.isShadowRoot(iframeEl.contentWindow));
  ok(!element.isShadowRoot(xulEl));
  ok(!element.isShadowRoot(domElInPrivilegedDocument));
  ok(!element.isShadowRoot(xulElInPrivilegedDocument));

  for (const type of [true, 42, "foo", {}, [], undefined, null]) {
    ok(!element.isShadowRoot(type));
  }

  const documentFragment = browser.document.createDocumentFragment();
  ok(!element.isShadowRoot(documentFragment));
});

add_task(function test_isReadOnly() {
  const { browser, divEl, textareaEl } = setupTest();

  const input = browser.document.createElement("input");
  input.readOnly = true;
  ok(element.isReadOnly(input));

  textareaEl.readOnly = true;
  ok(element.isReadOnly(textareaEl));

  ok(!element.isReadOnly(divEl));
  divEl.readOnly = true;
  ok(!element.isReadOnly(divEl));

  ok(!element.isReadOnly(null));
});

add_task(function test_isDisabled() {
  const { browser, divEl, svgEl } = setupTest();

  const select = browser.document.createElement("select");
  const option = browser.document.createElement("option");
  select.appendChild(option);
  select.disabled = true;
  ok(element.isDisabled(option));

  const optgroup = browser.document.createElement("optgroup");
  option.parentNode = optgroup;
  ok(element.isDisabled(option));

  optgroup.parentNode = select;
  ok(element.isDisabled(option));

  select.disabled = false;
  ok(!element.isDisabled(option));

  for (const type of ["button", "input", "select", "textarea"]) {
    const elem = browser.document.createElement(type);
    ok(!element.isDisabled(elem));
    elem.disabled = true;
    ok(element.isDisabled(elem));
  }

  ok(!element.isDisabled(divEl));

  svgEl.disabled = true;
  ok(!element.isDisabled(svgEl));

  ok(!element.isDisabled(new MockXULElement("browser", { disabled: true })));
});

add_task(function test_isEditingHost() {
  const { browser, divEl, svgEl } = setupTest();

  ok(!element.isEditingHost(null));

  ok(!element.isEditingHost(divEl));
  divEl.contentEditable = true;
  ok(element.isEditingHost(divEl));

  ok(!element.isEditingHost(svgEl));
  browser.document.designMode = "on";
  ok(element.isEditingHost(svgEl));
});

add_task(function test_isEditable() {
  const { browser, divEl, svgEl, textareaEl } = setupTest();

  ok(!element.isEditable(null));

  for (let type of [
    "checkbox",
    "radio",
    "hidden",
    "submit",
    "button",
    "image",
  ]) {
    const input = browser.document.createElement("input");
    input.setAttribute("type", type);

    ok(!element.isEditable(input));
  }

  const input = browser.document.createElement("input");
  ok(element.isEditable(input));
  input.setAttribute("type", "text");
  ok(element.isEditable(input));

  ok(element.isEditable(textareaEl));

  const textareaDisabled = browser.document.createElement("textarea");
  textareaDisabled.disabled = true;
  ok(!element.isEditable(textareaDisabled));

  const textareaReadOnly = browser.document.createElement("textarea");
  textareaReadOnly.readOnly = true;
  ok(!element.isEditable(textareaReadOnly));

  ok(!element.isEditable(divEl));
  divEl.contentEditable = true;
  ok(element.isEditable(divEl));

  ok(!element.isEditable(svgEl));
  browser.document.designMode = "on";
  ok(element.isEditable(svgEl));
});

add_task(function test_isMutableFormControlElement() {
  const { browser, divEl, textareaEl } = setupTest();

  ok(!element.isMutableFormControl(null));

  ok(element.isMutableFormControl(textareaEl));

  const textareaDisabled = browser.document.createElement("textarea");
  textareaDisabled.disabled = true;
  ok(!element.isMutableFormControl(textareaDisabled));

  const textareaReadOnly = browser.document.createElement("textarea");
  textareaReadOnly.readOnly = true;
  ok(!element.isMutableFormControl(textareaReadOnly));

  const mutableStates = new Set([
    "color",
    "date",
    "datetime-local",
    "email",
    "file",
    "month",
    "number",
    "password",
    "range",
    "search",
    "tel",
    "text",
    "url",
    "week",
  ]);
  for (const type of mutableStates) {
    const input = browser.document.createElement("input");
    input.setAttribute("type", type);
    ok(element.isMutableFormControl(input));
  }

  const inputHidden = browser.document.createElement("input");
  inputHidden.setAttribute("type", "hidden");
  ok(!element.isMutableFormControl(inputHidden));

  ok(!element.isMutableFormControl(divEl));
  divEl.contentEditable = true;
  ok(!element.isMutableFormControl(divEl));
  browser.document.designMode = "on";
  ok(!element.isMutableFormControl(divEl));
});

add_task(function test_coordinates() {
  const { divEl } = setupTest();

  let coords = element.coordinates(divEl);
  ok(coords.hasOwnProperty("x"));
  ok(coords.hasOwnProperty("y"));
  equal(typeof coords.x, "number");
  equal(typeof coords.y, "number");

  deepEqual(element.coordinates(divEl), { x: 0, y: 0 });
  deepEqual(element.coordinates(divEl, 10, 10), { x: 10, y: 10 });
  deepEqual(element.coordinates(divEl, -5, -5), { x: -5, y: -5 });

  Assert.throws(() => element.coordinates(null), /node is null/);

  Assert.throws(
    () => element.coordinates(divEl, "string", undefined),
    /Offset must be a number/
  );
  Assert.throws(
    () => element.coordinates(divEl, undefined, "string"),
    /Offset must be a number/
  );
  Assert.throws(
    () => element.coordinates(divEl, "string", "string"),
    /Offset must be a number/
  );
  Assert.throws(
    () => element.coordinates(divEl, {}, undefined),
    /Offset must be a number/
  );
  Assert.throws(
    () => element.coordinates(divEl, undefined, {}),
    /Offset must be a number/
  );
  Assert.throws(
    () => element.coordinates(divEl, {}, {}),
    /Offset must be a number/
  );
  Assert.throws(
    () => element.coordinates(divEl, [], undefined),
    /Offset must be a number/
  );
  Assert.throws(
    () => element.coordinates(divEl, undefined, []),
    /Offset must be a number/
  );
  Assert.throws(
    () => element.coordinates(divEl, [], []),
    /Offset must be a number/
  );
});

add_task(function test_isNodeReferenceKnown() {
  const { browser, nodeCache, childEl, iframeEl, videoEl } = setupTest();

  // Unknown node reference
  ok(!element.isNodeReferenceKnown(browser.browsingContext, "foo", nodeCache));

  // Known node reference
  const videoElRef = nodeCache.getOrCreateNodeReference(videoEl);
  ok(
    element.isNodeReferenceKnown(browser.browsingContext, videoElRef, nodeCache)
  );

  // Different top-level browsing context
  const browser2 = Services.appShell.createWindowlessBrowser(false);
  ok(
    !element.isNodeReferenceKnown(
      browser2.browsingContext,
      videoElRef,
      nodeCache
    )
  );

  // Different child browsing context
  const childElRef = nodeCache.getOrCreateNodeReference(childEl);
  const childBrowsingContext = iframeEl.contentWindow.browsingContext;
  ok(element.isNodeReferenceKnown(childBrowsingContext, childElRef, nodeCache));

  const iframeEl2 = browser2.document.createElement("iframe");
  browser2.document.body.appendChild(iframeEl2);
  const childBrowsingContext2 = iframeEl2.contentWindow.browsingContext;
  ok(
    !element.isNodeReferenceKnown(childBrowsingContext2, childElRef, nodeCache)
  );
});

add_task(function test_getKnownElement() {
  const { browser, nodeCache, shadowRoot, videoEl } = setupTest();

  // Unknown element reference
  Assert.throws(() => {
    element.getKnownElement(browser.browsingContext, "foo", nodeCache);
  }, /NoSuchElementError/);

  // With a ShadowRoot reference
  const shadowRootRef = nodeCache.getOrCreateNodeReference(shadowRoot);
  Assert.throws(() => {
    element.getKnownElement(browser.browsingContext, shadowRootRef, nodeCache);
  }, /NoSuchElementError/);

  // Deleted element (eg. garbage collected)
  let detachedEl = browser.document.createElement("div");
  const detachedElRef = nodeCache.getOrCreateNodeReference(detachedEl);

  // ... not connected to the DOM
  Assert.throws(() => {
    element.getKnownElement(browser.browsingContext, detachedElRef, nodeCache);
  }, /StaleElementReferenceError/);

  // ... element garbage collected
  detachedEl = null;
  MemoryReporter.minimizeMemoryUsage(() => {
    Assert.throws(() => {
      element.getKnownElement(
        browser.browsingContext,
        detachedElRef,
        nodeCache
      );
    }, /StaleElementReferenceError/);
  });

  // Known element reference
  const videoElRef = nodeCache.getOrCreateNodeReference(videoEl);
  equal(
    element.getKnownElement(browser.browsingContext, videoElRef, nodeCache),
    videoEl
  );
});

add_task(function test_getKnownShadowRoot() {
  const { browser, nodeCache, shadowRoot, videoEl } = setupTest();

  const videoElRef = nodeCache.getOrCreateNodeReference(videoEl);

  // Unknown ShadowRoot reference
  Assert.throws(() => {
    element.getKnownShadowRoot(browser.browsingContext, "foo", nodeCache);
  }, /NoSuchShadowRootError/);

  // With a HTMLElement reference
  Assert.throws(() => {
    element.getKnownShadowRoot(browser.browsingContext, videoElRef, nodeCache);
  }, /NoSuchShadowRootError/);

  // Known ShadowRoot reference
  const shadowRootRef = nodeCache.getOrCreateNodeReference(shadowRoot);
  equal(
    element.getKnownShadowRoot(
      browser.browsingContext,
      shadowRootRef,
      nodeCache
    ),
    shadowRoot
  );

  // Detached ShadowRoot host
  let el = browser.document.createElement("div");
  let detachedShadowRoot = el.attachShadow({ mode: "open" });
  detachedShadowRoot.innerHTML = "<input></input>";

  const detachedShadowRootRef =
    nodeCache.getOrCreateNodeReference(detachedShadowRoot);

  // ... not connected to the DOM
  Assert.throws(() => {
    element.getKnownShadowRoot(
      browser.browsingContext,
      detachedShadowRootRef,
      nodeCache
    );
  }, /DetachedShadowRootError/);

  // ... host and shadow root garbage collected
  el = null;
  detachedShadowRoot = null;
  MemoryReporter.minimizeMemoryUsage(() => {
    Assert.throws(() => {
      element.getKnownShadowRoot(
        browser.browsingContext,
        detachedShadowRootRef,
        nodeCache
      );
    }, /DetachedShadowRootError/);
  });
});

add_task(function test_isDetached() {
  const { childEl, iframeEl } = setupTest();

  let detachedShadowRoot = childEl.attachShadow({ mode: "open" });
  detachedShadowRoot.innerHTML = "<input></input>";

  // Connected to the DOM
  ok(!element.isDetached(detachedShadowRoot));

  // Node document (ownerDocument) is not the active document
  iframeEl.remove();
  ok(element.isDetached(detachedShadowRoot));

  // host element is stale (eg. not connected)
  detachedShadowRoot.host.remove();
  equal(childEl.isConnected, false);
  ok(element.isDetached(detachedShadowRoot));
});

add_task(function test_isStale() {
  const { childEl, iframeEl } = setupTest();

  // Connected to the DOM
  ok(!element.isStale(childEl));

  // Not part of the active document
  iframeEl.remove();
  ok(element.isStale(childEl));

  // Not connected to the DOM
  childEl.remove();
  ok(element.isStale(childEl));
});

add_task(function test_WebReference_ctor() {
  const el = new WebReference("foo");
  equal(el.uuid, "foo");

  for (let t of [42, true, [], {}, null, undefined]) {
    Assert.throws(() => new WebReference(t), /to be a string/);
  }
});

add_task(function test_WebElemenet_is() {
  const a = new WebReference("a");
  const b = new WebReference("b");

  ok(a.is(a));
  ok(b.is(b));
  ok(!a.is(b));
  ok(!b.is(a));

  ok(!a.is({}));
});

add_task(function test_WebReference_from() {
  const { divEl, iframeEl } = setupTest();

  ok(WebReference.from(divEl) instanceof WebElement);
  ok(WebReference.from(xulEl) instanceof WebElement);
  ok(WebReference.from(divEl.ownerGlobal) instanceof WebWindow);
  ok(WebReference.from(iframeEl.contentWindow) instanceof WebFrame);
  ok(WebReference.from(domElInPrivilegedDocument) instanceof WebElement);
  ok(WebReference.from(xulElInPrivilegedDocument) instanceof WebElement);

  Assert.throws(() => WebReference.from({}), /InvalidArgumentError/);
});

add_task(function test_WebReference_fromJSON_WebElement() {
  const { Identifier } = WebElement;

  const ref = { [Identifier]: "foo" };
  const webEl = WebReference.fromJSON(ref);
  ok(webEl instanceof WebElement);
  equal(webEl.uuid, "foo");

  let identifierPrecedence = {
    [Identifier]: "identifier-uuid",
  };
  const precedenceEl = WebReference.fromJSON(identifierPrecedence);
  ok(precedenceEl instanceof WebElement);
  equal(precedenceEl.uuid, "identifier-uuid");
});

add_task(function test_WebReference_fromJSON_WebWindow() {
  const ref = { [WebWindow.Identifier]: "foo" };
  const win = WebReference.fromJSON(ref);

  ok(win instanceof WebWindow);
  equal(win.uuid, "foo");
});

add_task(function test_WebReference_fromJSON_WebFrame() {
  const ref = { [WebFrame.Identifier]: "foo" };
  const frame = WebReference.fromJSON(ref);
  ok(frame instanceof WebFrame);
  equal(frame.uuid, "foo");
});

add_task(function test_WebReference_fromJSON_malformed() {
  Assert.throws(() => WebReference.fromJSON({}), /InvalidArgumentError/);
  Assert.throws(() => WebReference.fromJSON(null), /InvalidArgumentError/);
});

add_task(function test_WebReference_isReference() {
  for (let t of [42, true, "foo", [], {}]) {
    ok(!WebReference.isReference(t));
  }

  ok(WebReference.isReference({ [WebElement.Identifier]: "foo" }));
  ok(WebReference.isReference({ [WebWindow.Identifier]: "foo" }));
  ok(WebReference.isReference({ [WebFrame.Identifier]: "foo" }));
});

add_task(function test_WebElement_toJSON() {
  const { Identifier } = WebElement;

  const el = new WebElement("foo");
  const json = el.toJSON();

  ok(Identifier in json);
  equal(json[Identifier], "foo");
});

add_task(function test_WebElement_fromJSON() {
  const { Identifier } = WebElement;

  const el = WebElement.fromJSON({ [Identifier]: "foo" });
  ok(el instanceof WebElement);
  equal(el.uuid, "foo");

  Assert.throws(() => WebElement.fromJSON({}), /InvalidArgumentError/);
});

add_task(function test_WebElement_fromUUID() {
  const domWebEl = WebElement.fromUUID("bar");

  ok(domWebEl instanceof WebElement);
  equal(domWebEl.uuid, "bar");

  Assert.throws(() => WebElement.fromUUID(), /InvalidArgumentError/);
});

add_task(function test_ShadowRoot_toJSON() {
  const { Identifier } = ShadowRoot;

  const shadowRoot = new ShadowRoot("foo");
  const json = shadowRoot.toJSON();

  ok(Identifier in json);
  equal(json[Identifier], "foo");
});

add_task(function test_ShadowRoot_fromJSON() {
  const { Identifier } = ShadowRoot;

  const shadowRoot = ShadowRoot.fromJSON({ [Identifier]: "foo" });
  ok(shadowRoot instanceof ShadowRoot);
  equal(shadowRoot.uuid, "foo");

  Assert.throws(() => ShadowRoot.fromJSON({}), /InvalidArgumentError/);
});

add_task(function test_ShadowRoot_fromUUID() {
  const shadowRoot = ShadowRoot.fromUUID("baz");

  ok(shadowRoot instanceof ShadowRoot);
  equal(shadowRoot.uuid, "baz");

  Assert.throws(() => ShadowRoot.fromUUID(), /InvalidArgumentError/);
});

add_task(function test_WebWindow_toJSON() {
  const win = new WebWindow("foo");
  const json = win.toJSON();

  ok(WebWindow.Identifier in json);
  equal(json[WebWindow.Identifier], "foo");
});

add_task(function test_WebWindow_fromJSON() {
  const ref = { [WebWindow.Identifier]: "foo" };
  const win = WebWindow.fromJSON(ref);

  ok(win instanceof WebWindow);
  equal(win.uuid, "foo");
});

add_task(function test_WebFrame_toJSON() {
  const frame = new WebFrame("foo");
  const json = frame.toJSON();

  ok(WebFrame.Identifier in json);
  equal(json[WebFrame.Identifier], "foo");
});

add_task(function test_WebFrame_fromJSON() {
  const ref = { [WebFrame.Identifier]: "foo" };
  const win = WebFrame.fromJSON(ref);

  ok(win instanceof WebFrame);
  equal(win.uuid, "foo");
});