summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/OnPlatform.xaml.cs
blob: 23ebe923a23d8797431adf4464cd6dc541e8c935 (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
using System;
using System.Collections.Generic;

using Xamarin.Forms;

using NUnit.Framework;
using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{
	public partial class OnPlatform : ContentPage
	{
		public OnPlatform ()
		{
			InitializeComponent ();
		}

		public OnPlatform (bool useCompiledXaml)
		{
			//this stub will be replaced at compile time
		}

		[TestFixture]
		public class Tests
		{
			[SetUp]
			public void Setup()
			{
				Device.PlatformServices = new MockPlatformServices();
			}

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

			[TestCase (false)]
			[TestCase (true)]
			public void BoolToVisibility (bool useCompiledXaml)
			{
				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.iOS;
				var layout = new OnPlatform (useCompiledXaml);
				Assert.AreEqual (true, layout.label0.IsVisible);

				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.Android;
				layout = new OnPlatform (useCompiledXaml);
				Assert.AreEqual (false, layout.label0.IsVisible);
			}

			[TestCase(false)]
			[TestCase(true)]
			public void DoubleToWidth(bool useCompiledXaml)
			{
				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.iOS;
				var layout = new OnPlatform(useCompiledXaml);
				Assert.AreEqual(20, layout.label0.WidthRequest);

				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.Android;
				layout = new OnPlatform(useCompiledXaml);
				Assert.AreEqual(30, layout.label0.WidthRequest);
			}

			[TestCase(false)]
			[TestCase(true)]
			public void StringToText(bool useCompiledXaml)
			{
				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.iOS;
				var layout = new OnPlatform(useCompiledXaml);
				Assert.AreEqual("Foo", layout.label0.Text);

				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.Android;
				layout = new OnPlatform(useCompiledXaml);
				Assert.AreEqual("Bar", layout.label0.Text);
			}

			[TestCase(false)]
			[TestCase(true)]
			public void OnPlatformAsResource(bool useCompiledXaml)
			{
				var layout = new OnPlatform(useCompiledXaml);
				var onplat = layout.Resources ["fontAttributes"] as OnPlatform<FontAttributes>;
				Assert.NotNull(onplat);
#pragma warning disable 612
				Assert.AreEqual(FontAttributes.Bold, onplat.iOS);
				Assert.AreEqual(FontAttributes.Italic, onplat.Android);
#pragma warning restore 612

			}

			[TestCase(false)]
			[TestCase(true)]
			public void OnPlatformAsResourceAreApplied(bool useCompiledXaml)
			{
				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.iOS;
				var layout = new OnPlatform(useCompiledXaml);
				var onidiom = layout.Resources ["fontSize"] as OnIdiom<double>;
				Assert.NotNull(onidiom);
				Assert.That(onidiom.Phone, Is.TypeOf<double>());
				Assert.AreEqual(20, onidiom.Phone);
				Assert.AreEqual(FontAttributes.Bold, layout.label0.FontAttributes);

				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.Android;
				layout = new OnPlatform(useCompiledXaml);
				Assert.AreEqual(FontAttributes.Italic, layout.label0.FontAttributes);
			}

			[TestCase(false)]
			[TestCase(true)]
			public void OnPlatform2Syntax(bool useCompiledXaml)
			{
				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.Android;
				var layout = new OnPlatform(useCompiledXaml);
				Assert.AreEqual(42, layout.label0.HeightRequest);

				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.iOS;
				layout = new OnPlatform(useCompiledXaml);
				Assert.AreEqual(21, layout.label0.HeightRequest);


				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = "FooBar";
				layout = new OnPlatform(useCompiledXaml);
				Assert.AreEqual(42, layout.label0.HeightRequest);
			}

			[TestCase(false)]
			[TestCase(true)]
			public void OnPlatformDefault(bool useCompiledXaml)
			{
				((MockPlatformServices)Device.PlatformServices).RuntimePlatform = "\ud83d\ude80";
				var layout = new OnPlatform(useCompiledXaml);
				Assert.AreEqual(63, layout.label0.HeightRequest);
			}
		}
	}
}