summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/SetValue.xaml.cs
blob: 6e3cdc1a8f93e312bacbd304dd03d20a7dc91255 (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
using System;
using System.Linq;

using NUnit.Framework;
using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{
	public class ConvertibleToView
	{
		public static implicit operator View(ConvertibleToView source)
		{
			return new Button();
		}
	}

	public class MockViewWithValues : View
	{ 
		public UInt16 UShort { get; set; }
		public decimal ADecimal { get; set; }
		public SV_Foo Foo { get; set; }
		public string Bar { get; set; }

		public static readonly BindableProperty BPFooProperty =
			BindableProperty.Create("BPFoo", typeof(SV_Foo), typeof(MockViewWithValues), default(SV_Foo));

		public SV_Foo BPFoo {
			get { throw new NotImplementedException(); }
			set { throw new NotImplementedException(); }
		}

		public static readonly BindableProperty BPBarProperty =
			BindableProperty.Create("BPBar", typeof(string), typeof(MockViewWithValues), default(string));

		public string BPBar {
			get { throw new NotImplementedException(); }
			set { throw new NotImplementedException(); }
		}
	}

	public class SV_Foo
	{
		public string Value { get; set; }
		public static implicit operator SV_Foo(string value)
		{
			return new SV_Foo { Value = value };
		}

		public static implicit operator string(SV_Foo foo)
		{
			return foo.Value;
		}
	}

	public enum IntEnum
	{
		Foo,
		Bar,
		Baz
	}

	public enum ByteEnum : byte
	{
		Foo,
		Bar,
		Baz
	}

	public class ViewWithEnums : View
	{
		public IntEnum IntEnum { get; set; }
		public ByteEnum ByteEnum { get; set; }
	}

	public partial class SetValue : ContentPage
	{	
		public SetValue ()
		{
			InitializeComponent ();
		}

		public SetValue (bool useCompiledXaml)
		{
			//this stub will be replaced at compile time
		}

		bool fired;
		void onButtonClicked (object sender, EventArgs args)
		{
			fired = true;
		}

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

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

			[TestCase (false)]
			[TestCase (true)]
			public void SetValueToBP (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual ("Foo", page.label0.Text);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetBindingToBP (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual (Label.TextProperty.DefaultValue, page.label1.Text);

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

			[TestCase (false)]
			[TestCase (true)]
			public void SetBindingWithImplicitPath (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual (Label.TextProperty.DefaultValue, page.label11.Text);

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

			[TestCase (false)]
			[TestCase (true)]
			public void SetEvent (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.False (page.fired);
				(page.button0 as IButtonController).SendClicked ();
				Assert.True (page.fired);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetBoolValue (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.True (page.image0.IsOpaque);
			}

			//TODO test all value conversions

			[TestCase (false)]
			[TestCase (true)]
			public void SetAttachedBP (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual (1, Grid.GetColumn (page.label2));
				Assert.AreEqual (2, Grid.GetRow (page.label2));
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetContent (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreSame (page.label3, page.contentview0.Content);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetImplicitContent (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreSame (page.label4, page.contentview1.Content);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetCollectionContent (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.True (page.stack0.Children.Contains(page.label5));
				Assert.True (page.stack0.Children.Contains(page.label6));
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetImplicitCollectionContent (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.True (page.stack1.Children.Contains(page.label7));
				Assert.True (page.stack1.Children.Contains(page.label8));
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetSingleCollectionContent (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.True (page.stack2.Children.Contains(page.label9));
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetImplicitSingleCollectionContent (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.True (page.stack3.Children.Contains(page.label10));
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetPropertyDefinedOnGenericType (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual (2, page.listView.ItemsSource.Cast<object>().Count ());
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetConvertibleProperties (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual (Color.Red, page.label12.TextColor);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void SetValueTypeProperties (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual (Color.Pink, page.label13.TextColor);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void CreateValueTypes (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual (Color.Purple, page.Resources ["purple"]);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void DefCollections (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual (2, page.grid0.RowDefinitions.Count);
				Assert.AreEqual (1, page.grid0.ColumnDefinitions.Count);
			}

			[TestCase (false)]
			[TestCase (true)]
			public void FlagsAreApplied (bool useCompiledXaml)
			{
				var page = new SetValue (useCompiledXaml);
				Assert.AreEqual (AbsoluteLayoutFlags.PositionProportional | AbsoluteLayoutFlags.WidthProportional, AbsoluteLayout.GetLayoutFlags (page.label14));
			}

			[TestCase(false)]
			[TestCase(true)]
			public void ConversionsAreAppliedOnSet(bool useCompiledXaml)
			{
				var page = new SetValue(useCompiledXaml);
				Assert.That(page.content0.Content, Is.TypeOf<Button>());
			}

			[TestCase(false)]
			[TestCase(true)]
			public void ConversionsAreAppliedOnAdd(bool useCompiledXaml)
			{
				var page = new SetValue(useCompiledXaml);
				Assert.That(page.stack4.Children[0], Is.TypeOf<Button>());
			}

			[TestCase(false)]
			[TestCase(true)]
			public void ListsAreSimplified(bool useCompiledXaml)
			{
				var page = new SetValue(useCompiledXaml);
				Assert.That(page.contentview2.Content, Is.TypeOf<Label>());
			}

			[TestCase(false)]
			[TestCase(true)]
			public void MorePrimitiveTypes(bool useCompiledXaml)
			{ 
				var page = new SetValue(useCompiledXaml);
				Assert.AreEqual((ushort)32, page.mockView0.UShort);
				Assert.AreEqual((decimal)42, page.mockView0.ADecimal);
			}

			[TestCase(false)]
			[TestCase(true)]
			public void NonIntEnums(bool useCompiledXaml)
			{
				var page = new SetValue(useCompiledXaml);
				Assert.AreEqual(IntEnum.Foo, page.enums.IntEnum);
				Assert.AreEqual(ByteEnum.Bar, page.enums.ByteEnum);
			}

			public void SetValueWithImplicitOperatorOnSource(bool useCompiledXaml)
			{
				var page = new SetValue(useCompiledXaml);
				Assert.AreEqual("Bar", page.implicit0.GetValue(MockViewWithValues.BPBarProperty));
			}

			[TestCase(false)]
			[TestCase(true)]
			public void SetValueWithImplicitOperatorOnTarget(bool useCompiledXaml)
			{
				var page = new SetValue(useCompiledXaml);
				Assert.AreEqual("Foo", ((SV_Foo)page.implicit1.GetValue(MockViewWithValues.BPFooProperty)).Value);
			}

			[TestCase(false)]
			[TestCase(true)]
			public void SetWithImplicitOperatorOnSource(bool useCompiledXaml)
			{
				var page = new SetValue(useCompiledXaml);
				Assert.AreEqual("Bar", page.implicit2.Bar);
			}

			[TestCase(false)]
			[TestCase(true)]
			public void SetWithImplicitOperatorOnTarget(bool useCompiledXaml)
			{
				var page = new SetValue(useCompiledXaml);
				Assert.AreEqual("Foo", page.implicit3.Foo.Value);
			}
		}
	}
}