summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/Issues/Issue1549.cs
blob: 8b5bf30e6fa6dd02f222bc091c20add6f14b6262 (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
using System;
using NUnit.Framework;
using System.Collections.ObjectModel;
using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{

	public class SeverityColorConverter : IValueConverter
	{

		public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			count++;
			return Color.Blue;
		}

		public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException ();
		}


		public static int count=0;
	}

	public class InvertBoolenConverter : IValueConverter
	{
		public object Convert (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			count++;
			if (value is bool)  {

				return !(bool)value;
			}
			return value;
		}

		public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException ();
		}
		public static int count=0;
	}

	public class Item
	{
		public bool IsLocked {
			get;
			set;
		}
	}

	[TestFixture]
	public class Issue1549
	{
		[SetUp]
		public void Setup ()
		{
			SeverityColorConverter.count = 0;
			InvertBoolenConverter.count = 0;
			Device.PlatformServices = new MockPlatformServices();
		}

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

		[Test]
		public void ConverterIsInvoked ()
		{
			var xaml = @"
<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"">

<ContentPage.Resources>
<ResourceDictionary>
<local:SeverityColorConverter x:Key=""SeverityColorConverter"" />
</ResourceDictionary>
</ContentPage.Resources>
				<Label Text=""{Binding value, StringFormat='{0}'}"" 
					WidthRequest=""50"" 
					TextColor=""Black""
					x:Name=""label""
					BackgroundColor=""{Binding Severity, Converter={StaticResource SeverityColorConverter}}""
					XAlign=""Center"" YAlign=""Center""/>
</ContentPage>";

			var layout = new ContentPage ().LoadFromXaml (xaml);
			layout.BindingContext = new {Value = "Foo", Severity = "Bar"};
			var label = layout.FindByName<Label> ("label");
			Assert.AreEqual (Color.Blue, label.BackgroundColor);
			Assert.AreEqual (1, SeverityColorConverter.count);
		}

		[Test]
		public void ResourcesInNonXFBaseClassesAreFound ()
		{
			var xaml = @"<local:BaseView 
	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""
	Padding=""0,40,0,0"">
    <local:BaseView.Resources>
    <ResourceDictionary>
     	<local:InvertBoolenConverter x:Key=""cnvInvert""></local:InvertBoolenConverter>
    </ResourceDictionary>
    </local:BaseView.Resources>
	<local:BaseView.Content>
		<ListView x:Name=""lst"" VerticalOptions=""FillAndExpand""
        		HorizontalOptions=""FillAndExpand""
			
				ItemsSource=""{Binding Items}""

			>
		<ListView.ItemTemplate >
			<DataTemplate> 
				<ViewCell >
				<ViewCell.View>
					<Grid  VerticalOptions=""FillAndExpand"" HorizontalOptions=""FillAndExpand""  >			
					<Label  IsVisible=""{Binding IsLocked}""  Text=""Show Is Locked""  />
					<Label  IsVisible=""{Binding IsLocked, Converter={StaticResource cnvInvert}}"" Text=""Show Is Not locked"" />
				</Grid>
				</ViewCell.View>
				</ViewCell>
			</DataTemplate>
		</ListView.ItemTemplate>
		</ListView>
	</local:BaseView.Content>
</local:BaseView>";
			var page = new Issue1549Page ().LoadFromXaml (xaml);
			var lst = page.FindByName<ListView> ("lst");
			ObservableCollection<Item> items;
			lst.BindingContext = new {
				Items = items = new ObservableCollection<Item> {
					new Item { IsLocked = true},
					new Item { IsLocked = false},
					new Item { IsLocked = true},
					new Item { IsLocked = true},
				},
			};

			var cell0 = (ViewCell)lst.TemplatedItems.GetOrCreateContent (0, items [0]);
			var cell1 = (ViewCell)lst.TemplatedItems.GetOrCreateContent (1, items [1]);
			var cell2 = (ViewCell)lst.TemplatedItems.GetOrCreateContent (2, items [2]);
			var cell3 = (ViewCell)lst.TemplatedItems.GetOrCreateContent (3, items [3]);

			var label00 = (cell0.View as Grid).Children [0] as Label;
			var label01 = (cell0.View as Grid).Children [1] as Label;

			Assert.AreEqual ("Show Is Locked", label00.Text);
			Assert.AreEqual ("Show Is Not locked", label01.Text);

			Assert.AreEqual (true, label00.IsVisible);
			Assert.AreEqual (false, label01.IsVisible);

			Assert.AreEqual (4, InvertBoolenConverter.count);

		}
	}

	public class BaseView : ContentPage
	{
	}

	public partial class Issue1549Page : BaseView
	{	
	}
}