summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/LoaderTests.cs
blob: 4d3e004a267b62e928e5bd1fe88a53fd8b67b7c8 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
using NUnit.Framework;
using System.Linq;
using System;
using System.Collections.Generic;

using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{
	[ContentProperty ("Content")]
	public class CustomView : View
	{
		public string NotBindable { get; set; }

		public View Content { get; set; }

		public MockFlags MockFlags { get; set; }
	}

	[ContentProperty ("Children")]
	public class ViewWithChildrenContent : View
	{
		public ViewWithChildrenContent ()
		{
			Children = DefaultChildren = new ViewList ();
		}
		public ViewList DefaultChildren;
		public ViewList Children { get; set; }
	}

	public class ViewList : List<View>
	{

	}

	public class ReverseConverter : IValueConverter
	{
		public static ReverseConverter Instance = new ReverseConverter ();

		public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			var s = value as string;
			if (s == null)
				return value;
			return new string (s.Reverse ().ToArray ());
		}

		public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			var s = value as string;
			if (s == null)
				return value;
			return new string (s.Reverse ().ToArray ());
		}
	}

	public class Catalog
	{
		public static readonly BindableProperty MessageProperty = 
#pragma warning disable 618
			BindableProperty.CreateAttached<Catalog, string> (bindable => GetMessage (bindable), default(string), 
#pragma warning restore 618
				propertyChanged: (bindable, oldvalue, newvalue) => {
					var label = bindable as Label;
					if (label != null)
						label.SetValue (Label.TextProperty, new string (newvalue.Reverse ().ToArray ()));
				});

		public static string GetMessage (BindableObject bindable)
		{
			return (string)bindable.GetValue (MessageProperty);
		}

		public static void SetMessage (BindableObject bindable, string value)
		{
			bindable.SetValue (MessageProperty, value);
		}
	}
	
	[Flags]
	public enum MockFlags
	{
		Foo = 1<<0,
		Bar = 1<<1,
		Baz = 1<<2,
		Qux = 1<<3,
	}


	[TestFixture]
	public class LoaderTests : BaseTestFixture
	{
		[Test]
		public void TestRootName ()
		{
			var xaml = @"
				<View
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				x:Class=""Xamarin.Forms.Xaml.UnitTests.CustomView"" 
				x:Name=""customView"" 
				/>";

			var view = new CustomView ();
			view.LoadFromXaml (xaml);

			Assert.AreSame (view, ((Forms.Internals.INameScope)view).FindByName("customView"));
		}

		[Test]
		public void TestFindByXName ()
		{
			var xaml = @"
				<StackLayout 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
					<StackLayout.Children>
						<Label x:Name=""label0"" Text=""Foo""/>
					</StackLayout.Children>
				</StackLayout>";

			var stacklayout = new StackLayout ();
			stacklayout.LoadFromXaml (xaml);

			var label = stacklayout.FindByName<Label> ("label0");
			Assert.NotNull (label);
			Assert.AreEqual ("Foo", label.Text);		
		}

		[Test]
		public void TestUnknownPropertyShouldThrow ()
		{
			var xaml = @"
				<Label 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				Text=""Foo""
				UnknownProperty=""Bar""
			    />";

			var label = new Label ();
			Assert.Throws (new XamlParseExceptionConstraint (5, 5), () => label.LoadFromXaml (xaml));
		}

		[Test]
		public void TestSetValueToBindableProperty ()
		{
			var xaml = @"
			<Label 
			xmlns=""http://xamarin.com/schemas/2014/forms""
			Text=""Foo""
			/>";

			var label = new Label ();

			label.LoadFromXaml (xaml);
			Assert.AreEqual ("Foo", label.Text);

		}

		[Test]
		public void TestSetBindingToBindableProperty ()
		{
			var xaml = @"
			<Label 
			xmlns=""http://xamarin.com/schemas/2014/forms""
			Text=""{Binding Path=labeltext}""
			/>";

			var label = new Label ();
			label.LoadFromXaml (xaml);

			Assert.AreEqual (Label.TextProperty.DefaultValue, label.Text);

			label.BindingContext = new {labeltext="Foo"};
			Assert.AreEqual ("Foo", label.Text);
		}

		[Test]
		public void TestBindingAsElement ()
		{
			var xaml = @"
			<Label 
			xmlns=""http://xamarin.com/schemas/2014/forms"">
				<Label.Text>
					<Binding Path=""labeltext""/>
				</Label.Text>
			</Label>";

			var label = new Label ();
			label.LoadFromXaml (xaml);

			Assert.AreEqual (Label.TextProperty.DefaultValue, label.Text);

			label.BindingContext = new {labeltext="Foo"};
			Assert.AreEqual ("Foo", label.Text);

		}

		[Test]
		public void TestSetBindingToNonBindablePropertyShouldThrow ()
		{
			var xaml = @"
				<View 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				x:Class=""Xamarin.Forms.Xaml.UnitTests.CustomView"" 
				Name=""customView"" 
				NotBindable=""{Binding text}""
				/>";

			var view = new CustomView ();
			Assert.Throws (new XamlParseExceptionConstraint (6, 5), () => view.LoadFromXaml (xaml));
		}

		[Test]
		public void TestBindingPath ()
		{
			var xaml = @"
				<StackLayout 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
					<StackLayout.Children>
						<Label x:Name=""label0"" Text=""{Binding text}""/>
						<Label x:Name=""label1"" Text=""{Binding Path=text}""/>
					</StackLayout.Children>
				</StackLayout>";

			var stacklayout = new StackLayout ();
			stacklayout.LoadFromXaml (xaml);

			var label0 = stacklayout.FindByName<Label> ("label0");
			var label1 = stacklayout.FindByName<Label> ("label1");

			Assert.AreEqual (Label.TextProperty.DefaultValue, label0.Text);
			Assert.AreEqual (Label.TextProperty.DefaultValue, label1.Text);

			stacklayout.BindingContext = new {text = "Foo"};
			Assert.AreEqual ("Foo", label0.Text);
			Assert.AreEqual ("Foo", label1.Text);
		}


		class ViewModel
		{
			public string Text { get; set; }
		}

		[Test]
		public void TestBindingModeAndConverter ()
		{
			var xaml = @"
				<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"">
					<ContentPage.Resources>
						<ResourceDictionary>
							<local:ReverseConverter x:Key=""reverseConverter""/>
						</ResourceDictionary>
					</ContentPage.Resources>
					<ContentPage.Content>
						<StackLayout Orientation=""Vertical"">
							<StackLayout.Children>
								<Label x:Name=""label0"" Text=""{Binding Text, Converter={StaticResource reverseConverter}}""/>
								<Label x:Name=""label1"" Text=""{Binding Text, Mode=TwoWay}""/>
							</StackLayout.Children>
						</StackLayout>
					</ContentPage.Content>
				</ContentPage>";

			var contentPage = new ContentPage ();
			contentPage.LoadFromXaml (xaml);
			contentPage.BindingContext = new ViewModel { Text = "foobar" };
			var label0 = contentPage.FindByName<Label> ("label0");
			var label1 = contentPage.FindByName<Label> ("label1");
			Assert.AreEqual ("raboof", label0.Text);
	
			label1.Text = "baz";
			Assert.AreEqual ("baz", ((ViewModel)(contentPage.BindingContext)).Text);
		}

		[Test]
		public void TestNonEmptyCollectionMembers ()
		{
			var xaml = @"
				<StackLayout 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
					<StackLayout.Children>
						<Grid x:Name=""grid0"">
						</Grid>
						<Grid x:Name=""grid1"">
						</Grid>
					</StackLayout.Children>
				</StackLayout>";

			var stacklayout = new StackLayout ();
			stacklayout.LoadFromXaml (xaml);
			var grid0 = stacklayout.FindByName<Grid> ("grid0");
			var grid1 = stacklayout.FindByName<Grid> ("grid1");
			Assert.NotNull (grid0);
			Assert.NotNull (grid1);
		}

		[Test]
		public void TestUnknownType ()
		{
			var xaml = @"
				<StackLayout 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
					<StackLayout.Children>
						<CustomView />
					</StackLayout.Children>
				</StackLayout>";

			var stacklayout = new StackLayout ();
			Assert.Throws (new XamlParseExceptionConstraint (6, 8), () => stacklayout.LoadFromXaml (xaml));
		}

		[Test]
		public void TestResources ()
		{
			var xaml = @"
				<Label 
				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"">
					<Label.Resources>
						<ResourceDictionary>
							<local:ReverseConverter x:Key=""reverseConverter""/>
						</ResourceDictionary>
					</Label.Resources>
				</Label>";

			var label = new Label ().LoadFromXaml (xaml);
			Assert.NotNull (label.Resources);
			Assert.True (label.Resources.ContainsKey ("reverseConverter"));
			Assert.True (label.Resources ["reverseConverter"] is ReverseConverter);
		}

		[Test]
		public void TestResourceDoesRequireKey ()
		{
			var xaml = @"
				<Label 
				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"">
					<Label.Resources>
						<ResourceDictionary>
							<local:ReverseConverter />
						</ResourceDictionary>
					</Label.Resources>
				</Label>";
			var label = new Label ();
			Assert.Throws (new XamlParseExceptionConstraint (8, 9), () => label.LoadFromXaml (xaml));
		}

		[Test]
		public void UseResourcesOutsideOfBinding ()
		{
			var xaml = @"
				<ContentView
				  xmlns=""http://xamarin.com/schemas/2014/forms""
				  xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
				  <ContentView.Resources>
                    <ResourceDictionary>
                      <Label Text=""Foo"" x:Key=""bar""/>
                    </ResourceDictionary>
				  </ContentView.Resources>
				<ContentView.Content>
                  <ContentView Content=""{StaticResource bar}""/>
				</ContentView.Content>
                </ContentView>";

			var contentView = new ContentView ().LoadFromXaml (xaml);
			Assert.AreEqual ("Foo", (((ContentView)(contentView.Content)).Content as Label).Text);
		}

		[Test]
		public void MissingStaticResourceShouldThrow ()
		{
			var xaml = @"<Label xmlns=""http://xamarin.com/schemas/2014/forms"" Text=""{StaticResource foo}""/>";
			var label = new Label ();
			Assert.Throws (new XamlParseExceptionConstraint (1, 54), () => label.LoadFromXaml (xaml));
		}

		public class CustView : Button
		{
			public bool fired = false;
			public void onButtonClicked (object sender, EventArgs e)
			{
				fired = true;
			}

			public void wrongSignature (bool a, string b)
			{
			}
		}

		class MyApp : Application 
		{
			public MyApp ()
			{
				Resources = new ResourceDictionary {
					{"foo", "FOO"},
					{"bar", "BAR"}
				};
			}
		}

		[Test]
		public void StaticResourceLookForApplicationResources ()
		{
			Device.PlatformServices = new MockPlatformServices ();
			Application.Current = null;

			Application.Current = new MyApp ();
			var xaml = @"
				<ContentView
				  xmlns=""http://xamarin.com/schemas/2014/forms""
				  xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
				  <ContentView.Resources>
                    <ResourceDictionary>
                      <x:String x:Key=""bar"">BAZ</x:String>
                    </ResourceDictionary>
				  </ContentView.Resources>
				  <StackLayout>
				    <Label x:Name=""label0"" Text=""{StaticResource foo}""/>
				    <Label x:Name=""label1"" Text=""{StaticResource bar}""/>
				  </StackLayout>
                </ContentView>";
			var layout = new ContentView ().LoadFromXaml (xaml);
			var label0 = layout.FindByName<Label> ("label0");
			var label1 = layout.FindByName<Label> ("label1");

			//resource from App.Resources
			Assert.AreEqual ("FOO", label0.Text);

			//local resources have precedence
			Assert.AreEqual ("BAZ", label1.Text);
		}

		[Test]
		public void TestEvent ()
		{
			var xaml = @"
				<Button 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				x:Class=""Xamarin.Forms.Xaml.UnitTests.CustView"" Clicked=""onButtonClicked"" />
				</Button>";
			var view = new CustView ();
			view.LoadFromXaml (xaml);
			Assert.False (view.fired);
			((IButtonController) view).SendClicked ();
			Assert.True (view.fired);
		}

		[Test]
		public void TestFailingEvent ()
		{
			var xaml = @"
				<View 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				x:Class=""Xamarin.Forms.Xaml.UnitTests.CustView"" Activated=""missingMethod"" />
				</View>";
			var view = new CustView ();
			Assert.Throws (new XamlParseExceptionConstraint (5, 53), () => view.LoadFromXaml (xaml));
		}

		[Test]
		public void TestConnectingEventOnMethodWithWrongSignature ()
		{
			var xaml = @"
				<View 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				x:Class=""Xamarin.Forms.Xaml.UnitTests.CustView"" Activated=""wrongSignature"" />
				</View>";
			var view = new CustView ();

			Assert.Throws (new XamlParseExceptionConstraint (5, 53), () => view.LoadFromXaml (xaml));
		}


		public class CustEntry : Entry
		{
			public bool fired = false;
			public void onValueChanged (object sender, TextChangedEventArgs e)
			{
				fired = true;
			}

		}

		[Test]
		public void TestEventWithCustomEventArgs ()
		{
			var xaml = @"
			<Entry
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				x:Class=""Xamarin.Forms.Xaml.UnitTests.CustEntry"" TextChanged=""onValueChanged"" />
			</Entry>";
			new CustEntry ().LoadFromXaml (xaml);
		}

		[Test]
		public void TestEmptyTemplate ()
		{
			var xaml = @"
			<ContentPage
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
				<ContentPage.Resources>
					<ResourceDictionary>
						<DataTemplate x:Key=""datatemplate""/>
					</ResourceDictionary>
				</ContentPage.Resources>
			</ContentPage>";
			var page = new ContentPage ();
			page.LoadFromXaml (xaml);
			var template = page.Resources["datatemplate"]as Forms.DataTemplate;
			Assert.Throws<InvalidOperationException>(() => template.CreateContent());
		}

		[Test]
		public void TestBoolValue ()
		{
			var xaml = @"
				<Image 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				IsOpaque=""true""/>";

			var image = new Image ();
			Assert.AreEqual (Image.IsOpaqueProperty.DefaultValue, image.IsOpaque);
			image.LoadFromXaml (xaml);
			Assert.AreEqual (true, image.IsOpaque);
		}

		[Test]
		public void TestAttachedBP ()
		{
			var xaml = @"
				<View 
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
				Grid.Column=""1"">
					<Grid.Row>2</Grid.Row>
				</View>";
			var view = new View ().LoadFromXaml (xaml);
			Assert.AreEqual (1, Grid.GetColumn (view));
			Assert.AreEqual (2, Grid.GetRow (view));
		}

		[Test]
		public void TestAttachedBPWithDifferentNS ()
		{
			//If this looks very similar to Vernacular, well... it's on purpose :)
			var xaml = @"
				<Label
				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"" 
				local:Catalog.Message=""foobar""/>";
			var label = new Label ().LoadFromXaml (xaml);
			Assert.AreEqual ("raboof", label.Text);
		}

		[Test]
		public void TestBindOnAttachedBP ()
		{
			var xaml = @"
				<Label
				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"" 
				local:Catalog.Message=""{Binding .}""/>";
			var label = new Label ().LoadFromXaml (xaml);
			label.BindingContext = "foobar";
			Assert.AreEqual ("raboof", label.Text);
		}

		[Test]
		public void TestContentProperties ()
		{
			var xaml = @"
				<local:CustomView
				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"" >
					<Label x:Name=""contentview""/>
				</local:CustomView>";
			CustomView customView = null;
			Assert.DoesNotThrow(()=> customView = new CustomView ().LoadFromXaml (xaml));
			Assert.NotNull (customView.Content);
			Assert.AreSame (customView.Content, ((Forms.Internals.INameScope)customView).FindByName("contentview"));
		}

		[Test]
		public void TestCollectionContentProperties ()
		{
			var xaml = @"
				<StackLayout xmlns=""http://xamarin.com/schemas/2014/forms"">
					<Label Text=""Foo""/>
					<Label Text=""Bar""/>
				</StackLayout>";
			var layout = new StackLayout ().LoadFromXaml (xaml);
			Assert.AreEqual (2, layout.Children.Count);
			Assert.AreEqual ("Foo", ((Label)(layout.Children [0])).Text);
			Assert.AreEqual ("Bar", ((Label)(layout.Children [1])).Text);
		}

		[Test]
		public void TestCollectionContentPropertiesWithSingleElement ()
		{
			var xaml = @"
				<StackLayout xmlns=""http://xamarin.com/schemas/2014/forms"">
					<Label Text=""Foo""/>
				</StackLayout>";
			var layout = new StackLayout ().LoadFromXaml (xaml);
			Assert.AreEqual (1, layout.Children.Count);
			Assert.AreEqual ("Foo", ((Label)(layout.Children [0])).Text);
		}

		[Test]
		public void TestPropertiesWithContentProperties ()
		{
			var xaml = @"
				<ContentPage
				xmlns=""http://xamarin.com/schemas/2014/forms""
				xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
				<Grid.Row>1</Grid.Row>
				<Label Text=""foo""></Label>
				</ContentPage>";
			var contentPage = new ContentPage ().LoadFromXaml (xaml);
			Assert.AreEqual (1, Grid.GetRow (contentPage));
			Assert.NotNull (contentPage.Content);
		}

		[Test]
		public void LoadFromXamlResource ()
		{
			ContentView view = null;
			Assert.DoesNotThrow (() => view = new CustomXamlView ());
			Assert.NotNull (view);
			Assert.That (view.Content, Is.TypeOf<Label> ());
			Assert.AreEqual ("foobar", ((Label)view.Content).Text);
		}

		[Test]
		public void ThrowOnMissingXamlResource ()
		{
			var view = new CustomView ();
			Assert.Throws (new XamlParseExceptionConstraint (), () => view.LoadFromXaml (typeof(CustomView)));
		}

		[Test]
		public void CreateNewChildrenCollection ()
		{
			var xaml = @"
				<local:ViewWithChildrenContent
				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"" >
					<local:ViewWithChildrenContent.Children>
						<local:ViewList>
							<Label x:Name=""child0""/>
							<Label x:Name=""child1""/>
						</local:ViewList>
					</local:ViewWithChildrenContent.Children>
				</local:ViewWithChildrenContent>";
			ViewWithChildrenContent layout = null;
			Assert.DoesNotThrow (() => layout = new ViewWithChildrenContent ().LoadFromXaml (xaml));
			Assert.IsNotNull (layout);
			Assert.AreNotSame (layout.DefaultChildren, layout.Children);
			Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child0"), layout.Children);
			Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child1"), layout.Children);
		}

		[Test]
		public void AddChildrenToCollectionContentProperty ()
		{
			var xaml = @"
				<local:ViewWithChildrenContent
				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"" >
					<Label x:Name=""child0""/>
					<Label x:Name=""child1""/>
				</local:ViewWithChildrenContent>";
			ViewWithChildrenContent layout = null;
			Assert.DoesNotThrow (() => layout = new ViewWithChildrenContent ().LoadFromXaml (xaml));
			Assert.IsNotNull (layout);
			Assert.AreSame (layout.DefaultChildren, layout.Children);
			Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child0"), layout.Children);
			Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child1"), layout.Children);
		}

		[Test]
		public void AddChildrenToExistingCollection ()
		{
			var xaml = @"
				<local:ViewWithChildrenContent
				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"" >
					<local:ViewWithChildrenContent.Children>
						<Label x:Name=""child0""/>
						<Label x:Name=""child1""/>
					</local:ViewWithChildrenContent.Children>
				</local:ViewWithChildrenContent>";
			ViewWithChildrenContent layout = null;
			Assert.DoesNotThrow (() => layout = new ViewWithChildrenContent ().LoadFromXaml (xaml));
			Assert.IsNotNull (layout);
			Assert.AreSame (layout.DefaultChildren, layout.Children);
			Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child0"), layout.Children);
			Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child1"), layout.Children);

		}

		[Test]
		public void AddSingleChildToCollectionContentProperty ()
		{
			var xaml = @"
				<local:ViewWithChildrenContent
				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"" >
					<Label x:Name=""child0""/>
				</local:ViewWithChildrenContent>";
			ViewWithChildrenContent layout = null;
			Assert.DoesNotThrow (() => layout = new ViewWithChildrenContent ().LoadFromXaml (xaml));
			Assert.IsNotNull (layout);
			Assert.AreSame (layout.DefaultChildren, layout.Children);
			Assert.Contains (((Forms.Internals.INameScope)layout).FindByName ("child0"), layout.Children);
		}

		[Test]
		public void FindResourceByName ()
		{
			var xaml = @"
				<ContentPage
				    xmlns=""http://xamarin.com/schemas/2014/forms""
				    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"" 
				    x:Class=""Resources"">
				    <ContentPage.Resources>
						<ResourceDictionary>
						    <Button x:Key=""buttonKey"" x:Name=""buttonName""/>
						</ResourceDictionary>
				    </ContentPage.Resources>
				    <Label x:Name=""label""/>
				</ContentPage>";

			var layout = new ContentPage ().LoadFromXaml (xaml);
			Assert.True (layout.Resources.ContainsKey ("buttonKey"));
			var resource = layout.FindByName<Button> ("buttonName");
			Assert.NotNull (resource);
			Assert.That (resource, Is.TypeOf<Button> ());
		}

		[Test]
		public void ParseEnum ()
		{
			var xaml = @"
				<local:CustomView
				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"" 
				MockFlags=""Bar""
				/>";
			var view = new CustomView ().LoadFromXaml (xaml);
			Assert.AreEqual (MockFlags.Bar, view.MockFlags);
					
		}

		[Test]
		public void ParseFlags ()
		{
			var xaml = @"
				<local:CustomView
				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"" 
				MockFlags=""Baz,Bar""
				/>";
			var view = new CustomView ().LoadFromXaml (xaml);
			Assert.AreEqual (MockFlags.Bar | MockFlags.Baz, view.MockFlags);
		}

		[Test]
		public void StyleWithoutTargetTypeThrows ()
		{
			var xaml = @"
				<Label xmlns=""http://xamarin.com/schemas/2014/forms"">
					<Label.Style>
						<Style>
							<Setter Property=""Text"" Value=""Foo"" />
						</Style>
					</Label.Style>
				</Label>";
			var label = new Label ();
			Assert.Throws (new XamlParseExceptionConstraint (4, 8), () => label.LoadFromXaml (xaml));
		}
	}
}