Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MockPropertyProviderTests.cs « Xamarin.PropertyEditing.Tests - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eee6d2b408a163691b3c1219ced1b9aa5fccd585 (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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.PropertyEditing.Tests.MockControls;

namespace Xamarin.PropertyEditing.Tests
{
	/*
	[TestFixture]
	public class MockPropertyProviderTests
	{
		[Test]
		public async Task MockEditorHasSimpleProperty ()
		{
			var provider = new MockEditorProvider ();
			IObjectEditor editor = await provider.GetObjectEditorAsync (new TestClass ());
			Assume.That (editor, Is.Not.Null);

			Assert.That (editor.Properties.Count, Is.EqualTo (1));

			var propertyInfo = editor.Properties.Single();
			Assert.That (propertyInfo.Name, Is.EqualTo (TestClass.PropertyName));
			Assert.That (propertyInfo.Type, Is.EqualTo (typeof (string)));
		}
		
		[Test]
		public async Task MockSetValueConvert ()
		{
			var obj = new TestClass ();

			var provider = new MockEditorProvider ();
			IObjectEditor editor = await provider.GetObjectEditorAsync (obj);

			const string value = "1";

			var propertyInfo = editor.Properties.Single ();
			await editor.SetValueAsync (propertyInfo, new ValueInfo<int> {
				Value = 1
			});

			Assert.That (obj.GetValue<string> (propertyInfo), Is.EqualTo (value));
		}

		[Test]
		public async Task MockGetValueConvert ()
		{
			const string value = "1";
			var obj = new TestClass(value);

			var provider = new MockEditorProvider ();
			IObjectEditor editor = await provider.GetObjectEditorAsync (obj);

			ValueInfo<int> info = await editor.GetValueAsync<int> (editor.Properties.Single ());
			Assert.That (info.Value, Is.EqualTo (1));
			Assert.That (info.Source, Is.EqualTo (ValueSource.Local));
		}

		[Test]
		public async Task MockPropertyChanged ()
		{
			var obj = new TestClass ();

			var provider = new MockEditorProvider ();
			IObjectEditor editor = await provider.GetObjectEditorAsync (obj);

			const string value = "value";
			var propertyInfo = editor.Properties.Single ();

			var changed = false;
			editor.PropertyChanged += (sender, args) => {
				if (Equals (args.Property, propertyInfo))
					changed = true;
			};

			await editor.SetValueAsync (propertyInfo, new ValueInfo<string> {
				Value = value
			});

			Assert.That (changed, Is.True, "PropertyChanged was not raised for the given property");
		}

		[Test]
		public async Task MockTypeConverterTo ()
		{
			const string value = "value";
			var obj = new ConversionClass ();
			obj.SetValue (ConversionClass.PropertyName, new TestClass (value));

			var provider = new MockEditorProvider ();
			IObjectEditor editor = await provider.GetObjectEditorAsync (obj);
			Assume.That (editor.Properties.Count, Is.EqualTo (1));

			ValueInfo<string> info = await editor.GetValueAsync<string> (editor.Properties.Single ());
			Assert.That (info.Value, Is.EqualTo (value));
			Assert.That (info.Source, Is.EqualTo (ValueSource.Local));
		}

		[Test]
		public async Task MockTypeConvertFrom ()
		{
			const string value = "value";
			var obj = new ConversionClass ();

			var provider = new MockEditorProvider ();
			IObjectEditor editor = await provider.GetObjectEditorAsync (obj);
			Assume.That (editor.Properties.Count, Is.EqualTo (1));

			await editor.SetValueAsync (editor.Properties.Single (), new ValueInfo<string> {
				Value = value,
				Source = ValueSource.Local
			});

			var val = obj.GetValue<TestClass> (ConversionClass.PropertyName);
			Assert.That (val, Is.Not.Null);
			Assert.That (val.GetValue<string>(TestClass.PropertyName), Is.EqualTo (value));
		}

		[Test]
		public async Task MockCombinableEnum ()
		{
			var enumObj = new EnumClass ();

			var provider = new MockEditorProvider ();
			IObjectEditor editor = await provider.GetObjectEditorAsync (enumObj);

			var values = new List<int> {
				(int)FlagsTestEnum.Flag1,
				(int)FlagsTestEnum.Flag3
			};

			FlagsTestEnum expected = FlagsTestEnum.Flag1 | FlagsTestEnum.Flag3;

			var propertyInfo = editor.Properties.Single ();

			await editor.SetValueAsync (propertyInfo, new ValueInfo<IReadOnlyList<int>> {
				Value = values
			});

			var underlying = await editor.GetValueAsync<int> (propertyInfo);
			Assert.That ((FlagsTestEnum)underlying.Value, Is.EqualTo (expected));

			var underlyingList = await editor.GetValueAsync<IReadOnlyList<int>> (propertyInfo);
			Assert.That (underlyingList.Value.First(), Is.EqualTo ((int)FlagsTestEnum.Flag1));
			Assert.That (underlyingList.Value.Skip(1).First(), Is.EqualTo ((int)FlagsTestEnum.Flag3));
		}

		private class Converter
			: TypeConverter
		{
			public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
			{
				return (sourceType == typeof (string));
			}

			public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
			{
				return (destinationType == typeof (string));
			}

			public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
			{
				if (destinationType != typeof (string))
					throw new ArgumentException ();

				return (value as TestClass)?.GetValue<string>(TestClass.PropertyName);
			}

			public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
			{
				var converted = new TestClass ();
				converted.SetValue<string> (TestClass.PropertyName, value as string);
				return converted;
			}
		}

		private class ConversionClass : MockControl
		{
			public static readonly string PropertyName = "TestProperty";

			public ConversionClass()
			{
				AddProperty<TestClass> (PropertyName, converterTypes: new[] { typeof (Converter) });
			}
		}

		private class TestClass : MockControl
		{
			public static readonly string PropertyName = "Property";

			public TestClass()
			{
				AddProperty<string> (PropertyName, converterTypes: new[] { typeof (StringConverter) });
			}

			public TestClass(string value) : this()
			{
				SetValue (PropertyName, value);
			}
		}

		private class EnumClass : MockControl
		{
			public static readonly string PropertyName = "Value";

			public EnumClass ()
			{
				AddProperty<FlagsTestEnum> (PropertyName, flag: true);
			}
		}
	}
	*/
}