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

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

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

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

			var property = editor.Properties.Single();
			Assert.That (property.Name, Is.EqualTo (nameof (TestClass.Property)));
			Assert.That (property.Type, Is.EqualTo (typeof (string)));
		}

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

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

			const string value = "value";

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

			Assert.That (obj.Property, Is.EqualTo (value));
		}

		[Test]
		public async Task GetValue ()
		{
			const string value = "value";
			var obj = new TestClass { Property = value };

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

			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 SetValueConvert ()
		{
			var obj = new TestClass ();

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

			const string value = "1";

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

			Assert.That (obj.Property, Is.EqualTo (value));
		}

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

			var provider = new ReflectionEditorProvider ();
			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 PropertyChanged ()
		{
			var obj = new TestClass ();

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

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

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

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

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

		[Test]
		public async Task TypeConverterTo ()
		{
			const string value = "value";
			var obj = new ConversionClass {
				Property = new TestClass {
					Property = value
				}
			};

			var provider = new ReflectionEditorProvider();
			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 TypeConvertFrom ()
		{
			const string value = "value";
			var obj = new ConversionClass ();

			var provider = new ReflectionEditorProvider ();
			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
			});

			Assert.That (obj.Property, Is.Not.Null);
			Assert.That (obj.Property.Property, Is.EqualTo (value));
		}

		[Test]
		public async Task TypeConverterToPropertyAttribute ()
		{
			const string value = "value";
			var obj = new ConversionClass2 {
				Property = new TestClass2 {
					Property = value
				}
			};

			var provider = new ReflectionEditorProvider ();
			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 TypeConvertFromPropertyAttribute ()
		{
			const string value = "value";
			var obj = new ConversionClass2 ();

			var provider = new ReflectionEditorProvider ();
			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
			});

			Assert.That (obj.Property, Is.Not.Null);
			Assert.That (obj.Property.Property, Is.EqualTo (value));
		}

		/// <remarks>
		/// Mac objects can sometimes define properties on types (say, like NSView) that aren't accessible on
		/// some implementations. Normally you'd ask it if it can respond to the selector, but in generic code
		/// your only recourse is to look for the DebuggerBrowsableAttribute.
		/// </remarks>
		[Test]
		[NUnit.Framework.Description ("Properties marked DebuggerBrowsable (Never) should not be included in the list of properties.")]
		public async Task DontSetupPropertiesMarkedUnbrowsable ()
		{
			var unbrowsable = new Unbrowsable();

			var provider = new ReflectionEditorProvider();
			IObjectEditor editor = await provider.GetObjectEditorAsync (unbrowsable);

			Assert.That (editor.Properties.Count, Is.EqualTo (1), "Wrong number of properties");
			Assert.That (editor.Properties.Single ().Name, Is.EqualTo (nameof (Unbrowsable.VisibleProperty)));
		}

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

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

			List<int> values = new List<int> ();
			values.Add ((int)FlagsTestEnum.Flag1);
			values.Add ((int)FlagsTestEnum.Flag3);

			FlagsTestEnum expected = FlagsTestEnum.Flag1 | FlagsTestEnum.Flag3;

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

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

			ValueInfo<IReadOnlyList<int>> underlyingList = await editor.GetValueAsync<IReadOnlyList<int>> (editor.Properties.First ());
			Assert.That (underlyingList.Value, Contains.Item ((int) FlagsTestEnum.Flag1));
			Assert.That (underlyingList.Value, Contains.Item ((int) FlagsTestEnum.Flag3));
		}

		private class Unbrowsable
		{
			public string VisibleProperty
			{
				get;
				set;
			}

			[DebuggerBrowsable (DebuggerBrowsableState.Never)]
			public string InvisibleProperty
			{
				get;
				set;
			}
		}

		private class Converter2
			: 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 TestClass2)?.Property;
			}

			public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
			{
				return new TestClass2 { Property = value as string };
			}
		}

		private class ConversionClass2
		{
			[TypeConverter (typeof (Converter2))]
			public TestClass2 Property
			{
				get;
				set;
			}
		}

		
		private class TestClass2
		{
			public string Property
			{
				get;
				set;
			}
		}

		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)?.Property;
			}

			public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
			{
				return new TestClass { Property = value as string };
			}
		}

		private class ConversionClass
		{
			public TestClass Property
			{
				get;
				set;
			}
		}

		[TypeConverter (typeof (Converter))]
		private class TestClass
		{
			public string Property
			{
				get;
				set;
			}
		}

		
		[Flags]

		internal enum FlagsTestEnum
		{
			None = 0,
			Flag1 = 1,
			Flag2 = 2,
			Flag3 = 4,
			Flag4 = 8
		}

		private class EnumClass
		{
			public FlagsTestEnum Value
			{
				get;
				set;
			}
		}
	}
}