| 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.dom.NodeOffsetTest'); |
| 16 |
goog.setTestOnly('goog.dom.NodeOffsetTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.NodeOffset'); |
| 20 |
goog.require('goog.dom.NodeType'); |
| 21 |
goog.require('goog.dom.TagName'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
|
| 24 |
var test1; |
| 25 |
var test2; |
| 26 |
var i; |
| 27 |
var empty; |
| 28 |
|
| 29 |
function setUpPage() { |
| 30 |
test1 = goog.dom.getElement('test1'); |
| 31 |
i = goog.dom.getElement('i'); |
| 32 |
test2 = goog.dom.getElement('test2'); |
| 33 |
test2.innerHTML = test1.innerHTML; |
| 34 |
empty = goog.dom.getElement('empty'); |
| 35 |
} |
| 36 |
|
| 37 |
function testElementOffset() { |
| 38 |
var nodeOffset = new goog.dom.NodeOffset(i, test1); |
| 39 |
|
| 40 |
var recovered = nodeOffset.findTargetNode(test2); |
| 41 |
assertNotNull('Should recover a node.', recovered); |
| 42 |
assertEquals( |
| 43 |
'Should recover an I node.', String(goog.dom.TagName.I), |
| 44 |
recovered.tagName); |
| 45 |
assertTrue( |
| 46 |
'Should recover a child of test2', goog.dom.contains(test2, recovered)); |
| 47 |
assertFalse( |
| 48 |
'Should not recover a child of test1', |
| 49 |
goog.dom.contains(test1, recovered)); |
| 50 |
|
| 51 |
nodeOffset.dispose(); |
| 52 |
} |
| 53 |
|
| 54 |
function testNodeOffset() { |
| 55 |
var nodeOffset = new goog.dom.NodeOffset(i.firstChild, test1); |
| 56 |
|
| 57 |
var recovered = nodeOffset.findTargetNode(test2); |
| 58 |
assertNotNull('Should recover a node.', recovered); |
| 59 |
assertEquals( |
| 60 |
'Should recover a text node.', goog.dom.NodeType.TEXT, |
| 61 |
recovered.nodeType); |
| 62 |
assertEquals('Should have correct contents.', 'text.', recovered.nodeValue); |
| 63 |
assertTrue( |
| 64 |
'Should recover a child of test2', goog.dom.contains(test2, recovered)); |
| 65 |
assertFalse( |
| 66 |
'Should not recover a child of test1', |
| 67 |
goog.dom.contains(test1, recovered)); |
| 68 |
|
| 69 |
nodeOffset.dispose(); |
| 70 |
} |
| 71 |
|
| 72 |
function testToString() { |
| 73 |
var nodeOffset = new goog.dom.NodeOffset(i.firstChild, test1); |
| 74 |
|
| 75 |
assertEquals( |
| 76 |
'Should have correct string representation', '3,B\n1,I\n0,#text', |
| 77 |
nodeOffset.toString()); |
| 78 |
|
| 79 |
nodeOffset.dispose(); |
| 80 |
} |
| 81 |
|
| 82 |
function testBadRecovery() { |
| 83 |
var nodeOffset = new goog.dom.NodeOffset(i.firstChild, test1); |
| 84 |
|
| 85 |
var recovered = nodeOffset.findTargetNode(empty); |
| 86 |
assertNull('Should recover nothing.', recovered); |
| 87 |
|
| 88 |
nodeOffset.dispose(); |
| 89 |
} |