Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / dom / resources / shadow-test-driver.js
blob047607a8294f6edd13fac4482a8bf962e618a19d
1 //
2 // shadow-test-driver.js
3 //
4 // To use shadow-test-driver.js, you should have
5 // <div id="actual-container"></div>
6 // <div id="expect-container"></div>
7 // <pre id="console"></pre>
8 // in your body.
9 //
10 // Then, define test functions having one argument 'callIfDone'.
11 // callIfDone should be called when your test function finished.
13 // In body.onload, call doTest(testFuncs) where testFuncs is an array of test functions.
15 // See content-element-move.html as an example.
18 function log(message) {
19 document.getElementById('console').innerHTML += (message + "\n");
22 function removeAllChildren(elem) {
23 while (elem.firstChild)
24 elem.removeChild(elem.firstChild);
27 function cleanUp() {
28 removeAllChildren(document.getElementById('actual-container'));
29 removeAllChildren(document.getElementById('expect-container'));
32 function removeContainerLines(text) {
33 var lines = text.split('\n');
34 lines.splice(0, 2);
35 return lines.join('\n');
38 function check() {
39 var expectContainer = document.getElementById('expect-container');
40 var actualContainer = document.getElementById('actual-container');
41 var originalDisplayValue = actualContainer.style.display;
42 actualContainer.style.display = 'none';
43 expectContainer.offsetLeft;
44 var refContainerLayoutTree = internals.elementLayoutTreeAsText(expectContainer);
45 var refLayoutTree = removeContainerLines(refContainerLayoutTree);
46 actualContainer.style.display = originalDisplayValue;
48 originalDisplayValue = expectContainer.style.display;
49 expectContainer.style.display = 'none';
50 actualContainer.offsetLeft;
51 var targetContainerLayoutTree = internals.elementLayoutTreeAsText(actualContainer);
52 var targetLayoutTree = removeContainerLines(targetContainerLayoutTree);
53 expectContainer.style.display = originalDisplayValue;
55 if (targetLayoutTree == refLayoutTree)
56 log("PASS");
57 else {
58 log("FAIL");
59 log("Expected: ");
60 log(refLayoutTree);
61 log("Actual: ");
62 log(targetLayoutTree);
66 function createSpanWithText(text, className) {
67 var span = document.createElement('span');
68 span.appendChild(document.createTextNode(text));
69 if (className)
70 span.className = className;
71 return span;
74 function createContentWithSelect(select, fallbackText) {
75 var content = document.createElement('content');
76 content.setAttribute('select', select);
77 if (fallbackText)
78 content.appendChild(createSpanWithText(fallbackText));
80 return content;
83 function createContentWithText(fallbackText) {
84 var content = document.createElement('content');
85 if (fallbackText)
86 content.innerHTML = fallbackText;
88 return content;
91 function appendShadow(target, select) {
92 var root = target.createShadowRoot();
94 var content = document.createElement('content');
95 content.setAttribute('select', select);
96 content.appendChild(createSpanWithText("FALLBACK"));
98 root.appendChild(document.createTextNode("{SHADOW: "));
99 root.appendChild(content);
100 root.appendChild(document.createTextNode("}"));
103 function appendShadowDeep(target, select) {
104 var root = target.createShadowRoot();
106 var child = document.createElement("span");
108 var content = document.createElement('content');
109 content.setAttribute('select', select);
110 content.appendChild(createSpanWithText("FALLBACK"));
112 child.appendChild(document.createTextNode("{INNER: "));
113 child.appendChild(content);
114 child.appendChild(document.createTextNode("}"));
117 root.appendChild(document.createTextNode("{SHADOW: "));
118 root.appendChild(child);
119 root.appendChild(document.createTextNode("}"));
122 function doTestIfLeft(restTests) {
123 var test = restTests.shift();
124 if (test == null)
125 return doneTest();
127 var callIfDone = function() {
128 setTimeout(function() {
129 check();
130 cleanUp();
131 doTestIfLeft(restTests);
132 }, 0);
135 log(test.name);
136 test(callIfDone);
139 function doneTest() {
140 log("TEST COMPLETED");
141 if (window.tearDownOnce)
142 window.tearDownOnce();
143 testRunner.notifyDone();
146 // A test driver. Call this body.onload.
147 function doTest(tests) {
148 if (window.setUpOnce)
149 window.setUpOnce();
151 if (window.testRunner) {
152 testRunner.waitUntilDone();
153 testRunner.dumpAsText();
156 cleanUp();
157 doTestIfLeft(tests);