| 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 |
|
| 15 |
/** |
| 16 |
* @fileoverview A utility class for making layout assertions. This is a port |
| 17 |
* of http://go/layoutbot.java |
| 18 |
* See {@link http://go/layouttesting}. |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.setTestOnly('goog.testing.style.layoutasserts'); |
| 22 |
goog.provide('goog.testing.style.layoutasserts'); |
| 23 |
|
| 24 |
goog.require('goog.style'); |
| 25 |
goog.require('goog.testing.asserts'); |
| 26 |
goog.require('goog.testing.style'); |
| 27 |
|
| 28 |
|
| 29 |
/** |
| 30 |
* Asserts that an element has: |
| 31 |
* 1 - a CSS rendering the makes the element visible. |
| 32 |
* 2 - a non-zero width and height. |
| 33 |
* @param {Element|string} a The element or optionally the comment string. |
| 34 |
* @param {Element=} opt_b The element when a comment string is present. |
| 35 |
*/ |
| 36 |
var assertIsVisible = function(a, opt_b) { |
| 37 |
_validateArguments(1, arguments); |
| 38 |
var element = nonCommentArg(1, 1, arguments); |
| 39 |
|
| 40 |
_assert( |
| 41 |
commentArg(1, arguments), goog.testing.style.isVisible(element) && |
| 42 |
goog.testing.style.hasVisibleDimensions(element), |
| 43 |
'Specified element should be visible.'); |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* The counter assertion of assertIsVisible(). |
| 49 |
* @param {Element|string} a The element or optionally the comment string. |
| 50 |
* @param {Element=} opt_b The element when a comment string is present. |
| 51 |
*/ |
| 52 |
var assertNotVisible = function(a, opt_b) { |
| 53 |
_validateArguments(1, arguments); |
| 54 |
var element = nonCommentArg(1, 1, arguments); |
| 55 |
if (!element) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
_assert( |
| 60 |
commentArg(1, arguments), !goog.testing.style.isVisible(element) || |
| 61 |
!goog.testing.style.hasVisibleDimensions(element), |
| 62 |
'Specified element should not be visible.'); |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Asserts that the two specified elements intersect. |
| 68 |
* @param {Element|string} a The first element or optionally the comment string. |
| 69 |
* @param {Element} b The second element or the first element if comment string |
| 70 |
* is present. |
| 71 |
* @param {Element=} opt_c The second element if comment string is present. |
| 72 |
*/ |
| 73 |
var assertIntersect = function(a, b, opt_c) { |
| 74 |
_validateArguments(2, arguments); |
| 75 |
var element = nonCommentArg(1, 2, arguments); |
| 76 |
var otherElement = nonCommentArg(2, 2, arguments); |
| 77 |
|
| 78 |
_assert( |
| 79 |
commentArg(1, arguments), |
| 80 |
goog.testing.style.intersects(element, otherElement), |
| 81 |
'Elements should intersect.'); |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Asserts that the two specified elements do not intersect. |
| 87 |
* @param {Element|string} a The first element or optionally the comment string. |
| 88 |
* @param {Element} b The second element or the first element if comment string |
| 89 |
* is present. |
| 90 |
* @param {Element=} opt_c The second element if comment string is present. |
| 91 |
*/ |
| 92 |
var assertNoIntersect = function(a, b, opt_c) { |
| 93 |
_validateArguments(2, arguments); |
| 94 |
var element = nonCommentArg(1, 2, arguments); |
| 95 |
var otherElement = nonCommentArg(2, 2, arguments); |
| 96 |
|
| 97 |
_assert( |
| 98 |
commentArg(1, arguments), |
| 99 |
!goog.testing.style.intersects(element, otherElement), |
| 100 |
'Elements should not intersect.'); |
| 101 |
}; |
| 102 |
|
| 103 |
|
| 104 |
/** |
| 105 |
* Asserts that the element must have the specified width. |
| 106 |
* @param {Element|string} a The first element or optionally the comment string. |
| 107 |
* @param {Element} b The second element or the first element if comment string |
| 108 |
* is present. |
| 109 |
* @param {Element=} opt_c The second element if comment string is present. |
| 110 |
*/ |
| 111 |
var assertWidth = function(a, b, opt_c) { |
| 112 |
_validateArguments(2, arguments); |
| 113 |
var element = nonCommentArg(1, 2, arguments); |
| 114 |
var width = nonCommentArg(2, 2, arguments); |
| 115 |
var size = goog.style.getSize(element); |
| 116 |
var elementWidth = size.width; |
| 117 |
|
| 118 |
_assert( |
| 119 |
commentArg(1, arguments), |
| 120 |
goog.testing.style.layoutasserts.isWithinThreshold_( |
| 121 |
width, elementWidth, 0 /* tolerance */), |
| 122 |
'Element should have width ' + width + ' but was ' + elementWidth + '.'); |
| 123 |
}; |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Asserts that the element must have the specified width within the specified |
| 128 |
* tolerance. |
| 129 |
* @param {Element|string} a The element or optionally the comment string. |
| 130 |
* @param {number|Element} b The height or the element if comment string is |
| 131 |
* present. |
| 132 |
* @param {number} c The tolerance or the height if comment string is |
| 133 |
* present. |
| 134 |
* @param {number=} opt_d The tolerance if comment string is present. |
| 135 |
*/ |
| 136 |
var assertWidthWithinTolerance = function(a, b, c, opt_d) { |
| 137 |
_validateArguments(3, arguments); |
| 138 |
var element = nonCommentArg(1, 3, arguments); |
| 139 |
var width = nonCommentArg(2, 3, arguments); |
| 140 |
var tolerance = nonCommentArg(3, 3, arguments); |
| 141 |
var size = goog.style.getSize(element); |
| 142 |
var elementWidth = size.width; |
| 143 |
|
| 144 |
_assert( |
| 145 |
commentArg(1, arguments), |
| 146 |
goog.testing.style.layoutasserts.isWithinThreshold_( |
| 147 |
width, elementWidth, tolerance), |
| 148 |
'Element width(' + elementWidth + ') should be within given width(' + |
| 149 |
width + ') with tolerance value of ' + tolerance + '.'); |
| 150 |
}; |
| 151 |
|
| 152 |
|
| 153 |
/** |
| 154 |
* Asserts that the element must have the specified height. |
| 155 |
* @param {Element|string} a The first element or optionally the comment string. |
| 156 |
* @param {Element} b The second element or the first element if comment string |
| 157 |
* is present. |
| 158 |
* @param {Element=} opt_c The second element if comment string is present. |
| 159 |
*/ |
| 160 |
var assertHeight = function(a, b, opt_c) { |
| 161 |
_validateArguments(2, arguments); |
| 162 |
var element = nonCommentArg(1, 2, arguments); |
| 163 |
var height = nonCommentArg(2, 2, arguments); |
| 164 |
var size = goog.style.getSize(element); |
| 165 |
var elementHeight = size.height; |
| 166 |
|
| 167 |
_assert( |
| 168 |
commentArg(1, arguments), |
| 169 |
goog.testing.style.layoutasserts.isWithinThreshold_( |
| 170 |
height, elementHeight, 0 /* tolerance */), |
| 171 |
'Element should have height ' + height + '.'); |
| 172 |
}; |
| 173 |
|
| 174 |
|
| 175 |
/** |
| 176 |
* Asserts that the element must have the specified height within the specified |
| 177 |
* tolerance. |
| 178 |
* @param {Element|string} a The element or optionally the comment string. |
| 179 |
* @param {number|Element} b The height or the element if comment string is |
| 180 |
* present. |
| 181 |
* @param {number} c The tolerance or the height if comment string is |
| 182 |
* present. |
| 183 |
* @param {number=} opt_d The tolerance if comment string is present. |
| 184 |
*/ |
| 185 |
var assertHeightWithinTolerance = function(a, b, c, opt_d) { |
| 186 |
_validateArguments(3, arguments); |
| 187 |
var element = nonCommentArg(1, 3, arguments); |
| 188 |
var height = nonCommentArg(2, 3, arguments); |
| 189 |
var tolerance = nonCommentArg(3, 3, arguments); |
| 190 |
var size = goog.style.getSize(element); |
| 191 |
var elementHeight = size.height; |
| 192 |
|
| 193 |
_assert( |
| 194 |
commentArg(1, arguments), |
| 195 |
goog.testing.style.layoutasserts.isWithinThreshold_( |
| 196 |
height, elementHeight, tolerance), |
| 197 |
'Element width(' + elementHeight + ') should be within given width(' + |
| 198 |
height + ') with tolerance value of ' + tolerance + '.'); |
| 199 |
}; |
| 200 |
|
| 201 |
|
| 202 |
/** |
| 203 |
* Asserts that the first element is to the left of the second element. |
| 204 |
* @param {Element|string} a The first element or optionally the comment string. |
| 205 |
* @param {Element} b The second element or the first element if comment string |
| 206 |
* is present. |
| 207 |
* @param {Element=} opt_c The second element if comment string is present. |
| 208 |
*/ |
| 209 |
var assertIsLeftOf = function(a, b, opt_c) { |
| 210 |
_validateArguments(2, arguments); |
| 211 |
var element = nonCommentArg(1, 2, arguments); |
| 212 |
var otherElement = nonCommentArg(2, 2, arguments); |
| 213 |
var elementRect = goog.style.getBounds(element); |
| 214 |
var otherElementRect = goog.style.getBounds(otherElement); |
| 215 |
|
| 216 |
_assert( |
| 217 |
commentArg(1, arguments), elementRect.left < otherElementRect.left, |
| 218 |
'Elements should be left to right.'); |
| 219 |
}; |
| 220 |
|
| 221 |
|
| 222 |
/** |
| 223 |
* Asserts that the first element is strictly left of the second element. |
| 224 |
* @param {Element|string} a The first element or optionally the comment string. |
| 225 |
* @param {Element} b The second element or the first element if comment string |
| 226 |
* is present. |
| 227 |
* @param {Element=} opt_c The second element if comment string is present. |
| 228 |
*/ |
| 229 |
var assertIsStrictlyLeftOf = function(a, b, opt_c) { |
| 230 |
_validateArguments(2, arguments); |
| 231 |
var element = nonCommentArg(1, 2, arguments); |
| 232 |
var otherElement = nonCommentArg(2, 2, arguments); |
| 233 |
var elementRect = goog.style.getBounds(element); |
| 234 |
var otherElementRect = goog.style.getBounds(otherElement); |
| 235 |
|
| 236 |
_assert( |
| 237 |
commentArg(1, arguments), |
| 238 |
elementRect.left + elementRect.width < otherElementRect.left, |
| 239 |
'Elements should be strictly left to right.'); |
| 240 |
}; |
| 241 |
|
| 242 |
|
| 243 |
/** |
| 244 |
* Asserts that the first element is higher than the second element. |
| 245 |
* @param {Element|string} a The first element or optionally the comment string. |
| 246 |
* @param {Element} b The second element or the first element if comment string |
| 247 |
* is present. |
| 248 |
* @param {Element=} opt_c The second element if comment string is present. |
| 249 |
*/ |
| 250 |
var assertIsAbove = function(a, b, opt_c) { |
| 251 |
_validateArguments(2, arguments); |
| 252 |
var element = nonCommentArg(1, 2, arguments); |
| 253 |
var otherElement = nonCommentArg(2, 2, arguments); |
| 254 |
var elementRect = goog.style.getBounds(element); |
| 255 |
var otherElementRect = goog.style.getBounds(otherElement); |
| 256 |
|
| 257 |
_assert( |
| 258 |
commentArg(1, arguments), elementRect.top < otherElementRect.top, |
| 259 |
'Elements should be top to bottom.'); |
| 260 |
}; |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Asserts that the first element is strictly higher than the second element. |
| 265 |
* @param {Element|string} a The first element or optionally the comment string. |
| 266 |
* @param {Element} b The second element or the first element if comment string |
| 267 |
* is present. |
| 268 |
* @param {Element=} opt_c The second element if comment string is present. |
| 269 |
*/ |
| 270 |
var assertIsStrictlyAbove = function(a, b, opt_c) { |
| 271 |
_validateArguments(2, arguments); |
| 272 |
var element = nonCommentArg(1, 2, arguments); |
| 273 |
var otherElement = nonCommentArg(2, 2, arguments); |
| 274 |
var elementRect = goog.style.getBounds(element); |
| 275 |
var otherElementRect = goog.style.getBounds(otherElement); |
| 276 |
|
| 277 |
_assert( |
| 278 |
commentArg(1, arguments), |
| 279 |
elementRect.top + elementRect.height < otherElementRect.top, |
| 280 |
'Elements should be strictly top to bottom.'); |
| 281 |
}; |
| 282 |
|
| 283 |
|
| 284 |
/** |
| 285 |
* Asserts that the first element's bounds contain the bounds of the second |
| 286 |
* element. |
| 287 |
* @param {Element|string} a The first element or optionally the comment string. |
| 288 |
* @param {Element} b The second element or the first element if comment string |
| 289 |
* is present. |
| 290 |
* @param {Element=} opt_c The second element if comment string is present. |
| 291 |
*/ |
| 292 |
var assertContained = function(a, b, opt_c) { |
| 293 |
_validateArguments(2, arguments); |
| 294 |
var element = nonCommentArg(1, 2, arguments); |
| 295 |
var otherElement = nonCommentArg(2, 2, arguments); |
| 296 |
var elementRect = goog.style.getBounds(element); |
| 297 |
var otherElementRect = goog.style.getBounds(otherElement); |
| 298 |
|
| 299 |
_assert( |
| 300 |
commentArg(1, arguments), elementRect.contains(otherElementRect), |
| 301 |
'Element should be contained within the other element.'); |
| 302 |
}; |
| 303 |
|
| 304 |
|
| 305 |
/** |
| 306 |
* Returns true if the difference between val1 and val2 is less than or equal to |
| 307 |
* the threashold. |
| 308 |
* @param {number} val1 The first value. |
| 309 |
* @param {number} val2 The second value. |
| 310 |
* @param {number} threshold The threshold value. |
| 311 |
* @return {boolean} Whether or not the the values are within the threshold. |
| 312 |
* @private |
| 313 |
*/ |
| 314 |
goog.testing.style.layoutasserts.isWithinThreshold_ = function( |
| 315 |
val1, val2, threshold) { |
| 316 |
return Math.abs(val1 - val2) <= threshold; |
| 317 |
}; |