summaryrefslogtreecommitdiffstats
path: root/remote/shared/webdriver/test/xpcshell/test_NodeCache.js
blob: 8111cd0bc7c7fc121b0e3f4f2bf72dec6d8c09bc (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
const { NodeCache } = ChromeUtils.importESModule(
  "chrome://remote/content/shared/webdriver/NodeCache.sys.mjs"
);

const nodeCache = new NodeCache();

const SVG_NS = "http://www.w3.org/2000/svg";

const browser = Services.appShell.createWindowlessBrowser(false);

const domEl = browser.document.createElement("div");
browser.document.body.appendChild(domEl);

const svgEl = browser.document.createElementNS(SVG_NS, "rect");
browser.document.body.appendChild(svgEl);

registerCleanupFunction(() => {
  nodeCache.clear({ all: true });
});

add_test(function addElement() {
  const domElRef = nodeCache.add(domEl);
  equal(nodeCache.size, 1);

  const domElRefOther = nodeCache.add(domEl);
  equal(nodeCache.size, 1);
  equal(domElRefOther, domElRef);

  nodeCache.add(svgEl);
  equal(nodeCache.size, 2);

  run_next_test();
});

add_test(function addInvalidElement() {
  Assert.throws(() => nodeCache.add("foo"), /UnknownError/);

  run_next_test();
});

add_test(function clear() {
  nodeCache.add(domEl);
  nodeCache.add(svgEl);
  equal(nodeCache.size, 2);

  // Clear requires explicit arguments.
  Assert.throws(() => nodeCache.clear(), /Error/);

  // Clear references for a different browsing context
  const browser2 = Services.appShell.createWindowlessBrowser(false);
  let imgEl = browser2.document.createElement("img");
  browser2.document.body.appendChild(imgEl);

  nodeCache.add(imgEl);
  nodeCache.clear({ browsingContext: browser.browsingContext });
  equal(nodeCache.size, 1);

  // Clear all references
  nodeCache.add(domEl);
  equal(nodeCache.size, 2);

  nodeCache.clear({ all: true });
  equal(nodeCache.size, 0);

  run_next_test();
});

add_test(function resolveElement() {
  const domElSharedId = nodeCache.add(domEl);
  deepEqual(nodeCache.resolve(domElSharedId), domEl);

  const svgElSharedId = nodeCache.add(svgEl);
  deepEqual(nodeCache.resolve(svgElSharedId), svgEl);
  deepEqual(nodeCache.resolve(domElSharedId), domEl);

  run_next_test();
});

add_test(function resolveUnknownElement() {
  Assert.throws(() => nodeCache.resolve("foo"), /NoSuchElementError/);

  run_next_test();
});

add_test(function resolveElementNotAttachedToDOM() {
  const imgEl = browser.document.createElement("img");

  const imgElSharedId = nodeCache.add(imgEl);
  deepEqual(nodeCache.resolve(imgElSharedId), imgEl);

  run_next_test();
});

add_test(async function resolveElementRemoved() {
  let imgEl = browser.document.createElement("img");
  const imgElSharedId = nodeCache.add(imgEl);

  // Delete element and force a garbage collection
  imgEl = null;

  await doGC();

  const el = nodeCache.resolve(imgElSharedId);
  deepEqual(el, null);

  run_next_test();
});

add_test(function elementReferencesDifferentPerNodeCache() {
  const sharedId = nodeCache.add(domEl);

  const nodeCache2 = new NodeCache();
  const sharedId2 = nodeCache2.add(domEl);

  notEqual(sharedId, sharedId2);
  equal(nodeCache.resolve(sharedId), nodeCache2.resolve(sharedId2));

  Assert.throws(() => nodeCache.resolve(sharedId2), /NoSuchElementError/);

  nodeCache2.clear({ all: true });

  run_next_test();
});