buttonrenderer_test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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.style.app.ButtonRendererTest');
  15. goog.setTestOnly('goog.ui.style.app.ButtonRendererTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.testing.jsunit');
  18. goog.require('goog.testing.ui.style');
  19. goog.require('goog.ui.Button');
  20. goog.require('goog.ui.Component');
  21. goog.require('goog.ui.style.app.ButtonRenderer');
  22. goog.require('goog.userAgent');
  23. var renderer = goog.ui.style.app.ButtonRenderer.getInstance();
  24. var button;
  25. // Write iFrame tag to load reference FastUI markup. Then, our tests will
  26. // compare the generated markup to the reference markup.
  27. var refPath = '../../../../../webutil/css/fastui/app/button_spec.html';
  28. goog.testing.ui.style.writeReferenceFrame(refPath);
  29. function shouldRunTests() {
  30. // Disable tests when being run as a part of open-source repo as the button
  31. // specs are not included in closure-library.
  32. return !(/closure\/goog\/ui/.test(location.pathname));
  33. }
  34. function setUp() {
  35. button = new goog.ui.Button('Hello Generated', renderer);
  36. button.setTooltip('Click for Generated');
  37. }
  38. function tearDown() {
  39. if (button) {
  40. button.dispose();
  41. }
  42. goog.dom.removeChildren(goog.dom.getElement('sandbox'));
  43. }
  44. function testGeneratedButton() {
  45. button.render(goog.dom.getElement('sandbox'));
  46. goog.testing.ui.style.assertStructureMatchesReference(
  47. button.getElement(), 'normal-resting');
  48. assertEquals('Hello Generated', button.getContentElement().innerHTML);
  49. assertEquals(
  50. 'Click for Generated', button.getElement().getAttribute('title'));
  51. }
  52. function testButtonStates() {
  53. button.render(goog.dom.getElement('sandbox'));
  54. goog.testing.ui.style.assertStructureMatchesReference(
  55. button.getElement(), 'normal-resting');
  56. button.setState(goog.ui.Component.State.HOVER, true);
  57. goog.testing.ui.style.assertStructureMatchesReference(
  58. button.getElement(), 'normal-hover');
  59. button.setState(goog.ui.Component.State.HOVER, false);
  60. button.setState(goog.ui.Component.State.FOCUSED, true);
  61. goog.testing.ui.style.assertStructureMatchesReference(
  62. button.getElement(), 'normal-focused');
  63. button.setState(goog.ui.Component.State.FOCUSED, false);
  64. button.setState(goog.ui.Component.State.ACTIVE, true);
  65. goog.testing.ui.style.assertStructureMatchesReference(
  66. button.getElement(), 'normal-active');
  67. button.setState(goog.ui.Component.State.ACTIVE, false);
  68. button.setState(goog.ui.Component.State.DISABLED, true);
  69. goog.testing.ui.style.assertStructureMatchesReference(
  70. button.getElement(), 'normal-disabled');
  71. }
  72. function testDecoratedButton() {
  73. button.decorate(goog.dom.getElement('button'));
  74. goog.testing.ui.style.assertStructureMatchesReference(
  75. button.getElement(), 'normal-resting');
  76. assertEquals('Hello Decorated', button.getContentElement().innerHTML);
  77. assertEquals(
  78. 'Click for Decorated', button.getElement().getAttribute('title'));
  79. }
  80. function testDecoratedButtonBox() {
  81. button.decorate(goog.dom.getElement('button-box'));
  82. goog.testing.ui.style.assertStructureMatchesReference(
  83. button.getElement(), 'normal-resting');
  84. assertEquals('Hello Decorated Box', button.getContentElement().innerHTML);
  85. assertEquals(
  86. 'Click for Decorated Box', button.getElement().getAttribute('title'));
  87. }
  88. /*
  89. * This test demonstrates what happens when you put whitespace in a
  90. * decorated button's content, and the decorated element 'hasBoxStructure'.
  91. * Note that this behavior is different than when the element does not
  92. * have box structure. Should this be fixed?
  93. */
  94. function testDecoratedButtonBoxWithSpaceInContent() {
  95. button.decorate(goog.dom.getElement('button-box-with-space-in-content'));
  96. goog.testing.ui.style.assertStructureMatchesReference(
  97. button.getElement(), 'normal-resting');
  98. if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) {
  99. assertEquals(
  100. 'Hello Decorated Box with space ',
  101. button.getContentElement().innerHTML);
  102. } else {
  103. assertEquals(
  104. '\n Hello Decorated Box with space\n ',
  105. button.getContentElement().innerHTML);
  106. }
  107. }
  108. function testExistingContentIsUsed() {
  109. button.decorate(goog.dom.getElement('button-box-with-dom-content'));
  110. goog.testing.ui.style.assertStructureMatchesReference(
  111. button.getElement(), 'normal-resting');
  112. // Safari 3 adds style="-webkit-user-select" to the strong tag, so we
  113. // can't simply look at the HTML.
  114. var content = button.getContentElement();
  115. assertEquals(
  116. 'Unexpected number of child nodes', 3, content.childNodes.length);
  117. assertEquals('Unexpected tag', 'STRONG', content.childNodes[0].tagName);
  118. assertEquals(
  119. 'Unexpected text content', 'Hello', content.childNodes[0].innerHTML);
  120. assertEquals('Unexpected tag', 'EM', content.childNodes[2].tagName);
  121. assertEquals(
  122. 'Unexpected text content', 'Box', content.childNodes[2].innerHTML);
  123. }