PipsPagerTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System.Threading.Tasks;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Presenters;
  4. using Avalonia.Controls.Shapes;
  5. using Avalonia.Controls.Templates;
  6. using Avalonia.Data;
  7. using Avalonia.Layout;
  8. using Avalonia.Media;
  9. using Avalonia.Styling;
  10. using Avalonia.Threading;
  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 PipsPagerTests : TestBase
  19. {
  20. public PipsPagerTests()
  21. : base(@"Controls\PipsPager")
  22. {
  23. }
  24. private static IControlTemplate CreatePipsPagerTemplate()
  25. {
  26. return new FuncControlTemplate<PipsPager>((control, scope) =>
  27. {
  28. var stackPanel = new StackPanel
  29. {
  30. Name = "PART_RootPanel",
  31. Spacing = 4,
  32. [!StackPanel.OrientationProperty] = control[!PipsPager.OrientationProperty],
  33. HorizontalAlignment = HorizontalAlignment.Center,
  34. VerticalAlignment = VerticalAlignment.Center
  35. };
  36. var buttonTemplate = new FuncControlTemplate<Button>((b, s) =>
  37. new Border
  38. {
  39. Background = Brushes.LightGray,
  40. Child = new TextBlock
  41. {
  42. [!TextBlock.TextProperty] = b[!Button.ContentProperty],
  43. FontFamily = TestFontFamily,
  44. VerticalAlignment = VerticalAlignment.Center,
  45. HorizontalAlignment = HorizontalAlignment.Center
  46. }
  47. });
  48. var prevButton = new Button
  49. {
  50. Name = "PART_PreviousButton",
  51. Content = "<",
  52. Template = buttonTemplate,
  53. Width = 20,
  54. Height = 20,
  55. [!Button.IsVisibleProperty] = control[!PipsPager.IsPreviousButtonVisibleProperty]
  56. }.RegisterInNameScope(scope);
  57. var nextButton = new Button
  58. {
  59. Name = "PART_NextButton",
  60. Content = ">",
  61. Template = buttonTemplate,
  62. Width = 20,
  63. Height = 20,
  64. [!Button.IsVisibleProperty] = control[!PipsPager.IsNextButtonVisibleProperty]
  65. }.RegisterInNameScope(scope);
  66. var listBoxTemplate = new FuncControlTemplate<ListBox>((lb, s) =>
  67. new ItemsPresenter
  68. {
  69. Name = "PART_ItemsPresenter",
  70. [~ItemsPresenter.ItemsPanelProperty] = lb[~ListBox.ItemsPanelProperty],
  71. }.RegisterInNameScope(s));
  72. var pipsList = new ListBox
  73. {
  74. Name = "PART_PipsPagerList",
  75. Template = listBoxTemplate,
  76. [!ListBox.ItemsSourceProperty] = new Binding("TemplateSettings.Pips") { Source = control },
  77. [!ListBox.SelectedIndexProperty] = control[!PipsPager.SelectedPageIndexProperty],
  78. ItemsPanel = new FuncTemplate<Panel?>(() => new StackPanel
  79. {
  80. Spacing = 2,
  81. [!StackPanel.OrientationProperty] = control[!PipsPager.OrientationProperty]
  82. })
  83. }.RegisterInNameScope(scope);
  84. var itemStyle = new Style(x => x.OfType<ListBoxItem>());
  85. itemStyle.Setters.Add(new Setter(ListBoxItem.TemplateProperty,
  86. new FuncControlTemplate<ListBoxItem>((item, s) =>
  87. new Rectangle { Name = "Pip", Width = 10, Height = 10 }.RegisterInNameScope(s))));
  88. var defaultPipStyle = new Style(x => x.OfType<ListBoxItem>().Template().Name("Pip"));
  89. defaultPipStyle.Setters.Add(new Setter(Rectangle.FillProperty, Brushes.Gray));
  90. var selectedStyle = new Style(x => x.OfType<ListBoxItem>().Class(":selected").Template().Name("Pip"));
  91. selectedStyle.Setters.Add(new Setter(Rectangle.FillProperty, Brushes.Red));
  92. pipsList.Styles.Add(itemStyle);
  93. pipsList.Styles.Add(defaultPipStyle);
  94. pipsList.Styles.Add(selectedStyle);
  95. stackPanel.Children.Add(prevButton);
  96. stackPanel.Children.Add(pipsList);
  97. stackPanel.Children.Add(nextButton);
  98. return stackPanel;
  99. });
  100. }
  101. [Fact]
  102. public async Task PipsPager_Default()
  103. {
  104. var pipsPager = new PipsPager
  105. {
  106. Template = CreatePipsPagerTemplate(),
  107. NumberOfPages = 5,
  108. SelectedPageIndex = 1
  109. };
  110. var target = new Border
  111. {
  112. Padding = new Thickness(20),
  113. Background = Brushes.White,
  114. Child = pipsPager,
  115. Width = 400,
  116. Height = 150
  117. };
  118. target.Measure(new Size(400, 150));
  119. target.Arrange(new Rect(0, 0, 400, 150));
  120. Dispatcher.UIThread.RunJobs(null, TestContext.Current.CancellationToken);
  121. await RenderToFile(target);
  122. CompareImages(skipImmediate: true);
  123. }
  124. [Fact]
  125. public async Task PipsPager_Preselected_Index()
  126. {
  127. var pipsPager = new PipsPager
  128. {
  129. Template = CreatePipsPagerTemplate(),
  130. NumberOfPages = 5,
  131. SelectedPageIndex = 3
  132. };
  133. var target = new Border
  134. {
  135. Padding = new Thickness(20),
  136. Background = Brushes.White,
  137. Child = pipsPager,
  138. Width = 400,
  139. Height = 150
  140. };
  141. target.Measure(new Size(400, 150));
  142. target.Arrange(new Rect(0, 0, 400, 150));
  143. Dispatcher.UIThread.RunJobs(null, TestContext.Current.CancellationToken);
  144. await RenderToFile(target);
  145. CompareImages(skipImmediate: true);
  146. }
  147. }
  148. }