A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

182 lines
6.0 KiB

// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System.Collections.ObjectModel;
using System.Linq;
using Perspex.Controls.Presenters;
using Perspex.Controls.Primitives;
using Perspex.Controls.Templates;
using Perspex.LogicalTree;
using Xunit;
namespace Perspex.Controls.UnitTests
{
public class TabControlTests
{
[Fact]
public void First_Tab_Should_Be_Selected_By_Default()
{
TabItem selected;
var target = new TabControl
{
Template = new ControlTemplate<TabControl>(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>(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>(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>(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>(CreateTabStripTemplate),
[!ItemsControl.ItemsProperty] = parent[!ItemsControl.ItemsProperty],
[!!TabStrip.SelectedTabProperty] = parent[!!TabControl.SelectedTabProperty]
},
new Deck
{
Name = "deck",
Template = new ControlTemplate<Deck>(CreateDeckTemplate),
DataTemplates = new DataTemplates
{
new DataTemplate<TabItem>(x => (Control)parent.MaterializeDataTemplate(x.Content)),
},
[!ItemsControl.ItemsProperty] = parent[!ItemsControl.ItemsProperty],
[!SelectingItemsControl.SelectedItemProperty] = parent[!SelectingItemsControl.SelectedItemProperty],
}
}
};
}
private Control CreateTabStripTemplate(TabStrip parent)
{
return new ItemsPresenter
{
Name = "itemsPresenter",
[~ItemsPresenter.ItemsProperty] = parent[~ItemsControl.ItemsProperty],
};
}
private Control CreateDeckTemplate(Deck control)
{
return new DeckPresenter
{
Name = "itemsPresenter",
[!ItemsPresenter.ItemsProperty] = control[!ItemsControl.ItemsProperty],
[!ItemsPresenter.ItemsPanelProperty] = control[!ItemsControl.ItemsPanelProperty],
[!DeckPresenter.SelectedIndexProperty] = control[!SelectingItemsControl.SelectedIndexProperty],
[~DeckPresenter.TransitionProperty] = control[~Deck.TransitionProperty],
};
}
}
}