Debian Codegraph @

closure-library/closure/goog/testing/dom_test.js

raw
1 // Copyright 2008 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.testing.domTest');
16 goog.setTestOnly('goog.testing.domTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.testing.TestCase');
21 goog.require('goog.testing.dom');
22 goog.require('goog.testing.jsunit');
23 goog.require('goog.userAgent');
24
25
26 function shouldRunTests() {
27 // This test has not yet been updated to run on IE8. See b/2997682.
28 return !goog.userAgent.IE || goog.userAgent.isVersionOrHigher(9);
29 }
30
31
32 var root;
33 function setUpPage() {
34 // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
35 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
36
37 root = goog.dom.getElement('root');
38 }
39
40 function setUp() {
41 goog.dom.removeChildren(root);
42 }
43
44 function testFindNode() {
45 // Test the easiest case.
46 root.innerHTML = 'a<br>b';
47 assertEquals(goog.testing.dom.findTextNode('a', root), root.firstChild);
48 assertEquals(goog.testing.dom.findTextNode('b', root), root.lastChild);
49 assertNull(goog.testing.dom.findTextNode('c', root));
50 }
51
52 function testFindNodeDuplicate() {
53 // Test duplicate.
54 root.innerHTML = 'c<br>c';
55 assertEquals(
56 'Should return first duplicate', goog.testing.dom.findTextNode('c', root),
57 root.firstChild);
58 }
59
60 function findNodeWithHierarchy() {
61 // Test a more complicated hierarchy.
62 root.innerHTML = '<div>a<p>b<span>c</span>d</p>e</div>';
63 assertEquals(
64 String(goog.dom.TagName.DIV),
65 goog.testing.dom.findTextNode('a', root).parentNode.tagName);
66 assertEquals(
67 String(goog.dom.TagName.P),
68 goog.testing.dom.findTextNode('b', root).parentNode.tagName);
69 assertEquals(
70 String(goog.dom.TagName.SPAN),
71 goog.testing.dom.findTextNode('c', root).parentNode.tagName);
72 assertEquals(
73 String(goog.dom.TagName.P),
74 goog.testing.dom.findTextNode('d', root).parentNode.tagName);
75 assertEquals(
76 String(goog.dom.TagName.DIV),
77 goog.testing.dom.findTextNode('e', root).parentNode.tagName);
78 }
79
80 function setUpAssertHtmlMatches() {
81 var tag1, tag2;
82 if (goog.userAgent.EDGE_OR_IE) {
83 tag1 = goog.dom.TagName.DIV;
84 } else if (goog.userAgent.WEBKIT) {
85 tag1 = goog.dom.TagName.P;
86 tag2 = goog.dom.TagName.BR;
87 } else if (goog.userAgent.GECKO) {
88 tag1 = goog.dom.TagName.SPAN;
89 tag2 = goog.dom.TagName.BR;
90 }
91
92 var parent = goog.dom.createDom(goog.dom.TagName.DIV);
93 root.appendChild(parent);
94 parent.style.fontSize = '2em';
95 parent.style.display = 'none';
96 if (!goog.userAgent.WEBKIT) {
97 parent.appendChild(goog.dom.createTextNode('NonWebKitText'));
98 }
99
100 if (tag1) {
101 var e1 = goog.dom.createDom(tag1);
102 parent.appendChild(e1);
103 parent = e1;
104 }
105 if (tag2) {
106 parent.appendChild(goog.dom.createDom(tag2));
107 }
108 parent.appendChild(goog.dom.createTextNode('Text'));
109 if (goog.userAgent.WEBKIT) {
110 root.firstChild.appendChild(goog.dom.createTextNode('WebKitText'));
111 }
112 }
113
114 function testAssertHtmlContentsMatch() {
115 setUpAssertHtmlMatches();
116
117 goog.testing.dom.assertHtmlContentsMatch(
118 '<div style="display: none; font-size: 2em">' +
119 '[[!WEBKIT]]NonWebKitText<div class="IE EDGE"><p class="WEBKIT">' +
120 '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
121 '</div>[[WEBKIT]]WebKitText',
122 root);
123 }
124
125 function testAssertHtmlMismatchText() {
126 setUpAssertHtmlMatches();
127
128 var e = assertThrows('Should fail due to mismatched text', function() {
129 goog.testing.dom.assertHtmlContentsMatch(
130 '<div style="display: none; font-size: 2em">' +
131 '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
132 '<span class="GECKO"><br class="GECKO WEBKIT">Bad</span></p></div>' +
133 '</div>[[WEBKIT]]Extra',
134 root);
135 });
136 assertContains('Text should match', e.message);
137 }
138
139 function testAssertHtmlMismatchTag() {
140 setUpAssertHtmlMatches();
141
142 var e = assertThrows('Should fail due to mismatched tag', function() {
143 goog.testing.dom.assertHtmlContentsMatch(
144 '<span style="display: none; font-size: 2em">' +
145 '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
146 '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
147 '</span>[[WEBKIT]]Extra',
148 root);
149 });
150 assertContains('Tag names should match', e.message);
151 }
152
153 function testAssertHtmlMismatchStyle() {
154 setUpAssertHtmlMatches();
155
156 var e = assertThrows('Should fail due to mismatched style', function() {
157 goog.testing.dom.assertHtmlContentsMatch(
158 '<div style="display: none; font-size: 3em">' +
159 '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
160 '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
161 '</div>[[WEBKIT]]Extra',
162 root);
163 });
164 assertContains('Should have same styles', e.message);
165 }
166
167 function testAssertHtmlMismatchOptionalText() {
168 setUpAssertHtmlMatches();
169
170 var e = assertThrows('Should fail due to mismatched text', function() {
171 goog.testing.dom.assertHtmlContentsMatch(
172 '<div style="display: none; font-size: 2em">' +
173 '[[IE GECKO]]Bad<div class="IE"><p class="WEBKIT">' +
174 '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
175 '</div>[[WEBKIT]]Bad',
176 root);
177 });
178 assertContains('Text should match', e.message);
179 }
180
181 function testAssertHtmlMismatchExtraActualAfterText() {
182 root.innerHTML = '<div>abc</div>def';
183
184 var e = assertThrows('Should fail due to extra actual nodes', function() {
185 goog.testing.dom.assertHtmlContentsMatch('<div>abc</div>', root);
186 });
187 assertContains('Finished expected HTML before', e.message);
188 }
189
190 function testAssertHtmlMismatchExtraActualAfterElement() {
191 root.innerHTML = '<br>def';
192
193 var e = assertThrows('Should fail due to extra actual nodes', function() {
194 goog.testing.dom.assertHtmlContentsMatch('<br>', root);
195 });
196 assertContains('Finished expected HTML before', e.message);
197 }
198
199 function testAssertHtmlMatchesWithSplitTextNodes() {
200 root.appendChild(goog.dom.createTextNode('1'));
201 root.appendChild(goog.dom.createTextNode('2'));
202 root.appendChild(goog.dom.createTextNode('3'));
203 goog.testing.dom.assertHtmlContentsMatch('123', root);
204 }
205
206 function testAssertHtmlMatchesWithDifferentlyOrderedAttributes() {
207 root.innerHTML = '<div foo="a" bar="b" class="className"></div>';
208
209 goog.testing.dom.assertHtmlContentsMatch(
210 '<div bar="b" class="className" foo="a"></div>', root, true);
211 }
212
213 function testAssertHtmlMismatchWithDifferentNumberOfAttributes() {
214 root.innerHTML = '<div foo="a" bar="b"></div>';
215
216 var e = assertThrows(function() {
217 goog.testing.dom.assertHtmlContentsMatch('<div foo="a"></div>', root, true);
218 });
219 assertContains('Unexpected attribute with name bar in element', e.message);
220 }
221
222 function testAssertHtmlMismatchWithDifferentAttributeNames() {
223 root.innerHTML = '<div foo="a" bar="b"></div>';
224
225 var e = assertThrows(function() {
226 goog.testing.dom.assertHtmlContentsMatch(
227 '<div foo="a" baz="b"></div>', root, true);
228 });
229 assertContains('Expected to find attribute with name baz', e.message);
230 }
231
232 function testAssertHtmlMismatchWithDifferentClassNames() {
233 root.innerHTML = '<div class="className1"></div>';
234
235 var e = assertThrows(function() {
236 goog.testing.dom.assertHtmlContentsMatch(
237 '<div class="className2"></div>', root, true);
238 });
239 assertContains(
240 'Expected class was: className2, but actual class was: className1',
241 e.message);
242 }
243
244 function testAssertHtmlMatchesWithClassNameAndUserAgentSpecified() {
245 root.innerHTML = '<div>' +
246 (goog.userAgent.GECKO ? '<div class="foo"></div>' : '') + '</div>';
247
248 goog.testing.dom.assertHtmlContentsMatch(
249 '<div><div class="foo GECKO"></div></div>', root, true);
250 }
251
252 function testAssertHtmlMatchesWithClassesInDifferentOrder() {
253 root.innerHTML = '<div class="class1 class2"></div>';
254
255 goog.testing.dom.assertHtmlContentsMatch(
256 '<div class="class2 class1"></div>', root, true);
257 }
258
259 function testAssertHtmlMismatchWithDifferentAttributeValues() {
260 root.innerHTML = '<div foo="b" bar="a"></div>';
261
262 var e = assertThrows(function() {
263 goog.testing.dom.assertHtmlContentsMatch(
264 '<div foo="a" bar="a"></div>', root, true);
265 });
266 assertContains('Expected attribute foo has a different value', e.message);
267 }
268
269 function testAssertHtmlMatchesWhenStrictAttributesIsFalse() {
270 root.innerHTML = '<div foo="a" bar="b"></div>';
271
272 goog.testing.dom.assertHtmlContentsMatch('<div foo="a"></div>', root);
273 }
274
275 function testAssertHtmlMatchesForMethodsAttribute() {
276 root.innerHTML = '<a methods="get"></a>';
277
278 goog.testing.dom.assertHtmlContentsMatch('<a></a>', root);
279 goog.testing.dom.assertHtmlContentsMatch('<a methods="get"></a>', root);
280 goog.testing.dom.assertHtmlContentsMatch('<a methods="get"></a>', root, true);
281 }
282
283 function testAssertHtmlMatchesForMethodsAttribute() {
284 root.innerHTML = '<input></input>';
285
286 goog.testing.dom.assertHtmlContentsMatch('<input></input>', root);
287 goog.testing.dom.assertHtmlContentsMatch('<input></input>', root, true);
288 }
289
290 function testAssertHtmlMatchesForIdAttribute() {
291 root.innerHTML = '<div id="foo"></div>';
292
293 goog.testing.dom.assertHtmlContentsMatch('<div></div>', root);
294 goog.testing.dom.assertHtmlContentsMatch('<div id="foo"></div>', root);
295 goog.testing.dom.assertHtmlContentsMatch('<div id="foo"></div>', root, true);
296 }
297
298 function testAssertHtmlMatchesWhenIdIsNotSpecified() {
299 root.innerHTML = '<div id="someId"></div>';
300
301 goog.testing.dom.assertHtmlContentsMatch('<div></div>', root);
302 }
303
304 function testAssertHtmlMismatchWhenIdIsNotSpecified() {
305 root.innerHTML = '<div id="someId"></div>';
306
307 var e = assertThrows(function() {
308 goog.testing.dom.assertHtmlContentsMatch('<div></div>', root, true);
309 });
310 assertContains('Unexpected attribute with name id in element', e.message);
311 }
312
313 function testAssertHtmlMismatchWhenIdIsSpecified() {
314 root.innerHTML = '<div></div>';
315
316 var e = assertThrows(function() {
317 goog.testing.dom.assertHtmlContentsMatch('<div id="someId"></div>', root);
318 });
319 assertContains(
320 'Expected to find attribute with name id, in element', e.message);
321
322 e = assertThrows(function() {
323 goog.testing.dom.assertHtmlContentsMatch(
324 '<div id="someId"></div>', root, true);
325 });
326 assertContains(
327 'Expected to find attribute with name id, in element', e.message);
328 }
329
330 function testAssertHtmlMatchesWhenIdIsEmpty() {
331 root.innerHTML = '<div></div>';
332
333 goog.testing.dom.assertHtmlContentsMatch('<div></div>', root);
334 goog.testing.dom.assertHtmlContentsMatch('<div></div>', root, true);
335 }
336
337 function testAssertHtmlMatchesWithDisabledAttribute() {
338 var disabledShortest = '<input disabled="disabled">';
339 var disabledShort = '<input disabled="">';
340 var disabledLong = '<input disabled="disabled">';
341 var enabled = '<input>';
342
343 root.innerHTML = disabledLong;
344 goog.testing.dom.assertHtmlContentsMatch(disabledShortest, root, true);
345 goog.testing.dom.assertHtmlContentsMatch(disabledShort, root, true);
346 goog.testing.dom.assertHtmlContentsMatch(disabledLong, root, true);
347
348
349 var e = assertThrows('Should fail due to mismatched text', function() {
350 goog.testing.dom.assertHtmlContentsMatch(enabled, root, true);
351 });
352 // Attribute value mismatch in IE.
353 // Unexpected attribute error in other browsers.
354 assertContains('disabled', e.message);
355 }
356
357 function testAssertHtmlMatchesWithCheckedAttribute() {
358 var checkedShortest = '<input type="radio" name="x" checked="checked">';
359 var checkedShort = '<input type="radio" name="x" checked="">';
360 var checkedLong = '<input type="radio" name="x" checked="checked">';
361 var unchecked = '<input type="radio" name="x">';
362
363 root.innerHTML = checkedLong;
364 goog.testing.dom.assertHtmlContentsMatch(checkedShortest, root, true);
365 goog.testing.dom.assertHtmlContentsMatch(checkedShort, root, true);
366 goog.testing.dom.assertHtmlContentsMatch(checkedLong, root, true);
367 if (!goog.userAgent.IE) {
368 // CHECKED attribute is ignored because it's among BAD_IE_ATTRIBUTES_.
369 var e = assertThrows('Should fail due to mismatched text', function() {
370 goog.testing.dom.assertHtmlContentsMatch(unchecked, root, true);
371 });
372 assertContains('Unexpected attribute with name checked', e.message);
373 }
374 }
375
376 function testAssertHtmlMatchesWithWhitespace() {
377 goog.dom.removeChildren(root);
378 root.appendChild(goog.dom.createTextNode(' A '));
379 goog.testing.dom.assertHtmlContentsMatch(' A ', root);
380
381 goog.dom.removeChildren(root);
382 root.appendChild(goog.dom.createTextNode(' A '));
383 root.appendChild(goog.dom.createDom(goog.dom.TagName.SPAN, null, ' B '));
384 root.appendChild(goog.dom.createTextNode(' C '));
385 goog.testing.dom.assertHtmlContentsMatch(
386 ' A <span> B </span> C ', root);
387
388 goog.dom.removeChildren(root);
389 root.appendChild(goog.dom.createTextNode(' A'));
390 root.appendChild(goog.dom.createDom(goog.dom.TagName.SPAN, null, ' B'));
391 root.appendChild(goog.dom.createTextNode(' C'));
392 goog.testing.dom.assertHtmlContentsMatch(' A<span> B</span> C', root);
393 }
394
395 function testAssertHtmlMatchesWithWhitespaceAndNesting() {
396 goog.dom.removeChildren(root);
397 root.appendChild(
398 goog.dom.createDom(
399 goog.dom.TagName.DIV, null,
400 goog.dom.createDom(goog.dom.TagName.B, null, ' A '),
401 goog.dom.createDom(goog.dom.TagName.B, null, ' B ')));
402 root.appendChild(
403 goog.dom.createDom(
404 goog.dom.TagName.DIV, null,
405 goog.dom.createDom(goog.dom.TagName.B, null, ' C '),
406 goog.dom.createDom(goog.dom.TagName.B, null, ' D ')));
407
408 goog.testing.dom.assertHtmlContentsMatch(
409 '<div><b> A </b><b> B </b></div>' +
410 '<div><b> C </b><b> D </b></div>',
411 root);
412
413 goog.dom.removeChildren(root);
414 root.appendChild(
415 goog.dom.createDom(
416 goog.dom.TagName.B, null,
417 goog.dom.createDom(
418 goog.dom.TagName.B, null,
419 goog.dom.createDom(goog.dom.TagName.B, null, ' A '))));
420 root.appendChild(goog.dom.createDom(goog.dom.TagName.B, null, ' B '));
421
422 goog.testing.dom.assertHtmlContentsMatch(
423 '<b><b><b> A </b></b></b><b> B </b>', root);
424
425 goog.dom.removeChildren(root);
426 root.appendChild(
427 goog.dom.createDom(
428 goog.dom.TagName.DIV, null,
429 goog.dom.createDom(
430 goog.dom.TagName.B, null,
431 goog.dom.createDom(goog.dom.TagName.B, null, ' A '))));
432 root.appendChild(goog.dom.createDom(goog.dom.TagName.B, null, ' B '));
433
434 goog.testing.dom.assertHtmlContentsMatch(
435 '<div><b><b> A </b></b></div><b> B </b>', root);
436
437 root.innerHTML = '&nbsp;';
438 goog.testing.dom.assertHtmlContentsMatch('&nbsp;', root);
439 }
440
441 function testAssertHtmlMatches() {
442 // Since assertHtmlMatches is based on assertHtmlContentsMatch, we leave the
443 // majority of edge case testing to the above. Here we just do a sanity
444 // check.
445 goog.testing.dom.assertHtmlMatches('<div>abc</div>', '<div>abc</div>');
446 goog.testing.dom.assertHtmlMatches('<div>abc</div>', '<div>abc</div> ');
447 goog.testing.dom.assertHtmlMatches(
448 '<div style="font-size: 1px; color: red">abc</div>',
449 '<div style="color: red; font-size: 1px;;">abc</div>');
450
451 var e = assertThrows('Should fail due to mismatched text', function() {
452 goog.testing.dom.assertHtmlMatches('<div>abc</div>', '<div>abd</div>');
453 });
454 assertContains('Text should match', e.message);
455 }
456
457 function testAssertHtmlMatchesWithSvgAttributes() {
458 goog.testing.dom.assertHtmlMatches(
459 '<svg height="10px"></svg>', '<svg height="10px"></svg>');
460 }
461
462 function testAssertHtmlMatchesWithScriptWithNewLines() {
463 goog.testing.dom.assertHtmlMatches(
464 '<script>var a;\nvar b;</script>', '<script>var a;\nvar b;</script>');
465 }