| 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.ui.TabRendererTest'); |
| 16 |
goog.setTestOnly('goog.ui.TabRendererTest'); |
| 17 |
|
| 18 |
goog.require('goog.a11y.aria.Role'); |
| 19 |
goog.require('goog.dom'); |
| 20 |
goog.require('goog.dom.classlist'); |
| 21 |
goog.require('goog.testing.dom'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.testing.ui.rendererasserts'); |
| 24 |
goog.require('goog.ui.Tab'); |
| 25 |
goog.require('goog.ui.TabRenderer'); |
| 26 |
|
| 27 |
var sandbox; |
| 28 |
var renderer; |
| 29 |
var tab; |
| 30 |
|
| 31 |
function setUp() { |
| 32 |
sandbox = goog.dom.getElement('sandbox'); |
| 33 |
renderer = goog.ui.TabRenderer.getInstance(); |
| 34 |
tab = new goog.ui.Tab('Hello'); |
| 35 |
} |
| 36 |
|
| 37 |
function tearDown() { |
| 38 |
tab.dispose(); |
| 39 |
goog.dom.removeChildren(sandbox); |
| 40 |
} |
| 41 |
|
| 42 |
function testConstructor() { |
| 43 |
assertNotNull('Renderer must not be null', renderer); |
| 44 |
} |
| 45 |
|
| 46 |
function testGetCssClass() { |
| 47 |
assertEquals( |
| 48 |
'CSS class must have expected value', goog.ui.TabRenderer.CSS_CLASS, |
| 49 |
renderer.getCssClass()); |
| 50 |
} |
| 51 |
|
| 52 |
function testGetAriaRole() { |
| 53 |
assertEquals( |
| 54 |
'ARIA role must have expected value', goog.a11y.aria.Role.TAB, |
| 55 |
renderer.getAriaRole()); |
| 56 |
} |
| 57 |
|
| 58 |
function testCreateDom() { |
| 59 |
var element = renderer.createDom(tab); |
| 60 |
assertNotNull('Element must not be null', element); |
| 61 |
goog.testing.dom.assertHtmlMatches( |
| 62 |
'<div class="goog-tab">Hello</div>', goog.dom.getOuterHtml(element)); |
| 63 |
} |
| 64 |
|
| 65 |
function testCreateDomWithTooltip() { |
| 66 |
tab.setTooltip('Hello, world!'); |
| 67 |
var element = renderer.createDom(tab); |
| 68 |
assertNotNull('Element must not be null', element); |
| 69 |
assertEquals( |
| 70 |
'Element must have expected tooltip', 'Hello, world!', |
| 71 |
renderer.getTooltip(element)); |
| 72 |
} |
| 73 |
|
| 74 |
function testRender() { |
| 75 |
tab.setRenderer(renderer); |
| 76 |
tab.render(); |
| 77 |
var element = tab.getElementStrict(); |
| 78 |
assertNotNull('Element must not be null', element); |
| 79 |
assertEquals( |
| 80 |
'aria-selected should be false', 'false', |
| 81 |
element.getAttribute('aria-selected')); |
| 82 |
} |
| 83 |
|
| 84 |
function testDecorate() { |
| 85 |
sandbox.innerHTML = '<div id="foo">Foo</div>\n' + |
| 86 |
'<div id="bar" title="Yes">Bar</div>'; |
| 87 |
|
| 88 |
var foo = renderer.decorate(tab, goog.dom.getElement('foo')); |
| 89 |
assertNotNull('Decorated element must not be null', foo); |
| 90 |
assertSameElements( |
| 91 |
'Decorated element must have expected class', ['goog-tab'], |
| 92 |
goog.dom.classlist.get(foo)); |
| 93 |
assertEquals( |
| 94 |
'Decorated tab must have expected content', 'Foo', |
| 95 |
tab.getContent().nodeValue); |
| 96 |
assertUndefined('Decorated tab must not have tooltip', tab.getTooltip()); |
| 97 |
assertEquals( |
| 98 |
'Decorated element must not have title', '', renderer.getTooltip(foo)); |
| 99 |
|
| 100 |
var bar = renderer.decorate(tab, goog.dom.getElement('bar')); |
| 101 |
assertNotNull('Decorated element must not be null', bar); |
| 102 |
assertSameElements( |
| 103 |
'Decorated element must have expected class', ['goog-tab'], |
| 104 |
goog.dom.classlist.get(bar)); |
| 105 |
assertEquals( |
| 106 |
'Decorated tab must have expected content', 'Bar', |
| 107 |
tab.getContent().nodeValue); |
| 108 |
assertEquals( |
| 109 |
'Decorated tab must have expected tooltip', 'Yes', tab.getTooltip()); |
| 110 |
assertEquals( |
| 111 |
'Decorated element must have expected title', 'Yes', |
| 112 |
renderer.getTooltip(bar)); |
| 113 |
} |
| 114 |
|
| 115 |
function testGetTooltip() { |
| 116 |
sandbox.innerHTML = '<div id="foo">Foo</div>\n' + |
| 117 |
'<div id="bar" title="">Bar</div>\n' + |
| 118 |
'<div id="baz" title="BazTitle">Baz</div>'; |
| 119 |
assertEquals( |
| 120 |
'getTooltip() must return empty string for no title', '', |
| 121 |
renderer.getTooltip(goog.dom.getElement('foo'))); |
| 122 |
assertEquals( |
| 123 |
'getTooltip() must return empty string for empty title', '', |
| 124 |
renderer.getTooltip(goog.dom.getElement('bar'))); |
| 125 |
assertEquals( |
| 126 |
'Tooltip must have expected value', 'BazTitle', |
| 127 |
renderer.getTooltip(goog.dom.getElement('baz'))); |
| 128 |
} |
| 129 |
|
| 130 |
function testSetTooltip() { |
| 131 |
sandbox.innerHTML = '<div id="foo">Foo</div>'; |
| 132 |
var element = goog.dom.getElement('foo'); |
| 133 |
|
| 134 |
renderer.setTooltip(null, null); // Must not error. |
| 135 |
|
| 136 |
renderer.setTooltip(element, null); |
| 137 |
assertEquals('Tooltip must be the empty string', '', element.title); |
| 138 |
|
| 139 |
renderer.setTooltip(element, ''); |
| 140 |
assertEquals('Tooltip must be the empty string', '', element.title); |
| 141 |
|
| 142 |
renderer.setTooltip(element, 'Foo'); |
| 143 |
assertEquals('Tooltip must have expected value', 'Foo', element.title); |
| 144 |
} |
| 145 |
|
| 146 |
function testDoesntCallGetCssClassInConstructor() { |
| 147 |
goog.testing.ui.rendererasserts.assertNoGetCssClassCallsInConstructor( |
| 148 |
goog.ui.TabRenderer); |
| 149 |
} |