layoutasserts.js 11 KB

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