From 7f608ffa6cf888a824821ed31a3ffaa2a080fb2d Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Wed, 6 May 2015 15:24:23 +0200 Subject: [PATCH] Fixed failing tests. --- .../Collections/PerspexReadOnlyListView.cs | 4 +- .../Generators/ItemContainerGenerator.cs | 15 ++-- Perspex.Controls/Presenters/DeckPresenter.cs | 10 ++- Perspex.Controls/Primitives/TabStrip.cs | 3 +- Perspex.Controls/TabControl.cs | 9 ++- Perspex.Themes.Default/TabControlStyle.cs | 4 +- .../TabControlTests.cs | 69 +++++++++---------- 7 files changed, 61 insertions(+), 53 deletions(-) diff --git a/Perspex.Base/Collections/PerspexReadOnlyListView.cs b/Perspex.Base/Collections/PerspexReadOnlyListView.cs index c6bae2e354..0f9d1cd0c4 100644 --- a/Perspex.Base/Collections/PerspexReadOnlyListView.cs +++ b/Perspex.Base/Collections/PerspexReadOnlyListView.cs @@ -38,7 +38,7 @@ namespace Perspex.Collections public int Count { - get { return this.source.Count; } + get { return this.source?.Count ?? 0; } } public IPerspexReadOnlyList Source @@ -169,7 +169,7 @@ namespace Perspex.Collections public int Count { - get { return this.source.Count; } + get { return this.source?.Count ?? 0; } } public IPerspexReadOnlyList Source diff --git a/Perspex.Controls/Generators/ItemContainerGenerator.cs b/Perspex.Controls/Generators/ItemContainerGenerator.cs index 1505bc15cd..eff9ca06f5 100644 --- a/Perspex.Controls/Generators/ItemContainerGenerator.cs +++ b/Perspex.Controls/Generators/ItemContainerGenerator.cs @@ -85,14 +85,17 @@ namespace Perspex.Controls.Generators { Control container = this.CreateContainerOverride(item); - if (container.DataContext == null) + if (container != null) { - container.DataContext = item; + if (container.DataContext == null) + { + container.DataContext = item; + } + + container.TemplatedParent = null; + this.AddInternal(item, container); + result.Add(container); } - - container.TemplatedParent = null; - this.AddInternal(item, container); - result.Add(container); } } finally diff --git a/Perspex.Controls/Presenters/DeckPresenter.cs b/Perspex.Controls/Presenters/DeckPresenter.cs index d4e3c79a8c..bcaba7594b 100644 --- a/Perspex.Controls/Presenters/DeckPresenter.cs +++ b/Perspex.Controls/Presenters/DeckPresenter.cs @@ -135,9 +135,13 @@ namespace Perspex.Controls.Presenters if (value.Item2 != null) { - to = generator.Generate(new[] { value.Item2 }).Single(); - this.Panel.Children.Add(to); - toIndex = this.Items.IndexOf(value.Item2); + to = generator.Generate(new[] { value.Item2 }).FirstOrDefault(); + + if (to != null) + { + this.Panel.Children.Add(to); + toIndex = this.Items.IndexOf(value.Item2); + } } if (this.Transition != null) diff --git a/Perspex.Controls/Primitives/TabStrip.cs b/Perspex.Controls/Primitives/TabStrip.cs index e336a86aaa..37859ab188 100644 --- a/Perspex.Controls/Primitives/TabStrip.cs +++ b/Perspex.Controls/Primitives/TabStrip.cs @@ -27,7 +27,8 @@ namespace Perspex.Controls.Primitives public TabStrip() { - this.BindTwoWay(SelectedTabProperty, this, SelectingItemsControl.SelectedItemProperty); + this.GetObservable(SelectedItemProperty).Subscribe(x => this.SelectedTab = x as TabItem); + this.GetObservable(SelectedTabProperty).Subscribe(x => this.SelectedItem = x as TabItem); } public TabItem SelectedTab diff --git a/Perspex.Controls/TabControl.cs b/Perspex.Controls/TabControl.cs index edad4881d9..e0e7b1bd4d 100644 --- a/Perspex.Controls/TabControl.cs +++ b/Perspex.Controls/TabControl.cs @@ -35,14 +35,15 @@ namespace Perspex.Controls public TabControl() { + this.BindTwoWay(SelectedTabProperty, this, SelectingItemsControl.SelectedItemProperty); + this.GetObservable(SelectedItemProperty).Subscribe(x => { ContentControl c = x as ContentControl; object content = (c != null) ? c.Content : c; + this.SetValue(SelectedTabProperty, x); this.SetValue(SelectedContentProperty, content); }); - - this.BindTwoWay(SelectedTabProperty, this, SelectingItemsControl.SelectedItemProperty); } public object SelectedContent @@ -73,11 +74,13 @@ namespace Perspex.Controls // Don't handle keypresses. } + Deck deck; + protected override void OnTemplateApplied() { base.OnTemplateApplied(); - var deck = this.GetTemplateChild("deck"); + this.deck = this.GetTemplateChild("deck"); this.logicalChildren.Source = ((ILogical)deck).LogicalChildren; } } diff --git a/Perspex.Themes.Default/TabControlStyle.cs b/Perspex.Themes.Default/TabControlStyle.cs index 6f566b3904..4e1ce72efa 100644 --- a/Perspex.Themes.Default/TabControlStyle.cs +++ b/Perspex.Themes.Default/TabControlStyle.cs @@ -47,7 +47,7 @@ namespace Perspex.Themes.Default { Name = "tabStrip", [~TabStrip.ItemsProperty] = control[~TabControl.ItemsProperty], - [~~TabStrip.SelectedItemProperty] = control[~~TabControl.SelectedItemProperty], + [!!TabStrip.SelectedItemProperty] = control[!!TabControl.SelectedItemProperty], }, new Deck { @@ -57,7 +57,7 @@ namespace Perspex.Themes.Default new DataTemplate(x => control.MaterializeDataTemplate(x.Content)), }, [~Deck.ItemsProperty] = control[~TabControl.ItemsProperty], - [!Deck.SelectedItemProperty] = control[!TabControl.SelectedItemProperty], + [~Deck.SelectedItemProperty] = control[~TabControl.SelectedItemProperty], [~Deck.TransitionProperty] = control[~TabControl.TransitionProperty], [Grid.RowProperty] = 1, } diff --git a/Tests/Perspex.Controls.UnitTests/TabControlTests.cs b/Tests/Perspex.Controls.UnitTests/TabControlTests.cs index d214652008..21863409ca 100644 --- a/Tests/Perspex.Controls.UnitTests/TabControlTests.cs +++ b/Tests/Perspex.Controls.UnitTests/TabControlTests.cs @@ -6,11 +6,11 @@ namespace Perspex.Controls.UnitTests { - using System.Linq; using Perspex.Controls.Presenters; using Perspex.Controls.Primitives; + using Perspex.Controls.Templates; using Perspex.LogicalTree; - using Perspex.Styling; + using System.Linq; using Xunit; public class TabControlTests @@ -18,50 +18,30 @@ namespace Perspex.Controls.UnitTests [Fact] public void First_Tab_Should_Be_Selected_By_Default() { + TabItem selected; var target = new TabControl { Template = ControlTemplate.Create(this.CreateTabControlTemplate), Items = new[] { - new TabItem - { - Name = "first" - }, - new TabItem - { - Name = "second" - }, - } - }; - - target.ApplyTemplate(); - - Assert.NotNull(target.SelectedTab); - Assert.Equal(target.SelectedTab, target.SelectedItem); - } - - [Fact] - public void First_Tab_Content_Should_Be_Displayed_By_Default() - { - var target = new TabControl - { - Template = ControlTemplate.Create(this.CreateTabControlTemplate), - Items = new[] - { - new TabItem + (selected = new TabItem { - Content = new TextBlock(), - }, + Name = "first", + Content = "foo", + }), new TabItem { - Content = new Border(), + Name = "second", + Content = "bar", }, } }; target.ApplyTemplate(); - Assert.IsType(target.SelectedContent); + Assert.Equal(selected, target.SelectedItem); + Assert.Equal(selected, target.SelectedTab); + Assert.Equal("foo", target.SelectedContent); } [Fact] @@ -124,11 +104,11 @@ namespace Perspex.Controls.UnitTests { new TabItem { - Content = new TextBlock { Name = "Foo" } + Content = "foo" }, new TabItem { - Content = new TextBlock { Name = "Foo" } + Content = "bar" }, }, }; @@ -136,7 +116,7 @@ namespace Perspex.Controls.UnitTests target.ApplyTemplate(); Assert.Equal(1, target.GetLogicalChildren().Count()); - Assert.Equal("Foo", ((TextBlock)target.GetLogicalChildren().First()).Name); + Assert.Equal("foo", ((TextBlock)target.GetLogicalChildren().First()).Text); } private Control CreateTabControlTemplate(TabControl parent) @@ -155,8 +135,13 @@ namespace Perspex.Controls.UnitTests new Deck { Name = "deck", + Template = ControlTemplate.Create(this.CreateDeckTemplate), + DataTemplates = new DataTemplates + { + new DataTemplate(x => parent.MaterializeDataTemplate(x.Content)), + }, [~Deck.ItemsProperty] = parent[~TabControl.ItemsProperty], - [!Deck.SelectedItemProperty] = parent[!TabControl.SelectedItemProperty], + [~Deck.SelectedItemProperty] = parent[~TabControl.SelectedItemProperty], } } }; @@ -170,5 +155,17 @@ namespace Perspex.Controls.UnitTests [~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.SelectedItemProperty] = control[~Deck.SelectedItemProperty], + [~DeckPresenter.TransitionProperty] = control[~Deck.TransitionProperty], + }; + } } }