CarouselTests.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Copyright (c) The Perspex 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 Perspex.Controls.Presenters;
  5. using Perspex.Controls.Primitives;
  6. using Perspex.Controls.Templates;
  7. using Perspex.LogicalTree;
  8. using Xunit;
  9. namespace Perspex.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. private Control CreateTemplate(Carousel control)
  49. {
  50. return new CarouselPresenter
  51. {
  52. Name = "PART_ItemsPresenter",
  53. [~ItemsPresenter.ItemsProperty] = control[~ItemsControl.ItemsProperty],
  54. [~ItemsPresenter.ItemsPanelProperty] = control[~ItemsControl.ItemsPanelProperty],
  55. [~CarouselPresenter.SelectedIndexProperty] = control[~SelectingItemsControl.SelectedIndexProperty],
  56. [~CarouselPresenter.TransitionProperty] = control[~Carousel.TransitionProperty],
  57. };
  58. }
  59. }
  60. }