1
0

CarouselTests.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System.Threading.Tasks;
  2. using Avalonia.Controls;
  3. using Avalonia.Input;
  4. using Avalonia.Layout;
  5. using Avalonia.Media;
  6. using Avalonia.Media.Imaging;
  7. using Avalonia.Platform;
  8. using Avalonia.Styling;
  9. using Avalonia.Themes.Simple;
  10. using Avalonia.UnitTests;
  11. using Xunit;
  12. #if AVALONIA_SKIA
  13. namespace Avalonia.Skia.RenderTests
  14. #else
  15. namespace Avalonia.Direct2D1.RenderTests.Controls
  16. #endif
  17. {
  18. public class CarouselRenderTests : TestBase
  19. {
  20. public CarouselRenderTests()
  21. : base(@"Controls\Carousel")
  22. {
  23. }
  24. private static Style FontStyle => new Style(x => x.OfType<TextBlock>())
  25. {
  26. Setters = { new Setter(TextBlock.FontFamilyProperty, TestFontFamily) }
  27. };
  28. [Fact]
  29. public async Task Carousel_ViewportFraction_MiddleItemSelected_ShowsSidePeeks()
  30. {
  31. var carousel = new Carousel
  32. {
  33. Background = Brushes.Transparent,
  34. ViewportFraction = 0.8,
  35. SelectedIndex = 1,
  36. HorizontalAlignment = HorizontalAlignment.Stretch,
  37. VerticalAlignment = VerticalAlignment.Stretch,
  38. ItemsSource = new Control[]
  39. {
  40. CreateCard("One", "#D8574B", "#F7C5BE"),
  41. CreateCard("Two", "#3E7AD9", "#BCD0F7"),
  42. CreateCard("Three", "#3D9B67", "#BEE4CB"),
  43. }
  44. };
  45. var target = new Border
  46. {
  47. Width = 520,
  48. Height = 340,
  49. Background = Brushes.White,
  50. Padding = new Thickness(20),
  51. Child = carousel
  52. };
  53. AvaloniaLocator.CurrentMutable.Bind<ICursorFactory>().ToConstant(new CursorFactoryStub());
  54. target.Styles.Add(new SimpleTheme());
  55. target.Styles.Add(FontStyle);
  56. await RenderToFile(target);
  57. CompareImages(skipImmediate: true);
  58. }
  59. private static Control CreateCard(string label, string background, string accent)
  60. {
  61. return new Border
  62. {
  63. Margin = new Thickness(14, 12),
  64. CornerRadius = new CornerRadius(18),
  65. ClipToBounds = true,
  66. Background = Brush.Parse(background),
  67. BorderBrush = Brushes.White,
  68. BorderThickness = new Thickness(2),
  69. Child = new Grid
  70. {
  71. Children =
  72. {
  73. new Border
  74. {
  75. Height = 56,
  76. Background = Brush.Parse(accent),
  77. VerticalAlignment = VerticalAlignment.Top
  78. },
  79. new Border
  80. {
  81. Width = 88,
  82. Height = 88,
  83. CornerRadius = new CornerRadius(44),
  84. Background = Brushes.White,
  85. Opacity = 0.9,
  86. HorizontalAlignment = HorizontalAlignment.Center,
  87. VerticalAlignment = VerticalAlignment.Center
  88. },
  89. new Border
  90. {
  91. Background = new SolidColorBrush(Color.Parse("#80000000")),
  92. VerticalAlignment = VerticalAlignment.Bottom,
  93. Padding = new Thickness(12),
  94. Child = new TextBlock
  95. {
  96. Text = label,
  97. Foreground = Brushes.White,
  98. HorizontalAlignment = HorizontalAlignment.Center,
  99. FontWeight = FontWeight.SemiBold
  100. }
  101. }
  102. }
  103. }
  104. };
  105. }
  106. private sealed class CursorFactoryStub : ICursorFactory
  107. {
  108. public ICursorImpl GetCursor(StandardCursorType cursorType) => new CursorStub();
  109. public ICursorImpl CreateCursor(Bitmap cursor, PixelPoint hotSpot) => new CursorStub();
  110. private sealed class CursorStub : ICursorImpl
  111. {
  112. public void Dispose()
  113. {
  114. }
  115. }
  116. }
  117. }
  118. }