tabrenderer_test.js 4.8 KB

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