TabControlTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // -----------------------------------------------------------------------
  2. // <copyright file="TabControlTests.cs" company="Steven Kirk">
  3. // Copyright 2015 MIT Licence. See licence.md for more information.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace Perspex.Controls.UnitTests
  7. {
  8. using System.Collections.ObjectModel;
  9. using System.Linq;
  10. using Perspex.Controls.Presenters;
  11. using Perspex.Controls.Primitives;
  12. using Perspex.Controls.Templates;
  13. using Perspex.LogicalTree;
  14. using Xunit;
  15. public class TabControlTests
  16. {
  17. [Fact]
  18. public void First_Tab_Should_Be_Selected_By_Default()
  19. {
  20. TabItem selected;
  21. var target = new TabControl
  22. {
  23. Template = new ControlTemplate<TabControl>(this.CreateTabControlTemplate),
  24. Items = new[]
  25. {
  26. (selected = new TabItem
  27. {
  28. Name = "first",
  29. Content = "foo",
  30. }),
  31. new TabItem
  32. {
  33. Name = "second",
  34. Content = "bar",
  35. },
  36. }
  37. };
  38. target.ApplyTemplate();
  39. Assert.Equal(selected, target.SelectedItem);
  40. Assert.Equal(selected, target.SelectedTab);
  41. }
  42. [Fact]
  43. public void Setting_SelectedItem_Should_Set_SelectedTab()
  44. {
  45. var target = new TabControl
  46. {
  47. Template = new ControlTemplate<TabControl>(this.CreateTabControlTemplate),
  48. Items = new[]
  49. {
  50. new TabItem
  51. {
  52. Name = "first",
  53. Content = "foo",
  54. },
  55. new TabItem
  56. {
  57. Name = "second",
  58. Content = "bar",
  59. },
  60. }
  61. };
  62. target.ApplyTemplate();
  63. target.SelectedItem = target.Items.Cast<TabItem>().ElementAt(1);
  64. Assert.Same(target.SelectedTab, target.SelectedItem);
  65. }
  66. [Fact]
  67. public void Logical_Child_Should_Be_Selected_Tab_Content()
  68. {
  69. var target = new TabControl
  70. {
  71. Template = new ControlTemplate<TabControl>(this.CreateTabControlTemplate),
  72. Items = new[]
  73. {
  74. new TabItem
  75. {
  76. Content = "foo"
  77. },
  78. new TabItem
  79. {
  80. Content = "bar"
  81. },
  82. },
  83. };
  84. target.ApplyTemplate();
  85. Assert.Equal(1, target.GetLogicalChildren().Count());
  86. Assert.Equal("foo", ((TextBlock)target.GetLogicalChildren().First()).Text);
  87. }
  88. [Fact]
  89. public void Removal_Should_Set_Next_Tab()
  90. {
  91. var collection = new ObservableCollection<TabItem>()
  92. {
  93. new TabItem
  94. {
  95. Name = "first",
  96. Content = "foo",
  97. },
  98. new TabItem
  99. {
  100. Name = "second",
  101. Content = "bar",
  102. },
  103. new TabItem
  104. {
  105. Name = "3rd",
  106. Content = "barf",
  107. },
  108. };
  109. var target = new TabControl
  110. {
  111. Template = new ControlTemplate<TabControl>(this.CreateTabControlTemplate),
  112. Items = collection,
  113. };
  114. target.ApplyTemplate();
  115. target.SelectedItem = collection[1];
  116. collection.RemoveAt(1);
  117. // compare with former [2] now [1] == "3rd"
  118. Assert.Same(collection[1], target.SelectedItem);
  119. Assert.Same(target.SelectedTab, target.SelectedItem);
  120. }
  121. private Control CreateTabControlTemplate(TabControl parent)
  122. {
  123. return new StackPanel
  124. {
  125. Children = new Controls
  126. {
  127. new TabStrip
  128. {
  129. Name = "tabStrip",
  130. Template = new ControlTemplate<TabStrip>(this.CreateTabStripTemplate),
  131. [!TabStrip.ItemsProperty] = parent[!TabControl.ItemsProperty],
  132. [!!TabStrip.SelectedTabProperty] = parent[!!TabControl.SelectedTabProperty]
  133. },
  134. new Deck
  135. {
  136. Name = "deck",
  137. Template = new ControlTemplate<Deck>(this.CreateDeckTemplate),
  138. DataTemplates = new DataTemplates
  139. {
  140. new DataTemplate<TabItem>(x => (Control)parent.MaterializeDataTemplate(x.Content)),
  141. },
  142. [!Deck.ItemsProperty] = parent[!TabControl.ItemsProperty],
  143. [!Deck.SelectedItemProperty] = parent[!TabControl.SelectedItemProperty],
  144. }
  145. }
  146. };
  147. }
  148. private Control CreateTabStripTemplate(TabStrip parent)
  149. {
  150. return new ItemsPresenter
  151. {
  152. Name = "itemsPresenter",
  153. [~ItemsPresenter.ItemsProperty] = parent[~TabStrip.ItemsProperty],
  154. };
  155. }
  156. private Control CreateDeckTemplate(Deck control)
  157. {
  158. return new DeckPresenter
  159. {
  160. Name = "itemsPresenter",
  161. [!ItemsPresenter.ItemsProperty] = control[!Deck.ItemsProperty],
  162. [!ItemsPresenter.ItemsPanelProperty] = control[!Deck.ItemsPanelProperty],
  163. [!DeckPresenter.SelectedIndexProperty] = control[!Deck.SelectedIndexProperty],
  164. [~DeckPresenter.TransitionProperty] = control[~Deck.TransitionProperty],
  165. };
  166. }
  167. }
  168. }