5 <title>Check for contextlost/restored events after GPU process restart
</title>
6 <script src=
"/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel=
"stylesheet" href=
"/tests/SimpleTest/test.css">
10 <canvas id=
"c" width=
"512" height=
"512"></canvas>
12 <script type=
"application/javascript">
15 return new Promise((resolve, reject) =
> {
16 window.requestAnimationFrame(resolve);
20 async function restartGPUProcess() {
21 return await SpecialPowers.spawnChrome([], async () =
> {
22 const gfxInfo = Cc[
"@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo);
24 if (gfxInfo.usingGPUProcess) {
25 const { TestUtils } = ChromeUtils.importESModule(
26 "resource://testing-common/TestUtils.sys.mjs"
28 let promise = TestUtils.topicObserved(
"compositor-reinitialized");
30 const remoteCanvas = gfxInfo.usingRemoteCanvas;
31 const acceleratedCanvas = gfxInfo.usingAcceleratedCanvas;
32 ok(true,
"Restarting GPU process, remote canvas " + remoteCanvas +
", accelerated canvas " + acceleratedCanvas);
34 gfxInfo.killGPUProcessForTests();
37 return remoteCanvas || acceleratedCanvas;
40 ok(true,
"Not using GPU process");
45 const canvas = document.getElementById(
"c");
46 const context = canvas.getContext(
"2d");
48 let restoredPromiseResolve;
49 let restoredPromiseReject;
51 const restoredPromise = new Promise((resolve, reject) =
> {
52 restoredPromiseResolve = resolve;
53 restoredPromiseReject = reject;
56 let countLostEvents =
0;
57 let countRestoredEvents =
0;
59 function onContextLost() {
60 ok(context.isContextLost(),
"Canvas context should be lost during contextlost event");
63 let transform = context.getTransform();
64 ok(transform.isIdentity,
"Canvas context should return identity transform while context lost");
70 function onContextRestored() {
71 ok(!context.isContextLost(),
"Canvas context should not be lost during contextrestored event");
72 countRestoredEvents +=
1;
74 restoredPromiseResolve(true);
77 function waitContextRestored() {
78 let timeoutId = window.setTimeout(restoredPromiseReject,
5000);
79 return restoredPromise.then(() =
> {
80 window.clearTimeout(timeoutId);
84 async function start() {
86 canvas.addEventListener(
"contextlost", onContextLost);
87 canvas.addEventListener(
"contextrestored", onContextRestored);
89 ok(!context.isContextLost(),
"Canvas context should not be lost before initial fill");
91 context.fillStyle = 'red';
95 ok(!context.isContextLost(),
"Canvas context should not be lost after initial fill");
97 let transform = context.getTransform();
98 ok(transform.isIdentity,
"Canvas context should default to identity transform");
99 context.setTransform(
2.0,
3.0,
4.0,
5.0,
6.0,
7.0);
100 transform = context.getTransform();
101 ok(!transform.isIdentity,
"Canvas context should have non-identity transform");
103 const restarted = await restartGPUProcess();
104 const expectedEvents = restarted ?
1 :
0;
106 if (expectedEvents) {
107 await waitContextRestored();
111 is(countLostEvents, expectedEvents,
"Should have fired " + expectedEvents +
" contextlost events");
112 is(countRestoredEvents, expectedEvents,
"Should have fired " + expectedEvents +
" contextrestored events");
113 ok(!context.isContextLost(),
"Canvas context should not be lost after restoration");
115 context.fillStyle = 'green';
119 ok(!context.isContextLost(),
"Canvas context should not be lost at completion");
121 ok(false,
"Caught exception " + err);
127 SimpleTest.waitForExplicitFinish();
128 SimpleTest.requestFlakyTimeout(
"Wait for failure condition");