1 description("Test the behavior of canvas recovery after a gpu context loss");
4 var lostEventHasFired = false;
7 if (window.internals && window.testRunner) {
8 testRunner.dumpAsText();
9 var canvas = document.createElement('canvas');
10 canvas.addEventListener('contextlost', contextLost);
11 canvas.addEventListener('contextrestored', contextRestored);
12 ctx = canvas.getContext('2d');
13 document.body.appendChild(ctx.canvas);
14 verifyContextLost(false);
15 window.internals.loseSharedGraphicsContext3D();
16 // for the canvas to realize it Graphics context was lost we must try to use the canvas
17 ctx.fillRect(0, 0, 1, 1);
18 if (!ctx.isContextLost()) {
19 debug('<span>Aborting test: Graphics context loss did not destroy canvas contents. This is expected if canvas is not accelerated.</span>');
21 verifyContextLost(true);
22 testRunner.waitUntilDone();
25 testFailed('This test requires window.internals and window.testRunner.');
29 function verifyContextLost(shouldBeLost) {
30 // Verify context loss experimentally as well as isContextLost()
31 ctx.fillStyle = '#0f0';
32 ctx.fillRect(0, 0, 1, 1);
33 contextLostTest = ctx.getImageData(0, 0, 1, 1).data[1] == 0;
35 shouldBeTrue('contextLostTest');
36 shouldBeTrue('ctx.isContextLost()');
38 shouldBeFalse('contextLostTest');
39 shouldBeFalse('ctx.isContextLost()');
43 function contextLost() {
44 if (lostEventHasFired) {
45 testFailed('Context lost event was dispatched more than once.');
47 testPassed('Graphics context lost event dispatched.');
49 lostEventHasFired = true;
50 verifyContextLost(true);
53 function contextRestored() {
54 if (lostEventHasFired) {
55 testPassed('Context restored event dispatched after context lost.');
57 testFailed('Context restored event was dispatched before a context lost event.');
59 verifyContextLost(false);
60 testRunner.notifyDone();