Path: blob/trunk/javascript/atoms/test/text_util.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* @fileoverview Utilities for testing {@code bot.dom.getVisibleText}.19*/2021goog.require('bot.dom');22goog.require('goog.array');23goog.require('goog.dom');24goog.require('goog.dom.NodeType');25goog.require('goog.testing.TestCase');262728function getTestContainer() {29function createTestContainer() {30var testContainer = goog.dom.createDom('DIV', {31id: 'test-container',32style: 'border: 1px dotted silver'33}, goog.dom.createDom('DIV', {34style: 'font-weight:bold;' +35'text-decoration:underline;' +36'font-style:italic;' +37'margin: 0.5em'38}, 'Test Container'));39document.body.appendChild(testContainer);40return testContainer;41}4243return goog.dom.getElement('test-container') || createTestContainer();44}454647/**48* Verifies that the visible text for the test DOM structure.49* @param {!Element} element The element to check the text of.50* @param {...string} var_args Variadic args for the expected lines51* of visible text.52*/53function assertTextIs(element, var_args) {54if (!element.parentNode ||55// IE sets the parentNode of unattached elements to a document fragment.56element.parentNode.nodeType != goog.dom.NodeType.ELEMENT) {57var testContainer = getTestContainer();58testContainer.appendChild(createTestDom(element));59}6061var expected = goog.array.slice(arguments, 1).join('\n');62var actual = bot.dom.getVisibleText(element);6364assertEquals(65'Expected: ' + escapeText(expected) +66'\n but was: ' + escapeText(actual) +67'\n raw html:\n' + element.innerHTML +68'\n------\n',69expected, actual);7071function createTestDom(element) {72return goog.dom.createDom('div', {'style': 'width: 25em;'},73goog.dom.createDom('div',74{'style': [75'margin: 0 0.5em;',76'padding: 0;',77'font-style: italic;',78'color: gray;'79].join('')},80goog.testing.TestCase.currentTestName),81goog.dom.createDom('div', {82'style': 'margin-bottom: 0.5em'83}, element));84}8586function escapeText(text) {87return text.replace(/\n/g, '\\n');//.replace(/\s/g, '\\s');88}89}909192