-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathOnPlatformTests.cs
182 lines (160 loc) · 5.34 KB
/
OnPlatformTests.cs
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
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Devices;
using NUnit.Framework;
namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
[TestFixture]
public class OnPlatformTests : BaseTestFixture
{
MockDeviceInfo mockDeviceInfo;
[SetUp]
public override void Setup()
{
base.Setup();
DeviceInfo.SetCurrent(mockDeviceInfo = new MockDeviceInfo());
}
[Test]
public void ApplyToProperty()
{
mockDeviceInfo.Platform = DevicePlatform.iOS;
var xaml = @"
<ContentPage
xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
xmlns:scg=""clr-namespace:System.Collections.Generic;assembly=mscorlib"">
<OnPlatform x:TypeArguments=""View"">
<On Platform=""iOS""><Button Text=""iOS""/></On>
<On Platform=""Android""><Button Text=""Android""/></On>
<On Platform=""UWP""><Button Text=""UWP""/></On>
</OnPlatform>
</ContentPage>";
var layout = new ContentPage().LoadFromXaml(xaml);
Assert.NotNull(layout.Content);
}
[Test]
public void UseTypeConverters()
{
var xaml = @"
<ContentPage xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
Title=""Grid Demo Page"">
<ContentPage.Padding>
<OnPlatform x:TypeArguments=""Thickness"">
<On Platform=""iOS"">
0, 20, 0, 0
</On>
<On Platform=""Android"">
0, 0, 10, 0
</On>
<On Platform=""UWP"">
0, 20, 0, 20
</On>
</OnPlatform>
</ContentPage.Padding>
</ContentPage>";
ContentPage layout;
mockDeviceInfo.Platform = DevicePlatform.iOS;
layout = new ContentPage().LoadFromXaml(xaml);
Assert.AreEqual(new Thickness(0, 20, 0, 0), layout.Padding);
mockDeviceInfo.Platform = DevicePlatform.Android;
layout = new ContentPage().LoadFromXaml(xaml);
Assert.AreEqual(new Thickness(0, 0, 10, 0), layout.Padding);
mockDeviceInfo.Platform = DevicePlatform.UWP;
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://schemas.microsoft.com/dotnet/2021/maui""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"">
<Image.Source>
<OnPlatform x:TypeArguments=""ImageSource"">
<On Platform=""iOS"">icon_twitter.png</On>
<On Platform=""Android"">icon_twitter.png</On>
<On Platform=""UWP"">Images/icon_twitter.png</On>
</OnPlatform>
</Image.Source>
</Image>";
Image image;
mockDeviceInfo.Platform = DevicePlatform.iOS;
image = new Image().LoadFromXaml(xaml);
Assert.AreEqual("icon_twitter.png", (image.Source as FileImageSource).File);
}
[Test]
public void UWPisWinUI()
{
var xaml = @"
<Image xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"">
<Image.Source>
<OnPlatform x:TypeArguments=""ImageSource"">
<On Platform=""iOS"">icon_twitter.png</On>
<On Platform=""Android"">icon_twitter.png</On>
<On Platform=""UWP"">Images/icon_twitter.png</On>
</OnPlatform>
</Image.Source>
</Image>";
Image image;
mockDeviceInfo.Platform = DevicePlatform.WinUI;
image = new Image().LoadFromXaml(xaml);
Assert.AreEqual("Images/icon_twitter.png", (image.Source as FileImageSource).File);
}
[Test]
public void ChecksPreferWinUI()
{
var xaml = @"
<Image xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"">
<Image.Source>
<OnPlatform x:TypeArguments=""ImageSource"">
<On Platform=""iOS"">icon_twitter.png</On>
<On Platform=""Android"">icon_twitter.png</On>
<On Platform=""UWP"">Images/icon_twitter.png</On>
<On Platform=""WinUI"">Images/icon_twitter_preferred.png</On>
</OnPlatform>
</Image.Source>
</Image>";
Image image;
mockDeviceInfo.Platform = DevicePlatform.WinUI;
image = new Image().LoadFromXaml(xaml);
Assert.AreEqual("Images/icon_twitter_preferred.png", (image.Source as FileImageSource).File);
}
}
[TestFixture]
public class OnIdiomTests : BaseTestFixture
{
MockDeviceInfo mockDeviceInfo;
[SetUp]
public override void Setup()
{
base.Setup();
DeviceInfo.SetCurrent(mockDeviceInfo = new MockDeviceInfo());
}
[Test]
public void StackLayoutOrientation()
{
var xaml = @"
<StackLayout
xmlns=""http://schemas.microsoft.com/dotnet/2021/maui""
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>";
mockDeviceInfo.Idiom = DeviceIdiom.Phone;
var layout = new StackLayout().LoadFromXaml(xaml);
Assert.AreEqual(StackOrientation.Vertical, layout.Orientation);
mockDeviceInfo.Idiom = DeviceIdiom.Tablet;
layout = new StackLayout().LoadFromXaml(xaml);
Assert.AreEqual(StackOrientation.Horizontal, layout.Orientation);
}
}
}