summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/ControlTemplateTests.cs
blob: f0d22d1cdce6186a74c790e54de6957555e76ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
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 child1 = ((IElementController)testView).LogicalChildren[0];
			var child2 = ((IElementController)child1).LogicalChildren[0];

			var originalPresenter = (ContentPresenter)child2;

			Assert.AreEqual (label, originalPresenter.Content);

			testView.ControlTemplate = new ControlTemplate (typeof (PresenterWrapper));

			Assert.IsNull (originalPresenter.Content);
		}

		[Test]
		public void NestedTemplateBindings ()
		{
			var testView = new TestView ();

			var child1 = ((IElementController)testView).LogicalChildren[0];
			var child2 = ((IElementController)child1).LogicalChildren[0];

			var label = (Label)child2;

			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 child1 = ((IElementController)childView).LogicalChildren[0];
			var child2 = ((IElementController)child1).LogicalChildren[1];

			var childPresenter = (ContentPresenter)child2;

			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)((IElementController)page).LogicalChildren[0]).Content as Entry;
			((IElementController)entry).SetValueFromRenderer (Entry.TextProperty, "Bar");
			viewModel.Name = "Raz";

			Assert.AreEqual ("Raz", entry.Text);
		}
	}
}