Index: LayoutTests/fast/canvas/script-tests/canvas-lost-gpu-context.js |
diff --git a/LayoutTests/fast/canvas/script-tests/canvas-lost-gpu-context.js b/LayoutTests/fast/canvas/script-tests/canvas-lost-gpu-context.js |
index 623134a7b2b0d9f36ba931549cd5495f768b9361..363045a1e5661d041a2e000f4909668a0a293f95 100644 |
--- a/LayoutTests/fast/canvas/script-tests/canvas-lost-gpu-context.js |
+++ b/LayoutTests/fast/canvas/script-tests/canvas-lost-gpu-context.js |
@@ -1,62 +1,61 @@ |
description("Test the behavior of canvas recovery after a gpu context loss"); |
-var recoveryLoopPeriod = 5; |
var ctx; |
-var imageData; |
-var imgdata; |
+var lostEventHasFired = false; |
+var contextLostTest; |
if (window.internals && window.testRunner) { |
testRunner.dumpAsText(); |
- ctx = document.createElement('canvas').getContext('2d'); |
+ var canvas = document.createElement('canvas'); |
+ canvas.addEventListener('contextlost', contextLost); |
+ canvas.addEventListener('contextrestored', contextRestored); |
+ ctx = canvas.getContext('2d'); |
document.body.appendChild(ctx.canvas); |
- ctx.fillStyle = '#f00'; |
- ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
- imageData = ctx.getImageData(0, 0, 1, 1); |
- imgdata = imageData.data; |
- shouldBe("imgdata[0]", "255"); |
- shouldBe("imgdata[1]", "0"); |
- shouldBe("imgdata[2]", "0"); |
- shouldBe("imgdata[3]", "255"); |
- |
+ verifyContextLost(false); |
window.internals.loseSharedGraphicsContext3D(); |
- // Verify whether canvas contents are lost with the graphics context. |
- imageData = ctx.getImageData(0, 0, 1, 1); |
- if (imageData.data[0] == 255) { |
+ // for the canvas to realize it Graphics context was lost we must try to use the canvas |
+ ctx.fillRect(0, 0, 1, 1); |
+ if (!ctx.isContextLost()) { |
debug('<span>Aborting test: Graphics context loss did not destroy canvas contents. This is expected if canvas is not accelerated.</span>'); |
} else { |
- // Redrawing immediately will fail because we are working with an |
- // unrecovered context here. The context recovery is asynchronous |
- // because it requires the context loss notification task to be |
- // processed on the renderer main thread, which triggers the |
- // re-creation of the SharedGC3D. |
- ctx.fillStyle = '#0f0'; |
- ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
- imageData = ctx.getImageData(0, 0, 1, 1); |
- imgdata = imageData.data; |
- shouldBe("imgdata[0]", "0"); |
- shouldBe("imgdata[1]", "0"); |
- shouldBe("imgdata[2]", "0"); |
- shouldBe("imgdata[3]", "0"); |
- |
+ verifyContextLost(true); |
testRunner.waitUntilDone(); |
- setTimeout(recoveryLoop, recoveryLoopPeriod); |
} |
} else { |
testFailed('This test requires window.internals and window.testRunner.'); |
} |
-// Graphics context recovery happens asynchronously. To test for recovery, we keep |
-// retrying to use the canvas until it succeeds, which should hapen long before the test |
-// times-out. |
-function recoveryLoop() { |
- ctx.fillStyle = '#00f'; |
- ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
- imageData = ctx.getImageData(0, 0, 1, 1); |
- if (imageData.data[2] == 255) { |
- testPassed('Graphics context recovered.'); |
- testRunner.notifyDone(); |
+ |
+function verifyContextLost(shouldBeLost) { |
+ // Verify context loss experimentally as well as isContextLost() |
+ ctx.fillStyle = '#0f0'; |
+ ctx.fillRect(0, 0, 1, 1); |
+ contextLostTest = ctx.getImageData(0, 0, 1, 1).data[1] == 0; |
+ if (shouldBeLost) { |
+ shouldBeTrue('contextLostTest'); |
+ shouldBeTrue('ctx.isContextLost()'); |
+ } else { |
+ shouldBeFalse('contextLostTest'); |
+ shouldBeFalse('ctx.isContextLost()'); |
+ } |
+} |
+ |
+function contextLost() { |
+ if (lostEventHasFired) { |
+ testFailed('Context lost event was dispatched more than once.'); |
+ } else { |
+ testPassed('Graphics context lost event dispatched.'); |
+ } |
+ lostEventHasFired = true; |
+ verifyContextLost(true); |
+} |
+ |
+function contextRestored() { |
+ if (lostEventHasFired) { |
+ testPassed('Context restored event dispatched after context lost.'); |
} else { |
- // Context not yet recovered. Try again. |
- setTimeout(recoveryLoop, recoveryLoopPeriod); |
+ testFailed('Context restored event was dispatched before a context lost event.'); |
} |
+ verifyContextLost(false); |
+ testRunner.notifyDone(); |
} |