layoutasserts_test.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. goog.provide('goog.testing.style.layoutassertsTest');
  15. goog.setTestOnly('goog.testing.style.layoutassertsTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.style');
  19. goog.require('goog.testing.TestCase');
  20. goog.require('goog.testing.jsunit');
  21. /** @suppress {extraRequire} */
  22. goog.require('goog.testing.style.layoutasserts');
  23. var div1;
  24. var div2;
  25. var DEFAULT_WIDTH = 200;
  26. var DEFAULT_HEIGHT = 100;
  27. function setUp() {
  28. // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
  29. goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
  30. div1 = goog.dom.createDom(goog.dom.TagName.DIV, {
  31. id: 'test',
  32. className: 'test',
  33. style: 'position:absolute;top:0;left:0;' +
  34. 'width:' + DEFAULT_WIDTH + 'px;' +
  35. 'height:' + DEFAULT_HEIGHT + 'px;' +
  36. 'background-color:#EEE',
  37. innerHTML: 'abc'
  38. });
  39. div2 = goog.dom.createDom(goog.dom.TagName.DIV, {
  40. id: 'test2',
  41. className: 'test2',
  42. style: 'position:absolute;' +
  43. 'top:0;left:0;' +
  44. 'width:' + DEFAULT_WIDTH + 'px;' +
  45. 'height:' + DEFAULT_HEIGHT + 'px;' +
  46. 'background-color:#F00',
  47. innerHTML: 'abc'
  48. });
  49. }
  50. function tearDown() {
  51. div1 = null;
  52. div2 = null;
  53. }
  54. /**
  55. * Tests assertIsVisible.
  56. */
  57. function testAssertIsVisible() {
  58. assertThrows(
  59. 'Exception should be thrown when asserting visibility.',
  60. goog.bind(assertIsVisible, null, null)); // assertIsVisible(null)
  61. // Attach it to BODY tag and assert that it is visible.
  62. document.body.appendChild(div1);
  63. assertIsVisible('Div should be visible.', div1);
  64. // Tests with hidden element
  65. failed = false;
  66. goog.style.setElementShown(div1, false /* display */);
  67. assertThrows(
  68. 'Exception should be thrown when asserting visibility.',
  69. goog.bind(assertIsVisible, null, div1));
  70. // Clean up.
  71. document.body.removeChild(div1);
  72. }
  73. /**
  74. * Tests assertNotVisible.
  75. */
  76. function testAssertNotVisible() {
  77. // Tests null as a parameter.
  78. var element = null;
  79. assertNotVisible(element);
  80. // Attach the element to BODY element, assert should fail.
  81. document.body.appendChild(div1);
  82. assertThrows(
  83. 'Exception should be thrown when asserting non-visibility.',
  84. goog.bind(assertNotVisible, null, div1));
  85. // Clean up.
  86. document.body.removeChild(div1);
  87. }
  88. /**
  89. * Tests assertIsIntersect.
  90. */
  91. function testAssertIntersect() {
  92. document.body.appendChild(div1);
  93. document.body.appendChild(div2);
  94. // No intersection
  95. goog.style.setPosition(div1, 0, 0);
  96. goog.style.setPosition(div2, 500, 500);
  97. assertThrows(
  98. 'Exception should be thrown when asserting intersection.',
  99. goog.bind(assertIntersect, null, div1, div2));
  100. assertNoIntersect(div1, div2);
  101. // Some intersection
  102. goog.style.setPosition(div1, 0, 0);
  103. goog.style.setPosition(div2, 50, 50);
  104. assertThrows(
  105. 'Exception should be thrown when asserting no intersection.',
  106. goog.bind(assertNoIntersect, null, div1, div2));
  107. assertIntersect(div1, div2);
  108. // Completely superimposed.
  109. goog.style.setPosition(div1, 0, 0);
  110. goog.style.setPosition(div2, 0, 0);
  111. assertThrows(
  112. 'Exception should be thrown when asserting no intersection.',
  113. goog.bind(assertNoIntersect, null, div1, div2));
  114. assertIntersect(div1, div2);
  115. }
  116. /**
  117. * Tests assertWidth.
  118. */
  119. function testAssertWidth() {
  120. document.body.appendChild(div1);
  121. // Test correct width
  122. assertWidth(div1, DEFAULT_WIDTH);
  123. // Test wrong width
  124. assertThrows(
  125. 'Exception should be thrown when elements has wrong width',
  126. goog.bind(assertWidth, null, div1, 400));
  127. // Test a valid tolerance value
  128. assertWidthWithinTolerance(div1, 180, 20);
  129. // Test exceeding tolerance value
  130. assertThrows(
  131. 'Exception should be thrown when element\'s width exceeds tolerance',
  132. goog.bind(assertWidthWithinTolerance, null, div1, 100, 0.1));
  133. }
  134. /**
  135. * Tests assertHeight.
  136. */
  137. function testAssertHeight() {
  138. document.body.appendChild(div1);
  139. // Test correct height
  140. assertHeight(div1, DEFAULT_HEIGHT);
  141. // Test wrong height
  142. assertThrows(
  143. 'Exception should be thrown when element has wrong height.',
  144. goog.bind(assertHeightWithinTolerance, null, div1, 300));
  145. // Test a valid tolerance value
  146. assertHeightWithinTolerance(div1, 90, 10);
  147. // Test exceeding tolerance value
  148. assertThrows(
  149. 'Exception should be thrown when element\'s height exceeds tolerance',
  150. goog.bind(assertHeight, null, div1, 50, 0.2));
  151. }
  152. /**
  153. * Tests assertIsLeftOf.
  154. */
  155. function testAssertIsLeftOf() {
  156. document.body.appendChild(div1);
  157. document.body.appendChild(div2);
  158. // Test elements of same size & location
  159. assertThrows(
  160. 'Exception should be thrown when elements intersect.',
  161. goog.bind(assertIsLeftOf, null, div1, div2));
  162. assertThrows(
  163. 'Exception should be thrown when elements intersect.',
  164. goog.bind(assertIsStrictlyLeftOf, null, div1, div2));
  165. // Test elements that are not left to right
  166. goog.style.setPosition(div1, 100, 0);
  167. goog.style.setPosition(div2, 0, 0);
  168. assertThrows(
  169. 'Exception should be thrown when elements are not left to right.',
  170. goog.bind(assertIsLeftOf, null, div1, div2));
  171. assertThrows(
  172. 'Exception should be thrown when elements are not left to right.',
  173. goog.bind(assertIsStrictlyLeftOf, null, div1, div2));
  174. // Test elements that intersect, but is left to right
  175. goog.style.setPosition(div1, 0, 0);
  176. goog.style.setPosition(div2, 100, 0);
  177. assertIsLeftOf(div1, div2);
  178. assertThrows(
  179. 'Exception should be thrown when elements intersect.',
  180. goog.bind(assertIsStrictlyLeftOf, null, div1, div2));
  181. // Test elements that are strictly left to right
  182. goog.style.setPosition(div1, 0, 0);
  183. goog.style.setPosition(div2, 999, 0);
  184. assertIsLeftOf(div1, div2);
  185. assertIsStrictlyLeftOf(div1, div2);
  186. }
  187. /**
  188. * Tests assertIsAbove.
  189. */
  190. function testAssertIsAbove() {
  191. document.body.appendChild(div1);
  192. document.body.appendChild(div2);
  193. // Test elements of same size & location
  194. assertThrows(
  195. 'Exception should be thrown when elements intersect.',
  196. goog.bind(assertIsAbove, null, div1, div2));
  197. assertThrows(
  198. 'Exception should be thrown when elements intersect.',
  199. goog.bind(assertIsStrictlyAbove, null, div1, div2));
  200. // Test elements that are not top to bottom
  201. goog.style.setPosition(div1, 0, 999);
  202. goog.style.setPosition(div2, 0, 0);
  203. assertThrows(
  204. 'Exception should be thrown when elements are not top to bottom.',
  205. goog.bind(assertIsAbove, null, div1, div2));
  206. assertThrows(
  207. 'Exception should be thrown when elements are not top to bottom.',
  208. goog.bind(assertIsStrictlyAbove, null, div1, div2));
  209. // Test elements that intersect, but is top to bottom
  210. goog.style.setPosition(div1, 0, 0);
  211. goog.style.setPosition(div2, 0, 50);
  212. assertIsAbove(div1, div2);
  213. assertThrows(
  214. 'Exception should be thrown when elements intersect.',
  215. goog.bind(assertIsStrictlyAbove, null, div1, div2));
  216. // Test elements that are top to bottom
  217. goog.style.setPosition(div1, 0, 0);
  218. goog.style.setPosition(div2, 0, 999);
  219. assertIsAbove(div1, div2);
  220. assertIsStrictlyAbove(div1, div2);
  221. }