123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649 |
- goog.setTestOnly('goog.testing.dom');
- goog.provide('goog.testing.dom');
- goog.require('goog.array');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.dom.InputType');
- goog.require('goog.dom.NodeIterator');
- goog.require('goog.dom.NodeType');
- goog.require('goog.dom.TagIterator');
- goog.require('goog.dom.TagName');
- goog.require('goog.dom.classlist');
- goog.require('goog.iter');
- goog.require('goog.object');
- goog.require('goog.string');
- goog.require('goog.style');
- goog.require('goog.testing.asserts');
- goog.require('goog.userAgent');
- goog.forwardDeclare('goog.dom.AbstractRange');
- goog.testing.dom.createEndTagMarker_ = function() {
- var marker = goog.dom.createElement(goog.dom.TagName.DIV);
- marker.id = goog.getUid(marker);
- return marker;
- };
- goog.testing.dom.END_TAG_MARKER_ = goog.testing.dom.createEndTagMarker_();
- goog.testing.dom.assertNodesMatch = function(it, array) {
- var i = 0;
- goog.iter.forEach(it, function(node) {
- if (array.length <= i) {
- fail(
- 'Got more nodes than expected: ' +
- goog.testing.dom.describeNode_(node));
- }
- var expected = array[i];
- if (goog.dom.isNodeLike(expected)) {
- assertEquals('Nodes should match at position ' + i, expected, node);
- } else if (goog.isNumber(expected)) {
- assertEquals(
- 'Node types should match at position ' + i, expected, node.nodeType);
- } else if (expected.charAt(0) == '#') {
- assertEquals(
- 'Expected element at position ' + i, goog.dom.NodeType.ELEMENT,
- node.nodeType);
- var expectedId = expected.substr(1);
- assertEquals('IDs should match at position ' + i, expectedId, node.id);
- } else {
- assertEquals(
- 'Expected text node at position ' + i, goog.dom.NodeType.TEXT,
- node.nodeType);
- assertEquals(
- 'Node contents should match at position ' + i, expected,
- node.nodeValue);
- }
- i++;
- });
- assertEquals('Used entire match array', array.length, i);
- };
- goog.testing.dom.exposeNode = function(node) {
- return (node.tagName || node.nodeValue) + (node.id ? '#' + node.id : '') +
- ':"' + (node.innerHTML || '') + '"';
- };
- goog.testing.dom.exposeRange = function(range) {
-
-
-
-
-
- if (!range) {
- return 'null';
- }
- return goog.testing.dom.exposeNode(range.getStartNode()) + ':' +
- range.getStartOffset() + ' to ' +
- goog.testing.dom.exposeNode(range.getEndNode()) + ':' +
- range.getEndOffset();
- };
- goog.testing.dom.checkUserAgents_ = function(userAgents) {
- if (goog.string.startsWith(userAgents, '!')) {
- if (goog.string.contains(userAgents, ' ')) {
- throw new Error('Only a single negative user agent may be specified');
- }
- return !goog.userAgent[userAgents.substr(1)];
- }
- var agents = userAgents.split(' ');
- var hasUserAgent = false;
- for (var i = 0, len = agents.length; i < len; i++) {
- var cls = agents[i];
- if (cls in goog.userAgent) {
- hasUserAgent = true;
- if (goog.userAgent[cls]) {
- return true;
- }
- }
- }
-
- return !hasUserAgent;
- };
- goog.testing.dom.endTagMap_ = function(node, ignore, iterator) {
- return iterator.isEndTag() ? goog.testing.dom.END_TAG_MARKER_ : node;
- };
- goog.testing.dom.nodeFilter_ = function(node) {
- if (node.nodeType == goog.dom.NodeType.TEXT) {
-
-
- if (goog.string.isBreakingWhitespace(node.nodeValue) &&
- (!node.previousSibling ||
- node.previousSibling.nodeType != goog.dom.NodeType.TEXT) &&
- (!node.nextSibling ||
- node.nextSibling.nodeType != goog.dom.NodeType.TEXT)) {
- return false;
- }
-
- var match = node.nodeValue.match(/^\[\[(.+)\]\]/);
- if (match) {
- return goog.testing.dom.checkUserAgents_(match[1]);
- }
- } else if (node.className && goog.isString(node.className)) {
- return goog.testing.dom.checkUserAgents_(node.className);
- }
- return true;
- };
- goog.testing.dom.getExpectedText_ = function(node) {
-
- return node.nodeValue.match(/^(\[\[.+\]\])?([\s\S]*)/)[2];
- };
- goog.testing.dom.describeNode_ = function(node) {
- if (node.nodeType == goog.dom.NodeType.TEXT) {
- return '[Text: ' + node.nodeValue + ']';
- } else {
- return '<' + node.tagName + (node.id ? ' #' + node.id : '') + ' .../>';
- }
- };
- goog.testing.dom.assertHtmlContentsMatch = function(
- htmlPattern, actual, opt_strictAttributes) {
- var div = goog.dom.createDom(goog.dom.TagName.DIV);
- div.innerHTML = htmlPattern;
- var errorSuffix =
- '\nExpected\n' + div.innerHTML + '\nActual\n' + actual.innerHTML;
- var actualIt = goog.iter.filter(
- goog.iter.map(
- new goog.dom.TagIterator(actual), goog.testing.dom.endTagMap_),
- goog.testing.dom.nodeFilter_);
- var expectedIt = goog.iter.filter(
- new goog.dom.NodeIterator(div), goog.testing.dom.nodeFilter_);
- var actualNode;
- var preIterated = false;
- var advanceActualNode = function() {
-
- if (!preIterated) {
- actualNode = goog.iter.nextOrValue(actualIt, null);
- }
- preIterated = false;
-
- while (actualNode == goog.testing.dom.END_TAG_MARKER_) {
- actualNode = goog.iter.nextOrValue(actualIt, null);
- }
- };
-
-
-
-
-
-
-
- var IE_TEXT_COLLAPSE =
- goog.userAgent.IE && !goog.userAgent.isVersionOrHigher('9');
- var collapsible = true;
- var number = 0;
- goog.iter.forEach(expectedIt, function(expectedNode) {
- advanceActualNode();
- assertNotNull(
- 'Finished actual HTML before finishing expected HTML at ' +
- 'node number ' + number + ': ' +
- goog.testing.dom.describeNode_(expectedNode) + errorSuffix,
- actualNode);
-
- if (expectedNode == div) {
- return;
- }
- assertEquals(
- 'Should have the same node type, got ' +
- goog.testing.dom.describeNode_(actualNode) + ' but expected ' +
- goog.testing.dom.describeNode_(expectedNode) + '.' + errorSuffix,
- expectedNode.nodeType, actualNode.nodeType);
- if (expectedNode.nodeType == goog.dom.NodeType.ELEMENT) {
- var expectedElem = goog.asserts.assertElement(expectedNode);
- var actualElem = goog.asserts.assertElement(actualNode);
- assertEquals(
- 'Tag names should match' + errorSuffix, expectedElem.tagName,
- actualElem.tagName);
- assertObjectEquals(
- 'Should have same styles' + errorSuffix,
- goog.style.parseStyleAttribute(expectedElem.style.cssText),
- goog.style.parseStyleAttribute(actualElem.style.cssText));
- goog.testing.dom.assertAttributesEqual_(
- errorSuffix, expectedElem, actualElem, !!opt_strictAttributes);
- if (IE_TEXT_COLLAPSE &&
- goog.style.getCascadedStyle(actualElem, 'display') != 'inline') {
-
- collapsible = true;
- }
- } else {
-
- var actualText = actualNode.nodeValue;
- preIterated = true;
- while ((actualNode = goog.iter.nextOrValue(actualIt, null)) &&
- actualNode.nodeType == goog.dom.NodeType.TEXT) {
- actualText += actualNode.nodeValue;
- }
- if (IE_TEXT_COLLAPSE) {
-
-
- if (collapsible && !goog.string.isEmptyOrWhitespace(actualText)) {
- actualText = goog.string.trimLeft(actualText);
- }
-
-
- collapsible = /\s$/.test(actualText);
- }
- var expectedText = goog.testing.dom.getExpectedText_(expectedNode);
- if ((actualText && !goog.string.isBreakingWhitespace(actualText)) ||
- (expectedText && !goog.string.isBreakingWhitespace(expectedText))) {
- var normalizedActual = actualText.replace(/\s+/g, ' ');
- var normalizedExpected = expectedText.replace(/\s+/g, ' ');
- assertEquals(
- 'Text should match' + errorSuffix, normalizedExpected,
- normalizedActual);
- }
- }
- number++;
- });
- advanceActualNode();
- assertNull(
- 'Finished expected HTML before finishing actual HTML' + errorSuffix,
- goog.iter.nextOrValue(actualIt, null));
- };
- goog.testing.dom.assertHtmlMatches = function(
- htmlPattern, actual, opt_strictAttributes) {
- var div = goog.dom.createDom(goog.dom.TagName.DIV);
- div.innerHTML = actual;
- goog.testing.dom.assertHtmlContentsMatch(
- htmlPattern, div, opt_strictAttributes);
- };
- goog.testing.dom.findTextNode = function(textOrRegexp, root) {
- var it = new goog.dom.NodeIterator(root);
- var ret = goog.iter.nextOrValue(goog.iter.filter(it, function(node) {
- if (node.nodeType == goog.dom.NodeType.TEXT) {
- if (goog.isString(textOrRegexp)) {
- return node.nodeValue == textOrRegexp;
- } else {
- return !!node.nodeValue.match(textOrRegexp);
- }
- } else {
- return false;
- }
- }), null);
- return ret;
- };
- goog.testing.dom.assertRangeEquals = function(
- start, startOffset, end, endOffset, range) {
- assertEquals('Unexpected start node', start, range.getStartNode());
- assertEquals('Unexpected end node', end, range.getEndNode());
- assertEquals('Unexpected start offset', startOffset, range.getStartOffset());
- assertEquals('Unexpected end offset', endOffset, range.getEndOffset());
- };
- goog.testing.dom.getAttributeValue_ = function(node, name) {
-
-
-
-
-
-
-
- if (goog.userAgent.WEBKIT && node.tagName == goog.dom.TagName.INPUT &&
- node['type'] == goog.dom.InputType.RADIO && name == 'checked') {
- return false;
- }
- return goog.isDef(node[name]) &&
- typeof node.getAttribute(name) != typeof node[name] ?
- node[name] :
- node.getAttribute(name);
- };
- goog.testing.dom.assertAttributesEqual_ = function(
- errorSuffix, expectedElem, actualElem, strictAttributes) {
- if (strictAttributes) {
- goog.testing.dom.compareClassAttribute_(expectedElem, actualElem);
- }
- var expectedAttributes = expectedElem.attributes;
- var actualAttributes = actualElem.attributes;
- for (var i = 0, len = expectedAttributes.length; i < len; i++) {
- var expectedName = expectedAttributes[i].name;
- var expectedValue =
- goog.testing.dom.getAttributeValue_(expectedElem, expectedName);
- var actualAttribute = actualAttributes[expectedName];
- var actualValue =
- goog.testing.dom.getAttributeValue_(actualElem, expectedName);
-
-
- if (!expectedValue && !actualValue) {
- continue;
- }
- if (expectedName == 'id' && goog.userAgent.IE) {
- goog.testing.dom.compareIdAttributeForIe_(
- (expectedValue), actualAttribute,
- strictAttributes, errorSuffix);
- continue;
- }
- if (goog.testing.dom.ignoreAttribute_(expectedName)) {
- continue;
- }
- assertNotUndefined(
- 'Expected to find attribute with name ' + expectedName +
- ', in element ' + goog.testing.dom.describeNode_(actualElem) +
- errorSuffix,
- actualAttribute);
- assertEquals(
- 'Expected attribute ' + expectedName + ' has a different value ' +
- errorSuffix,
- String(expectedValue), String(
- goog.testing.dom.getAttributeValue_(
- actualElem, actualAttribute.name)));
- }
- if (strictAttributes) {
- for (i = 0; i < actualAttributes.length; i++) {
- var actualName = actualAttributes[i].name;
- var actualAttribute = actualAttributes.getNamedItem(actualName);
- if (!actualAttribute || goog.testing.dom.ignoreAttribute_(actualName)) {
- continue;
- }
- assertNotUndefined(
- 'Unexpected attribute with name ' + actualName + ' in element ' +
- goog.testing.dom.describeNode_(actualElem) + errorSuffix,
- expectedAttributes[actualName]);
- }
- }
- };
- goog.testing.dom.compareClassAttribute_ = function(expectedElem, actualElem) {
- var classes = goog.dom.classlist.get(expectedElem);
- var expectedClasses = [];
- for (var i = 0, len = classes.length; i < len; i++) {
- if (!(classes[i] in goog.userAgent)) {
- expectedClasses.push(classes[i]);
- }
- }
- expectedClasses.sort();
- var actualClasses = goog.array.toArray(goog.dom.classlist.get(actualElem));
- actualClasses.sort();
- assertArrayEquals(
- 'Expected class was: ' + expectedClasses.join(' ') +
- ', but actual class was: ' + actualElem.className + ' in node ' +
- goog.testing.dom.describeNode_(actualElem),
- expectedClasses, actualClasses);
- };
- goog.testing.dom.BAD_IE_ATTRIBUTES_ = goog.object.createSet(
- 'methods', 'CHECKED', 'dataFld', 'dataFormatAs', 'dataSrc');
- goog.testing.dom.ignoreAttribute_ = function(name) {
- if (name == 'style' || name == 'class') {
- return true;
- }
- return goog.userAgent.IE && goog.testing.dom.BAD_IE_ATTRIBUTES_[name];
- };
- goog.testing.dom.compareIdAttributeForIe_ = function(
- expectedValue, actualAttribute, strictAttributes, errorSuffix) {
- if (expectedValue === '') {
- if (strictAttributes) {
- assertTrue(
- 'Unexpected attribute with name id in element ' + errorSuffix,
- actualAttribute.value == '');
- }
- } else {
- assertNotUndefined(
- 'Expected to find attribute with name id, in element ' + errorSuffix,
- actualAttribute);
- assertNotEquals(
- 'Expected to find attribute with name id, in element ' + errorSuffix,
- '', actualAttribute.value);
- assertEquals(
- 'Expected attribute has a different value ' + errorSuffix,
- expectedValue, actualAttribute.value);
- }
- };
|