| 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.editor.TestHelperTest'); |
| 16 |
goog.setTestOnly('goog.testing.editor.TestHelperTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.editor.node'); |
| 21 |
goog.require('goog.testing.TestCase'); |
| 22 |
goog.require('goog.testing.editor.TestHelper'); |
| 23 |
goog.require('goog.testing.jsunit'); |
| 24 |
goog.require('goog.userAgent'); |
| 25 |
|
| 26 |
var root; |
| 27 |
var helper; |
| 28 |
|
| 29 |
function setUp() { |
| 30 |
// TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts). |
| 31 |
goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false; |
| 32 |
|
| 33 |
root = goog.dom.getElement('root'); |
| 34 |
goog.dom.removeChildren(root); |
| 35 |
helper = new goog.testing.editor.TestHelper(root); |
| 36 |
} |
| 37 |
|
| 38 |
function tearDown() { |
| 39 |
helper.dispose(); |
| 40 |
} |
| 41 |
|
| 42 |
function testSetRoot() { |
| 43 |
helper.setRoot(goog.dom.getElement('root2')); |
| 44 |
helper.assertHtmlMatches('Root 2'); |
| 45 |
} |
| 46 |
|
| 47 |
function testSetupEditableElement() { |
| 48 |
helper.setUpEditableElement(); |
| 49 |
assertTrue(goog.editor.node.isEditableContainer(root)); |
| 50 |
} |
| 51 |
|
| 52 |
function testTearDownEditableElement() { |
| 53 |
helper.setUpEditableElement(); |
| 54 |
assertTrue(goog.editor.node.isEditableContainer(root)); |
| 55 |
|
| 56 |
helper.tearDownEditableElement(); |
| 57 |
assertFalse(goog.editor.node.isEditableContainer(root)); |
| 58 |
} |
| 59 |
|
| 60 |
function testFindNode() { |
| 61 |
// Test the easiest case. |
| 62 |
root.innerHTML = 'a<br>b'; |
| 63 |
assertEquals(helper.findTextNode('a'), root.firstChild); |
| 64 |
assertEquals(helper.findTextNode('b'), root.lastChild); |
| 65 |
assertNull(helper.findTextNode('c')); |
| 66 |
} |
| 67 |
|
| 68 |
function testFindNodeDuplicate() { |
| 69 |
// Test duplicate. |
| 70 |
root.innerHTML = 'c<br>c'; |
| 71 |
assertEquals( |
| 72 |
'Should return first duplicate', helper.findTextNode('c'), |
| 73 |
root.firstChild); |
| 74 |
} |
| 75 |
|
| 76 |
function findNodeWithHierarchy() { |
| 77 |
// Test a more complicated hierarchy. |
| 78 |
root.innerHTML = '<div>a<p>b<span>c</span>d</p>e</div>'; |
| 79 |
assertEquals( |
| 80 |
String(goog.dom.TagName.DIV), |
| 81 |
helper.findTextNode('a').parentNode.tagName); |
| 82 |
assertEquals( |
| 83 |
String(goog.dom.TagName.P), helper.findTextNode('b').parentNode.tagName); |
| 84 |
assertEquals( |
| 85 |
String(goog.dom.TagName.SPAN), |
| 86 |
helper.findTextNode('c').parentNode.tagName); |
| 87 |
assertEquals( |
| 88 |
String(goog.dom.TagName.P), helper.findTextNode('d').parentNode.tagName); |
| 89 |
assertEquals( |
| 90 |
String(goog.dom.TagName.DIV), |
| 91 |
helper.findTextNode('e').parentNode.tagName); |
| 92 |
} |
| 93 |
|
| 94 |
function setUpAssertHtmlMatches() { |
| 95 |
var tag1, tag2; |
| 96 |
if (goog.userAgent.EDGE_OR_IE) { |
| 97 |
tag1 = goog.dom.TagName.DIV; |
| 98 |
} else if (goog.userAgent.WEBKIT) { |
| 99 |
tag1 = goog.dom.TagName.P; |
| 100 |
tag2 = goog.dom.TagName.BR; |
| 101 |
} else if (goog.userAgent.GECKO) { |
| 102 |
tag1 = goog.dom.TagName.SPAN; |
| 103 |
tag2 = goog.dom.TagName.BR; |
| 104 |
} |
| 105 |
|
| 106 |
var parent = goog.dom.createDom(goog.dom.TagName.DIV); |
| 107 |
root.appendChild(parent); |
| 108 |
parent.style.fontSize = '2em'; |
| 109 |
parent.style.display = 'none'; |
| 110 |
if (goog.userAgent.EDGE_OR_IE || goog.userAgent.GECKO) { |
| 111 |
parent.appendChild(goog.dom.createTextNode('NonWebKitText')); |
| 112 |
} |
| 113 |
|
| 114 |
if (tag1) { |
| 115 |
var e1 = goog.dom.createDom(tag1); |
| 116 |
parent.appendChild(e1); |
| 117 |
parent = e1; |
| 118 |
} |
| 119 |
if (tag2) { |
| 120 |
parent.appendChild(goog.dom.createDom(tag2)); |
| 121 |
} |
| 122 |
parent.appendChild(goog.dom.createTextNode('Text')); |
| 123 |
if (goog.userAgent.WEBKIT) { |
| 124 |
root.firstChild.appendChild(goog.dom.createTextNode('WebKitText')); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
function testAssertHtmlMatches() { |
| 129 |
setUpAssertHtmlMatches(); |
| 130 |
|
| 131 |
helper.assertHtmlMatches( |
| 132 |
'<div style="display: none; font-size: 2em">' + |
| 133 |
'[[IE EDGE GECKO]]NonWebKitText<div class="IE EDGE"><p class="WEBKIT">' + |
| 134 |
'<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' + |
| 135 |
'</div>[[WEBKIT]]WebKitText'); |
| 136 |
} |
| 137 |
|
| 138 |
function testAssertHtmlMismatchText() { |
| 139 |
setUpAssertHtmlMatches(); |
| 140 |
|
| 141 |
var e = assertThrows('Should fail due to mismatched text', function() { |
| 142 |
helper.assertHtmlMatches( |
| 143 |
'<div style="display: none; font-size: 2em">' + |
| 144 |
'[[IE GECKO]]NonWebKitText<div class="IE"><p class="WEBKIT">' + |
| 145 |
'<span class="GECKO"><br class="GECKO WEBKIT">Bad</span></p></div>' + |
| 146 |
'</div>[[WEBKIT]]Extra'); |
| 147 |
}); |
| 148 |
assertContains('Text should match', e.message); |
| 149 |
} |
| 150 |
|
| 151 |
function testAssertHtmlMismatchTag() { |
| 152 |
setUpAssertHtmlMatches(); |
| 153 |
|
| 154 |
var e = assertThrows('Should fail due to mismatched tag', function() { |
| 155 |
helper.assertHtmlMatches( |
| 156 |
'<span style="display: none; font-size: 2em">[[IE EDGE GECKO]]' + |
| 157 |
'NonWebKitText<div class="IE EDGE"><p class="WEBKIT">' + |
| 158 |
'<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' + |
| 159 |
'</span>[[WEBKIT]]Extra'); |
| 160 |
}); |
| 161 |
assertContains('Tag names should match', e.message); |
| 162 |
} |
| 163 |
|
| 164 |
function testAssertHtmlMismatchStyle() { |
| 165 |
setUpAssertHtmlMatches(); |
| 166 |
|
| 167 |
var e = assertThrows('Should fail due to mismatched style', function() { |
| 168 |
helper.assertHtmlMatches( |
| 169 |
'<div style="display: none; font-size: 3em">[[IE EDGE GECKO]]' + |
| 170 |
'NonWebKitText<div class="IE EDGE"><p class="WEBKIT">' + |
| 171 |
'<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' + |
| 172 |
'</div>[[WEBKIT]]Extra'); |
| 173 |
}); |
| 174 |
assertContains('Should have same styles', e.message); |
| 175 |
} |
| 176 |
|
| 177 |
function testAssertHtmlMismatchOptionalText() { |
| 178 |
setUpAssertHtmlMatches(); |
| 179 |
|
| 180 |
var e = assertThrows('Should fail due to mismatched style', function() { |
| 181 |
helper.assertHtmlMatches( |
| 182 |
'<div style="display: none; font-size: 2em">' + |
| 183 |
'[[IE EDGE GECKO]]Bad<div class="IE EDGE"><p class="WEBKIT">' + |
| 184 |
'<span class="GECKO"><br class="GECKO WEBKIT">Text</span></p></div>' + |
| 185 |
'</div>[[WEBKIT]]Bad'); |
| 186 |
}); |
| 187 |
assertContains('Text should match', e.message); |
| 188 |
} |