Web Inspector: Audit: provide a way to get the contents of resources
[WebKit-https.git] / LayoutTests / inspector / audit / run-resources.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script src="resources/audit-utilities.js"></script>
6 <script src="resources/sample-resource.js"></script>
7 <link rel="stylesheet" href="resources/sample-resource.css">
8 <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAABNJREFUCB1j/M/AAEQMDEwgAgQAHxcCAmtAm/sAAAAASUVORK5CYII=">
9 <script>
10 function test()
11 {
12     InspectorTest.debug();
13
14     let suite = InspectorTest.Audit.createSuite("Audit.run.Resources");
15
16     function evaluateStringForTest(func, args) {
17         return `WebInspectorAudit.Resources.${func}(${Array.isArray(args) ? args.map(JSON.stringify).join(", ") : ""})`;
18     }
19
20     const tests = [
21         { name: "Audit.run.Resources.getResources", func: "getResources" },
22         { name: "Audit.run.Resources.getResourceContent.InvalidId", func: "getResourceContent", args: [-1], shouldError: true, },
23     ];
24
25     for (let {name, func, args, shouldError} of tests) {
26         suite.addTestCase({
27             name,
28             async test() {
29                 async function getContentForResource(resource) {
30                     let {result, wasThrown} = await AuditAgent.run(`function() { return JSON.stringify(${evaluateStringForTest("getResourceContent", [resource.id])}); }`);
31                     InspectorTest.assert(!wasThrown, "Should not throw an exception");
32                     return JSON.parse(result.value);
33                 }
34
35                 let functionString = evaluateStringForTest(func, args);
36
37                 await InspectorTest.Audit.setupAudit();
38
39                 InspectorTest.log(`Audit run \`${functionString}\`...`);
40                 let {result, wasThrown} = await AuditAgent.run(`function() { return JSON.stringify(${functionString}); }`);
41
42                 if (shouldError)
43                     InspectorTest.expectThat(wasThrown, "Should throw an exception.");
44                 else
45                     InspectorTest.assert(!wasThrown, "Should not throw an exception.");
46
47                 if (!wasThrown) {
48                     let resources = JSON.parse(result.value);
49
50                     resources.sort((a, b) => a.url.extendedLocaleCompare(b.url));
51
52                     for (let resource of resources) {
53                         InspectorTest.assert(resource.id, `All resources should have "id".`);
54                         InspectorTest.assert(resource.url, `All resources should have "url".`);
55                         InspectorTest.assert(resource.mimeType, `All resources should have "mimeType".`);
56
57                         if (resource.url.endsWith("sample-resource.js")) {
58                             InspectorTest.log("Found sample-resource.js.");
59                             InspectorTest.assert(resource.mimeType === "text/javascript", "sample-resource.js should have a text/javascript MIME type.");
60
61                             let content = await getContentForResource(resource);
62                             InspectorTest.expectEqual(content.data, "/* TEST JS */", "sample-resource.js should have the expected content.");
63                             InspectorTest.expectFalse(content.base64Encoded, "sample-resource.js should not be base64 encoded.");
64                         }
65
66                         if (resource.url.endsWith("sample-resource.css")) {
67                             InspectorTest.log("Found sample-resource.css.");
68                             InspectorTest.assert(resource.mimeType === "text/css", "sample-resource.css should have a text/css MIME type.");
69
70                             let content = await getContentForResource(resource);
71                             InspectorTest.expectEqual(content.data, "/* TEST CSS */", "sample-resource.css should have the expected content.");
72                             InspectorTest.expectFalse(content.base64Encoded, "sample-resource.css should not be base64 encoded.");
73                         }
74
75                         if (resource.url.startsWith("data:image/png;base64")) {
76                             InspectorTest.log("Found dataURL image.");
77                             InspectorTest.assert(resource.mimeType === "image/png", "dataURL image should have a image/png MIME type.");
78
79                             let content = await getContentForResource(resource);
80                             InspectorTest.expectEqual(content.data, "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAABNJREFUCB1j/M/AAEQMDEwgAgQAHxcCAmtAm/sAAAAASUVORK5CYII=", "dataURL image should have the expected content.");
81                             InspectorTest.expectThat(content.base64Encoded, "dataURL image should be base64 encoded.");
82                         }
83                     }
84                 } else
85                     InspectorTest.log(result.description);
86
87                 await InspectorTest.Audit.teardownAudit();
88             },
89         });
90     }
91
92     suite.addTestCase({
93         name: "Audit.run.Resources.InvalidCopiedFunctionCall",
94         description: "Check that WebInspectorAudit.Resources functions throw an error when called outside of an audit.",
95         async test() {
96             let functions = new Map;
97             for (let test of tests)
98                 functions.set(test.func, test);
99
100             await InspectorTest.Audit.setupAudit();
101             InspectorTest.log(`Copying WebInspectorAudit to window...`);
102             let {wasThrown} = await AuditAgent.run(`function() { window.CopiedWebInspectorAudit = WebInspectorAudit; }`);
103             InspectorTest.assert(!wasThrown, "Should not throw an exception.");
104             await InspectorTest.Audit.teardownAudit();
105
106             for (let {func, args} of functions.values()) {
107                 InspectorTest.log(`Testing copied ${func}...`);
108                 await InspectorTest.expectException(async function() {
109                     await InspectorTest.evaluateInPage("window.Copied" + evaluateStringForTest(func, args));
110                 });
111             }
112         },
113     });
114
115     suite.runTestCasesAndFinish();
116 }
117 </script>
118 </head>
119 <body onload="runTest()">
120     <p>Tests for the injected WebInspectorAudit.Resources functions.</p>
121 </body>
122 </html>