box_test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // Copyright 2006 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.math.BoxTest');
  15. goog.setTestOnly('goog.math.BoxTest');
  16. goog.require('goog.math.Box');
  17. goog.require('goog.math.Coordinate');
  18. goog.require('goog.testing.jsunit');
  19. function testBoxEquals() {
  20. var a = new goog.math.Box(1, 2, 3, 4);
  21. var b = new goog.math.Box(1, 2, 3, 4);
  22. assertTrue(goog.math.Box.equals(a, a));
  23. assertTrue(goog.math.Box.equals(a, b));
  24. assertTrue(goog.math.Box.equals(b, a));
  25. assertFalse('Box should not equal null.', goog.math.Box.equals(a, null));
  26. assertFalse('Box should not equal null.', goog.math.Box.equals(null, a));
  27. assertFalse(goog.math.Box.equals(a, new goog.math.Box(4, 2, 3, 4)));
  28. assertFalse(goog.math.Box.equals(a, new goog.math.Box(1, 4, 3, 4)));
  29. assertFalse(goog.math.Box.equals(a, new goog.math.Box(1, 2, 4, 4)));
  30. assertFalse(goog.math.Box.equals(a, new goog.math.Box(1, 2, 3, 1)));
  31. assertTrue('Null boxes should be equal.', goog.math.Box.equals(null, null));
  32. }
  33. function testBoxClone() {
  34. var b = new goog.math.Box(0, 0, 0, 0);
  35. assertTrue(goog.math.Box.equals(b, b.clone()));
  36. b.left = 0;
  37. b.top = 1;
  38. b.right = 2;
  39. b.bottom = 3;
  40. assertTrue(goog.math.Box.equals(b, b.clone()));
  41. }
  42. function testBoxRelativePositionX() {
  43. var box = new goog.math.Box(50, 100, 100, 50);
  44. assertEquals(
  45. 0, goog.math.Box.relativePositionX(box, new goog.math.Coordinate(75, 0)));
  46. assertEquals(
  47. 0,
  48. goog.math.Box.relativePositionX(box, new goog.math.Coordinate(75, 75)));
  49. assertEquals(
  50. 0,
  51. goog.math.Box.relativePositionX(box, new goog.math.Coordinate(75, 105)));
  52. assertEquals(
  53. -5,
  54. goog.math.Box.relativePositionX(box, new goog.math.Coordinate(45, 75)));
  55. assertEquals(
  56. 5,
  57. goog.math.Box.relativePositionX(box, new goog.math.Coordinate(105, 75)));
  58. }
  59. function testBoxRelativePositionY() {
  60. var box = new goog.math.Box(50, 100, 100, 50);
  61. assertEquals(
  62. 0, goog.math.Box.relativePositionY(box, new goog.math.Coordinate(0, 75)));
  63. assertEquals(
  64. 0,
  65. goog.math.Box.relativePositionY(box, new goog.math.Coordinate(75, 75)));
  66. assertEquals(
  67. 0,
  68. goog.math.Box.relativePositionY(box, new goog.math.Coordinate(105, 75)));
  69. assertEquals(
  70. -5,
  71. goog.math.Box.relativePositionY(box, new goog.math.Coordinate(75, 45)));
  72. assertEquals(
  73. 5,
  74. goog.math.Box.relativePositionY(box, new goog.math.Coordinate(75, 105)));
  75. }
  76. function testBoxDistance() {
  77. var box = new goog.math.Box(50, 100, 100, 50);
  78. assertEquals(
  79. 0, goog.math.Box.distance(box, new goog.math.Coordinate(75, 75)));
  80. assertEquals(
  81. 25, goog.math.Box.distance(box, new goog.math.Coordinate(75, 25)));
  82. assertEquals(
  83. 10, goog.math.Box.distance(box, new goog.math.Coordinate(40, 80)));
  84. assertEquals(
  85. 5, goog.math.Box.distance(box, new goog.math.Coordinate(46, 47)));
  86. assertEquals(
  87. 10, goog.math.Box.distance(box, new goog.math.Coordinate(106, 108)));
  88. }
  89. function testBoxContains() {
  90. var box = new goog.math.Box(50, 100, 100, 50);
  91. assertTrue(goog.math.Box.contains(box, new goog.math.Coordinate(75, 75)));
  92. assertTrue(goog.math.Box.contains(box, new goog.math.Coordinate(50, 100)));
  93. assertTrue(goog.math.Box.contains(box, new goog.math.Coordinate(100, 99)));
  94. assertFalse(goog.math.Box.contains(box, new goog.math.Coordinate(100, 101)));
  95. assertFalse(goog.math.Box.contains(box, new goog.math.Coordinate(49, 50)));
  96. assertFalse(goog.math.Box.contains(box, new goog.math.Coordinate(25, 25)));
  97. }
  98. function testBoxContainsBox() {
  99. var box = new goog.math.Box(50, 100, 100, 50);
  100. function assertContains(boxB) {
  101. assertTrue(
  102. box + ' expected to contain ' + boxB,
  103. goog.math.Box.contains(box, boxB));
  104. }
  105. function assertNotContains(boxB) {
  106. assertFalse(
  107. box + ' expected to not contain ' + boxB,
  108. goog.math.Box.contains(box, boxB));
  109. }
  110. assertContains(new goog.math.Box(60, 90, 90, 60));
  111. assertNotContains(new goog.math.Box(1, 3, 4, 2));
  112. assertNotContains(new goog.math.Box(30, 90, 60, 60));
  113. assertNotContains(new goog.math.Box(60, 110, 60, 60));
  114. assertNotContains(new goog.math.Box(60, 90, 110, 60));
  115. assertNotContains(new goog.math.Box(60, 90, 60, 40));
  116. }
  117. function testBoxesIntersect() {
  118. var box = new goog.math.Box(50, 100, 100, 50);
  119. function assertIntersects(boxB) {
  120. assertTrue(
  121. box + ' expected to intersect ' + boxB,
  122. goog.math.Box.intersects(box, boxB));
  123. }
  124. function assertNotIntersects(boxB) {
  125. assertFalse(
  126. box + ' expected to not intersect ' + boxB,
  127. goog.math.Box.intersects(box, boxB));
  128. }
  129. assertIntersects(box);
  130. assertIntersects(new goog.math.Box(20, 80, 80, 20));
  131. assertIntersects(new goog.math.Box(50, 80, 100, 20));
  132. assertIntersects(new goog.math.Box(80, 80, 120, 20));
  133. assertIntersects(new goog.math.Box(20, 100, 80, 50));
  134. assertIntersects(new goog.math.Box(80, 100, 120, 50));
  135. assertIntersects(new goog.math.Box(20, 120, 80, 80));
  136. assertIntersects(new goog.math.Box(50, 120, 100, 80));
  137. assertIntersects(new goog.math.Box(80, 120, 120, 80));
  138. assertIntersects(new goog.math.Box(20, 120, 120, 20));
  139. assertIntersects(new goog.math.Box(70, 80, 80, 70));
  140. assertNotIntersects(new goog.math.Box(10, 30, 30, 10));
  141. assertNotIntersects(new goog.math.Box(10, 70, 30, 30));
  142. assertNotIntersects(new goog.math.Box(10, 100, 30, 50));
  143. assertNotIntersects(new goog.math.Box(10, 120, 30, 80));
  144. assertNotIntersects(new goog.math.Box(10, 140, 30, 120));
  145. assertNotIntersects(new goog.math.Box(30, 30, 70, 10));
  146. assertNotIntersects(new goog.math.Box(30, 140, 70, 120));
  147. assertNotIntersects(new goog.math.Box(50, 30, 100, 10));
  148. assertNotIntersects(new goog.math.Box(50, 140, 100, 120));
  149. assertNotIntersects(new goog.math.Box(80, 30, 120, 10));
  150. assertNotIntersects(new goog.math.Box(80, 140, 120, 120));
  151. assertNotIntersects(new goog.math.Box(120, 30, 140, 10));
  152. assertNotIntersects(new goog.math.Box(120, 70, 140, 30));
  153. assertNotIntersects(new goog.math.Box(120, 100, 140, 50));
  154. assertNotIntersects(new goog.math.Box(120, 120, 140, 80));
  155. assertNotIntersects(new goog.math.Box(120, 140, 140, 120));
  156. }
  157. function testBoxesIntersectWithPadding() {
  158. var box = new goog.math.Box(50, 100, 100, 50);
  159. function assertIntersects(boxB, padding) {
  160. assertTrue(
  161. box + ' expected to intersect ' + boxB + ' with padding ' + padding,
  162. goog.math.Box.intersectsWithPadding(box, boxB, padding));
  163. }
  164. function assertNotIntersects(boxB, padding) {
  165. assertFalse(
  166. box + ' expected to not intersect ' + boxB + ' with padding ' + padding,
  167. goog.math.Box.intersectsWithPadding(box, boxB, padding));
  168. }
  169. assertIntersects(box, 10);
  170. assertIntersects(new goog.math.Box(20, 80, 80, 20), 10);
  171. assertIntersects(new goog.math.Box(50, 80, 100, 20), 10);
  172. assertIntersects(new goog.math.Box(80, 80, 120, 20), 10);
  173. assertIntersects(new goog.math.Box(20, 100, 80, 50), 10);
  174. assertIntersects(new goog.math.Box(80, 100, 120, 50), 10);
  175. assertIntersects(new goog.math.Box(20, 120, 80, 80), 10);
  176. assertIntersects(new goog.math.Box(50, 120, 100, 80), 10);
  177. assertIntersects(new goog.math.Box(80, 120, 120, 80), 10);
  178. assertIntersects(new goog.math.Box(20, 120, 120, 20), 10);
  179. assertIntersects(new goog.math.Box(70, 80, 80, 70), 10);
  180. assertIntersects(new goog.math.Box(10, 30, 30, 10), 20);
  181. assertIntersects(new goog.math.Box(10, 70, 30, 30), 20);
  182. assertIntersects(new goog.math.Box(10, 100, 30, 50), 20);
  183. assertIntersects(new goog.math.Box(10, 120, 30, 80), 20);
  184. assertIntersects(new goog.math.Box(10, 140, 30, 120), 20);
  185. assertIntersects(new goog.math.Box(30, 30, 70, 10), 20);
  186. assertIntersects(new goog.math.Box(30, 140, 70, 120), 20);
  187. assertIntersects(new goog.math.Box(50, 30, 100, 10), 20);
  188. assertIntersects(new goog.math.Box(50, 140, 100, 120), 20);
  189. assertIntersects(new goog.math.Box(80, 30, 120, 10), 20);
  190. assertIntersects(new goog.math.Box(80, 140, 120, 120), 20);
  191. assertIntersects(new goog.math.Box(120, 30, 140, 10), 20);
  192. assertIntersects(new goog.math.Box(120, 70, 140, 30), 20);
  193. assertIntersects(new goog.math.Box(120, 100, 140, 50), 20);
  194. assertIntersects(new goog.math.Box(120, 120, 140, 80), 20);
  195. assertIntersects(new goog.math.Box(120, 140, 140, 120), 20);
  196. assertNotIntersects(new goog.math.Box(10, 30, 30, 10), 10);
  197. assertNotIntersects(new goog.math.Box(10, 70, 30, 30), 10);
  198. assertNotIntersects(new goog.math.Box(10, 100, 30, 50), 10);
  199. assertNotIntersects(new goog.math.Box(10, 120, 30, 80), 10);
  200. assertNotIntersects(new goog.math.Box(10, 140, 30, 120), 10);
  201. assertNotIntersects(new goog.math.Box(30, 30, 70, 10), 10);
  202. assertNotIntersects(new goog.math.Box(30, 140, 70, 120), 10);
  203. assertNotIntersects(new goog.math.Box(50, 30, 100, 10), 10);
  204. assertNotIntersects(new goog.math.Box(50, 140, 100, 120), 10);
  205. assertNotIntersects(new goog.math.Box(80, 30, 120, 10), 10);
  206. assertNotIntersects(new goog.math.Box(80, 140, 120, 120), 10);
  207. assertNotIntersects(new goog.math.Box(120, 30, 140, 10), 10);
  208. assertNotIntersects(new goog.math.Box(120, 70, 140, 30), 10);
  209. assertNotIntersects(new goog.math.Box(120, 100, 140, 50), 10);
  210. assertNotIntersects(new goog.math.Box(120, 120, 140, 80), 10);
  211. assertNotIntersects(new goog.math.Box(120, 140, 140, 120), 10);
  212. }
  213. function testExpandToInclude() {
  214. var box = new goog.math.Box(10, 50, 50, 10);
  215. box.expandToInclude(new goog.math.Box(60, 70, 70, 60));
  216. assertEquals(10, box.left);
  217. assertEquals(10, box.top);
  218. assertEquals(70, box.right);
  219. assertEquals(70, box.bottom);
  220. box.expandToInclude(new goog.math.Box(30, 40, 40, 30));
  221. assertEquals(10, box.left);
  222. assertEquals(10, box.top);
  223. assertEquals(70, box.right);
  224. assertEquals(70, box.bottom);
  225. box.expandToInclude(new goog.math.Box(0, 100, 100, 0));
  226. assertEquals(0, box.left);
  227. assertEquals(0, box.top);
  228. assertEquals(100, box.right);
  229. assertEquals(100, box.bottom);
  230. }
  231. function testExpandToIncludeCoordinate() {
  232. var box = new goog.math.Box(10, 50, 50, 10);
  233. box.expandToIncludeCoordinate(new goog.math.Coordinate(0, 0));
  234. assertEquals(0, box.left);
  235. assertEquals(0, box.top);
  236. assertEquals(50, box.right);
  237. assertEquals(50, box.bottom);
  238. box.expandToIncludeCoordinate(new goog.math.Coordinate(100, 0));
  239. assertEquals(0, box.left);
  240. assertEquals(0, box.top);
  241. assertEquals(100, box.right);
  242. assertEquals(50, box.bottom);
  243. box.expandToIncludeCoordinate(new goog.math.Coordinate(0, 100));
  244. assertEquals(0, box.left);
  245. assertEquals(0, box.top);
  246. assertEquals(100, box.right);
  247. assertEquals(100, box.bottom);
  248. }
  249. function testGetWidth() {
  250. var box = new goog.math.Box(10, 50, 30, 25);
  251. assertEquals(25, box.getWidth());
  252. }
  253. function testGetHeight() {
  254. var box = new goog.math.Box(10, 50, 30, 25);
  255. assertEquals(20, box.getHeight());
  256. }
  257. function testBoundingBox() {
  258. assertObjectEquals(
  259. new goog.math.Box(1, 10, 11, 0),
  260. goog.math.Box.boundingBox(
  261. new goog.math.Coordinate(5, 5), new goog.math.Coordinate(5, 11),
  262. new goog.math.Coordinate(0, 5), new goog.math.Coordinate(5, 1),
  263. new goog.math.Coordinate(10, 5)));
  264. }
  265. function testBoxCeil() {
  266. var box = new goog.math.Box(11.4, 26.6, 17.8, 9.2);
  267. assertEquals(
  268. 'The function should return the target instance', box, box.ceil());
  269. assertObjectEquals(new goog.math.Box(12, 27, 18, 10), box);
  270. }
  271. function testBoxFloor() {
  272. var box = new goog.math.Box(11.4, 26.6, 17.8, 9.2);
  273. assertEquals(
  274. 'The function should return the target instance', box, box.floor());
  275. assertObjectEquals(new goog.math.Box(11, 26, 17, 9), box);
  276. }
  277. function testBoxRound() {
  278. var box = new goog.math.Box(11.4, 26.6, 17.8, 9.2);
  279. assertEquals(
  280. 'The function should return the target instance', box, box.round());
  281. assertObjectEquals(new goog.math.Box(11, 27, 18, 9), box);
  282. }
  283. function testBoxTranslateCoordinate() {
  284. var box = new goog.math.Box(10, 30, 20, 5);
  285. var c = new goog.math.Coordinate(10, 5);
  286. assertEquals(
  287. 'The function should return the target instance', box, box.translate(c));
  288. assertObjectEquals(new goog.math.Box(15, 40, 25, 15), box);
  289. }
  290. function testBoxTranslateXY() {
  291. var box = new goog.math.Box(10, 30, 20, 5);
  292. assertEquals(
  293. 'The function should return the target instance', box,
  294. box.translate(5, 2));
  295. assertObjectEquals(new goog.math.Box(12, 35, 22, 10), box);
  296. }
  297. function testBoxTranslateX() {
  298. var box = new goog.math.Box(10, 30, 20, 5);
  299. assertEquals(
  300. 'The function should return the target instance', box, box.translate(3));
  301. assertObjectEquals(new goog.math.Box(10, 33, 20, 8), box);
  302. }
  303. function testBoxScaleXY() {
  304. var box = new goog.math.Box(10, 20, 30, 5);
  305. assertEquals(
  306. 'The function should return the target instance', box, box.scale(2, 3));
  307. assertObjectEquals(new goog.math.Box(30, 40, 90, 10), box);
  308. }
  309. function testBoxScaleFactor() {
  310. var box = new goog.math.Box(10, 20, 30, 5);
  311. assertEquals(
  312. 'The function should return the target instance', box, box.scale(2));
  313. assertObjectEquals(new goog.math.Box(20, 40, 60, 10), box);
  314. }