141e2518760a82cce516afa399b43a4f13960769
[WebKit-https.git] / LayoutTests / inspector / canvas / resources / create-context-utilities.js
1 let contexts = [];
2
3 function createAttachedCanvas(contextType) {
4     let canvas = document.body.appendChild(document.createElement("canvas"));
5     contexts.push(canvas.getContext(contextType));
6 }
7
8 function createDetachedCanvas(contextType) {
9     contexts.push(document.createElement("canvas").getContext(contextType));
10 }
11
12 function createCSSCanvas(contextType, canvasName) {
13     contexts.push(document.getCSSCanvasContext(contextType, canvasName, 10, 10));
14 }
15
16 function destroyCanvases() {
17     for (let context of contexts) {
18         let canvasElement = context.canvas;
19         if (canvasElement && canvasElement.parentNode)
20             canvasElement.remove();
21     }
22
23     contexts = [];
24
25     // Force GC to make sure the canvas element is destroyed, otherwise the frontend
26     // does not receive WI.CanvasManager.Event.CanvasRemoved events.
27     setTimeout(() => { GCController.collect(); }, 0);
28 }
29
30 TestPage.registerInitializer(() => {
31     let suite = null;
32
33     function awaitCanvasAdded(contextType) {
34         return WI.canvasManager.awaitEvent(WI.CanvasManager.Event.CanvasAdded)
35         .then((event) => {
36             let canvas = event.data.canvas;
37             let contextDisplayName = WI.Canvas.displayNameForContextType(contextType);
38             InspectorTest.expectEqual(canvas.contextType, contextType, `Canvas context should be ${contextDisplayName}.`);
39
40             for (let i = 0; i < canvas.backtrace.length; ++i) {
41                 let callFrame = canvas.backtrace[i];
42                 let traceText = `  ${i}: `;
43                 traceText += callFrame.functionName || "(anonymous function)";
44
45                 if (callFrame.nativeCode)
46                     traceText += " - [native code]";
47                 else if (callFrame.programCode)
48                     traceText += " - [program code]";
49                 else if (callFrame.sourceCodeLocation) {
50                     let location = callFrame.sourceCodeLocation;
51                     traceText += " - " + sanitizeURL(location.sourceCode.url) + `:${location.lineNumber}:${location.columnNumber}`;
52                 }
53
54                 InspectorTest.log(traceText);
55             }
56
57             InspectorTest.log("");
58
59             return canvas;
60         });
61     }
62
63     function awaitCanvasRemoved(canvasIdentifier) {
64         return WI.canvasManager.awaitEvent(WI.CanvasManager.Event.CanvasRemoved)
65         .then((event) => {
66             let canvas = event.data.canvas;
67             InspectorTest.expectEqual(canvas.identifier, canvasIdentifier, "Removed canvas has expected ID.");
68         });
69     }
70
71     window.initializeTestSuite = function(name) {
72         suite = InspectorTest.createAsyncSuite(name);
73
74         suite.addTestCase({
75             name: `${suite.name}.NoCanvases`,
76             description: "Check that the CanvasManager has no canvases initially.",
77             test(resolve, reject) {
78                 InspectorTest.expectEqual(WI.canvasManager.canvases.length, 0, "CanvasManager should have no canvases.");
79                 resolve();
80             }
81         });
82
83         return suite;
84     };
85
86     window.addSimpleTestCase = function({name, description, expression, contextType}) {
87         suite.addTestCase({
88             name: suite.name + "." + name,
89             description,
90             test(resolve, reject) {
91                 awaitCanvasAdded(contextType)
92                 .then((canvas) => {
93                     if (canvas.cssCanvasName) {
94                         InspectorTest.log("CSS canvas will not be destroyed");
95                         resolve();
96                         return;
97                     }
98
99                     let promise = awaitCanvasRemoved(canvas.identifier);
100                     InspectorTest.evaluateInPage(`destroyCanvases()`);
101                     return promise;
102                 })
103                 .then(resolve, reject);
104
105                 InspectorTest.evaluateInPage(expression);
106             },
107         });
108     };
109
110     let previousCSSCanvasContextType = null;
111     window.addCSSCanvasTestCase = function(contextType) {
112         InspectorTest.assert(!previousCSSCanvasContextType || previousCSSCanvasContextType === contextType, "addCSSCanvasTestCase cannot be called more than once with different context types.");
113         if (!previousCSSCanvasContextType)
114             previousCSSCanvasContextType = contextType;
115
116         suite.addTestCase({
117             name: `${suite.name}.CSSCanvas`,
118             description: "Check that CSS canvases have the correct name and type.",
119             test(resolve, reject) {
120                 awaitCanvasAdded(contextType)
121                 .then((canvas) => {
122                     InspectorTest.expectEqual(canvas.cssCanvasName, "css-canvas", "Canvas name should equal the identifier passed to -webkit-canvas.");
123                 })
124                 .then(resolve, reject);
125
126                 let contextId = contextType === WI.Canvas.ContextType.Canvas2D ? "2d" : contextType;
127                 InspectorTest.log(`Create CSS canvas from -webkit-canvas(css-canvas).`);
128                 InspectorTest.evaluateInPage(`createCSSCanvas("${contextId}", "css-canvas")`);
129             },
130         });
131     };
132 });