PathTests.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.Shapes;
  5. using Avalonia.Layout;
  6. using Avalonia.Media;
  7. using Xunit;
  8. #if AVALONIA_SKIA
  9. namespace Avalonia.Skia.RenderTests
  10. #else
  11. namespace Avalonia.Direct2D1.RenderTests.Shapes
  12. #endif
  13. {
  14. using System.Threading.Tasks;
  15. using Avalonia.Collections;
  16. public class PathTests : TestBase
  17. {
  18. public PathTests()
  19. : base(@"Shapes\Path")
  20. {
  21. }
  22. [Fact]
  23. public async Task Line_Absolute()
  24. {
  25. Decorator target = new Decorator
  26. {
  27. Width = 200,
  28. Height = 200,
  29. Child = new Path
  30. {
  31. Stroke = Brushes.Red,
  32. StrokeThickness = 1,
  33. HorizontalAlignment = HorizontalAlignment.Center,
  34. VerticalAlignment = VerticalAlignment.Center,
  35. Data = StreamGeometry.Parse("M 10,190 L 190,10 M0,0M200,200"),
  36. }
  37. };
  38. await RenderToFile(target);
  39. CompareImages();
  40. }
  41. [Fact]
  42. public async Task Line_Relative()
  43. {
  44. Decorator target = new Decorator
  45. {
  46. Width = 200,
  47. Height = 200,
  48. Child = new Path
  49. {
  50. Stroke = Brushes.Red,
  51. StrokeThickness = 1,
  52. HorizontalAlignment = HorizontalAlignment.Center,
  53. VerticalAlignment = VerticalAlignment.Center,
  54. Data = StreamGeometry.Parse("M10,190 l190,-190 M0,0M200,200"),
  55. }
  56. };
  57. await RenderToFile(target);
  58. CompareImages();
  59. }
  60. [Fact]
  61. public async Task HorizontalLine_Absolute()
  62. {
  63. Decorator target = new Decorator
  64. {
  65. Width = 200,
  66. Height = 200,
  67. Child = new Path
  68. {
  69. Stroke = Brushes.Red,
  70. StrokeThickness = 1,
  71. HorizontalAlignment = HorizontalAlignment.Center,
  72. VerticalAlignment = VerticalAlignment.Center,
  73. Data = StreamGeometry.Parse("M190,100 H10 M0,0M200,200"),
  74. }
  75. };
  76. await RenderToFile(target);
  77. CompareImages();
  78. }
  79. [Fact]
  80. public async Task HorizontalLine_Relative()
  81. {
  82. Decorator target = new Decorator
  83. {
  84. Width = 200,
  85. Height = 200,
  86. Child = new Path
  87. {
  88. Stroke = Brushes.Red,
  89. StrokeThickness = 1,
  90. HorizontalAlignment = HorizontalAlignment.Center,
  91. VerticalAlignment = VerticalAlignment.Center,
  92. Data = StreamGeometry.Parse("M190,100 h-180 M0,0M200,200"),
  93. }
  94. };
  95. await RenderToFile(target);
  96. CompareImages();
  97. }
  98. [Fact]
  99. public async Task VerticalLine_Absolute()
  100. {
  101. Decorator target = new Decorator
  102. {
  103. Width = 200,
  104. Height = 200,
  105. Child = new Path
  106. {
  107. Stroke = Brushes.Red,
  108. StrokeThickness = 1,
  109. HorizontalAlignment = HorizontalAlignment.Center,
  110. VerticalAlignment = VerticalAlignment.Center,
  111. Data = StreamGeometry.Parse("M100,190 V10 M0,0M200,200"),
  112. }
  113. };
  114. await RenderToFile(target);
  115. CompareImages();
  116. }
  117. [Fact]
  118. public async Task VerticalLine_Relative()
  119. {
  120. Decorator target = new Decorator
  121. {
  122. Width = 200,
  123. Height = 200,
  124. Child = new Path
  125. {
  126. Stroke = Brushes.Red,
  127. StrokeThickness = 1,
  128. HorizontalAlignment = HorizontalAlignment.Center,
  129. VerticalAlignment = VerticalAlignment.Center,
  130. Data = StreamGeometry.Parse("M100,190 V-180 M0,0M200,200"),
  131. }
  132. };
  133. await RenderToFile(target);
  134. CompareImages();
  135. }
  136. [Fact]
  137. public async Task CubicBezier_Absolute()
  138. {
  139. Decorator target = new Decorator
  140. {
  141. Width = 200,
  142. Height = 200,
  143. Child = new Path
  144. {
  145. Fill = Brushes.Gray,
  146. Stroke = Brushes.Red,
  147. StrokeThickness = 1,
  148. HorizontalAlignment = HorizontalAlignment.Center,
  149. VerticalAlignment = VerticalAlignment.Center,
  150. Data = StreamGeometry.Parse("M190,0 C10,10 190,190 10,190 M0,0M200,200"),
  151. }
  152. };
  153. await RenderToFile(target);
  154. CompareImages();
  155. }
  156. [Fact]
  157. public async Task CubicBezier_Relative()
  158. {
  159. Decorator target = new Decorator
  160. {
  161. Width = 200,
  162. Height = 200,
  163. Child = new Path
  164. {
  165. Fill = Brushes.Gray,
  166. Stroke = Brushes.Red,
  167. StrokeThickness = 1,
  168. HorizontalAlignment = HorizontalAlignment.Center,
  169. VerticalAlignment = VerticalAlignment.Center,
  170. Data = StreamGeometry.Parse("M190,0 c-180,10 0,190 -180,190 M0,0M200,200"),
  171. }
  172. };
  173. await RenderToFile(target);
  174. CompareImages();
  175. }
  176. [Fact]
  177. public async Task Arc_Absolute()
  178. {
  179. Decorator target = new Decorator
  180. {
  181. Width = 200,
  182. Height = 200,
  183. Child = new Path
  184. {
  185. Fill = Brushes.Gray,
  186. Stroke = Brushes.Red,
  187. StrokeThickness = 1,
  188. HorizontalAlignment = HorizontalAlignment.Center,
  189. VerticalAlignment = VerticalAlignment.Center,
  190. Data = StreamGeometry.Parse("M190,100 A90,90 0 1,0 10,100 M0,0M200,200"),
  191. }
  192. };
  193. await RenderToFile(target);
  194. CompareImages();
  195. }
  196. [Fact]
  197. public async Task Arc_Relative()
  198. {
  199. Decorator target = new Decorator
  200. {
  201. Width = 200,
  202. Height = 200,
  203. Child = new Path
  204. {
  205. Fill = Brushes.Gray,
  206. Stroke = Brushes.Red,
  207. StrokeThickness = 1,
  208. HorizontalAlignment = HorizontalAlignment.Center,
  209. VerticalAlignment = VerticalAlignment.Center,
  210. Data = StreamGeometry.Parse("M190,100 a90,90 0 1,0 -180,0 M0,0M200,200"),
  211. }
  212. };
  213. await RenderToFile(target);
  214. CompareImages();
  215. }
  216. [Fact]
  217. public async Task Path_100px_Triangle_Centered()
  218. {
  219. Decorator target = new Decorator
  220. {
  221. Width = 200,
  222. Height = 200,
  223. Child = new Path
  224. {
  225. Fill = Brushes.Gray,
  226. Stroke = Brushes.Red,
  227. StrokeThickness = 2,
  228. HorizontalAlignment = HorizontalAlignment.Center,
  229. VerticalAlignment = VerticalAlignment.Center,
  230. Data = StreamGeometry.Parse("M 0,100 L 100,100 50,0 Z"),
  231. }
  232. };
  233. await RenderToFile(target);
  234. CompareImages();
  235. }
  236. [Fact]
  237. public async Task Path_Tick_Scaled()
  238. {
  239. Decorator target = new Decorator
  240. {
  241. Width = 200,
  242. Height = 200,
  243. Child = new Path
  244. {
  245. Fill = Brushes.Gray,
  246. Stroke = Brushes.Red,
  247. StrokeThickness = 2,
  248. Stretch = Stretch.Uniform,
  249. HorizontalAlignment = HorizontalAlignment.Center,
  250. VerticalAlignment = VerticalAlignment.Center,
  251. Data = StreamGeometry.Parse("M 1145.607177734375,430 C1145.607177734375,430 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1138,434.5538330078125 1138,434.5538330078125 1138,434.5538330078125 1141.482177734375,438 1141.482177734375,438 1141.482177734375,438 1141.96875,437.9375 1141.96875,437.9375 1141.96875,437.9375 1147,431.34619140625 1147,431.34619140625 1147,431.34619140625 1145.607177734375,430 1145.607177734375,430 z"),
  252. }
  253. };
  254. await RenderToFile(target);
  255. CompareImages();
  256. }
  257. [Fact]
  258. public async Task Path_Tick_Scaled_Stroke_8px()
  259. {
  260. Decorator target = new Decorator
  261. {
  262. Width = 200,
  263. Height = 200,
  264. Child = new Path
  265. {
  266. Fill = Brushes.Gray,
  267. Stroke = Brushes.Red,
  268. StrokeThickness = 8,
  269. Stretch = Stretch.Uniform,
  270. HorizontalAlignment = HorizontalAlignment.Center,
  271. VerticalAlignment = VerticalAlignment.Center,
  272. Data = StreamGeometry.Parse("M 1145.607177734375,430 C1145.607177734375,430 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1138,434.5538330078125 1138,434.5538330078125 1138,434.5538330078125 1141.482177734375,438 1141.482177734375,438 1141.482177734375,438 1141.96875,437.9375 1141.96875,437.9375 1141.96875,437.9375 1147,431.34619140625 1147,431.34619140625 1147,431.34619140625 1145.607177734375,430 1145.607177734375,430 z"),
  273. }
  274. };
  275. await RenderToFile(target);
  276. CompareImages();
  277. }
  278. [Fact]
  279. public async Task Path_Expander_With_Border()
  280. {
  281. Decorator target = new Decorator
  282. {
  283. Width = 200,
  284. Height = 200,
  285. Child = new Border
  286. {
  287. BorderBrush = Brushes.Red,
  288. BorderThickness = new Thickness(1),
  289. HorizontalAlignment = HorizontalAlignment.Center,
  290. VerticalAlignment = VerticalAlignment.Center,
  291. Child = new Path
  292. {
  293. Fill = Brushes.Black,
  294. Stroke = Brushes.Black,
  295. StrokeThickness = 1,
  296. Stretch = Stretch.Uniform,
  297. Data = StreamGeometry.Parse("M 0 2 L 4 6 L 0 10 Z"),
  298. }
  299. }
  300. };
  301. await RenderToFile(target);
  302. CompareImages();
  303. }
  304. #if AVALONIA_SKIA_SKIP_FAIL
  305. [Fact(Skip = "FIXME")]
  306. #else
  307. [Fact]
  308. #endif
  309. public async Task Path_With_PenLineCap()
  310. {
  311. Decorator target = new Decorator
  312. {
  313. Width = 200,
  314. Height = 200,
  315. Child = new Path
  316. {
  317. Stroke = Brushes.Black,
  318. StrokeThickness = 10,
  319. HorizontalAlignment = HorizontalAlignment.Center,
  320. VerticalAlignment = VerticalAlignment.Center,
  321. StrokeDashCap = PenLineCap.Triangle,
  322. StrokeDashArray = new AvaloniaList<double>(3, 1),
  323. StrokeStartLineCap = PenLineCap.Round,
  324. StrokeEndLineCap = PenLineCap.Square,
  325. Data = StreamGeometry.Parse("M 20,20 L 180,180"),
  326. }
  327. };
  328. await RenderToFile(target);
  329. CompareImages();
  330. }
  331. [Fact]
  332. public async Task Path_With_Rotated_Geometry()
  333. {
  334. var target = new Border
  335. {
  336. Width = 200,
  337. Height = 200,
  338. Background = Brushes.White,
  339. Child = new Path
  340. {
  341. Fill = Brushes.Red,
  342. Data = new RectangleGeometry
  343. {
  344. Rect = new Rect(50, 50, 100, 100),
  345. Transform = new RotateTransform(45),
  346. }
  347. }
  348. };
  349. await RenderToFile(target);
  350. CompareImages();
  351. }
  352. }
  353. }