| 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 |
/** |
| 16 |
* @fileoverview Tools for testing Closure renderers against static markup |
| 17 |
* spec pages. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.setTestOnly('goog.testing.ui.style'); |
| 22 |
goog.provide('goog.testing.ui.style'); |
| 23 |
|
| 24 |
goog.require('goog.array'); |
| 25 |
goog.require('goog.asserts'); |
| 26 |
goog.require('goog.dom'); |
| 27 |
goog.require('goog.dom.classlist'); |
| 28 |
goog.require('goog.testing.asserts'); |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Uses document.write to add an iFrame to the page with the reference path in |
| 33 |
* the src attribute. Used for loading an html file containing reference |
| 34 |
* structures to test against into the page. Should be called within the body of |
| 35 |
* the jsunit test page. |
| 36 |
* @param {string} referencePath A path to a reference HTML file. |
| 37 |
*/ |
| 38 |
goog.testing.ui.style.writeReferenceFrame = function(referencePath) { |
| 39 |
document.write( |
| 40 |
'<iframe id="reference" name="reference" ' + |
| 41 |
'src="' + referencePath + '"></iframe>'); |
| 42 |
}; |
| 43 |
|
| 44 |
|
| 45 |
/** |
| 46 |
* Returns a reference to the first element child of a node with the given id |
| 47 |
* from the page loaded into the reference iFrame. Used to retrieve a particular |
| 48 |
* reference DOM structure to test against. |
| 49 |
* @param {string} referenceId The id of a container element for a reference |
| 50 |
* structure in the reference page. |
| 51 |
* @return {Node} The root element of the reference structure. |
| 52 |
*/ |
| 53 |
goog.testing.ui.style.getReferenceNode = function(referenceId) { |
| 54 |
return goog.dom.getFirstElementChild( |
| 55 |
window.frames['reference'].document.getElementById(referenceId)); |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
/** |
| 60 |
* Returns an array of all element children of a given node. |
| 61 |
* @param {Node} element The node to get element children of. |
| 62 |
* @return {!Array<!Node>} An array of all the element children. |
| 63 |
*/ |
| 64 |
goog.testing.ui.style.getElementChildren = function(element) { |
| 65 |
var first = goog.dom.getFirstElementChild(element); |
| 66 |
if (!first) { |
| 67 |
return []; |
| 68 |
} |
| 69 |
var children = [first], next; |
| 70 |
while (next = goog.dom.getNextElementSibling(children[children.length - 1])) { |
| 71 |
children.push(next); |
| 72 |
} |
| 73 |
return children; |
| 74 |
}; |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Tests whether a given node is a "content" node of a reference structure, |
| 79 |
* which means it is allowed to have arbitrary children. |
| 80 |
* @param {Node} element The node to test. |
| 81 |
* @return {boolean} Whether the given node is a content node or not. |
| 82 |
*/ |
| 83 |
goog.testing.ui.style.isContentNode = function(element) { |
| 84 |
return element.className.indexOf('content') != -1; |
| 85 |
}; |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Tests that the structure, node names, and classes of the given element are |
| 90 |
* the same as the reference structure with the given id. Throws an error if the |
| 91 |
* element doesn't have the same nodes at each level of the DOM with the same |
| 92 |
* classes on each. The test ignores all DOM structure within content nodes. |
| 93 |
* @param {Node} element The root node of the DOM structure to test. |
| 94 |
* @param {string} referenceId The id of the container for the reference |
| 95 |
* structure to test against. |
| 96 |
*/ |
| 97 |
goog.testing.ui.style.assertStructureMatchesReference = function( |
| 98 |
element, referenceId) { |
| 99 |
goog.testing.ui.style.assertStructureMatchesReferenceInner_( |
| 100 |
element, goog.testing.ui.style.getReferenceNode(referenceId)); |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* A recursive function for comparing structure, node names, and classes between |
| 106 |
* a test and reference DOM structure. Throws an error if one of these things |
| 107 |
* doesn't match. Used internally by |
| 108 |
* {@link goog.testing.ui.style.assertStructureMatchesReference}. |
| 109 |
* @param {Node} element DOM element to test. |
| 110 |
* @param {Node} reference DOM element to use as a reference (test against). |
| 111 |
* @private |
| 112 |
*/ |
| 113 |
goog.testing.ui.style.assertStructureMatchesReferenceInner_ = function( |
| 114 |
element, reference) { |
| 115 |
if (!element && !reference) { |
| 116 |
return; |
| 117 |
} |
| 118 |
assertTrue('Expected two elements.', !!element && !!reference); |
| 119 |
assertEquals( |
| 120 |
'Expected nodes to have the same nodeName.', element.nodeName, |
| 121 |
reference.nodeName); |
| 122 |
var testElem = goog.asserts.assertElement(element); |
| 123 |
var refElem = goog.asserts.assertElement(reference); |
| 124 |
var elementClasses = goog.dom.classlist.get(testElem); |
| 125 |
goog.array.forEach(goog.dom.classlist.get(refElem), function(referenceClass) { |
| 126 |
assertContains( |
| 127 |
'Expected test node to have all reference classes.', referenceClass, |
| 128 |
elementClasses); |
| 129 |
}); |
| 130 |
// Call assertStructureMatchesReferenceInner_ on all element children |
| 131 |
// unless this is a content node |
| 132 |
var elChildren = goog.testing.ui.style.getElementChildren(element), |
| 133 |
refChildren = goog.testing.ui.style.getElementChildren(reference); |
| 134 |
if (!goog.testing.ui.style.isContentNode(reference)) { |
| 135 |
if (elChildren.length != refChildren.length) { |
| 136 |
assertEquals( |
| 137 |
'Expected same number of children for a non-content node.', |
| 138 |
elChildren.length, refChildren.length); |
| 139 |
} |
| 140 |
for (var i = 0; i < elChildren.length; i++) { |
| 141 |
goog.testing.ui.style.assertStructureMatchesReferenceInner_( |
| 142 |
elChildren[i], refChildren[i]); |
| 143 |
} |
| 144 |
} |
| 145 |
}; |