haikuwebkit/LayoutTests/inspector/audit/run-resources.html

121 lines
6.0 KiB
HTML
Raw Permalink Normal View History

Web Inspector: Audit: provide a way to get the contents of resources https://bugs.webkit.org/show_bug.cgi?id=195266 <rdar://problem/48550911> Reviewed by Joseph Pecoraro. Source/JavaScriptCore: * inspector/InjectedScriptBase.cpp: (Inspector::InjectedScriptBase::makeAsyncCall): Drive-by: fix missing `else`. Source/WebCore: Test: inspector/audit/run-resources.html * inspector/InspectorAuditResourcesObject.idl: Added. * inspector/InspectorAuditResourcesObject.h: Added. (WebCore::InspectorAuditResourcesObject::create): (WebCore::InspectorAuditResourcesObject::Resource): (WebCore::InspectorAuditResourcesObject::ResourceContent): (WebCore::InspectorAuditResourcesObject::InspectorAuditCachedResourceClient): (WebCore::InspectorAuditResourcesObject::InspectorAuditCachedFontClient): (WebCore::InspectorAuditResourcesObject::InspectorAuditCachedImageClient): (WebCore::InspectorAuditResourcesObject::InspectorAuditCachedRawResourceClient): (WebCore::InspectorAuditResourcesObject::InspectorAuditCachedStyleSheetClient): (WebCore::InspectorAuditResourcesObject::InspectorAuditCachedSVGDocumentClient): * inspector/InspectorAuditResourcesObject.cpp: Added. (WebCore::InspectorAuditResourcesObject::InspectorAuditResourcesObject): (WebCore::InspectorAuditResourcesObject::getResources): (WebCore::InspectorAuditResourcesObject::getResourceContent): (WebCore::InspectorAuditResourcesObject::clientForResource): * inspector/agents/InspectorPageAgent.h: * inspector/agents/InspectorPageAgent.cpp: (WebCore::InspectorPageAgent::cachedResourcesForFrame): Added. (WebCore::allResourcesURLsForFrame): Moved a file static function to be a class static function so it can be used elsewhere. * CMakeLists.txt: * DerivedSources-input.xcfilelist: * DerivedSources-output.xcfilelist: * DerivedSources.make: * Sources.txt: * WebCore.xcodeproj/project.pbxproj: LayoutTests: * inspector/audit/resources/sample-resource.css: Added. * inspector/audit/resources/sample-resource.js: Added. * inspector/audit/run-resources.html: Added. * inspector/audit/run-resources-expected.txt: Added. Canonical link: https://commits.webkit.org/210031@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@242941 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2019-03-14 10:13:57 +00:00
<!DOCTYPE html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script src="resources/audit-utilities.js"></script>
<script src="resources/sample-resource.js"></script>
<link rel="stylesheet" href="resources/sample-resource.css">
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAABNJREFUCB1j/M/AAEQMDEwgAgQAHxcCAmtAm/sAAAAASUVORK5CYII=">
<script>
function test()
{
let suite = InspectorTest.Audit.createSuite("Audit.run.Resources");
function evaluateStringForTest(func, args) {
return `WebInspectorAudit.Resources.${func}(${Array.isArray(args) ? args.map(JSON.stringify).join(", ") : ""})`;
}
const tests = [
{ name: "Audit.run.Resources.getResources", func: "getResources" },
{ name: "Audit.run.Resources.getResourceContent.InvalidId", func: "getResourceContent", args: [-1], shouldError: true, },
];
for (let {name, func, args, shouldError} of tests) {
suite.addTestCase({
name,
async test() {
async function getContentForResource(resource) {
let {result, wasThrown} = await AuditAgent.run(`function() { return JSON.stringify(${evaluateStringForTest("getResourceContent", [resource.id])}); }`);
InspectorTest.assert(!wasThrown, "Should not throw an exception");
return JSON.parse(result.value);
}
let functionString = evaluateStringForTest(func, args);
await InspectorTest.Audit.setupAudit();
InspectorTest.log(`Audit run \`${functionString}\`...`);
let {result, wasThrown} = await AuditAgent.run(`function() { return JSON.stringify(${functionString}); }`);
if (shouldError)
InspectorTest.expectThat(wasThrown, "Should throw an exception.");
else
InspectorTest.assert(!wasThrown, "Should not throw an exception.");
if (!wasThrown) {
let resources = JSON.parse(result.value);
resources.sort((a, b) => a.url.extendedLocaleCompare(b.url));
for (let resource of resources) {
InspectorTest.assert(resource.id, `All resources should have "id".`);
InspectorTest.assert(resource.url, `All resources should have "url".`);
InspectorTest.assert(resource.mimeType, `All resources should have "mimeType".`);
if (resource.url.endsWith("sample-resource.js")) {
InspectorTest.log("Found sample-resource.js.");
InspectorTest.assert(resource.mimeType === "text/javascript", "sample-resource.js should have a text/javascript MIME type.");
let content = await getContentForResource(resource);
InspectorTest.expectEqual(content.data, "/* TEST JS */", "sample-resource.js should have the expected content.");
InspectorTest.expectFalse(content.base64Encoded, "sample-resource.js should not be base64 encoded.");
}
if (resource.url.endsWith("sample-resource.css")) {
InspectorTest.log("Found sample-resource.css.");
InspectorTest.assert(resource.mimeType === "text/css", "sample-resource.css should have a text/css MIME type.");
let content = await getContentForResource(resource);
InspectorTest.expectEqual(content.data, "/* TEST CSS */", "sample-resource.css should have the expected content.");
InspectorTest.expectFalse(content.base64Encoded, "sample-resource.css should not be base64 encoded.");
}
if (resource.url.startsWith("data:image/png;base64")) {
InspectorTest.log("Found dataURL image.");
InspectorTest.assert(resource.mimeType === "image/png", "dataURL image should have a image/png MIME type.");
let content = await getContentForResource(resource);
InspectorTest.expectEqual(content.data, "iVBORw0KGgoAAAANSUhEUgAAAAIAAAACCAYAAABytg0kAAAAAXNSR0IArs4c6QAAABNJREFUCB1j/M/AAEQMDEwgAgQAHxcCAmtAm/sAAAAASUVORK5CYII=", "dataURL image should have the expected content.");
InspectorTest.expectThat(content.base64Encoded, "dataURL image should be base64 encoded.");
}
}
} else
InspectorTest.log(result.description);
await InspectorTest.Audit.teardownAudit();
},
});
}
suite.addTestCase({
name: "Audit.run.Resources.InvalidCopiedFunctionCall",
description: "Check that WebInspectorAudit.Resources functions throw an error when called outside of an audit.",
async test() {
let functions = new Map;
for (let test of tests)
functions.set(test.func, test);
await InspectorTest.Audit.setupAudit();
InspectorTest.log(`Copying WebInspectorAudit to window...`);
let {wasThrown} = await AuditAgent.run(`function() { window.CopiedWebInspectorAudit = WebInspectorAudit; }`);
InspectorTest.assert(!wasThrown, "Should not throw an exception.");
await InspectorTest.Audit.teardownAudit();
for (let {func, args} of functions.values()) {
InspectorTest.log(`Testing copied ${func}...`);
await InspectorTest.expectException(async function() {
await InspectorTest.evaluateInPage("window.Copied" + evaluateStringForTest(func, args));
});
}
},
});
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onload="runTest()">
<p>Tests for the injected WebInspectorAudit.Resources functions.</p>
</body>
</html>