rendererharness.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Copyright 2009 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. // All Rights Reserved
  15. /**
  16. * @fileoverview A driver for testing renderers.
  17. *
  18. * @author nicksantos@google.com (Nick Santos)
  19. */
  20. goog.setTestOnly('goog.testing.ui.RendererHarness');
  21. goog.provide('goog.testing.ui.RendererHarness');
  22. goog.require('goog.Disposable');
  23. goog.require('goog.dom.NodeType');
  24. goog.require('goog.testing.asserts');
  25. goog.require('goog.testing.dom');
  26. /**
  27. * A driver for testing renderers.
  28. *
  29. * @param {goog.ui.ControlRenderer} renderer A renderer to test.
  30. * @param {Element} renderParent The parent of the element where controls will
  31. * be rendered.
  32. * @param {Element} decorateParent The parent of the element where controls will
  33. * be decorated.
  34. * @constructor
  35. * @extends {goog.Disposable}
  36. * @final
  37. */
  38. goog.testing.ui.RendererHarness = function(
  39. renderer, renderParent, decorateParent) {
  40. goog.Disposable.call(this);
  41. /**
  42. * The renderer under test.
  43. * @type {goog.ui.ControlRenderer}
  44. * @private
  45. */
  46. this.renderer_ = renderer;
  47. /**
  48. * The parent of the element where controls will be rendered.
  49. * @type {Element}
  50. * @private
  51. */
  52. this.renderParent_ = renderParent;
  53. /**
  54. * The original HTML of the render element.
  55. * @type {string}
  56. * @private
  57. */
  58. this.renderHtml_ = renderParent.innerHTML;
  59. /**
  60. * The parent of the element where controls will be decorated.
  61. * @type {Element}
  62. * @private
  63. */
  64. this.decorateParent_ = decorateParent;
  65. /**
  66. * The original HTML of the decorated element.
  67. * @type {string}
  68. * @private
  69. */
  70. this.decorateHtml_ = decorateParent.innerHTML;
  71. };
  72. goog.inherits(goog.testing.ui.RendererHarness, goog.Disposable);
  73. /**
  74. * A control to create by decoration.
  75. * @type {goog.ui.Control}
  76. * @private
  77. */
  78. goog.testing.ui.RendererHarness.prototype.decorateControl_;
  79. /**
  80. * A control to create by rendering.
  81. * @type {goog.ui.Control}
  82. * @private
  83. */
  84. goog.testing.ui.RendererHarness.prototype.renderControl_;
  85. /**
  86. * Whether all the necessary assert methods have been called.
  87. * @type {boolean}
  88. * @private
  89. */
  90. goog.testing.ui.RendererHarness.prototype.verified_ = false;
  91. /**
  92. * Attach a control and render its DOM.
  93. * @param {goog.ui.Control} control A control.
  94. * @return {Element} The element created.
  95. */
  96. goog.testing.ui.RendererHarness.prototype.attachControlAndRender = function(
  97. control) {
  98. this.renderControl_ = control;
  99. control.setRenderer(this.renderer_);
  100. control.render(this.renderParent_);
  101. return control.getElement();
  102. };
  103. /**
  104. * Attach a control and decorate the element given in the constructor.
  105. * @param {goog.ui.Control} control A control.
  106. * @return {Element} The element created.
  107. */
  108. goog.testing.ui.RendererHarness.prototype.attachControlAndDecorate = function(
  109. control) {
  110. this.decorateControl_ = control;
  111. control.setRenderer(this.renderer_);
  112. var child = this.decorateParent_.firstChild;
  113. assertEquals(
  114. 'The decorated node must be an element', goog.dom.NodeType.ELEMENT,
  115. child.nodeType);
  116. control.decorate(/** @type {!Element} */ (child));
  117. return control.getElement();
  118. };
  119. /**
  120. * Assert that the rendered element and the decorated element match.
  121. */
  122. goog.testing.ui.RendererHarness.prototype.assertDomMatches = function() {
  123. assert(
  124. 'Both elements were not generated',
  125. !!(this.renderControl_ && this.decorateControl_));
  126. goog.testing.dom.assertHtmlMatches(
  127. this.renderControl_.getElement().innerHTML,
  128. this.decorateControl_.getElement().innerHTML);
  129. this.verified_ = true;
  130. };
  131. /**
  132. * Destroy the harness, verifying that all assertions had been checked.
  133. * @override
  134. * @protected
  135. */
  136. goog.testing.ui.RendererHarness.prototype.disposeInternal = function() {
  137. // If the harness was not verified appropriately, throw an exception.
  138. assert(
  139. 'Expected assertDomMatches to be called',
  140. this.verified_ || !this.renderControl_ || !this.decorateControl_);
  141. if (this.decorateControl_) {
  142. this.decorateControl_.dispose();
  143. }
  144. if (this.renderControl_) {
  145. this.renderControl_.dispose();
  146. }
  147. this.renderParent_.innerHTML = this.renderHtml_;
  148. this.decorateParent_.innerHTML = this.decorateHtml_;
  149. goog.testing.ui.RendererHarness.superClass_.disposeInternal.call(this);
  150. };