summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/Issues/TestCases.cs
blob: 04d7ff0d06f1d507ae334f620222e58be104cd12 (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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.Linq;
using NUnit.Framework;
using System.Collections.Generic;
using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{
	public class BasePage : ContentPage
	{

	}

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

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

		public static readonly BindableProperty InnerViewProperty = 
#pragma warning disable 618
			BindableProperty.CreateAttached<TestCases, View> (bindable => GetInnerView (bindable), default(View));
#pragma warning restore 618

		public static View GetInnerView (BindableObject bindable)
		{
			return (View)bindable.GetValue (InnerViewProperty);
		}

		public static void SetInnerView (BindableObject bindable, View value)
		{
			bindable.SetValue (InnerViewProperty, value);
		}

		[Test]
		public void TestCase001 ()
		{
			var xaml = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>
			<ContentPage
			xmlns=""http://xamarin.com/schemas/2014/forms""
			xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
			xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests""
			Title=""Home"">
				<local:TestCases.InnerView>
					<Label x:Name=""innerView""/>
				</local:TestCases.InnerView>
				<ContentPage.Content>
					<Grid RowSpacing=""9"" ColumnSpacing=""6"" Padding=""6,9"" VerticalOptions=""Fill"" HorizontalOptions=""Fill"" BackgroundColor=""Red"">
						<Grid.Children>
							<Label x:Name=""label0""/>
							<Label x:Name=""label1""/>
							<Label x:Name=""label2""/>
							<Label x:Name=""label3""/>
						</Grid.Children>
					</Grid>
				</ContentPage.Content>
			</ContentPage>";
			var contentPage = new ContentPage ().LoadFromXaml (xaml);
			var label0 = contentPage.FindByName<Label> ("label0");
			var label1 = contentPage.FindByName<Label> ("label1");

			Assert.NotNull (GetInnerView (contentPage));
//			Assert.AreEqual ("innerView", GetInnerView (contentPage).Name);
			Assert.AreEqual (GetInnerView (contentPage), ((Forms.Internals.INameScope)contentPage).FindByName ("innerView"));
			Assert.NotNull (label0);
			Assert.NotNull (label1);
			Assert.AreEqual (4, contentPage.Content.Descendants ().Count ());
		}


		[Test]
		public void TestCase002 ()
		{
			var xaml = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>
            <local:BasePage
                xmlns=""http://xamarin.com/schemas/2014/forms""
                xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
			    xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests""
                x:Class=""Tramchester.App.Views.HomeView"">
                <local:BasePage.Content>
                  <Label Text=""Hi There!"" />
                </local:BasePage.Content>
           </local:BasePage>";
			var contentPage = new ContentPage ().LoadFromXaml (xaml);
			Assert.That (contentPage.Content, Is.InstanceOf<Label> ());
		}

		[Test]
		public void TestCase003 ()
		{
			var xaml = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>
				<ContentPage
					xmlns=""http://xamarin.com/schemas/2014/forms""
					xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
					Title=""People"">

					<StackLayout Spacing=""0"">
						<SearchBar x:Name=""searchBar""/>
						<ListView ItemsSource=""{Binding Path=.}"" RowHeight=""42"" x:Name=""listview"">
							<ListView.ItemTemplate>
								<DataTemplate>
									<ViewCell>
										<ViewCell.View>
											<StackLayout Orientation=""Horizontal"" HorizontalOptions=""FillAndExpand"" VerticalOptions=""CenterAndExpand"" BackgroundColor=""#fff4f4f4"">
												<BoxView WidthRequest=""10""/>
												<Grid WidthRequest=""42"" HeightRequest=""32"" VerticalOptions=""CenterAndExpand"" HorizontalOptions=""Start"">
													<Image WidthRequest=""32"" HeightRequest=""32"" Aspect=""AspectFill"" HorizontalOptions=""FillAndExpand"" Source=""Images/icone_nopic_members_42.png""/>
													<!--<Image WidthRequest=""32"" HeightRequest=""32"" Aspect=""AspectFill"" HorizontalOptions=""FillAndExpand"">
														<Image.Source>
															<UriImageSource Uri=""{Binding Picture}"" CacheValidity=""30""/>
														</Image.Source>
													</Image>-->
													<Image Source=""Images/cropcircle.png"" HorizontalOptions=""FillAndExpand"" VerticalOptions=""FillAndExpand"" WidthRequest=""32"" HeightRequest=""32"" Aspect=""Fill""/>
												</Grid>
												<Label Text=""{Binding FirstName}"" VerticalOptions=""CenterAndExpand""/>
												<Label Text=""{Binding LastName}"" Font=""HelveticaNeue-Bold, Medium"" VerticalOptions=""CenterAndExpand"" />
											</StackLayout>
										</ViewCell.View>
									</ViewCell>
								</DataTemplate>
							</ListView.ItemTemplate>
						</ListView>
					</StackLayout>
				</ContentPage>";
			var page = new ContentPage ().LoadFromXaml (xaml);
			var model = new List<object> { 
				new {FirstName = "John", LastName="Lennon", Picture="http://www.johnlennon.com/wp-content/themes/jl/images/home-gallery/2.jpg"},
				new {FirstName = "Paul", LastName="McCartney", Picture="http://t0.gstatic.com/images?q=tbn:ANd9GcRjNUGJ00Mt85n2XDu8CZM0w1em0Wv4ZaemLuIVmLCMwPMOLUO1SQ"},
				new {FirstName = "George", LastName="Harisson", Picture="http://cdn.riffraf.net/wp-content/uploads/2013/02/george-harrison-living.jpg"},
				new {FirstName = "Ringo", LastName="Starr", Picture="http://www.biography.com/imported/images/Biography/Images/Profiles/S/Ringo-Starr-306872-1-402.jpg"},
			};
			page.BindingContext = model;

			var listview = page.FindByName<ListView> ("listview");
			Cell cell = null;
			Assert.DoesNotThrow(() => { cell = listview.TemplatedItems[0]; });
			Assert.NotNull (cell);
			Assert.That (cell, Is.TypeOf<ViewCell> ());
			Assert.AreSame (model [0], cell.BindingContext);
		}

		[Test]
		public void TestCase004 ()
		{
			var xaml = @"<?xml version=""1.0"" encoding=""UTF-8"" ?>
				<ContentPage
					xmlns=""http://xamarin.com/schemas/2014/forms""
					xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
					<ContentPage.Content>
					    <ScrollView Orientation=""Horizontal"">
					        <ScrollView.Content>
					            <Grid>
					                <Grid.ColumnDefinitions>
					                    <ColumnDefinition />
					                    <ColumnDefinition />
					                </Grid.ColumnDefinitions>
					                <Image Grid.Column=""0"" Grid.Row=""0"" Aspect=""AspectFill"">
					                    <Image.Source>
					                        <StreamImageSource Stream=""{Binding HeroPicture.Stream}"" />
					                    </Image.Source>
					                </Image>
					            </Grid>
					        </ScrollView.Content>
					    </ScrollView>
					</ContentPage.Content>
				</ContentPage>";

			var page = new ContentPage ();
			Assert.DoesNotThrow (()=> page.LoadFromXaml (xaml));
		}

		[Test]
		public void Issue1415 ()
		{
			var xaml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
						<ContentPage 
							xmlns=""http://xamarin.com/schemas/2014/forms"" 
							xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
							xmlns:local=""clr-namespace:Xamarin.Forms.Xaml.UnitTests;assembly=Xamarin.Forms.Xaml.UnitTests"">
							<Label x:Name=""label"" Text=""{Binding Converter={x:Static local:ReverseConverter.Instance}, Mode=TwoWay}""/>
						</ContentPage>";
			var page = new ContentPage ().LoadFromXaml (xaml);
			var label = page.FindByName<Label> ("label");
			Assert.NotNull (label);
			label.BindingContext = "foo";
			Assert.AreEqual ("oof", label.Text);
		}

		[Test]
		//only happens in european cultures
		[SetCulture ("fr-FR")]
		public void Issue1493 ()
		{
			var xaml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
						<View 
							xmlns=""http://xamarin.com/schemas/2014/forms"" 
							xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml""
							RelativeLayout.HeightConstraint=""{ConstraintExpression Type=RelativeToParent, Property=Height, Factor=0.25}""
							RelativeLayout.WidthConstraint=""{ConstraintExpression Type=RelativeToParent, Property=Width, Factor=0.6}""/>";
			View view = new View ();
			view.LoadFromXaml (xaml);
			Assert.DoesNotThrow (() => view.LoadFromXaml (xaml));
		}
	}
}