Path: blob/trunk/javascript/atoms/test/text_util.js
4500 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');252627function getTestContainer() {28function createTestContainer() {29var testContainer = goog.dom.createDom('DIV', {30id: 'test-container',31style: 'border: 1px dotted silver'32}, goog.dom.createDom('DIV', {33style: 'font-weight:bold;' +34'text-decoration:underline;' +35'font-style:italic;' +36'margin: 0.5em'37}, 'Test Container'));38document.body.appendChild(testContainer);39return testContainer;40}4142return goog.dom.getElement('test-container') || createTestContainer();43}444546/**47* Verifies that the visible text for the test DOM structure.48* @param {!Object} assert The QUnit assert object.49* @param {string} testName The name of the current test.50* @param {!Element} element The element to check the text of.51* @param {...string} var_args Variadic args for the expected lines52* of visible text.53*/54function assertTextIs(assert, testName, element, var_args) {55if (!element.parentNode ||56// IE sets the parentNode of unattached elements to a document fragment.57element.parentNode.nodeType != goog.dom.NodeType.ELEMENT) {58var testContainer = getTestContainer();59testContainer.appendChild(createTestDom(element));60}6162var expected = goog.array.slice(arguments, 3).join('\n');63var actual = bot.dom.getVisibleText(element);6465assert.strictEqual(actual, expected,66'Expected: ' + escapeText(expected) +67'\n but was: ' + escapeText(actual) +68'\n raw html:\n' + element.innerHTML +69'\n------\n');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('')},80testName),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