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