Skip to content

Instantly share code, notes, and snippets.

@burg
Created December 11, 2017 21:02
Show Gist options
  • Select an option

  • Save burg/71af52ce1f1d3c346345522e7a75ae69 to your computer and use it in GitHub Desktop.

Select an option

Save burg/71af52ce1f1d3c346345522e7a75ae69 to your computer and use it in GitHub Desktop.
testing with async functions
<!doctype html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script>
function test()
{
const color = undefined;
const outlineColor = undefined;
async function getDocumentNode() {
let documentReady = new WI.WrappedPromise;
// FIXME: someday, requestDocument should return a promise. Then, this bridge can be deleted.
WI.domTreeManager.requestDocument((documentNode) => {
if (!documentNode)
documentReady.reject(new Error("Unable to obtain document node."));
else
documentReady.resolve(documentNode);
});
return documentReady.promise;
}
async function getHighlightRects() {
let highlightRectsJSONString = await InspectorTest.evaluateInPage("JSON.stringify(Array.from(window.internals.inspectorHighlightRects()))");
return JSON.parse(highlightRectsJSONString);
}
async function dumpHighlightRects(callback) {
let highlightRects = await getHighlightRects();
InspectorTest.expectThat(highlightRects.length === 1, "Should be one highlight rect.");
InspectorTest.log("Highlight Rect: " + JSON.stringify(highlightRects[0]));
}
async function getHighlightPayload() {
let highlightJSONString = await InspectorTest.evaluateInPage("window.internals.inspectorHighlightObject()");
return JSON.parse(highlightJSONString);
}
async function dumpHighlight() {
let highlightObjectPayload = await getHighlightPayload();
InspectorTest.expectThat(highlightObjectPayload.length === 1, "Should be one highlighted node.");
InspectorTest.log("Highlighted Element Data: " + JSON.stringify(highlightObjectPayload[0].elementData));
}
let suite = InspectorTest.createAsyncSuite("DOM.hideHighlight");
function addHideHighlightTestCase() {
suite.addTestCase({
name: "HideHighlight",
description: "Calling hideHighlight should hide the highlight.",
async test() {
await DOMAgent.hideHighlight();
let highlightRects = await getHighlightRects();
InspectorTest.expectThat(highlightRects.length === 0, "Should be no highlight.");
}
});
}
suite.addTestCase({
name: "CheckEmptyHighlight",
description: "Should not be a highlight yet.",
async test() {
let highlightRects = await getHighlightRects();
InspectorTest.expectThat(highlightRects.length === 0, "Should not be a highlight yet.");
}
});
suite.addTestCase({
name: "HighlightRect",
description: "Call highlightRect to create a highlight.",
async test() {
let rect = new WI.Rect(0, 0, 100, 100);
await DOMAgent.highlightRect(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height, color, outlineColor);
await dumpHighlightRects();
}
});
addHideHighlightTestCase();
suite.addTestCase({
name: "HighlightQuad",
description: "Call highlightQuad to create a highlight.",
async test() {
let quad = new WI.Quad([100, 100, 150, 150, 100, 200, 50, 150]);
await DOMAgent.highlightQuad(quad.toProtocol(), color, outlineColor);
await dumpHighlightRects();
}
});
addHideHighlightTestCase();
suite.addTestCase({
name: "HighlightNode",
description: "Call highlightNode to create a highlight.",
async test() {
const highlightConfig = {
showInfo: true,
contentColor: {r: 255, g: 255, b: 255},
paddingColor: {r: 255, g: 255, b: 255},
borderColor: {r: 255, g: 255, b: 255},
marginColor: {r: 255, g: 255, b: 255},
};
let documentNode = await getDocumentNode();
let nodeId = await WI.domTreeManager.querySelector(documentNode, "#target");
await DOMAgent.highlightNode(highlightConfig, nodeId);
await dumpHighlight();
}
});
addHideHighlightTestCase();
addHideHighlightTestCase(); // Test that a duplicate hideHighlight is not problematic.
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onload="runTest()">
<p id="target" style="width:500px; height:100px">Tests for the DOM.hideHighlight command.</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment