Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Unified Diff: LayoutTests/fast/canvas/script-tests/canvas-lost-gpu-context.js

Issue 211503006: Implementation of 2D canvas context lost/restored events (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: rebase Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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();
}

Powered by Google App Engine
This is Rietveld 408576698