| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- // -----------------------------------------------------------------------
- // <copyright file="TabControlTests.cs" company="Steven Kirk">
- // Copyright 2015 MIT Licence. See licence.md for more information.
- // </copyright>
- // -----------------------------------------------------------------------
- namespace Perspex.Controls.UnitTests
- {
- using System.Collections.ObjectModel;
- using System.Linq;
- using Perspex.Controls.Presenters;
- using Perspex.Controls.Primitives;
- using Perspex.Controls.Templates;
- using Perspex.LogicalTree;
- using Xunit;
- public class TabControlTests
- {
- [Fact]
- public void First_Tab_Should_Be_Selected_By_Default()
- {
- TabItem selected;
- var target = new TabControl
- {
- Template = new ControlTemplate<TabControl>(this.CreateTabControlTemplate),
- Items = new[]
- {
- (selected = new TabItem
- {
- Name = "first",
- Content = "foo",
- }),
- new TabItem
- {
- Name = "second",
- Content = "bar",
- },
- }
- };
- target.ApplyTemplate();
- Assert.Equal(selected, target.SelectedItem);
- Assert.Equal(selected, target.SelectedTab);
- }
- [Fact]
- public void Setting_SelectedItem_Should_Set_SelectedTab()
- {
- var target = new TabControl
- {
- Template = new ControlTemplate<TabControl>(this.CreateTabControlTemplate),
- Items = new[]
- {
- new TabItem
- {
- Name = "first",
- Content = "foo",
- },
- new TabItem
- {
- Name = "second",
- Content = "bar",
- },
- }
- };
- target.ApplyTemplate();
- target.SelectedItem = target.Items.Cast<TabItem>().ElementAt(1);
- Assert.Same(target.SelectedTab, target.SelectedItem);
- }
- [Fact]
- public void Logical_Child_Should_Be_Selected_Tab_Content()
- {
- var target = new TabControl
- {
- Template = new ControlTemplate<TabControl>(this.CreateTabControlTemplate),
- Items = new[]
- {
- new TabItem
- {
- Content = "foo"
- },
- new TabItem
- {
- Content = "bar"
- },
- },
- };
- target.ApplyTemplate();
- Assert.Equal(1, target.GetLogicalChildren().Count());
- Assert.Equal("foo", ((TextBlock)target.GetLogicalChildren().First()).Text);
- }
- [Fact]
- public void Removal_Should_Set_Next_Tab()
- {
- var collection = new ObservableCollection<TabItem>()
- {
- new TabItem
- {
- Name = "first",
- Content = "foo",
- },
- new TabItem
- {
- Name = "second",
- Content = "bar",
- },
- new TabItem
- {
- Name = "3rd",
- Content = "barf",
- },
- };
- var target = new TabControl
- {
- Template = new ControlTemplate<TabControl>(this.CreateTabControlTemplate),
- Items = collection,
- };
- target.ApplyTemplate();
- target.SelectedItem = collection[1];
- collection.RemoveAt(1);
- // compare with former [2] now [1] == "3rd"
- Assert.Same(collection[1], target.SelectedItem);
- Assert.Same(target.SelectedTab, target.SelectedItem);
- }
- private Control CreateTabControlTemplate(TabControl parent)
- {
- return new StackPanel
- {
- Children = new Controls
- {
- new TabStrip
- {
- Name = "tabStrip",
- Template = new ControlTemplate<TabStrip>(this.CreateTabStripTemplate),
- [!TabStrip.ItemsProperty] = parent[!TabControl.ItemsProperty],
- [!!TabStrip.SelectedTabProperty] = parent[!!TabControl.SelectedTabProperty]
- },
- new Deck
- {
- Name = "deck",
- Template = new ControlTemplate<Deck>(this.CreateDeckTemplate),
- DataTemplates = new DataTemplates
- {
- new DataTemplate<TabItem>(x => (Control)parent.MaterializeDataTemplate(x.Content)),
- },
- [!Deck.ItemsProperty] = parent[!TabControl.ItemsProperty],
- [!Deck.SelectedItemProperty] = parent[!TabControl.SelectedItemProperty],
- }
- }
- };
- }
- private Control CreateTabStripTemplate(TabStrip parent)
- {
- return new ItemsPresenter
- {
- Name = "itemsPresenter",
- [~ItemsPresenter.ItemsProperty] = parent[~TabStrip.ItemsProperty],
- };
- }
- private Control CreateDeckTemplate(Deck control)
- {
- return new DeckPresenter
- {
- Name = "itemsPresenter",
- [!ItemsPresenter.ItemsProperty] = control[!Deck.ItemsProperty],
- [!ItemsPresenter.ItemsPanelProperty] = control[!Deck.ItemsPanelProperty],
- [!DeckPresenter.SelectedIndexProperty] = control[!Deck.SelectedIndexProperty],
- [~DeckPresenter.TransitionProperty] = control[~Deck.TransitionProperty],
- };
- }
- }
- }
|