| 1 |
// Copyright 2009 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.domTest'); |
| 16 |
goog.setTestOnly('goog.testing.editor.domTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.functions'); |
| 21 |
goog.require('goog.testing.TestCase'); |
| 22 |
goog.require('goog.testing.editor.dom'); |
| 23 |
goog.require('goog.testing.jsunit'); |
| 24 |
|
| 25 |
var root; |
| 26 |
var parentNode, childNode1, childNode2, childNode3; |
| 27 |
var first, middle, last; |
| 28 |
|
| 29 |
function setUpPage() { |
| 30 |
// TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts). |
| 31 |
goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false; |
| 32 |
|
| 33 |
root = goog.dom.getElement('root'); |
| 34 |
} |
| 35 |
|
| 36 |
function tearDown() { |
| 37 |
goog.dom.removeChildren(root); |
| 38 |
} |
| 39 |
|
| 40 |
function setUpNonEmptyTests() { |
| 41 |
childNode1 = goog.dom.createElement(goog.dom.TagName.DIV); |
| 42 |
childNode2 = goog.dom.createElement(goog.dom.TagName.DIV); |
| 43 |
childNode3 = goog.dom.createElement(goog.dom.TagName.DIV); |
| 44 |
parentNode = goog.dom.createDom( |
| 45 |
goog.dom.TagName.DIV, null, childNode1, childNode2, childNode3); |
| 46 |
goog.dom.appendChild(root, parentNode); |
| 47 |
|
| 48 |
childNode1.appendChild(goog.dom.createTextNode('One')); |
| 49 |
childNode1.appendChild(goog.dom.createTextNode('')); |
| 50 |
|
| 51 |
childNode2.appendChild(goog.dom.createElement(goog.dom.TagName.BR)); |
| 52 |
childNode2.appendChild(goog.dom.createTextNode('TwoA')); |
| 53 |
childNode2.appendChild(goog.dom.createTextNode('TwoB')); |
| 54 |
childNode2.appendChild(goog.dom.createElement(goog.dom.TagName.BR)); |
| 55 |
|
| 56 |
childNode3.appendChild(goog.dom.createTextNode('')); |
| 57 |
childNode3.appendChild(goog.dom.createTextNode('Three')); |
| 58 |
} |
| 59 |
|
| 60 |
function testGetNextNonEmptyTextNode() { |
| 61 |
setUpNonEmptyTests(); |
| 62 |
|
| 63 |
var nodeOne = goog.testing.editor.dom.getNextNonEmptyTextNode(parentNode); |
| 64 |
assertEquals( |
| 65 |
'Should have found the next non-empty text node', 'One', |
| 66 |
nodeOne.nodeValue); |
| 67 |
var nodeTwoA = goog.testing.editor.dom.getNextNonEmptyTextNode(nodeOne); |
| 68 |
assertEquals( |
| 69 |
'Should have found the next non-empty text node', 'TwoA', |
| 70 |
nodeTwoA.nodeValue); |
| 71 |
var nodeTwoB = goog.testing.editor.dom.getNextNonEmptyTextNode(nodeTwoA); |
| 72 |
assertEquals( |
| 73 |
'Should have found the next non-empty text node', 'TwoB', |
| 74 |
nodeTwoB.nodeValue); |
| 75 |
var nodeThree = goog.testing.editor.dom.getNextNonEmptyTextNode(nodeTwoB); |
| 76 |
assertEquals( |
| 77 |
'Should have found the next non-empty text node', 'Three', |
| 78 |
nodeThree.nodeValue); |
| 79 |
var nodeNull = |
| 80 |
goog.testing.editor.dom.getNextNonEmptyTextNode(nodeThree, parentNode); |
| 81 |
assertNull('Should not have found any non-empty text node', nodeNull); |
| 82 |
|
| 83 |
var nodeStop = |
| 84 |
goog.testing.editor.dom.getNextNonEmptyTextNode(nodeOne, childNode1); |
| 85 |
assertNull('Should have stopped before finding a node', nodeStop); |
| 86 |
|
| 87 |
var nodeBeforeStop = |
| 88 |
goog.testing.editor.dom.getNextNonEmptyTextNode(nodeTwoA, childNode2); |
| 89 |
assertEquals( |
| 90 |
'Should have found the next non-empty text node', 'TwoB', |
| 91 |
nodeBeforeStop.nodeValue); |
| 92 |
} |
| 93 |
|
| 94 |
function testGetPreviousNonEmptyTextNode() { |
| 95 |
setUpNonEmptyTests(); |
| 96 |
|
| 97 |
var nodeThree = |
| 98 |
goog.testing.editor.dom.getPreviousNonEmptyTextNode(parentNode); |
| 99 |
assertEquals( |
| 100 |
'Should have found the previous non-empty text node', 'Three', |
| 101 |
nodeThree.nodeValue); |
| 102 |
var nodeTwoB = goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeThree); |
| 103 |
assertEquals( |
| 104 |
'Should have found the previous non-empty text node', 'TwoB', |
| 105 |
nodeTwoB.nodeValue); |
| 106 |
var nodeTwoA = goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeTwoB); |
| 107 |
assertEquals( |
| 108 |
'Should have found the previous non-empty text node', 'TwoA', |
| 109 |
nodeTwoA.nodeValue); |
| 110 |
var nodeOne = goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeTwoA); |
| 111 |
assertEquals( |
| 112 |
'Should have found the previous non-empty text node', 'One', |
| 113 |
nodeOne.nodeValue); |
| 114 |
var nodeNull = |
| 115 |
goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeOne, parentNode); |
| 116 |
assertNull('Should not have found any non-empty text node', nodeNull); |
| 117 |
|
| 118 |
var nodeStop = goog.testing.editor.dom.getPreviousNonEmptyTextNode( |
| 119 |
nodeThree, childNode3); |
| 120 |
assertNull('Should have stopped before finding a node', nodeStop); |
| 121 |
|
| 122 |
var nodeBeforeStop = |
| 123 |
goog.testing.editor.dom.getPreviousNonEmptyTextNode(nodeTwoB, childNode2); |
| 124 |
assertEquals( |
| 125 |
'Should have found the previous non-empty text node', 'TwoA', |
| 126 |
nodeBeforeStop.nodeValue); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
function setUpAssertRangeBetweenText() { |
| 131 |
// Create the following structure: <[01]><[]><[23]> |
| 132 |
// Where <> delimits spans, [] delimits text nodes, 01 and 23 are text. |
| 133 |
// We will test all 10 positions in between 0 and 2. All should pass. |
| 134 |
first = goog.dom.createDom(goog.dom.TagName.SPAN, null, '01'); |
| 135 |
middle = goog.dom.createElement(goog.dom.TagName.SPAN); |
| 136 |
var emptyTextNode = goog.dom.createTextNode(''); |
| 137 |
goog.dom.appendChild(middle, emptyTextNode); |
| 138 |
last = goog.dom.createDom(goog.dom.TagName.SPAN, null, '23'); |
| 139 |
goog.dom.appendChild(root, first); |
| 140 |
goog.dom.appendChild(root, middle); |
| 141 |
goog.dom.appendChild(root, last); |
| 142 |
} |
| 143 |
|
| 144 |
function createFakeRange(startNode, startOffset, opt_endNode, opt_endOffset) { |
| 145 |
opt_endNode = opt_endNode || startNode; |
| 146 |
opt_endOffset = opt_endOffset || startOffset; |
| 147 |
return { |
| 148 |
getStartNode: goog.functions.constant(startNode), |
| 149 |
getStartOffset: goog.functions.constant(startOffset), |
| 150 |
getEndNode: goog.functions.constant(opt_endNode), |
| 151 |
getEndOffset: goog.functions.constant(opt_endOffset) |
| 152 |
}; |
| 153 |
} |
| 154 |
|
| 155 |
function testAssertRangeBetweenText0() { |
| 156 |
setUpAssertRangeBetweenText(); |
| 157 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 158 |
'0', '1', createFakeRange(first.firstChild, 1)); |
| 159 |
} |
| 160 |
|
| 161 |
function testAssertRangeBetweenText1() { |
| 162 |
setUpAssertRangeBetweenText(); |
| 163 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 164 |
'1', '2', createFakeRange(first.firstChild, 2)); |
| 165 |
} |
| 166 |
|
| 167 |
function testAssertRangeBetweenText2() { |
| 168 |
setUpAssertRangeBetweenText(); |
| 169 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 170 |
'1', '2', createFakeRange(first, 1)); |
| 171 |
} |
| 172 |
|
| 173 |
function testAssertRangeBetweenText3() { |
| 174 |
setUpAssertRangeBetweenText(); |
| 175 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 176 |
'1', '2', createFakeRange(root, 1)); |
| 177 |
} |
| 178 |
|
| 179 |
function testAssertRangeBetweenText4() { |
| 180 |
setUpAssertRangeBetweenText(); |
| 181 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 182 |
'1', '2', createFakeRange(middle, 0)); |
| 183 |
} |
| 184 |
|
| 185 |
function testAssertRangeBetweenText5() { |
| 186 |
setUpAssertRangeBetweenText(); |
| 187 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 188 |
'1', '2', createFakeRange(middle.firstChild, 0)); |
| 189 |
} |
| 190 |
|
| 191 |
function testAssertRangeBetweenText6() { |
| 192 |
setUpAssertRangeBetweenText(); |
| 193 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 194 |
'1', '2', createFakeRange(middle, 1)); |
| 195 |
} |
| 196 |
|
| 197 |
function testAssertRangeBetweenText7() { |
| 198 |
setUpAssertRangeBetweenText(); |
| 199 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 200 |
'1', '2', createFakeRange(root, 2)); |
| 201 |
} |
| 202 |
|
| 203 |
function testAssertRangeBetweenText8() { |
| 204 |
setUpAssertRangeBetweenText(); |
| 205 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 206 |
'1', '2', createFakeRange(last, 0)); |
| 207 |
} |
| 208 |
|
| 209 |
function testAssertRangeBetweenText9() { |
| 210 |
setUpAssertRangeBetweenText(); |
| 211 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 212 |
'1', '2', createFakeRange(last.firstChild, 0)); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
function testAssertRangeBetweenTextBefore() { |
| 217 |
setUpAssertRangeBetweenText(); |
| 218 |
// Test that it works when the cursor is at the beginning of all text. |
| 219 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 220 |
'', '0', createFakeRange(first.firstChild, 0), |
| 221 |
root); // Restrict to root div so it won't find /n's and script. |
| 222 |
} |
| 223 |
|
| 224 |
function testAssertRangeBetweenTextAfter() { |
| 225 |
setUpAssertRangeBetweenText(); |
| 226 |
// Test that it works when the cursor is at the end of all text. |
| 227 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 228 |
'3', '', createFakeRange(last.firstChild, 2), |
| 229 |
root); // Restrict to root div so it won't find /n's and script. |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
function testAssertRangeBetweenTextFail1() { |
| 234 |
setUpAssertRangeBetweenText(); |
| 235 |
var e = assertThrows('assertRangeBetweenText should have failed', function() { |
| 236 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 237 |
'1', '3', createFakeRange(first.firstChild, 2)); |
| 238 |
}); |
| 239 |
assertContains( |
| 240 |
'Assert reason incorrect', 'Expected <3> after range but found <23>', |
| 241 |
e.message); |
| 242 |
} |
| 243 |
|
| 244 |
function testAssertRangeBetweenTextFail2() { |
| 245 |
setUpAssertRangeBetweenText(); |
| 246 |
var e = assertThrows('assertRangeBetweenText should have failed', function() { |
| 247 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 248 |
'1', '2', createFakeRange(first.firstChild, 2, last.firstChild, 1)); |
| 249 |
}); |
| 250 |
assertContains( |
| 251 |
'Assert reason incorrect', 'Expected <2> after range but found <3>', |
| 252 |
e.message); |
| 253 |
} |
| 254 |
|
| 255 |
function testAssertRangeBetweenTextBeforeFail() { |
| 256 |
setUpAssertRangeBetweenText(); |
| 257 |
// Test that it gives the right message when the cursor is at the beginning |
| 258 |
// of all text but you're expecting something before it. |
| 259 |
var e = assertThrows('assertRangeBetweenText should have failed', function() { |
| 260 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 261 |
'-1', '0', createFakeRange(first.firstChild, 0), |
| 262 |
root); // Restrict to root div so it won't find /n's and script. |
| 263 |
}); |
| 264 |
assertContains( |
| 265 |
'Assert reason incorrect', 'Expected <-1> before range but found nothing', |
| 266 |
e.message); |
| 267 |
} |
| 268 |
|
| 269 |
function testAssertRangeBetweenTextAfterFail() { |
| 270 |
setUpAssertRangeBetweenText(); |
| 271 |
// Test that it gives the right message when the cursor is at the end |
| 272 |
// of all text but you're expecting something after it. |
| 273 |
var e = assertThrows('assertRangeBetweenText should have failed', function() { |
| 274 |
goog.testing.editor.dom.assertRangeBetweenText( |
| 275 |
'3', '4', createFakeRange(last.firstChild, 2), |
| 276 |
root); // Restrict to root div so it won't find /n's and script. |
| 277 |
}); |
| 278 |
assertContains( |
| 279 |
'Assert reason incorrect', 'Expected <4> after range but found nothing', |
| 280 |
e.message); |
| 281 |
} |