summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/ControlTemplateTests.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Core.UnitTests/ControlTemplateTests.cs')
-rw-r--r--Xamarin.Forms.Core.UnitTests/ControlTemplateTests.cs181
1 files changed, 181 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core.UnitTests/ControlTemplateTests.cs b/Xamarin.Forms.Core.UnitTests/ControlTemplateTests.cs
new file mode 100644
index 00000000..ffced2d2
--- /dev/null
+++ b/Xamarin.Forms.Core.UnitTests/ControlTemplateTests.cs
@@ -0,0 +1,181 @@
+using System;
+using System.ComponentModel;
+using System.Runtime.CompilerServices;
+using NUnit.Framework;
+using Xamarin.Forms;
+
+namespace Xamarin.Forms.Core.UnitTests
+{
+ [TestFixture]
+ public class ControlTemplateTests : BaseTestFixture
+ {
+ [SetUp]
+ public override void Setup()
+ {
+ base.Setup ();
+ Device.PlatformServices = new MockPlatformServices ();
+ }
+
+ [TearDown]
+ public override void TearDown()
+ {
+ base.TearDown ();
+ Device.PlatformServices = null;
+ }
+
+ public class ContentControl : StackLayout
+ {
+ public ContentControl ()
+ {
+ var label = new Label ();
+ label.SetBinding (Label.TextProperty, new TemplateBinding ("Name"));
+
+ Children.Add (label);
+ Children.Add (new ContentPresenter ());
+ }
+ }
+
+ public class PresenterWrapper : ContentView
+ {
+ public PresenterWrapper ()
+ {
+ Content = new ContentPresenter ();
+ }
+ }
+
+ public class TestView : ContentView
+ {
+ public static readonly BindableProperty NameProperty =
+ BindableProperty.Create (nameof (Name), typeof (string), typeof (TestView), default(string));
+
+ public string Name
+ {
+ get { return (string)GetValue (NameProperty); }
+ set { SetValue (NameProperty, value); }
+ }
+
+ public TestView ()
+ {
+ ControlTemplate = new ControlTemplate(typeof (ContentControl));
+ }
+ }
+
+ [Test]
+ public void ResettingControlTemplateNullsPresenterContent ()
+ {
+ var testView = new TestView {
+ Platform = new UnitPlatform (),
+ ControlTemplate = new ControlTemplate (typeof (PresenterWrapper))
+ };
+
+ var label = new Label ();
+ testView.Content = label;
+ var originalPresenter = (ContentPresenter)testView.LogicalChildren[0].LogicalChildren[0];
+
+ Assert.AreEqual (label, originalPresenter.Content);
+
+ testView.ControlTemplate = new ControlTemplate (typeof (PresenterWrapper));
+
+ Assert.IsNull (originalPresenter.Content);
+ }
+
+ [Test]
+ public void NestedTemplateBindings ()
+ {
+ var testView = new TestView ();
+ var label = (Label)testView.LogicalChildren[0].LogicalChildren[0];
+
+ testView.Platform = new UnitPlatform ();
+ Assert.IsNull (label.Text);
+
+ testView.Name = "Bar";
+ Assert.AreEqual ("Bar", label.Text);
+ }
+
+ [Test]
+ public void ParentControlTemplateDoesNotClearChildTemplate ()
+ {
+ var parentView = new TestView ();
+ var childView = new TestView ();
+ parentView.Platform = new UnitPlatform ();
+
+ parentView.Content = childView;
+ childView.Content = new Button ();
+ var childPresenter = (ContentPresenter)childView.LogicalChildren[0].LogicalChildren[1];
+
+ parentView.ControlTemplate = new ControlTemplate (typeof (ContentControl));
+ Assert.IsNotNull (childPresenter.Content);
+ }
+
+ [Test]
+ public void NullConstructor ()
+ {
+ Assert.Throws<ArgumentNullException> (() => new ControlTemplate (null));
+ }
+
+ class TestPage : ContentPage
+ {
+ public static readonly BindableProperty NameProperty =
+ BindableProperty.Create (nameof (Name), typeof (string), typeof (TestPage), null);
+
+ public string Name
+ {
+ get { return (string)GetValue (NameProperty); }
+ set { SetValue (NameProperty, value); }
+ }
+ }
+
+ class TestContent : ContentView
+ {
+ public TestContent ()
+ {
+ Content = new Entry ();
+ Content.SetBinding (Entry.TextProperty, new TemplateBinding ("Name", BindingMode.TwoWay));
+ }
+ }
+
+ class ViewModel : INotifyPropertyChanged
+ {
+ string name;
+
+ public string Name
+ {
+ get { return name; }
+ set
+ {
+ if (name == value)
+ return;
+ name = value;
+ OnPropertyChanged ();
+ }
+ }
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ protected virtual void OnPropertyChanged ([CallerMemberName] string propertyName = null)
+ {
+ PropertyChanged?.Invoke (this, new PropertyChangedEventArgs (propertyName));
+ }
+ }
+
+ [Test]
+ public void DoubleTwoWayBindingWorks ()
+ {
+ var page = new TestPage ();
+ page.Platform = new UnitPlatform ();
+ var viewModel = new ViewModel {
+ Name = "Jason"
+ };
+ page.BindingContext = viewModel;
+
+ page.ControlTemplate = new ControlTemplate (typeof (TestContent));
+ page.SetBinding (TestPage.NameProperty, "Name");
+
+ var entry = ((ContentView)page.LogicalChildren[0]).Content as Entry;
+ ((IElementController)entry).SetValueFromRenderer (Entry.TextProperty, "Bar");
+ viewModel.Name = "Raz";
+
+ Assert.AreEqual ("Raz", entry.Text);
+ }
+ }
+} \ No newline at end of file