summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/TemplatedViewUnitTests.cs
blob: 64dc59be78c7dbf42672a8ecb0ec09af1047e7e5 (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
using System.Collections.Generic;
using NUnit.Framework;

namespace Xamarin.Forms.Core.UnitTests
{
    [TestFixture]
	public class TemplatedViewUnitTests : BaseTestFixture 
	{
        [Test]
        public void TemplatedView_should_have_the_InternalChildren_correctly_when_ControlTemplate_changed()
        {
            var sut = new TemplatedView();
            IList<Element> internalChildren = ((IControlTemplated)sut).InternalChildren;
            internalChildren.Add(new VisualElement());
            internalChildren.Add(new VisualElement());
            internalChildren.Add(new VisualElement());

            sut.ControlTemplate = new ControlTemplate(typeof(ExpectedView));

            Assert.AreEqual(1, internalChildren.Count);
            Assert.IsInstanceOf<ExpectedView>(internalChildren[0]);
        }

        private class ExpectedView : View
        {
            public ExpectedView()
            {
            }
        }

		public class MyTemplate : StackLayout
		{
			public MyTemplate()
			{
				Children.Add(new ContentPresenter());
			}
		}

		[Test]
		public void BindingsShouldBeAppliedOnTemplateChange()
		{
			var template0 = new ControlTemplate(typeof(MyTemplate));
			var template1 = new ControlTemplate(typeof(MyTemplate));
			var label = new Label();
			label.SetBinding(Label.TextProperty, ".");
			var cv = new ContentView {
				ControlTemplate = template0,
				Content = label
			};
			cv.BindingContext = "Foo";

			Assume.That(label.Text, Is.EqualTo("Foo"));
			cv.ControlTemplate = template1;
			Assert.That(label.Text, Is.EqualTo("Foo"));
		}
	}
}