Web Inspector: support undo/redo of insertAdjacentHTML
[WebKit-https.git] / LayoutTests / inspector / dom / insertAdjacentHTML.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../http/tests/inspector/resources/inspector-test.js"></script>
5 <script>
6 function test()
7 {
8     let domNode;
9
10     let suite = InspectorTest.createAsyncSuite("DOM.insertAdjacentHTML");
11
12     const positions = [
13         "beforebegin",
14         "afterbegin",
15         "beforeend",
16         "afterend",
17     ];
18
19     for (let position of positions) {
20         suite.addTestCase({
21             name: `DOM.insertAdjacentHTML.${position}`,
22             async test() {
23                 let html = `<div id="${position}"></div>`;
24                 await DOMAgent.insertAdjacentHTML(domNode.id, position, html);
25
26                 let insertAdjacentHTMLResult = await DOMAgent.getOuterHTML(domNode.parentNode.id);
27                 InspectorTest.expectThat(insertAdjacentHTMLResult.outerHTML.includes(html), "The given HTML should have been added to the DOM tree.")
28
29                 await DOMAgent.undo();
30
31                 let undoResult = await DOMAgent.getOuterHTML(domNode.parentNode.id);
32                 InspectorTest.expectThat(!undoResult.outerHTML.includes(html), "The given HTML should have been removed from the DOM tree.")
33
34                 await DOMAgent.redo();
35
36                 let redoResult = await DOMAgent.getOuterHTML(domNode.parentNode.id);
37                 InspectorTest.expectThat(redoResult.outerHTML.includes(html), "The given HTML should have been added back to the DOM tree.")
38             }
39         });
40     }
41
42     suite.addTestCase({
43         name: "DOM.insertAdjacentHTML.InvalidNodeId",
44         async test() {
45             try {
46                 const nodeId = 9999999;
47                 const position = "afterbegin";
48                 const html = "";
49                 await DOMAgent.insertAdjacentHTML(nodeId, position, html);
50             } catch (error) {
51                 InspectorTest.expectThat(error, "Should produce an error.");
52                 InspectorTest.pass("Error: " + error.message);
53             }
54         }
55     });
56
57     suite.addTestCase({
58         name: "DOM.insertAdjacentHTML.InvalidPosition",
59         async test() {
60             try {
61                 const position = "INVALID_POSITION";
62                 const html = "";
63                 await DOMAgent.insertAdjacentHTML(domNode.id, position, html);
64             } catch (error) {
65                 InspectorTest.expectThat(error, "Should produce an error.");
66                 InspectorTest.pass("Error: " + error.message);
67             }
68         }
69     });
70
71     WI.domTreeManager.requestDocument((documentNode) => {
72         WI.domTreeManager.querySelector(documentNode.id, "#reference", (nodeId) => {
73             domNode = WI.domTreeManager.nodeForId(nodeId);
74             InspectorTest.assert(domNode, `Node with id "reference" should exist.`);
75
76             suite.runTestCasesAndFinish();
77         });
78     });
79 }
80 </script>
81 </head>
82 <body onload="runTest()">
83     <p>Test for DOM.insertAdjacentHTML.</p>
84
85     <div id="container"><div id="reference"></div></div>
86 </body>
87 </html>