CarouselTests.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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.Linq;
  4. using Avalonia.Controls.Presenters;
  5. using Avalonia.Controls.Primitives;
  6. using Avalonia.Controls.Templates;
  7. using Avalonia.LogicalTree;
  8. using Xunit;
  9. namespace Avalonia.Controls.UnitTests
  10. {
  11. public class CarouselTests
  12. {
  13. [Fact]
  14. public void First_Item_Should_Be_Selected_By_Default()
  15. {
  16. var target = new Carousel
  17. {
  18. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  19. Items = new[]
  20. {
  21. "Foo",
  22. "Bar"
  23. }
  24. };
  25. target.ApplyTemplate();
  26. Assert.Equal(0, target.SelectedIndex);
  27. Assert.Equal("Foo", target.SelectedItem);
  28. }
  29. [Fact]
  30. public void LogicalChild_Should_Be_Selected_Item()
  31. {
  32. var target = new Carousel
  33. {
  34. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  35. Items = new[]
  36. {
  37. "Foo",
  38. "Bar"
  39. }
  40. };
  41. target.ApplyTemplate();
  42. target.Presenter.ApplyTemplate();
  43. Assert.Equal(1, target.GetLogicalChildren().Count());
  44. var child = target.GetLogicalChildren().Single();
  45. Assert.IsType<TextBlock>(child);
  46. Assert.Equal("Foo", ((TextBlock)child).Text);
  47. }
  48. [Fact]
  49. public void Should_Remove_NonCurrent_Page_When_IsVirtualized_True()
  50. {
  51. var target = new Carousel
  52. {
  53. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  54. Items = new[] { "foo", "bar" },
  55. IsVirtualized = true,
  56. SelectedIndex = 0,
  57. };
  58. target.ApplyTemplate();
  59. target.Presenter.ApplyTemplate();
  60. Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
  61. target.SelectedIndex = 1;
  62. Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
  63. }
  64. [Fact]
  65. public void Should_Not_Remove_NonCurrent_Page_When_IsVirtualized_False()
  66. {
  67. var target = new Carousel
  68. {
  69. Template = new FuncControlTemplate<Carousel>(CreateTemplate),
  70. Items = new[] { "foo", "bar" },
  71. IsVirtualized = false,
  72. SelectedIndex = 0,
  73. };
  74. target.ApplyTemplate();
  75. target.Presenter.ApplyTemplate();
  76. Assert.Equal(1, target.ItemContainerGenerator.Containers.Count());
  77. target.SelectedIndex = 1;
  78. Assert.Equal(2, target.ItemContainerGenerator.Containers.Count());
  79. }
  80. private Control CreateTemplate(Carousel control)
  81. {
  82. return new CarouselPresenter
  83. {
  84. Name = "PART_ItemsPresenter",
  85. [~CarouselPresenter.IsVirtualizedProperty] = control[~Carousel.IsVirtualizedProperty],
  86. [~CarouselPresenter.ItemsProperty] = control[~Carousel.ItemsProperty],
  87. [~CarouselPresenter.ItemsPanelProperty] = control[~Carousel.ItemsPanelProperty],
  88. [~CarouselPresenter.SelectedIndexProperty] = control[~Carousel.SelectedIndexProperty],
  89. [~CarouselPresenter.TransitionProperty] = control[~Carousel.TransitionProperty],
  90. };
  91. }
  92. }
  93. }