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

Side by Side Diff: LayoutTests/fast/canvas/canvas-lose-restore-max-int-size.html

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, 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 description("Series of tests to ensure correct behaviour when canvas size is ext remely large."); 1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
2 <html>
3 <head>
4 <script src="../../resources/js-test.js"></script>
5 </head>
6 <body>
7 <script>
8 description("Tests to ensure correct behaviour of canvas loss and restoration wh en size is extremely large then, restored to a reasonable value.");
9
10 if (window.testRunner) {
11 testRunner.dumpAsText();
12 testRunner.waitUntilDone();
13 }
14
2 var canvas = document.createElement('canvas') 15 var canvas = document.createElement('canvas')
16 canvas.addEventListener('contextlost', contextLost);
17 canvas.addEventListener('contextrestored', contextRestored);
3 var ctx = canvas.getContext('2d'); 18 var ctx = canvas.getContext('2d');
19 var lostEventHasFired = false;
20 verifyContextLost(false);
4 21
5 // WebIDL defines width and height as int. 2147483647 is int max. 22 // WebIDL defines width and height as int. 2147483647 is int max.
6 var extremelyLargeNumber = 2147483647; 23 var extremelyLargeNumber = 2147483647;
7 canvas.width = extremelyLargeNumber; 24 canvas.width = extremelyLargeNumber;
8 canvas.height = extremelyLargeNumber; 25 canvas.height = extremelyLargeNumber;
26 verifyContextLost(true);
27 canvas.width = extremelyLargeNumber;
28 verifyContextLost(true);
29 canvas.width = 100;
30 canvas.height = 100;
31 verifyContextLost(true); // Restoration is async.
32 // Restore a sane dimension
9 33
10 debug("check for crash on extremely large canvas size."); 34 function verifyContextLost(shouldBeLost) {
11 useCanvasContext(ctx); 35 // Verify context loss experimentally as well as isContextLost()
12 var imageData = ctx.getImageData(1, 1, 98, 98); 36 ctx.fillStyle = '#0f0';
13 var imgdata = imageData.data; 37 ctx.fillRect(0, 0, 1, 1);
14 // Blink returns zero color if the image buffer does not exist. 38 contextLostTest = ctx.getImageData(0, 0, 1, 1).data[1] == 0;
15 shouldBe("imgdata[4]", "0"); 39 if (shouldBeLost) {
16 shouldBe("imgdata[5]", "0"); 40 shouldBeTrue('contextLostTest');
17 shouldBe("imgdata[6]", "0"); 41 shouldBeTrue('ctx.isContextLost()');
18 42 } else {
19 debug("check for crash after resetting to the same size."); 43 shouldBeFalse('contextLostTest');
20 canvas.width = extremelyLargeNumber; 44 shouldBeFalse('ctx.isContextLost()');
21 useCanvasContext(ctx);
22 imageData = ctx.getImageData(1, 1, 98, 98);
23 imgdata = imageData.data;
24 shouldBe("imgdata[4]", "0");
25 shouldBe("imgdata[5]", "0");
26 shouldBe("imgdata[6]", "0");
27
28 // googol is parsed to 0.
29 var googol = Math.pow(10, 100);
30 debug("check for crash after resizing to googol.");
31 canvas.width = googol;
32 canvas.height = googol;
33 useCanvasContext(ctx);
34 imageData = ctx.getImageData(1, 1, 98, 98);
35 imgdata = imageData.data;
36 shouldBe("imgdata[4]", "0");
37 shouldBe("imgdata[5]", "0");
38 shouldBe("imgdata[6]", "0");
39
40 debug("check for crash after resetting to the same size.");
41 canvas.width = googol;
42 useCanvasContext(ctx);
43 imageData = ctx.getImageData(1, 1, 98, 98);
44 imgdata = imageData.data;
45 shouldBe("imgdata[4]", "0");
46 shouldBe("imgdata[5]", "0");
47 shouldBe("imgdata[6]", "0");
48
49 debug("check again for crash on extremely large canvas size.");
50 canvas.width = extremelyLargeNumber;
51 canvas.height = extremelyLargeNumber;
52 useCanvasContext(ctx);
53 imageData = ctx.getImageData(1, 1, 98, 98);
54 imgdata = imageData.data;
55 shouldBe("imgdata[4]", "0");
56 shouldBe("imgdata[5]", "0");
57 shouldBe("imgdata[6]", "0");
58
59 function useCanvasContext(ctx) {
60 ctx.fillStyle = 'green';
61 ctx.fillRect(0, 0, 100, 100);
62 for(var i = 0; i < 100; i++) {
63 // This API tries to create an image buffer if the image buffer is not c reated.
64 ctx.getImageData(1, 1, 1, 1);
65 } 45 }
66 ctx.beginPath();
67 ctx.rect(0,0,100,100);
68 ctx.save();
69 ctx.fillStyle = 'red';
70 ctx.fillRect(0, 0, 100, 100);
71 ctx.restore();
72 ctx.fillStyle = 'green';
73 ctx.fill();
74 } 46 }
75 47
76 debug("after resizing to normal size, the canvas must be in a valid state."); 48 function contextLost() {
77 canvas.width = 100; 49 if (lostEventHasFired) {
78 canvas.height = 100; 50 testFailed('Context lost event was dispatched more than once.');
79 ctx.fillStyle = 'blue'; 51 } else {
80 ctx.fillRect(0, 0, 100, 100); 52 testPassed('Graphics context lost event dispatched.');
81 imageData = ctx.getImageData(1, 1, 98, 98); 53 }
82 imgdata = imageData.data; 54 lostEventHasFired = true;
83 shouldBe("imgdata[4]", "0"); 55 verifyContextLost(true);
84 shouldBe("imgdata[5]", "0"); 56 }
85 shouldBe("imgdata[6]", "255"); 57
58 function contextRestored() {
59 if (lostEventHasFired) {
60 testPassed('Context restored event dispatched after context lost.');
61 } else {
62 testFailed('Context restored event was dispatched before a context lost event.');
63 }
64 verifyContextLost(false);
65 » if (window.testRunner) {
66 » testRunner.notifyDone();
67 }
68 }
69 </script>
70 </body>
71 </html>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698