aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/dom_test.html
blob: 0d595fe606cbd47a5bb36d3266b664b77d06e121 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<!DOCTYPE html>
<html lang="en" dir="ltr">
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<!--
This test has not yet been updated to run on IE8. See http://b/hotlist?id=36311
-->
<!--meta http-equiv="X-UA-Compatible" content="IE=edge"-->
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7">
  <title>Closure Unit Tests - goog.testing.dom</title>
  <script src="../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.dom.TagName');
    goog.require('goog.testing.dom');
    goog.require('goog.testing.jsunit');
    goog.require('goog.userAgent');
  </script>
</head>
<body>
<div id="root"></div>
<script type="text/javascript">
  var root = goog.dom.getElement('root');

  function setUp() {
    root.innerHTML = '';
  }

  function testFindNode() {
    // Test the easiest case.
    root.innerHTML = 'a<br>b';
    assertEquals(goog.testing.dom.findTextNode('a', root), root.firstChild);
    assertEquals(goog.testing.dom.findTextNode('b', root), root.lastChild);
    assertNull(goog.testing.dom.findTextNode('c', root));
  }

  function testFindNodeDuplicate() {
    // Test duplicate.
    root.innerHTML = 'c<br>c';
    assertEquals('Should return first duplicate',
        goog.testing.dom.findTextNode('c', root), root.firstChild);
  }

  function findNodeWithHierarchy() {
    // Test a more complicated hierarchy.
    root.innerHTML = '<div>a<p>b<span>c</span>d</p>e</div>';
    assertEquals(goog.dom.TagName.DIV,
        goog.testing.dom.findTextNode('a', root).parentNode.tagName);
    assertEquals(goog.dom.TagName.P,
        goog.testing.dom.findTextNode('b', root).parentNode.tagName);
    assertEquals(goog.dom.TagName.SPAN,
        goog.testing.dom.findTextNode('c', root).parentNode.tagName);
    assertEquals(goog.dom.TagName.P,
        goog.testing.dom.findTextNode('d', root).parentNode.tagName);
    assertEquals(goog.dom.TagName.DIV,
        goog.testing.dom.findTextNode('e', root).parentNode.tagName);
  }

  function setUpAssertHtmlMatches() {
    var tag1, tag2;
    if (goog.userAgent.IE) {
      tag1 = goog.dom.TagName.DIV;
    } else if (goog.userAgent.WEBKIT) {
      tag1 = goog.dom.TagName.P;
      tag2 = goog.dom.TagName.BR;
    } else if (goog.userAgent.GECKO) {
      tag1 = goog.dom.TagName.SPAN;
      tag2 = goog.dom.TagName.BR;
    }

    var parent = goog.dom.createDom(goog.dom.TagName.DIV);
    root.appendChild(parent);
    parent.style.fontSize = '2em';
    parent.style.display = 'none';
    if (!goog.userAgent.WEBKIT) {
      parent.appendChild(goog.dom.createTextNode('NonWebKitText'));
    }

    if (tag1) {
      var e1 = goog.dom.createDom(tag1);
      parent.appendChild(e1);
      parent = e1;
    }
    if (tag2) {
      parent.appendChild(goog.dom.createDom(tag2));
    }
    parent.appendChild(goog.dom.createTextNode('Text'));
    if (goog.userAgent.WEBKIT) {
      root.firstChild.appendChild(goog.dom.createTextNode('WebKitText'));
    }
  }

  function testAssertHtmlContentsMatch() {
    setUpAssertHtmlMatches();

    goog.testing.dom.assertHtmlContentsMatch(
        '<div style="display: none; font-size: 2em">' +
        '[[!WEBKIT]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
        '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
        '</div>[[WEBKIT]]WebKitText',
        root);
  }

  function testAssertHtmlMismatchText() {
    setUpAssertHtmlMatches();

    var e = assertThrows('Should fail due to mismatched text', function() {
      goog.testing.dom.assertHtmlContentsMatch(
          '<div style="display: none; font-size: 2em">' +
          '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
          '<span class="GECKO"><br class="GECKO WEBKIT">Bad</span></p></div>' +
          '</div>[[WEBKIT]]Extra',
          root);
    });
    assertContains('Text should match', e.message);
  }

  function testAssertHtmlMismatchTag() {
    setUpAssertHtmlMatches();

    var e = assertThrows('Should fail due to mismatched tag', function() {
      goog.testing.dom.assertHtmlContentsMatch(
          '<span style="display: none; font-size: 2em">' +
          '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
          '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
          '</span>[[WEBKIT]]Extra',
          root);
    });
    assertContains('Tag names should match', e.message);
  }

  function testAssertHtmlMismatchStyle() {
    setUpAssertHtmlMatches();

    var e = assertThrows('Should fail due to mismatched style', function() {
      goog.testing.dom.assertHtmlContentsMatch(
          '<div style="display: none; font-size: 3em">' +
          '[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' +
          '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
          '</div>[[WEBKIT]]Extra',
          root);
    });
    assertContains('Should have same styles', e.message);
  }

  function testAssertHtmlMismatchOptionalText() {
    setUpAssertHtmlMatches();

    var e = assertThrows('Should fail due to mismatched text', function() {
      goog.testing.dom.assertHtmlContentsMatch(
          '<div style="display: none; font-size: 2em">' +
          '[[IE GECKO]]Bad<div class="IE"><p class="WEBKIT">' +
          '<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' +
          '</div>[[WEBKIT]]Bad',
          root);
    });
    assertContains('Text should match', e.message);
  }

  function testAssertHtmlMismatchExtraActualAfterText() {
    root.innerHTML = '<div>abc</div>def';

    var e = assertThrows('Should fail due to extra actual nodes', function() {
      goog.testing.dom.assertHtmlContentsMatch('<div>abc</div>', root);
    });
    assertContains('Finished expected HTML before', e.message);
  }

  function testAssertHtmlMismatchExtraActualAfterElement() {
    root.innerHTML = '<br>def';

    var e = assertThrows('Should fail due to extra actual nodes', function() {
      goog.testing.dom.assertHtmlContentsMatch('<br>', root);
    });
    assertContains('Finished expected HTML before', e.message);
  }

  function testAssertHtmlMatchesWithSplitTextNodes() {
    root.appendChild(goog.dom.createTextNode('1'));
    root.appendChild(goog.dom.createTextNode('2'));
    root.appendChild(goog.dom.createTextNode('3'));
    goog.testing.dom.assertHtmlContentsMatch('123', root);
  }

  function testAssertHtmlMatchesWithDifferentlyOrderedAttributes() {
    root.innerHTML = '<div foo="a" bar="b" class="className"></div>';

    goog.testing.dom.assertHtmlContentsMatch(
        '<div bar="b" class="className" foo="a"></div>', root, true);
  }

  function testAssertHtmlMismatchWithDifferentNumberOfAttributes() {
    root.innerHTML = '<div foo="a" bar="b"></div>';

    var e = assertThrows(function() {
      goog.testing.dom.assertHtmlContentsMatch(
          '<div foo="a"></div>', root, true);
    });
    assertContains('Unexpected attribute with name bar in element', e.message);
  }

  function testAssertHtmlMismatchWithDifferentAttributeNames() {
    root.innerHTML = '<div foo="a" bar="b"></div>';

    var e = assertThrows(function() {
      goog.testing.dom.assertHtmlContentsMatch(
          '<div foo="a" baz="b"></div>', root, true);
    });
    assertContains('Expected to find attribute with name baz', e.message);
  }

  function testAssertHtmlMismatchWithDifferentClassNames() {
    root.innerHTML = '<div class="className1"></div>';

    var e = assertThrows(function() {
      goog.testing.dom.assertHtmlContentsMatch(
          '<div class="className2"></div>', root, true);
    });
    assertContains(
        'Expected class was: className2, but actual class was: className1',
        e.message);
  }

  function testAssertHtmlMatchesWithClassNameAndUserAgentSpecified() {
    root.innerHTML =
        '<div>' + (goog.userAgent.GECKO ? '<div class="foo"></div>' : '') +
        '</div>';

    goog.testing.dom.assertHtmlContentsMatch(
        '<div><div class="foo GECKO"></div></div>',
        root, true);
  }

  function testAssertHtmlMatchesWithClassesInDifferentOrder() {
    root.innerHTML = '<div class="class1 class2"></div>';

    goog.testing.dom.assertHtmlContentsMatch(
        '<div class="class2 class1"></div>', root, true);
  }

  function testAssertHtmlMismatchWithDifferentAttributeValues() {
    root.innerHTML = '<div foo="b" bar="a"></div>';

    var e = assertThrows(function() {
      goog.testing.dom.assertHtmlContentsMatch(
          '<div foo="a" bar="a"></div>', root, true);
    });
    assertContains('Expected attribute foo has a different value', e.message);
  }

  function testAssertHtmlMatchesWhenStrictAttributesIsFalse() {
    root.innerHTML = '<div foo="a" bar="b"></div>';

    goog.testing.dom.assertHtmlContentsMatch('<div foo="a"></div>', root);
  }

  function testAssertHtmlMatchesForMethodsAttribute() {
    root.innerHTML = '<a methods="get"></a>';

    goog.testing.dom.assertHtmlContentsMatch('<a></a>', root);
    goog.testing.dom.assertHtmlContentsMatch('<a methods="get"></a>', root);
    goog.testing.dom.assertHtmlContentsMatch('<a methods="get"></a>', root,
        true);
  }

  function testAssertHtmlMatchesForMethodsAttribute() {
    root.innerHTML = '<input></input>';

    goog.testing.dom.assertHtmlContentsMatch('<input></input>', root);
    goog.testing.dom.assertHtmlContentsMatch('<input></input>', root, true);
  }

  function testAssertHtmlMatchesForIdAttribute() {
    root.innerHTML = '<div id="foo"></div>';

    goog.testing.dom.assertHtmlContentsMatch('<div></div>', root);
    goog.testing.dom.assertHtmlContentsMatch('<div id="foo"></div>', root);
    goog.testing.dom.assertHtmlContentsMatch('<div id="foo"></div>', root,
        true);
  }

  function testAssertHtmlMatchesWhenIdIsNotSpecified() {
    root.innerHTML = '<div id="someId"></div>';

    goog.testing.dom.assertHtmlContentsMatch('<div></div>', root);
  }

  function testAssertHtmlMismatchWhenIdIsNotSpecified() {
    root.innerHTML = '<div id="someId"></div>';

    var e = assertThrows(function() {
      goog.testing.dom.assertHtmlContentsMatch('<div></div>', root, true);
    });
    assertContains('Unexpected attribute with name id in element', e.message);
  }

  function testAssertHtmlMismatchWhenIdIsSpecified() {
    root.innerHTML = '<div></div>';

    var e = assertThrows(function() {
      goog.testing.dom.assertHtmlContentsMatch('<div id="someId"></div>', root);
    });
    assertContains('Expected to find attribute with name id, in element',
        e.message);

    e = assertThrows(function() {
      goog.testing.dom.assertHtmlContentsMatch('<div id="someId"></div>', root,
          true);
    });
    assertContains('Expected to find attribute with name id, in element',
        e.message);
  }

  function testAssertHtmlMatchesWhenIdIsEmpty() {
    root.innerHTML = '<div></div>';

    goog.testing.dom.assertHtmlContentsMatch('<div></div>', root);
    goog.testing.dom.assertHtmlContentsMatch('<div></div>', root, true);
  }

  function testAssertHtmlMatchesWithDisabledAttribute() {
    var disabledShortest = '<input disabled>';
    var disabledShort = '<input DISABLED="">';
    var disabledLong = '<input disabled="disabled">';
    var enabled = '<input>';

    root.innerHTML = disabledLong;
    goog.testing.dom.assertHtmlContentsMatch(disabledShortest, root, true);
    goog.testing.dom.assertHtmlContentsMatch(disabledShort, root, true);
    goog.testing.dom.assertHtmlContentsMatch(disabledLong, root, true);


    var e = assertThrows('Should fail due to mismatched text', function() {
      goog.testing.dom.assertHtmlContentsMatch(enabled, root, true);
    });
    // Attribute value mismatch in IE.
    // Unexpected attribute error in other browsers.
    assertContains('disabled', e.message);
  }

  function testAssertHtmlMatchesWithCheckedAttribute() {
    var checkedShortest = '<input type="radio" name="x" checked>';
    var checkedShort = '<input type="radio" name="x" CHECKED="">';
    var checkedLong = '<input type="radio" name="x" checked="checked">';
    var unchecked = '<input type="radio" name="x">';

    root.innerHTML = checkedLong;
    goog.testing.dom.assertHtmlContentsMatch(checkedShortest, root, true);
    goog.testing.dom.assertHtmlContentsMatch(checkedShort, root, true);
    goog.testing.dom.assertHtmlContentsMatch(checkedLong, root, true);
    if (!goog.userAgent.IE) {
      // CHECKED attribute is ignored because it's among BAD_IE_ATTRIBUTES_.
      var e = assertThrows('Should fail due to mismatched text', function() {
        goog.testing.dom.assertHtmlContentsMatch(unchecked, root, true);
      });
      assertContains('Unexpected attribute with name checked', e.message);
    }
  }

  function testAssertHtmlMatchesWithWhitespace() {
    root.innerHTML = '';
    root.appendChild(goog.dom.createTextNode('  A  '));
    goog.testing.dom.assertHtmlContentsMatch('  A  ', root);

    root.innerHTML = '';
    root.appendChild(goog.dom.createTextNode('  A  '));
    root.appendChild(goog.dom.createDom('span', null, '  B  '));
    root.appendChild(goog.dom.createTextNode('  C  '));
    goog.testing.dom.assertHtmlContentsMatch(
        '  A  <span>  B  </span>  C  ', root);

    root.innerHTML = '';
    root.appendChild(goog.dom.createTextNode('  A'));
    root.appendChild(goog.dom.createDom('span', null, '  B'));
    root.appendChild(goog.dom.createTextNode('  C'));
    goog.testing.dom.assertHtmlContentsMatch(
        '  A<span>  B</span>  C', root);
  }

  function testAssertHtmlMatchesWithWhitespaceAndNesting() {
    root.innerHTML = '';
    root.appendChild(goog.dom.createDom('div', null,
        goog.dom.createDom('b', null, '  A  '),
        goog.dom.createDom('b', null, '  B  ')));
    root.appendChild(goog.dom.createDom('div', null,
        goog.dom.createDom('b', null, '  C  '),
        goog.dom.createDom('b', null, '  D  ')));

    goog.testing.dom.assertHtmlContentsMatch(
        '<div><b>  A  </b><b>  B  </b></div>' +
        '<div><b>  C  </b><b>  D  </b></div>', root);

    root.innerHTML = '';
    root.appendChild(goog.dom.createDom('b', null,
        goog.dom.createDom('b', null,
            goog.dom.createDom('b', null, '  A  '))));
    root.appendChild(goog.dom.createDom('b', null, '  B  '));

    goog.testing.dom.assertHtmlContentsMatch(
        '<b><b><b>  A  </b></b></b><b>  B  </b>', root);

    root.innerHTML = '';
    root.appendChild(goog.dom.createDom('div', null,
        goog.dom.createDom('b', null,
            goog.dom.createDom('b', null, '  A  '))));
    root.appendChild(goog.dom.createDom('b', null, '  B  '));

    goog.testing.dom.assertHtmlContentsMatch(
        '<div><b><b>  A  </b></b></div><b>  B  </b>', root);

    root.innerHTML = '&nbsp;';
    goog.testing.dom.assertHtmlContentsMatch(
        '&nbsp;', root);
  }

  function testAssertHtmlMatches() {
    // Since assertHtmlMatches is based on assertHtmlContentsMatch, we leave the
    // majority of edge case testing to the above.  Here we just do a sanity
    // check.
    goog.testing.dom.assertHtmlMatches('<div>abc</div>', '<div>abc</div>');
    goog.testing.dom.assertHtmlMatches('<div>abc</div>', '<div>abc</div> ');
    goog.testing.dom.assertHtmlMatches(
        '<div style="font-size: 1px; color: red">abc</div>',
        '<div style="color: red;  font-size: 1px;;">abc</div>');

    var e = assertThrows('Should fail due to mismatched text', function() {
      goog.testing.dom.assertHtmlMatches('<div>abc</div>', '<div>abd</div>');
    });
    assertContains('Text should match', e.message);
  }
</script>
</body>
</html>