VisualBrushTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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 System.Threading.Tasks;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Shapes;
  6. using Avalonia.Layout;
  7. using Avalonia.Media;
  8. using Avalonia.Media.Imaging;
  9. using Xunit;
  10. #if AVALONIA_SKIA
  11. namespace Avalonia.Skia.RenderTests
  12. #else
  13. namespace Avalonia.Direct2D1.RenderTests.Media
  14. #endif
  15. {
  16. public class VisualBrushTests : TestBase
  17. {
  18. public VisualBrushTests()
  19. : base(@"Media\VisualBrush")
  20. {
  21. }
  22. private string BitmapPath
  23. {
  24. get { return System.IO.Path.Combine(OutputPath, "github_icon.png"); }
  25. }
  26. private Control Visual
  27. {
  28. get
  29. {
  30. return new Panel
  31. {
  32. Children =
  33. {
  34. new Image
  35. {
  36. Source = new Bitmap(BitmapPath),
  37. },
  38. new Border
  39. {
  40. BorderBrush = Brushes.Blue,
  41. BorderThickness = new Thickness(2),
  42. HorizontalAlignment = HorizontalAlignment.Center,
  43. VerticalAlignment = VerticalAlignment.Center,
  44. Child = new TextBlock
  45. {
  46. FontSize = 24,
  47. FontFamily = TestFontFamily,
  48. Background = Brushes.Green,
  49. Foreground = Brushes.Yellow,
  50. Text = "VisualBrush",
  51. }
  52. }
  53. }
  54. };
  55. }
  56. }
  57. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  58. public async Task VisualBrush_NoStretch_NoTile_Alignment_TopLeft()
  59. {
  60. Decorator target = new Decorator
  61. {
  62. Padding = new Thickness(8),
  63. Width = 200,
  64. Height = 200,
  65. Child = new Rectangle
  66. {
  67. Fill = new VisualBrush
  68. {
  69. Stretch = Stretch.None,
  70. TileMode = TileMode.None,
  71. AlignmentX = AlignmentX.Left,
  72. AlignmentY = AlignmentY.Top,
  73. Visual = Visual,
  74. }
  75. }
  76. };
  77. await RenderToFile(target);
  78. CompareImages();
  79. }
  80. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  81. public async Task VisualBrush_NoStretch_NoTile_Alignment_Center()
  82. {
  83. Decorator target = new Decorator
  84. {
  85. Padding = new Thickness(8),
  86. Width = 200,
  87. Height = 200,
  88. Child = new Rectangle
  89. {
  90. Fill = new VisualBrush
  91. {
  92. Stretch = Stretch.None,
  93. TileMode = TileMode.None,
  94. AlignmentX = AlignmentX.Center,
  95. AlignmentY = AlignmentY.Center,
  96. Visual = Visual,
  97. }
  98. }
  99. };
  100. await RenderToFile(target);
  101. CompareImages();
  102. }
  103. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  104. public async Task VisualBrush_NoStretch_NoTile_Alignment_BottomRight()
  105. {
  106. Decorator target = new Decorator
  107. {
  108. Padding = new Thickness(8),
  109. Width = 200,
  110. Height = 200,
  111. Child = new Rectangle
  112. {
  113. Fill = new VisualBrush
  114. {
  115. Stretch = Stretch.None,
  116. TileMode = TileMode.None,
  117. AlignmentX = AlignmentX.Right,
  118. AlignmentY = AlignmentY.Bottom,
  119. Visual = Visual,
  120. }
  121. }
  122. };
  123. await RenderToFile(target);
  124. CompareImages();
  125. }
  126. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  127. public async Task VisualBrush_Fill_NoTile()
  128. {
  129. Decorator target = new Decorator
  130. {
  131. Padding = new Thickness(8),
  132. Width = 920,
  133. Height = 920,
  134. Child = new Rectangle
  135. {
  136. Fill = new VisualBrush
  137. {
  138. Stretch = Stretch.Fill,
  139. TileMode = TileMode.None,
  140. Visual = Visual,
  141. }
  142. }
  143. };
  144. await RenderToFile(target);
  145. CompareImages();
  146. }
  147. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  148. public async Task VisualBrush_Uniform_NoTile()
  149. {
  150. Decorator target = new Decorator
  151. {
  152. Padding = new Thickness(8),
  153. Width = 300,
  154. Height = 200,
  155. Child = new Rectangle
  156. {
  157. Fill = new VisualBrush
  158. {
  159. Stretch = Stretch.Uniform,
  160. TileMode = TileMode.None,
  161. Visual = Visual,
  162. }
  163. }
  164. };
  165. await RenderToFile(target);
  166. CompareImages();
  167. }
  168. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  169. public async Task VisualBrush_UniformToFill_NoTile()
  170. {
  171. Decorator target = new Decorator
  172. {
  173. Padding = new Thickness(8),
  174. Width = 300,
  175. Height = 200,
  176. Child = new Rectangle
  177. {
  178. Fill = new VisualBrush
  179. {
  180. Stretch = Stretch.UniformToFill,
  181. TileMode = TileMode.None,
  182. Visual = Visual,
  183. }
  184. }
  185. };
  186. await RenderToFile(target);
  187. CompareImages();
  188. }
  189. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  190. public async Task VisualBrush_NoStretch_NoTile_BottomRightQuarterSource()
  191. {
  192. Decorator target = new Decorator
  193. {
  194. Padding = new Thickness(8),
  195. Width = 200,
  196. Height = 200,
  197. Child = new Rectangle
  198. {
  199. Fill = new VisualBrush
  200. {
  201. Stretch = Stretch.None,
  202. TileMode = TileMode.None,
  203. SourceRect = new RelativeRect(250, 250, 250, 250, RelativeUnit.Absolute),
  204. Visual = Visual,
  205. }
  206. }
  207. };
  208. await RenderToFile(target);
  209. CompareImages();
  210. }
  211. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  212. public async Task VisualBrush_NoStretch_NoTile_BottomRightQuarterDest()
  213. {
  214. Decorator target = new Decorator
  215. {
  216. Padding = new Thickness(8),
  217. Width = 200,
  218. Height = 200,
  219. Child = new Rectangle
  220. {
  221. Fill = new VisualBrush
  222. {
  223. Stretch = Stretch.None,
  224. TileMode = TileMode.None,
  225. DestinationRect = new RelativeRect(92, 92, 92, 92, RelativeUnit.Absolute),
  226. Visual = Visual,
  227. }
  228. }
  229. };
  230. await RenderToFile(target);
  231. CompareImages();
  232. }
  233. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  234. public async Task VisualBrush_NoStretch_NoTile_BottomRightQuarterSource_BottomRightQuarterDest()
  235. {
  236. Decorator target = new Decorator
  237. {
  238. Padding = new Thickness(8),
  239. Width = 200,
  240. Height = 200,
  241. Child = new Rectangle
  242. {
  243. Fill = new VisualBrush
  244. {
  245. Stretch = Stretch.None,
  246. TileMode = TileMode.None,
  247. SourceRect = new RelativeRect(0.5, 0.5, 0.5, 0.5, RelativeUnit.Relative),
  248. DestinationRect = new RelativeRect(0.5, 0.5, 0.5, 0.5, RelativeUnit.Relative),
  249. Visual = Visual,
  250. }
  251. }
  252. };
  253. await RenderToFile(target);
  254. CompareImages();
  255. }
  256. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  257. public async Task VisualBrush_NoStretch_Tile_BottomRightQuarterSource_CenterQuarterDest()
  258. {
  259. Decorator target = new Decorator
  260. {
  261. Padding = new Thickness(8),
  262. Width = 200,
  263. Height = 200,
  264. Child = new Rectangle
  265. {
  266. Fill = new VisualBrush
  267. {
  268. Stretch = Stretch.None,
  269. TileMode = TileMode.Tile,
  270. SourceRect = new RelativeRect(0.5, 0.5, 0.5, 0.5, RelativeUnit.Relative),
  271. DestinationRect = new RelativeRect(0.25, 0.25, 0.5, 0.5, RelativeUnit.Relative),
  272. Visual = Visual,
  273. }
  274. }
  275. };
  276. await RenderToFile(target);
  277. CompareImages();
  278. }
  279. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  280. public async Task VisualBrush_NoStretch_FlipX_TopLeftDest()
  281. {
  282. Decorator target = new Decorator
  283. {
  284. Padding = new Thickness(8),
  285. Width = 200,
  286. Height = 200,
  287. Child = new Rectangle
  288. {
  289. Fill = new VisualBrush
  290. {
  291. Stretch = Stretch.None,
  292. TileMode = TileMode.FlipX,
  293. DestinationRect = new RelativeRect(0, 0, 0.5, 0.5, RelativeUnit.Relative),
  294. Visual = Visual,
  295. }
  296. }
  297. };
  298. await RenderToFile(target);
  299. CompareImages();
  300. }
  301. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  302. public async Task VisualBrush_NoStretch_FlipY_TopLeftDest()
  303. {
  304. Decorator target = new Decorator
  305. {
  306. Padding = new Thickness(8),
  307. Width = 200,
  308. Height = 200,
  309. Child = new Rectangle
  310. {
  311. Fill = new VisualBrush
  312. {
  313. Stretch = Stretch.None,
  314. TileMode = TileMode.FlipY,
  315. DestinationRect = new RelativeRect(0, 0, 0.5, 0.5, RelativeUnit.Relative),
  316. Visual = Visual,
  317. }
  318. }
  319. };
  320. await RenderToFile(target);
  321. CompareImages();
  322. }
  323. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  324. public async Task VisualBrush_NoStretch_FlipXY_TopLeftDest()
  325. {
  326. Decorator target = new Decorator
  327. {
  328. Padding = new Thickness(8),
  329. Width = 200,
  330. Height = 200,
  331. Child = new Rectangle
  332. {
  333. Fill = new VisualBrush
  334. {
  335. Stretch = Stretch.None,
  336. TileMode = TileMode.FlipXY,
  337. DestinationRect = new RelativeRect(0, 0, 0.5, 0.5, RelativeUnit.Relative),
  338. Visual = Visual,
  339. }
  340. }
  341. };
  342. await RenderToFile(target);
  343. CompareImages();
  344. }
  345. [Fact(Skip = "Visual brush is broken in combination with text rendering.")]
  346. public async Task VisualBrush_InTree_Visual()
  347. {
  348. Border source;
  349. Decorator target = new Decorator
  350. {
  351. Padding = new Thickness(8),
  352. Width = 200,
  353. Height = 200,
  354. Child = new Grid
  355. {
  356. RowDefinitions = new RowDefinitions("Auto,*"),
  357. Children =
  358. {
  359. (source = new Border
  360. {
  361. Background = Brushes.Yellow,
  362. HorizontalAlignment = HorizontalAlignment.Left,
  363. Child = new TextBlock
  364. {
  365. FontFamily = TestFontFamily,
  366. Text = "Visual"
  367. }
  368. }),
  369. new Border
  370. {
  371. Background = new VisualBrush
  372. {
  373. Stretch = Stretch.Uniform,
  374. Visual = source,
  375. },
  376. [Grid.RowProperty] = 1,
  377. }
  378. }
  379. }
  380. };
  381. await RenderToFile(target);
  382. CompareImages();
  383. }
  384. [Fact]
  385. public async Task VisualBrush_Grip_96_Dpi()
  386. {
  387. var target = new Border
  388. {
  389. Width = 100,
  390. Height = 10,
  391. Background = new VisualBrush
  392. {
  393. SourceRect = new RelativeRect(0, 0, 4, 5, RelativeUnit.Absolute),
  394. DestinationRect = new RelativeRect(0, 0, 4, 5, RelativeUnit.Absolute),
  395. TileMode = TileMode.Tile,
  396. Stretch = Stretch.UniformToFill,
  397. Visual = new Canvas
  398. {
  399. Width = 4,
  400. Height = 5,
  401. Background = Brushes.WhiteSmoke,
  402. Children =
  403. {
  404. new Rectangle
  405. {
  406. Width = 1,
  407. Height = 1,
  408. Fill = Brushes.Red,
  409. [Canvas.LeftProperty] = 2,
  410. },
  411. new Rectangle
  412. {
  413. Width = 1,
  414. Height = 1,
  415. Fill = Brushes.Red,
  416. [Canvas.TopProperty] = 2,
  417. },
  418. new Rectangle
  419. {
  420. Width = 1,
  421. Height = 1,
  422. Fill = Brushes.Red,
  423. [Canvas.LeftProperty] = 2,
  424. [Canvas.TopProperty] = 4,
  425. }
  426. }
  427. }
  428. }
  429. };
  430. await RenderToFile(target);
  431. CompareImages();
  432. }
  433. [Fact]
  434. public async Task VisualBrush_Grip_144_Dpi()
  435. {
  436. var target = new Border
  437. {
  438. Width = 100,
  439. Height = 7.5,
  440. Background = new VisualBrush
  441. {
  442. SourceRect = new RelativeRect(0, 0, 4, 5, RelativeUnit.Absolute),
  443. DestinationRect = new RelativeRect(0, 0, 4, 5, RelativeUnit.Absolute),
  444. TileMode = TileMode.Tile,
  445. Stretch = Stretch.UniformToFill,
  446. Visual = new Canvas
  447. {
  448. Width = 4,
  449. Height = 5,
  450. Background = Brushes.WhiteSmoke,
  451. Children =
  452. {
  453. new Rectangle
  454. {
  455. Width = 1,
  456. Height = 1,
  457. Fill = Brushes.Red,
  458. [Canvas.LeftProperty] = 2,
  459. },
  460. new Rectangle
  461. {
  462. Width = 1,
  463. Height = 1,
  464. Fill = Brushes.Red,
  465. [Canvas.TopProperty] = 2,
  466. },
  467. new Rectangle
  468. {
  469. Width = 1,
  470. Height = 1,
  471. Fill = Brushes.Red,
  472. [Canvas.LeftProperty] = 2,
  473. [Canvas.TopProperty] = 4,
  474. }
  475. }
  476. }
  477. }
  478. };
  479. await RenderToFile(target, dpi: 144);
  480. CompareImages();
  481. }
  482. [Fact]
  483. public async Task VisualBrush_Grip_192_Dpi()
  484. {
  485. var target = new Border
  486. {
  487. Width = 100,
  488. Height = 10,
  489. Background = new VisualBrush
  490. {
  491. SourceRect = new RelativeRect(0, 0, 4, 5, RelativeUnit.Absolute),
  492. DestinationRect = new RelativeRect(0, 0, 4, 5, RelativeUnit.Absolute),
  493. TileMode = TileMode.Tile,
  494. Stretch = Stretch.UniformToFill,
  495. Visual = new Canvas
  496. {
  497. Width = 4,
  498. Height = 5,
  499. Background = Brushes.WhiteSmoke,
  500. Children =
  501. {
  502. new Rectangle
  503. {
  504. Width = 1,
  505. Height = 1,
  506. Fill = Brushes.Red,
  507. [Canvas.LeftProperty] = 2,
  508. },
  509. new Rectangle
  510. {
  511. Width = 1,
  512. Height = 1,
  513. Fill = Brushes.Red,
  514. [Canvas.TopProperty] = 2,
  515. },
  516. new Rectangle
  517. {
  518. Width = 1,
  519. Height = 1,
  520. Fill = Brushes.Red,
  521. [Canvas.LeftProperty] = 2,
  522. [Canvas.TopProperty] = 4,
  523. }
  524. }
  525. }
  526. }
  527. };
  528. await RenderToFile(target, dpi: 192);
  529. CompareImages();
  530. }
  531. [Fact]
  532. public async Task VisualBrush_Checkerboard_96_Dpi()
  533. {
  534. var target = new Border
  535. {
  536. Width = 200,
  537. Height = 200,
  538. Background = new VisualBrush
  539. {
  540. DestinationRect = new RelativeRect(0, 0, 16, 16, RelativeUnit.Absolute),
  541. TileMode = TileMode.Tile,
  542. Visual = new Canvas
  543. {
  544. Width = 16,
  545. Height= 16,
  546. Background = Brushes.Red,
  547. Children =
  548. {
  549. new Rectangle
  550. {
  551. Width = 8,
  552. Height = 8,
  553. Fill = Brushes.Green,
  554. },
  555. new Rectangle
  556. {
  557. Width = 8,
  558. Height = 8,
  559. Fill = Brushes.Green,
  560. [Canvas.LeftProperty] = 8,
  561. [Canvas.TopProperty] = 8,
  562. },
  563. }
  564. }
  565. }
  566. };
  567. await RenderToFile(target);
  568. CompareImages();
  569. }
  570. [Fact]
  571. public async Task VisualBrush_Checkerboard_144_Dpi()
  572. {
  573. var target = new Border
  574. {
  575. Width = 200,
  576. Height = 200,
  577. Background = new VisualBrush
  578. {
  579. DestinationRect = new RelativeRect(0, 0, 16, 16, RelativeUnit.Absolute),
  580. TileMode = TileMode.Tile,
  581. Visual = new Canvas
  582. {
  583. Width = 16,
  584. Height = 16,
  585. Background = Brushes.Red,
  586. Children =
  587. {
  588. new Rectangle
  589. {
  590. Width = 8,
  591. Height = 8,
  592. Fill = Brushes.Green,
  593. },
  594. new Rectangle
  595. {
  596. Width = 8,
  597. Height = 8,
  598. Fill = Brushes.Green,
  599. [Canvas.LeftProperty] = 8,
  600. [Canvas.TopProperty] = 8,
  601. },
  602. }
  603. }
  604. }
  605. };
  606. await RenderToFile(target, dpi: 144);
  607. CompareImages();
  608. }
  609. [Fact]
  610. public async Task VisualBrush_Checkerboard_192_Dpi()
  611. {
  612. var target = new Border
  613. {
  614. Width = 200,
  615. Height = 200,
  616. Background = new VisualBrush
  617. {
  618. DestinationRect = new RelativeRect(0, 0, 16, 16, RelativeUnit.Absolute),
  619. TileMode = TileMode.Tile,
  620. Visual = new Canvas
  621. {
  622. Width = 16,
  623. Height = 16,
  624. Background = Brushes.Red,
  625. Children =
  626. {
  627. new Rectangle
  628. {
  629. Width = 8,
  630. Height = 8,
  631. Fill = Brushes.Green,
  632. },
  633. new Rectangle
  634. {
  635. Width = 8,
  636. Height = 8,
  637. Fill = Brushes.Green,
  638. [Canvas.LeftProperty] = 8,
  639. [Canvas.TopProperty] = 8,
  640. },
  641. }
  642. }
  643. }
  644. };
  645. await RenderToFile(target, dpi: 192);
  646. CompareImages();
  647. }
  648. }
  649. }