summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Xaml.UnitTests/Issues/Bz24910.xaml.cs
blob: 0fa35cc13a0cf83190dab19fe13c8da3643a96b7 (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
using System;
using System.Collections.Generic;

using Xamarin.Forms;
using NUnit.Framework;

namespace Xamarin.Forms.Xaml.UnitTests
{
	public partial class Bz24910 : ContentPage
	{
		public Bz24910 ()
		{
			InitializeComponent ();
		}

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

		[TestFixture]
		class Tests
		{
			[TestCase(true), TestCase(false)]
			public void AllowNullableIntProperties (bool useCompiledXaml)
			{
				var page = new Bz24910 (useCompiledXaml);
				var control = page.control0;
				Assert.AreEqual (1, control.NullableInt);
			}

			[TestCase(true), TestCase(false)]
			public void AllowNullableDoubleProperties (bool useCompiledXaml)
			{
				var page = new Bz24910 (useCompiledXaml);
				var control = page.control0;
				Assert.AreEqual (2.2d, control.NullableDouble);
			}

			[TestCase(true), TestCase(false)]
			public void ConversionForNullable (bool useCompiledXaml)
			{
				var page = new Bz24910 (useCompiledXaml);
				var control = page.control1;
				Assert.AreEqual (2d, control.NullableDouble);
			}

			[TestCase(true), TestCase(false)]
			public void AllowNull (bool useCompiledXaml)
			{
				var page = new Bz24910 (useCompiledXaml);
				var control = page.control2;
				Assert.Null (control.NullableInt);
			}

			[TestCase(true), TestCase(false)]
			public void AllowBindingToNullable (bool useCompiledXaml)
			{
				var page = new Bz24910 (useCompiledXaml);
				var control = page.control3;
				Assert.Null (control.NullableInt);

				page.BindingContext = 2;
				Assert.AreEqual (2, control.NullableInt);
			}

			[TestCase(true), TestCase(false)]
			public void NullableAttachedBPs (bool useCompiledXaml)
			{
				var page = new Bz24910 (useCompiledXaml);
				var control = page.control4;
				Assert.AreEqual (3, Bz24910Control.GetAttachedNullableInt (control));
			}

			[TestCase(true), TestCase(false)]
			public void AllowNonBindableNullable (bool useCompiledXaml)
			{
				var page = new Bz24910 (useCompiledXaml);
				var control = page.control5;

				Assert.AreEqual (5, control.NullableIntProp);
			}
		}
	}

	public class Bz24910Control : Button
	{
		public static readonly BindableProperty NullableIntProperty =
			BindableProperty.Create ("NullableInt", typeof(int?), typeof(Bz24910Control), default(int?));

		public int? NullableInt {
			get { return (int?)GetValue (NullableIntProperty); }
			set { SetValue (NullableIntProperty, value); }
		}

		public static readonly BindableProperty NullableDoubleProperty =
			BindableProperty.Create ("NullableDouble", typeof(double?), typeof(Bz24910Control), default(double?));

		public double? NullableDouble {
			get { return (double?)GetValue (NullableDoubleProperty); }
			set { SetValue (NullableDoubleProperty, value); }
		}

		public static readonly BindableProperty AttachedNullableIntProperty =
#pragma warning disable 618
			BindableProperty.CreateAttached<Bz24910Control, int?> (bindable => GetAttachedNullableInt (bindable), default(int?));
#pragma warning restore 618

		public static int? GetAttachedNullableInt (BindableObject bindable)
		{
			return (int?)bindable.GetValue (AttachedNullableIntProperty);
		}

		public static void SetAttachedNullableInt (BindableObject bindable, int? value)
		{
			bindable.SetValue (AttachedNullableIntProperty, value);
		}

		public int? NullableIntProp { get; set; }
	}
}