summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/OnPlatformTests.cs
blob: 04084dea7849233e219b9997be984f2b270cd3e0 (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
using NUnit.Framework;
using System;

using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{
	[TestFixture]
	public class OnPlatformTests : BaseTestFixture
	{
		[SetUp]
		public override void Setup()
		{
			base.Setup();
			Device.PlatformServices = new MockPlatformServices();
		}

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

		[Test]
		public void ApplyToProperty ()
		{
			var xaml = @"
			<ContentPage 
			xmlns=""http://xamarin.com/schemas/2014/forms""
			xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
			xmlns:scg=""clr-namespace:System.Collections.Generic;assembly=mscorlib"">
				<OnPlatform x:TypeArguments=""View"">
					<OnPlatform.iOS><Button Text=""iOS""/></OnPlatform.iOS>
					<OnPlatform.Android><Button Text=""Android""/></OnPlatform.Android>
					<OnPlatform.WinPhone><Button Text=""WinPhone""/></OnPlatform.WinPhone>
				</OnPlatform>
			</ContentPage>";
			var layout = new ContentPage ().LoadFromXaml (xaml);
			Assert.NotNull (layout.Content);
		}

		[Test]
		public void UseTypeConverters ()
		{
			var xaml = @"
			<ContentPage xmlns=""http://xamarin.com/schemas/2014/forms""
             xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
             Title=""Grid Demo Page"">
			  <ContentPage.Padding>
			    <OnPlatform x:TypeArguments=""Thickness"">
			      <OnPlatform.iOS>
			        0, 20, 0, 0
			      </OnPlatform.iOS>
			      <OnPlatform.Android>
			        0, 0, 10, 0
			      </OnPlatform.Android>
			      <OnPlatform.WinPhone>
			        0, 20, 0, 20
			      </OnPlatform.WinPhone>
			    </OnPlatform>
			  </ContentPage.Padding>  
			</ContentPage>";

			ContentPage layout;

			((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.iOS;
			layout = new ContentPage ().LoadFromXaml (xaml);
			Assert.AreEqual (new Thickness (0, 20, 0, 0), layout.Padding);

			((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.Android;
			layout = new ContentPage ().LoadFromXaml (xaml);
			Assert.AreEqual (new Thickness (0, 0, 10, 0), layout.Padding);

			((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.WinPhone;
			layout = new ContentPage ().LoadFromXaml (xaml);
			Assert.AreEqual (new Thickness (0, 20, 0, 20), layout.Padding);
		}

		[Test]
		//Issue 1480
		public void TypeConverterAndDerivedTypes ()
		{
			var xaml = @"
			<Image xmlns=""http://xamarin.com/schemas/2014/forms""
             xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"">
				<Image.Source>
	                <OnPlatform x:TypeArguments=""ImageSource"">
	                    <OnPlatform.iOS>icon_twitter.png</OnPlatform.iOS>
	                    <OnPlatform.Android>icon_twitter.png</OnPlatform.Android>
	                    <OnPlatform.WinPhone>Images/icon_twitter.png</OnPlatform.WinPhone>
	                </OnPlatform>
	            </Image.Source>
			</Image>";

			Image image;

			((MockPlatformServices)Device.PlatformServices).RuntimePlatform = Device.iOS;
			image = new Image ().LoadFromXaml (xaml);
			Assert.AreEqual ("icon_twitter.png", (image.Source as FileImageSource).File);
		}
	}

	[TestFixture]
	public class OnIdiomTests : BaseTestFixture
	{
		[Test]
		public void StackLayoutOrientation ()
		{
			var xaml = @"
			<StackLayout 
			xmlns=""http://xamarin.com/schemas/2014/forms""
			xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"">
				<StackLayout.Orientation>
				<OnIdiom x:TypeArguments=""StackOrientation"">
					<OnIdiom.Phone>Vertical</OnIdiom.Phone>
					<OnIdiom.Tablet>Horizontal</OnIdiom.Tablet>
				</OnIdiom>
				</StackLayout.Orientation>
				<Label Text=""child0""/>
				<Label Text=""child1""/>			
			</StackLayout>";
			Device.Idiom = TargetIdiom.Phone;
			var layout = new StackLayout ().LoadFromXaml (xaml);
			Assert.AreEqual (StackOrientation.Vertical, layout.Orientation);

			Device.Idiom = TargetIdiom.Tablet;
			layout = new StackLayout ().LoadFromXaml (xaml);
			Assert.AreEqual (StackOrientation.Horizontal, layout.Orientation);
		}
	}
}