path_test.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. // Copyright 2008 The Closure Library Authors. All Rights Reserved.
  2. // Use of this source code is governed by the Apache License, Version 2.0.
  3. goog.provide('goog.graphics.PathTest');
  4. goog.setTestOnly('goog.graphics.PathTest');
  5. goog.require('goog.array');
  6. goog.require('goog.math');
  7. goog.require('goog.graphics.Path');
  8. goog.require('goog.graphics.AffineTransform');
  9. goog.require('goog.testing.graphics');
  10. goog.require('goog.testing.jsunit');
  11. function testConstructor() {
  12. var path = new goog.graphics.Path();
  13. assertTrue(path.isSimple());
  14. assertNull(path.getCurrentPoint());
  15. goog.testing.graphics.assertPathEquals([], path);
  16. }
  17. function testGetSegmentCount() {
  18. assertArrayEquals([2, 2, 6, 6, 0], goog.array.map([
  19. goog.graphics.Path.Segment.MOVETO,
  20. goog.graphics.Path.Segment.LINETO,
  21. goog.graphics.Path.Segment.CURVETO,
  22. goog.graphics.Path.Segment.ARCTO,
  23. goog.graphics.Path.Segment.CLOSE
  24. ], goog.graphics.Path.getSegmentCount));
  25. }
  26. function testSimpleMoveTo() {
  27. var path = new goog.graphics.Path();
  28. path.moveTo(30, 50);
  29. assertTrue(path.isSimple());
  30. assertObjectEquals([30, 50], path.getCurrentPoint());
  31. goog.testing.graphics.assertPathEquals(['M', 30, 50], path);
  32. }
  33. function testRepeatedMoveTo() {
  34. var path = new goog.graphics.Path();
  35. path.moveTo(30, 50);
  36. path.moveTo(40, 60);
  37. assertTrue(path.isSimple());
  38. assertObjectEquals([40, 60], path.getCurrentPoint());
  39. goog.testing.graphics.assertPathEquals(['M', 40, 60], path);
  40. }
  41. function testSimpleLineTo() {
  42. var path = new goog.graphics.Path();
  43. var e = assertThrows(function() {
  44. path.lineTo(30, 50);
  45. });
  46. assertEquals('Path cannot start with lineTo', e.message);
  47. path.moveTo(0, 0);
  48. path.lineTo(30, 50);
  49. assertTrue(path.isSimple());
  50. assertObjectEquals([30, 50], path.getCurrentPoint());
  51. goog.testing.graphics.assertPathEquals(['M', 0, 0, 'L', 30, 50], path);
  52. }
  53. function testMultiArgLineTo() {
  54. var path = new goog.graphics.Path();
  55. path.moveTo(0, 0);
  56. path.lineTo(30, 50, 40 , 60);
  57. assertTrue(path.isSimple());
  58. assertObjectEquals([40, 60], path.getCurrentPoint());
  59. goog.testing.graphics.assertPathEquals(['M', 0, 0, 'L', 30, 50, 40, 60],
  60. path);
  61. }
  62. function testRepeatedLineTo() {
  63. var path = new goog.graphics.Path();
  64. path.moveTo(0, 0);
  65. path.lineTo(30, 50);
  66. path.lineTo(40, 60);
  67. assertTrue(path.isSimple());
  68. assertObjectEquals([40, 60], path.getCurrentPoint());
  69. goog.testing.graphics.assertPathEquals(['M', 0, 0, 'L', 30, 50, 40, 60],
  70. path);
  71. }
  72. function testSimpleCurveTo() {
  73. var path = new goog.graphics.Path();
  74. var e = assertThrows(function() {
  75. path.curveTo(10, 20, 30, 40, 50, 60);
  76. });
  77. assertEquals('Path cannot start with curve', e.message);
  78. path.moveTo(0, 0);
  79. path.curveTo(10, 20, 30, 40, 50, 60);
  80. assertTrue(path.isSimple());
  81. assertObjectEquals([50, 60], path.getCurrentPoint());
  82. goog.testing.graphics.assertPathEquals(
  83. ['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60], path);
  84. }
  85. function testMultiCurveTo() {
  86. var path = new goog.graphics.Path();
  87. path.moveTo(0, 0);
  88. path.curveTo(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120);
  89. assertTrue(path.isSimple());
  90. assertObjectEquals([110, 120], path.getCurrentPoint());
  91. goog.testing.graphics.assertPathEquals(
  92. ['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
  93. path);
  94. }
  95. function testRepeatedCurveTo() {
  96. var path = new goog.graphics.Path();
  97. path.moveTo(0, 0);
  98. path.curveTo(10, 20, 30, 40, 50, 60);
  99. path.curveTo(70, 80, 90, 100, 110, 120);
  100. assertTrue(path.isSimple());
  101. assertObjectEquals([110, 120], path.getCurrentPoint());
  102. goog.testing.graphics.assertPathEquals(
  103. ['M', 0, 0, 'C', 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
  104. path);
  105. }
  106. function testSimpleArc() {
  107. var path = new goog.graphics.Path();
  108. path.arc(50, 60, 10, 20, 30, 30, false);
  109. assertFalse(path.isSimple());
  110. var p = path.getCurrentPoint();
  111. assertEquals(55, p[0]);
  112. assertRoughlyEquals(77.32, p[1], 0.01);
  113. goog.testing.graphics.assertPathEquals(
  114. ['M', 58.66, 70, 'A', 10, 20, 30, 30, 55, 77.32], path);
  115. }
  116. function testArcNonConnectClose() {
  117. var path = new goog.graphics.Path();
  118. path.moveTo(0, 0);
  119. path.arc(10, 10, 10, 10, -90, 180);
  120. assertObjectEquals([10, 20], path.getCurrentPoint());
  121. path.close();
  122. assertObjectEquals([10, 0], path.getCurrentPoint());
  123. }
  124. function testRepeatedArc() {
  125. var path = new goog.graphics.Path();
  126. path.arc(50, 60, 10, 20, 30, 30, false);
  127. path.arc(50, 60, 10, 20, 60, 30, false);
  128. assertFalse(path.isSimple());
  129. assertObjectEquals([50, 80], path.getCurrentPoint());
  130. goog.testing.graphics.assertPathEquals(['M', 58.66, 70,
  131. 'A', 10, 20, 30, 30, 55, 77.32,
  132. 'M', 55, 77.32,
  133. 'A', 10, 20, 60, 30, 50, 80], path);
  134. }
  135. function testRepeatedArc2() {
  136. var path = new goog.graphics.Path();
  137. path.arc(50, 60, 10, 20, 30, 30, false);
  138. path.arc(50, 60, 10, 20, 60, 30, true);
  139. goog.testing.graphics.assertPathEquals(['M', 58.66, 70,
  140. 'A', 10, 20, 30, 30, 55, 77.32,
  141. 'A', 10, 20, 60, 30, 50, 80], path);
  142. }
  143. function testCompleteCircle() {
  144. var path = new goog.graphics.Path();
  145. path.arc(0, 0, 10, 10, 0, 360, false);
  146. assertFalse(path.isSimple());
  147. var p = path.getCurrentPoint();
  148. assertRoughlyEquals(10, p[0], 0.01);
  149. assertRoughlyEquals(0, p[1], 0.01);
  150. goog.testing.graphics.assertPathEquals(
  151. ['M', 10, 0, 'A', 10, 10, 0, 360, 10, 0], path);
  152. }
  153. function testClose() {
  154. var path = new goog.graphics.Path();
  155. try {
  156. path.close();
  157. fail();
  158. } catch (e) {
  159. // Expected
  160. assertEquals('Path cannot start with close', e.message);
  161. }
  162. path.moveTo(0, 0);
  163. path.lineTo(10, 20, 30, 40, 50, 60);
  164. path.close()
  165. assertTrue(path.isSimple());
  166. assertObjectEquals([0, 0], path.getCurrentPoint());
  167. goog.testing.graphics.assertPathEquals(
  168. ['M', 0, 0, 'L', 10, 20, 30, 40, 50, 60, 'X'], path);
  169. }
  170. function testClear() {
  171. var path = new goog.graphics.Path();
  172. path.moveTo(0, 0);
  173. path.arc(50, 60, 10, 20, 30, 30, false);
  174. path.clear();
  175. assertTrue(path.isSimple());
  176. assertNull(path.getCurrentPoint());
  177. goog.testing.graphics.assertPathEquals([], path);
  178. }
  179. function testCreateSimplifiedPath() {
  180. var path = new goog.graphics.Path();
  181. path.moveTo(0, 0);
  182. path.arc(50, 60, 10, 20, 30, 30, false);
  183. assertFalse(path.isSimple());
  184. path = goog.graphics.Path.createSimplifiedPath(path);
  185. assertTrue(path.isSimple());
  186. var p = path.getCurrentPoint();
  187. assertEquals(55, p[0]);
  188. assertRoughlyEquals(77.32, p[1], 0.01);
  189. goog.testing.graphics.assertPathEquals(['M', 58.66, 70,
  190. 'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32], path);
  191. }
  192. function testCreateSimplifiedPath2() {
  193. var path = new goog.graphics.Path();
  194. path.arc(50, 60, 10, 20, 30, 30, false);
  195. path.arc(50, 60, 10, 20, 60, 30, false);
  196. assertFalse(path.isSimple());
  197. path = goog.graphics.Path.createSimplifiedPath(path);
  198. assertTrue(path.isSimple());
  199. goog.testing.graphics.assertPathEquals(['M', 58.66, 70,
  200. 'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32,
  201. 'M', 55, 77.32,
  202. 'C', 53.48, 79.08, 51.76, 80, 50, 80], path);
  203. }
  204. function testCreateSimplifiedPath3() {
  205. var path = new goog.graphics.Path();
  206. path.arc(50, 60, 10, 20, 30, 30, false);
  207. path.arc(50, 60, 10, 20, 60, 30, true);
  208. path.close();
  209. path = goog.graphics.Path.createSimplifiedPath(path);
  210. goog.testing.graphics.assertPathEquals(['M', 58.66, 70,
  211. 'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32,
  212. 53.48, 79.08, 51.76, 80, 50, 80, 'X'], path);
  213. var p = path.getCurrentPoint();
  214. assertRoughlyEquals(58.66, p[0], 0.01);
  215. assertRoughlyEquals(70, p[1], 0.01);
  216. }
  217. function testArcToAsCurves() {
  218. var path = new goog.graphics.Path();
  219. path.moveTo(58.66, 70);
  220. path.arcToAsCurves(10, 20, 30, 30, false);
  221. goog.testing.graphics.assertPathEquals(['M', 58.66, 70,
  222. 'C', 57.78, 73.04, 56.52, 75.57, 55, 77.32], path);
  223. }
  224. function testCreateTransformedPath() {
  225. var path = new goog.graphics.Path();
  226. path.moveTo(0, 0);
  227. path.lineTo(0, 10, 10, 10, 10, 0);
  228. path.close();
  229. var tx = new goog.graphics.AffineTransform(2, 0, 0, 3, 10, 20);
  230. var path2 = path.createTransformedPath(tx);
  231. goog.testing.graphics.assertPathEquals(
  232. ['M', 0, 0, 'L', 0, 10, 10, 10, 10, 0, 'X'], path);
  233. goog.testing.graphics.assertPathEquals(
  234. ['M', 10, 20, 'L', 10, 50, 30, 50, 30, 20, 'X'], path2);
  235. }
  236. function testTransform() {
  237. var path = new goog.graphics.Path();
  238. path.moveTo(0, 0);
  239. path.lineTo(0, 10, 10, 10, 10, 0);
  240. path.close();
  241. var tx = new goog.graphics.AffineTransform(2, 0, 0, 3, 10, 20);
  242. var path2 = path.transform(tx);
  243. assertTrue(path === path2);
  244. goog.testing.graphics.assertPathEquals(
  245. ['M', 10, 20, 'L', 10, 50, 30, 50, 30, 20, 'X'], path2);
  246. }
  247. function testTransformCurrentAndClosePoints() {
  248. var path = new goog.graphics.Path();
  249. path.moveTo(0, 0);
  250. assertObjectEquals([0, 0], path.getCurrentPoint());
  251. path.transform(new goog.graphics.AffineTransform(1, 0, 0, 1, 10, 20));
  252. assertObjectEquals([10, 20], path.getCurrentPoint());
  253. path.lineTo(50, 50);
  254. path.close();
  255. assertObjectEquals([10, 20], path.getCurrentPoint());
  256. }
  257. function testTransformNonSimple() {
  258. var path = new goog.graphics.Path();
  259. path.arc(50, 60, 10, 20, 30, 30, false);
  260. assertThrows(function() {
  261. path.transform(new goog.graphics.AffineTransform(1, 0, 0, 1, 10, 20));
  262. });
  263. }
  264. function testAppendPath() {
  265. var path1 = new goog.graphics.Path();
  266. path1.moveTo(0, 0);
  267. path1.lineTo(0, 10, 10, 10, 10, 0);
  268. path1.close();
  269. var path2 = new goog.graphics.Path();
  270. path2.arc(50, 60, 10, 20, 30, 30, false);
  271. assertTrue(path1.isSimple());
  272. path1.appendPath(path2);
  273. assertFalse(path1.isSimple());
  274. goog.testing.graphics.assertPathEquals([
  275. 'M', 0, 0, 'L', 0, 10, 10, 10, 10, 0, 'X',
  276. 'M', 58.66, 70, 'A', 10, 20, 30, 30, 55, 77.32
  277. ], path1);
  278. }
  279. function testIsEmpty() {
  280. var path = new goog.graphics.Path();
  281. assertTrue('Initially path is empty', path.isEmpty());
  282. path.moveTo(0, 0);
  283. assertFalse('After command addition, path is not empty', path.isEmpty());
  284. path.clear();
  285. assertTrue('After clear, path is empty again', path.isEmpty());
  286. }