| <!DOCTYPE html> |
| <html> |
| <head> |
| <script src="../../http/tests/inspector/resources/inspector-test.js"></script> |
| <script src="resources/audit-utilities.js"></script> |
| <script> |
| function test() |
| { |
| let suite = InspectorTest.Audit.createSuite("Audit.run.DOM"); |
| |
| function evaluateStringForTest(func, args, prefix = "") { |
| switch (func) { |
| case "hasEventListeners": |
| return `${prefix}WebInspectorAudit.DOM.${func}(${args.join(", ")})`; |
| |
| case "simulateUserInteraction": |
| return `(() => { let result = undefined; ${prefix}WebInspectorAudit.DOM.${func}(${args.join(", ")}); return result; })()`; |
| } |
| |
| return "'FAIL'"; |
| } |
| |
| const tests = [ |
| { func: "hasEventListeners", name: "None.General", args: [`document.querySelector("#noListeners")`] }, |
| { func: "hasEventListeners", name: "None.click", args: [`document.querySelector("#noListeners")`, `"click"`] }, |
| { func: "hasEventListeners", name: "None.FakeEvent", args: [`document.querySelector("#noListeners")`, `"FakeEvent"`] }, |
| { func: "hasEventListeners", name: "Attribute.General", args: [`document.querySelector("#attributeListener")`] }, |
| { func: "hasEventListeners", name: "Attribute.click", args: [`document.querySelector("#attributeListener")`, `"click"`] }, |
| { func: "hasEventListeners", name: "Attribute.FakeEvent", args: [`document.querySelector("#attributeListener")`, `"FakeEvent"`] }, |
| { func: "hasEventListeners", name: "JavaScript.General", args: [`document.querySelector("#javascriptListener")`] }, |
| { func: "hasEventListeners", name: "JavaScript.click", args: [`document.querySelector("#javascriptListener")`, `"click"`] }, |
| { func: "hasEventListeners", name: "JavaScript.FakeEvent", args: [`document.querySelector("#javascriptListener")`, `"FakeEvent"`] }, |
| { func: "hasEventListeners", name: "Builtin.General", args: [`document.querySelector("#builtinListener")`] }, |
| { func: "hasEventListeners", name: "Builtin.play", args: [`document.querySelector("#builtinListener")`, `"play"`] }, |
| { func: "hasEventListeners", name: "Builtin.FakeEvent", args: [`document.querySelector("#builtinListener")`, `"FakeEvent"`] }, |
| |
| { func: "simulateUserInteraction", name: "UserGesture", args: [`() => { result = internals.isProcessingUserGesture(); }`] }, |
| { func: "simulateUserInteraction", name: "TransientActivation", args: [`() => { result = internals.hasTransientActivation(); }`] }, |
| ]; |
| |
| for (let {func, name, args} of tests) { |
| suite.addTestCase({ |
| name: "Audit.run.DOM." + func + (name ? "." + name : ""), |
| async test() { |
| let functionString = evaluateStringForTest(func, args); |
| |
| await InspectorTest.Audit.setupAudit(); |
| |
| InspectorTest.log(`Audit run \`${functionString}\`...`); |
| let {result, wasThrown} = await AuditAgent.run(`function() { return ${functionString}; }`); |
| InspectorTest.assert(!wasThrown, "Should not throw an exception."); |
| if (!wasThrown) |
| InspectorTest.log("Result: " + result.value); |
| else |
| InspectorTest.log(result.description); |
| |
| await InspectorTest.Audit.teardownAudit(); |
| }, |
| }); |
| } |
| |
| suite.addTestCase({ |
| name: "Audit.run.DOM.InvalidCopiedFunctionCall", |
| description: "Check that WebInspectorAudit.DOM 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(evaluateStringForTest(func, args, "window.Copied")); |
| }); |
| } |
| }, |
| }); |
| |
| suite.runTestCasesAndFinish(); |
| } |
| </script> |
| </head> |
| <body onload="runTest()"> |
| <p>Tests for the injected WebInspectorAudit.DOM functions.</p> |
| <div id="noListeners"></div> |
| <div id="attributeListener" onclick="void(0);"></div> |
| <div id="javascriptListener"><script>document.querySelector("#javascriptListener").addEventListener("click", () => {});</script></div> |
| <video id="builtinListener"></video> |
| </body> |
| </html> |