Web Inspector: Instrument active pixel memory used by canvases
[WebKit-https.git] / LayoutTests / inspector / canvas / memory.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 function load() {
7     window.context = document.body.appendChild(document.createElement("canvas")).getContext("2d");
8
9     runTest();
10 }
11
12 function changeSize(width, height) {
13     window.context.canvas.width = width;
14     window.context.canvas.height = height;
15
16     // Force the creation of the image buffer, which is used to determine memory cost.
17     window.context.canvas.toDataURL();
18 }
19
20 function test() {
21     let suite = InspectorTest.createAsyncSuite("Canvas.memory");
22
23     suite.addTestCase({
24         name: "Canvas.memory.memoryCost",
25         description: "Check that memory cost is sent with the canvas when able.",
26         test(resolve, reject) {
27             // NOTE: the memory cost of a canvas will not be retrievable until an operation is
28             // performed on that canvas that requires the image buffer.  A blank canvas that was
29             // just created will not have a buffer, so its memory cost will be 0/NaN.
30
31             let canvases = WebInspector.canvasManager.canvases.filter((canvas) => canvas.contextType === WebInspector.Canvas.ContextType.Canvas2D);
32             if (!canvases.length) {
33                 reject("Missing 2D canvas.");
34                 return;
35             }
36
37             InspectorTest.assert(canvases.length === 1, "There should only be one canvas-2d.");
38
39             InspectorTest.log(`Memory cost of canvas is ${canvases[0].memoryCost}.`);
40             resolve();
41         }
42     });
43
44     suite.addTestCase({
45         name: "Canvas.memory.canvasMemoryChanged",
46         description: "Check that memory cost is updated when the backend value changes.",
47         test(resolve, reject) {
48             let canvases = WebInspector.canvasManager.canvases.filter((canvas) => canvas.contextType === WebInspector.Canvas.ContextType.Canvas2D);
49             if (!canvases.length) {
50                 reject("Missing 2D canvas.");
51                 return;
52             }
53
54             InspectorTest.assert(canvases.length === 1, "There should only be one canvas-2d.");
55
56             canvases[0].awaitEvent(WebInspector.Canvas.Event.MemoryChanged)
57             .then((event) => {
58                 InspectorTest.log(`Memory cost of canvas updated to ${event.target.memoryCost}.`);
59             })
60             .then(resolve, reject);
61
62             const width = 200;
63             const height = 200;
64             InspectorTest.log(`Change size of canvas to ${width}x${height}.`);
65             InspectorTest.evaluateInPage(`changeSize(${width}, ${height})`);
66         }
67     });
68
69     suite.runTestCasesAndFinish();
70 }
71 </script>
72 </head>
73 <body onload="load()">
74     <p>Test that CanvasManager tracks canvas memory costs and is notified of changes.</p>
75 </body>
76 </html>