2 // shadow-test-driver.js
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>
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
);
28 removeAllChildren(document
.getElementById('actual-container'));
29 removeAllChildren(document
.getElementById('expect-container'));
32 function removeContainerLines(text
) {
33 var lines
= text
.split('\n');
35 return lines
.join('\n');
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
)
62 log(targetLayoutTree
);
66 function createSpanWithText(text
, className
) {
67 var span
= document
.createElement('span');
68 span
.appendChild(document
.createTextNode(text
));
70 span
.className
= className
;
74 function createContentWithSelect(select
, fallbackText
) {
75 var content
= document
.createElement('content');
76 content
.setAttribute('select', select
);
78 content
.appendChild(createSpanWithText(fallbackText
));
83 function createContentWithText(fallbackText
) {
84 var content
= document
.createElement('content');
86 content
.innerHTML
= fallbackText
;
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();
127 var callIfDone = function() {
128 setTimeout(function() {
131 doTestIfLeft(restTests
);
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
)
151 if (window
.testRunner
) {
152 testRunner
.waitUntilDone();
153 testRunner
.dumpAsText();