aboutsummaryrefslogtreecommitdiff
path: root/contexts/data/lib/closure-library/closure/goog/testing/editor/testhelper_test.html
blob: 56ad06e624e74995de37dfc4c82c9fe883acfc1e (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
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<!--
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.
-->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>Closure Unit Tests - goog.testing.editor.TestHelper</title>
  <script src="../../base.js"></script>
  <script>
    goog.require('goog.dom');
    goog.require('goog.dom.TagName');
    goog.require('goog.editor.node');
    goog.require('goog.testing.editor.TestHelper');
    goog.require('goog.testing.jsunit');
  </script>
</head>
<body>
<div id="root"></div>
<div id="root2">Root 2</div>
<script type="text/javascript">
  var root = goog.dom.getElement('root');
  var helper;

  function setUp() {
    goog.dom.removeChildren(root);
    helper = new goog.testing.editor.TestHelper(root);
  }

  function tearDown() {
    helper.dispose();
  }

  function testSetRoot() {
    helper.setRoot(goog.dom.getElement('root2'));
    helper.assertHtmlMatches('Root 2');
  }

  function testSetupEditableElement() {
    helper.setUpEditableElement();
    assertTrue(goog.editor.node.isEditableContainer(root));
  }

  function testTearDownEditableElement() {
    helper.setUpEditableElement();
    assertTrue(goog.editor.node.isEditableContainer(root));

    helper.tearDownEditableElement();
    assertFalse(goog.editor.node.isEditableContainer(root));
  }

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

  function testFindNodeDuplicate() {
    // Test duplicate.
    root.innerHTML = 'c<br>c';
    assertEquals('Should return first duplicate', helper.findTextNode('c'),
        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,
        helper.findTextNode('a').parentNode.tagName);
    assertEquals(goog.dom.TagName.P,
        helper.findTextNode('b').parentNode.tagName);
    assertEquals(goog.dom.TagName.SPAN,
        helper.findTextNode('c').parentNode.tagName);
    assertEquals(goog.dom.TagName.P,
        helper.findTextNode('d').parentNode.tagName);
    assertEquals(goog.dom.TagName.DIV,
        helper.findTextNode('e').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.IE || goog.userAgent.GECKO) {
      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 testAssertHtmlMatches() {
    setUpAssertHtmlMatches();

    helper.assertHtmlMatches('<div 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>' +
        '</div>[[WEBKIT]]WebKitText');
  }

  function testAssertHtmlMismatchText() {
    setUpAssertHtmlMatches();

    var e = assertThrows('Should fail due to mismatched text', function() {
      helper.assertHtmlMatches('<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');
    });
    assertContains('Text should match', e.message);
  }

  function testAssertHtmlMismatchTag() {
    setUpAssertHtmlMatches();

    var e = assertThrows('Should fail due to mismatched tag', function() {
      helper.assertHtmlMatches('<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');
    });
    assertContains('Tag names should match', e.message);
  }

  function testAssertHtmlMismatchStyle() {
    setUpAssertHtmlMatches();

    var e = assertThrows('Should fail due to mismatched style', function() {
      helper.assertHtmlMatches('<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');
    });
    assertContains('Should have same styles', e.message);
  }

  function testAssertHtmlMismatchOptionalText() {
    setUpAssertHtmlMatches();

    var e = assertThrows('Should fail due to mismatched style', function() {
      helper.assertHtmlMatches('<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');
    });
    assertContains('Text should match', e.message);
  }
</script>
</body>
</html>