NavigationPageTests.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Threading.Tasks;
  2. using Avalonia.Controls;
  3. using Avalonia.Layout;
  4. using Avalonia.Media;
  5. using Avalonia.Styling;
  6. using Avalonia.Themes.Simple;
  7. using Xunit;
  8. #if AVALONIA_SKIA
  9. namespace Avalonia.Skia.RenderTests
  10. #else
  11. namespace Avalonia.Direct2D1.RenderTests.Controls
  12. #endif
  13. {
  14. public class NavigationPageRenderTests : TestBase
  15. {
  16. public NavigationPageRenderTests()
  17. : base(@"Controls\NavigationPage")
  18. {
  19. }
  20. private static Style FontStyle => new Style(x => x.OfType<TextBlock>())
  21. {
  22. Setters = { new Setter(TextBlock.FontFamilyProperty, TestFontFamily) }
  23. };
  24. private static Border PageContent() => new Border
  25. {
  26. Width = 80,
  27. Height = 40,
  28. Background = new SolidColorBrush(Color.Parse("#F5F5F5")),
  29. HorizontalAlignment = HorizontalAlignment.Center,
  30. VerticalAlignment = VerticalAlignment.Center
  31. };
  32. [Fact]
  33. public async Task NavigationPage_SinglePage_ShowsNavBar()
  34. {
  35. var nav = new NavigationPage { Background = Brushes.White };
  36. nav.Resources["NavigationBarBackground"] = new SolidColorBrush(Color.Parse("#1565C0"));
  37. nav.Resources["NavigationBarForeground"] = Brushes.White;
  38. var target = new Decorator { Width = 400, Height = 300, Child = nav };
  39. await nav.PushAsync(new ContentPage
  40. {
  41. Header = "Home",
  42. Background = Brushes.White,
  43. Content = PageContent(),
  44. HorizontalContentAlignment = HorizontalAlignment.Center,
  45. VerticalContentAlignment = VerticalAlignment.Center
  46. });
  47. target.Styles.Add(new SimpleTheme());
  48. target.Styles.Add(FontStyle);
  49. await RenderToFile(target);
  50. CompareImages(skipImmediate: true);
  51. }
  52. [Fact]
  53. public async Task NavigationPage_TwoPages_ShowsBackButton()
  54. {
  55. var nav = new NavigationPage { Background = Brushes.White };
  56. nav.Resources["NavigationBarBackground"] = new SolidColorBrush(Color.Parse("#1565C0"));
  57. nav.Resources["NavigationBarForeground"] = Brushes.White;
  58. var target = new Decorator { Width = 400, Height = 300, Child = nav };
  59. await nav.PushAsync(new ContentPage
  60. {
  61. Header = "Home",
  62. Background = Brushes.White,
  63. Content = PageContent(),
  64. HorizontalContentAlignment = HorizontalAlignment.Center,
  65. VerticalContentAlignment = VerticalAlignment.Center
  66. });
  67. await nav.PushAsync(new ContentPage
  68. {
  69. Header = "Details",
  70. Background = Brushes.White,
  71. Content = PageContent(),
  72. HorizontalContentAlignment = HorizontalAlignment.Center,
  73. VerticalContentAlignment = VerticalAlignment.Center
  74. }, null);
  75. target.Styles.Add(new SimpleTheme());
  76. target.Styles.Add(FontStyle);
  77. await RenderToFile(target);
  78. CompareImages(skipImmediate: true);
  79. }
  80. [Fact]
  81. public async Task NavigationPage_CustomBarBackground()
  82. {
  83. var nav = new NavigationPage { Background = Brushes.White, HasShadow = true };
  84. nav.Resources["NavigationBarBackground"] = new SolidColorBrush(Color.Parse("#2E7D32"));
  85. nav.Resources["NavigationBarForeground"] = Brushes.White;
  86. var target = new Decorator { Width = 400, Height = 300, Child = nav };
  87. await nav.PushAsync(new ContentPage
  88. {
  89. Header = "Green Theme",
  90. Background = Brushes.White,
  91. Content = PageContent(),
  92. HorizontalContentAlignment = HorizontalAlignment.Center,
  93. VerticalContentAlignment = VerticalAlignment.Center
  94. });
  95. target.Styles.Add(new SimpleTheme());
  96. target.Styles.Add(FontStyle);
  97. await RenderToFile(target);
  98. CompareImages(skipImmediate: true);
  99. }
  100. }
  101. }