Skip to content

Commit

Permalink
test(TabView): Verify item selection content binding
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jan 19, 2021
1 parent 20b4af1 commit 8fa2a49
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using NUnit.Framework;
using SamplesApp.UITests.TestFramework;
using Uno.UITest.Helpers;
using Uno.UITest.Helpers.Queries;

namespace SamplesApp.UITests.Microsoft_UI_Xaml_Controls.TabViewTests
{
public class Given_TabView_Uno : SampleControlUITestBase
{
[Test]
[AutoRetry]
public void SelectionContentTest()
{
Run("UITests.Microsoft_UI_Xaml_Controls.TabViewTests.TabViewItemsSourceTests");

Console.WriteLine("Verify content is displayed for initially selected tab.");
_app.WaitForElement("SelectButton");
_app.Tap("SelectButton");

_app.WaitForElement("ContentButton");
var contentButton = _app.Marked("ContentButton");

var result = contentButton.GetDependencyPropertyValue<string>("Content");
Assert.AreEqual("Tab 2", result);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,24 @@
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<muxc:TabView Grid.Row="1"
x:Name="tabView"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
IsAddTabButtonVisible="False"
TabWidthMode="SizeToContent"
TabItemsSource="{x:Bind Path=DefaultViewModel.TabItems, Mode=OneWay}"
Margin="10">
x:Name="tabView"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
IsAddTabButtonVisible="False"
TabWidthMode="SizeToContent"
TabItemsSource="{x:Bind Path=DefaultViewModel.TabItems, Mode=OneWay}"
Margin="10">
<muxc:TabView.TabStripHeader>
<StackPanel Orientation="Horizontal">
<Button Click="OnAddTabViewItemButtonClick">Add TabViewItem</Button>
<Button Click="SelectItem" x:Name="SelectButton">Select second item</Button>
</StackPanel>
</muxc:TabView.TabStripHeader>
<muxc:TabView.TabItemTemplate>
<DataTemplate x:DataType="local:TabItemViewModel">
<muxc:TabViewItem Header="{x:Bind Header, Mode=OneWay}"
IsClosable="False">
<Button Margin="5"
<Button x:Name="ContentButton" Margin="5"
Content="{x:Bind Header, Mode=OneWay}" />
</muxc:TabViewItem>
</DataTemplate>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ namespace UITests.Microsoft_UI_Xaml_Controls.TabViewTests
[Sample("TabView", "WinUI")]
public sealed partial class TabViewItemsSourceTests : Page
{
public TabViewItemsSourceTests()
{
DemoViewModel defaultViewModel = new DemoViewModel();
// add Tabs as source for binding of TabView
defaultViewModel.TabItems.Add(new TabItemViewModel() { Header = "Tab 1" });
defaultViewModel.TabItems.Add(new TabItemViewModel() { Header = "Tab 2" });
defaultViewModel.TabItems.Add(new TabItemViewModel() { Header = "Tab 3" });
defaultViewModel.TabItems.Add(new TabItemViewModel() { Header = "Tab 4" });

this.DefaultViewModel = defaultViewModel;

this.InitializeComponent();
}

public DemoViewModel DefaultViewModel
{
get { return (DemoViewModel)GetValue(DefaultViewModelProperty); }
Expand All @@ -33,67 +47,28 @@ public DemoViewModel DefaultViewModel
nameof(DefaultViewModel),
typeof(DemoViewModel),
typeof(TabViewItemsSourceTests),
new PropertyMetadata(null));

new PropertyMetadata(null));


public TabViewItemsSourceTests()
private void OnAddTabViewItemButtonClick(object sender, RoutedEventArgs e)
{
DemoViewModel defaultViewModel = new DemoViewModel(Dispatcher);
// add Tabs as source for binding of TabView
defaultViewModel.TabItems.Add(new TabItemViewModel(Dispatcher) { Header = "Tab 1" });
defaultViewModel.TabItems.Add(new TabItemViewModel(Dispatcher) { Header = "Tab 2" });
defaultViewModel.TabItems.Add(new TabItemViewModel(Dispatcher) { Header = "Tab 3" });
defaultViewModel.TabItems.Add(new TabItemViewModel(Dispatcher) { Header = "Tab 4" });

defaultViewModel.RibbonTabs.Add(new TabItemViewModel(Dispatcher) { Header = "File" });
defaultViewModel.RibbonTabs.Add(new TabItemViewModel(Dispatcher) { Header = "View" });

this.DefaultViewModel = defaultViewModel;

this.InitializeComponent();
this.DefaultViewModel.TabItems.Add(new TabItemViewModel() { Header = $"Tab {this.DefaultViewModel.TabItems.Count + 1}" });
}

private void OnAddTabViewItemButtonClick(object sender, RoutedEventArgs e)
private void SelectItem(object sender, RoutedEventArgs e)
{
this.DefaultViewModel.TabItems.Add(new TabItemViewModel(Dispatcher) { Header = $"Tab {this.DefaultViewModel.TabItems.Count + 1}" });
tabView.SelectedItem = DefaultViewModel.TabItems[1];
}
}

[System.ComponentModel.Bindable(BindableSupport.Yes)]
public class DemoViewModel : ViewModelBase
public class DemoViewModel
{
public DemoViewModel(CoreDispatcher dispatcher) : base(dispatcher)
{
}

public ObservableCollection<TabItemViewModel> TabItems { get; } = new ObservableCollection<TabItemViewModel>();
public ObservableCollection<TabItemViewModel> RibbonTabs { get; } = new ObservableCollection<TabItemViewModel>();
}

[System.ComponentModel.Bindable(BindableSupport.Yes)]
public class TabItemViewModel : ViewModelBase
public class TabItemViewModel
{
private string header;

public TabItemViewModel(CoreDispatcher dispatcher) : base(dispatcher)
{
}

public string Header
{
get
{
return this.header;
}
set
{
if (this.header != value)
{
this.header = value;
RaisePropertyChanged(nameof(Header));
}
}
}
public string Header { get; set; }
}
}
8 changes: 4 additions & 4 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/TabView/TabView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -869,17 +869,17 @@ void OnTabContentPresenterLosingFocus(object sender, LosingFocusEventArgs args)

tabContentPresenter.LosingFocus += OnTabContentPresenterLosingFocus;

tabContentPresenter.Content = tvi.Content;
tabContentPresenter.ContentTemplate = tvi.ContentTemplate;
tabContentPresenter.ContentTemplateSelector = tvi.ContentTemplateSelector;

#if IS_UNO
// TODO: Uno specific - issue #4894 - in UWP the ContentPresenter does not become
// the parent of the Content. In Uno it does, so we need to make sure
// the inherited DataContext will match the TabViewItem.
tabContentPresenter.DataContext = tvi.DataContext;
#endif

tabContentPresenter.Content = tvi.Content;
tabContentPresenter.ContentTemplate = tvi.ContentTemplate;
tabContentPresenter.ContentTemplateSelector = tvi.ContentTemplateSelector;

// It is not ideal to call UpdateLayout here, but it is necessary to ensure that the ContentPresenter has expanded its content
// into the live visual tree.
tabContentPresenter.UpdateLayout();
Expand Down

0 comments on commit 8fa2a49

Please sign in to comment.