summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.UnitTests/RegistrarUnitTests.cs
blob: c477f022468cb83478e65e21a9890009359c4b7f (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
using System;
using System.Reflection;
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.Core.UnitTests;

[assembly: TestHandler (typeof (Button), typeof (ButtonTarget))]
[assembly: TestHandler (typeof (Slider), typeof (SliderTarget))]

namespace Xamarin.Forms.Core.UnitTests
{
	internal class TestHandlerAttribute : HandlerAttribute
	{
		public TestHandlerAttribute (Type handler, Type target) : base (handler, target)
		{
			
		}
	}

	internal class ButtonTarget : IRegisterable {}

	internal class SliderTarget : IRegisterable {}

	[TestFixture]
	public class RegistrarTests : BaseTestFixture
	{
		[SetUp]
		public override void Setup ()
		{
			base.Setup ();
			Device.PlatformServices = new MockPlatformServices ();
			Internals.Registrar.RegisterAll (new [] {
				typeof (TestHandlerAttribute)
			});

		}

		[TearDown]
		public override void TearDown()
		{
			base.TearDown ();
			Device.PlatformServices = null;
		}

		[Test]
		public void GetButtonHandler ()
		{
			var buttonTarget = Internals.Registrar.Registered.GetHandler<ButtonTarget> (typeof (Button));
			Assert.IsNotNull (buttonTarget);
			Assert.That (buttonTarget, Is.InstanceOf<ButtonTarget>());
		}

		[Test]
		public void GetSliderHandler()
		{
			var sliderTarget = Internals.Registrar.Registered.GetHandler<SliderTarget> (typeof (Slider));
			Assert.IsNotNull (sliderTarget);
			Assert.That (sliderTarget, Is.InstanceOf<SliderTarget> ());
		}
	}

	[TestFixture]
	public class SimpleRegistrarUnitTests
	{
		class MockRenderer {}
		class ButtonMockRenderer : MockRenderer {}
		class ShinyButtonMockRenderer : MockRenderer {}
		class CrashMockRenderer : MockRenderer
		{
			public CrashMockRenderer ()
			{
				throw new NotImplementedException();
			}
		}

		[Test]
		public void TestConstructor ()
		{
			var registrar = new Internals.Registrar<MockRenderer> ();

			var renderer = registrar.GetHandler (typeof (Button));

			Assert.Null (renderer);
		}

		[Test]
		public void TestGetRendererForKnownClass ()
		{
			var registrar = new Internals.Registrar<MockRenderer> ();

			registrar.Register (typeof(View), typeof(MockRenderer));

			var renderer = registrar.GetHandler (typeof (View));

			Assert.That (renderer, Is.InstanceOf<MockRenderer>());
		}

		[Test]
		public void TestGetRendererForUnknownSubclass ()
		{
			var registrar = new Internals.Registrar<MockRenderer> ();

			registrar.Register (typeof (View), typeof (MockRenderer));

			var renderer = registrar.GetHandler (typeof (Button));

			Assert.That (renderer, Is.InstanceOf<MockRenderer>());
		}

		[Test]
		public void TestGetRendererWithRegisteredSubclass ()
		{
			var registrar = new Internals.Registrar<MockRenderer> ();

			registrar.Register (typeof (View), typeof (MockRenderer));
			registrar.Register (typeof (Button), typeof (ButtonMockRenderer));

			var buttonRenderer = registrar.GetHandler (typeof (Button));
			var viewRenderer = registrar.GetHandler (typeof (View));

			Assert.That (buttonRenderer, Is.InstanceOf<ButtonMockRenderer>());
			Assert.That (viewRenderer, Is.Not.InstanceOf<ButtonMockRenderer>());
			Assert.That (viewRenderer, Is.InstanceOf<MockRenderer>());
		}

		[Test]
		public void TestReplaceRenderer ()
		{
			var registrar = new Internals.Registrar<MockRenderer> ();

			registrar.Register (typeof (View), typeof (MockRenderer));
			registrar.Register (typeof (Button), typeof (ButtonMockRenderer));
			registrar.Register (typeof (Button), typeof (ShinyButtonMockRenderer));

			var buttonRenderer = registrar.GetHandler (typeof (Button));

			Assert.That (buttonRenderer, Is.InstanceOf<ShinyButtonMockRenderer>());
		}

		[Test]
		public void GetHandlerType()
		{
			var registrar = new Internals.Registrar<MockRenderer>();
			registrar.Register (typeof (View), typeof (MockRenderer));

			Assert.AreEqual (typeof (MockRenderer), registrar.GetHandlerType (typeof (View)));
		}
	}
}