Skip to content

Commit

Permalink
Bug 1609636 - Part 2: Add mochitests for WeakRef in browser. r=smaug,…
Browse files Browse the repository at this point in the history
…jonco

Differential Revision: https://phabricator.services.mozilla.com/D60155

--HG--
extra : moz-landing-system : lando
  • Loading branch information
allstarschh committed Jan 20, 2020
1 parent e1ea5b9 commit 133ce9d
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
4 changes: 4 additions & 0 deletions js/xpconnect/tests/mochitest/mochitest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,7 @@ skip-if = (debug == false)
skip-if = !nightly_build
[test_finalizationGroupInWorker.html]
skip-if = !nightly_build
[test_weakRefs.html]
skip-if = !nightly_build
[test_weakRefs_cross_compartment.html]
skip-if = !nightly_build
42 changes: 42 additions & 0 deletions js/xpconnect/tests/mochitest/test_weakRefs.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!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">
function go() {
SimpleTest.waitForExplicitFinish();

let wr;
{
let obj = {};
wr = new WeakRef(obj);
obj = null;
}

// WeakRef should keep the target in the current task.
isnot(wr.deref(), undefined, "deref() should return its target.");

// Weakref should keep the target until the end of current Job, that
// includes microtask(Promise).
Promise.resolve().then(() => {
isnot(wr.deref(), undefined,
"deref() should return its target in promise");

});

// setTimeout will launch a new job and call ClearKeptObjects().
setTimeout(() => {
// Call gc() forcibly to clear the target of wr.
SpecialPowers.DOMWindowUtils.garbageCollect();

is(wr.deref(), undefined, "deref() should return undefined in the new job.");

SimpleTest.finish();
}, 0);
}
</script>
</head>
<body onload="go()"></body>
</html>
68 changes: 68 additions & 0 deletions js/xpconnect/tests/mochitest/test_weakRefs_cross_compartment.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Test WeakRef works when target is in different compartment in the browser</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript">
function go() {
SimpleTest.waitForExplicitFinish();

let Cu = SpecialPowers.Cu;
let isSameCompartment = Cu.getJSTestingFunctions().isSameCompartment;

// Open a new window, which will be from different compartment.
let win = window.open();
is(isSameCompartment(win, window), false,
"Test for opeing a window from a different compartment.");

let wr1, wr2, wr3;
{
let obj = {};

// WeakRef and target are both from different compartment.
wr1 = new win.WeakRef(new win.Object());

// WeakRef is same compartment, but target isn't.
wr2 = new WeakRef(new win.Object());

// WeakRef is in different compartment, but target is.
wr3 = new win.WeakRef(obj);

obj = null;
}

// WeakRef should keep the target in the current task.
isnot(wr1.deref(), undefined, "wr1.deref() should return its target.");
isnot(wr2.deref(), undefined, "wr2.deref() should return its target.");
isnot(wr3.deref(), undefined, "we3.deref() should return its target.");

// Weakref should keep the target until the end of current Job, that
// includes microtask(Promise).
Promise.resolve().then(() => {
isnot(wr1.deref(), undefined,
"wr1.deref() should return its target in promise");
isnot(wr2.deref(), undefined,
"wr2.deref() should return its target in promise");
isnot(wr3.deref(), undefined,
"wr3.deref() should return its target in promise");
});

// setTimeout will launch a new job and call ClearKeptObjects().
setTimeout(() => {
// Call gc() forcibly to clear the target of wr.
SpecialPowers.DOMWindowUtils.garbageCollect();

is(wr1.deref(), undefined, "wr1.deref() should return undefined in the new job.");
is(wr2.deref(), undefined, "wr2.deref() should return undefined in the new job.");
is(wr3.deref(), undefined, "wr3.deref() should return undefined in the new job.");

win.close();
SimpleTest.finish();
}, 0);
}

</script>
</head>
<body onload="go()"></body>
</html>

0 comments on commit 133ce9d

Please sign in to comment.