summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/mochitest/test_weakRefs.html
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/xpconnect/tests/mochitest/test_weakRefs.html
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/xpconnect/tests/mochitest/test_weakRefs.html')
-rw-r--r--js/xpconnect/tests/mochitest/test_weakRefs.html80
1 files changed, 80 insertions, 0 deletions
diff --git a/js/xpconnect/tests/mochitest/test_weakRefs.html b/js/xpconnect/tests/mochitest/test_weakRefs.html
new file mode 100644
index 0000000000..331bb9bb69
--- /dev/null
+++ b/js/xpconnect/tests/mochitest/test_weakRefs.html
@@ -0,0 +1,80 @@
+<!DOCTYPE HTML>
+<html>
+ <head>
+ <meta charset="utf-8">
+ <title>Test WeakRef works in the browser</title>
+ <script src="/tests/SimpleTest/SimpleTest.js"></script>
+ <script type="application/javascript">
+ let wr1, wr2, wr3, wr4;
+
+ function go() {
+ SimpleTest.waitForExplicitFinish();
+
+ // 1. WeakRef with JS target.
+ wr1 = new WeakRef({});
+
+ // 2. WeakRef with DOM object target (without preserved wrapper).
+ wr2 = new WeakRef(document.createElement("div"));
+
+ // 3. WeakRef with DOM object target (with preserved wrapper).
+ let object = document.createElement("div");
+ object.someProperty = true;
+ wr3 = new WeakRef(object);
+ object = null
+
+ // 4. WeakRef with reachable DOM object target without preserved wrapper.
+ document.body.appendChild(document.createElement("div"));
+ wr4 = new WeakRef(document.body.lastChild);
+
+ // WeakRef should keep the target in the current task.
+ isnot(wr1.deref(), undefined, "deref() should return its target.");
+ isnot(wr2.deref(), undefined, "deref() should return its target.");
+ isnot(wr3.deref(), undefined, "deref() should return its target.");
+ isnot(wr4.deref(), undefined, "deref() should return its target.");
+
+ // WeakRef should keep the target until the end of current task, which
+ // includes promise microtasks.
+ Promise.resolve().then(() => {
+ isnot(wr1.deref(), undefined, "deref() should return its target.");
+ isnot(wr2.deref(), undefined, "deref() should return its target.");
+ isnot(wr3.deref(), undefined, "deref() should return its target.");
+ isnot(wr4.deref(), undefined, "deref() should return its target.");
+ });
+
+ // setTimeout will call its callback in a new task.
+ setTimeout(task2, 0);
+ }
+
+ function task2() {
+ // Trigger a full GC/CC/GC cycle to collect WeakRef targets.
+ SpecialPowers.DOMWindowUtils.garbageCollect();
+ SpecialPowers.DOMWindowUtils.cycleCollect();
+ SpecialPowers.DOMWindowUtils.garbageCollect();
+
+ is(wr1.deref(), undefined, "deref() should return undefined.");
+ is(wr2.deref(), undefined, "deref() should return undefined.");
+ is(wr3.deref(), undefined, "deref() should return undefined.");
+ isnot(wr4.deref(), undefined, "deref() should return its target.");
+
+ // setTimeout will call its callback in a new task.
+ setTimeout(task3, 0);
+ }
+
+ function task3() {
+ document.body.removeChild(document.body.lastChild);
+
+ SpecialPowers.DOMWindowUtils.garbageCollect();
+ SpecialPowers.DOMWindowUtils.cycleCollect();
+ SpecialPowers.DOMWindowUtils.garbageCollect();
+
+ is(wr1.deref(), undefined, "deref() should return undefined.");
+ is(wr2.deref(), undefined, "deref() should return undefined.");
+ is(wr3.deref(), undefined, "deref() should return undefined.");
+ is(wr4.deref(), undefined, "deref() should return undefined.");
+
+ SimpleTest.finish();
+ }
+ </script>
+ </head>
+ <body onload="go()"></body>
+</html>