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

ObjectPropertyViewModelTests.cs « Xamarin.PropertyEditing.Tests - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cedf83537b0f2078981adb750a3aadf35d1ffd0e (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Moq;
using NUnit.Framework;
using NUnit.Framework.Internal;
using Xamarin.PropertyEditing.ViewModels;

namespace Xamarin.PropertyEditing.Tests
{
	[TestFixture]
	internal class ObjectPropertyViewModelTests
	{
		[SetUp]
		public void Setup ()
		{
			this.syncContext = new TestContext ();
			SynchronizationContext.SetSynchronizationContext (this.syncContext);
		}

		[TearDown]
		public void TearDown ()
		{
			SynchronizationContext.SetSynchronizationContext (null);
			syncContext.ThrowPendingExceptions ();
		}

		[Test]
		public void TypeAssumedWhenSingleAssignableType ()
		{
			object value = new object();

			var p = CreatePropertyMock ("prop");

			var childsubInfo = GetTypeInfo (typeof(SubChildClass));
			var editor = new MockObjectEditor (new[] { p.Object }, new Dictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> {
				{ p.Object, new[] { childsubInfo } }
			});

			var providerMock = CreateProviderMock (value, new MockObjectEditor { Target = value });

			var vm = new ObjectPropertyViewModel (new TargetPlatform (providerMock.Object), p.Object, new[] { editor });

			bool requested = false;
			vm.TypeRequested += (sender, args) => {
				requested = true;
			};

			Assume.That (vm.CreateInstanceCommand.CanExecute (childsubInfo), Is.True);
			vm.CreateInstanceCommand.Execute (childsubInfo);
			Assert.That (requested, Is.False, "TypeRequested was raised");

			providerMock.Verify (pr => pr.CreateObjectAsync (childsubInfo));
		}

		[Test]
		public void TypeRequestedWhenMultipleAssignableTypes ()
		{
			object value = new object();

			var p = CreatePropertyMock ("prop");

			var childsubInfo = GetTypeInfo (typeof(SubChildClass));
			var editor = new MockObjectEditor (new[] { p.Object }, new Dictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> {
				{
					p.Object,
					new[] {
						GetTypeInfo (typeof(ChildClass)),
						childsubInfo
					}
				}
			});

			var providerMock = CreateProviderMock (value, new MockObjectEditor { Target = value });

			var vm = new ObjectPropertyViewModel (new TargetPlatform (providerMock.Object), p.Object, new[] { editor });

			bool requested = false;
			vm.TypeRequested += (sender, args) => {
				requested = true;
			};

			Assume.That (vm.CreateInstanceCommand.CanExecute (childsubInfo), Is.True);
			vm.CreateInstanceCommand.Execute (childsubInfo);
			Assert.That (requested, Is.True, "TypeRequested was not raised");
		}

		[Test]
		[Description ("Values with multiple value sources should show unknown")]
		public async Task MultiValueSourcesUnknown ()
		{
			object value = new object();

			var p = CreatePropertyMock ("prop");

			var childsubInfo = GetTypeInfo (typeof(SubChildClass));
			var editor = new MockObjectEditor (new[] { p.Object }, new Dictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> {
				{ p.Object, new[] { childsubInfo } }
			});
			await editor.SetValueAsync (p.Object, new ValueInfo<object> {
				Value = value,
				Source = ValueSource.Local,
				ValueDescriptor = childsubInfo
			});

			var editor2 = new MockObjectEditor (new[] { p.Object }, new Dictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> {
				{ p.Object, new[] { childsubInfo } }
			});
			await editor2.SetValueAsync (p.Object, new ValueInfo<object> {
				Value = value,
				Source = ValueSource.Default,
				ValueDescriptor = childsubInfo
			});

			var providerMock = CreateProviderMock (value, new MockObjectEditor { Target = value });

			var vm = new ObjectPropertyViewModel (new TargetPlatform (providerMock.Object), p.Object, new[] { editor, editor2 });
			Assert.That (vm.ValueSource, Is.EqualTo (ValueSource.Unknown));
		}

		[Test]
		public async Task ClearedValueSourcesDefault ()
		{
			object value = new object();

			var p = CreatePropertyMock ("prop");

			var childsubInfo = GetTypeInfo (typeof(SubChildClass));
			var editor = new MockObjectEditor (new[] { p.Object }, new Dictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> {
				{ p.Object, new[] { childsubInfo } }
			});
			await editor.SetValueAsync (p.Object, new ValueInfo<object> {
				Value = value,
				Source = ValueSource.Local,
				ValueDescriptor = childsubInfo
			});

			var providerMock = CreateProviderMock (value, new MockObjectEditor { Target = value });

			var vm = new ObjectPropertyViewModel (new TargetPlatform (providerMock.Object), p.Object, new[] { editor });
			Assume.That (vm.ValueSource, Is.EqualTo (ValueSource.Local));

			bool changed = false;
			vm.PropertyChanged += (sender, args) => {
				if (args.PropertyName == nameof(ObjectPropertyViewModel.ValueSource))
					changed = true;
			};

			vm.Editors.Clear();
			Assert.That (vm.ValueSource, Is.EqualTo (ValueSource.Default));
			Assert.That (changed, Is.True, "PropertyChanged did not fire for ValueSource");
		}

		[Test]
		public void NullEditors ()
		{
			object value = new object();

			var p = CreatePropertyMock ("prop");
			var providerMock = CreateProviderMock (value, new MockObjectEditor { Target = value });

			var vm = new ObjectPropertyViewModel (new TargetPlatform (providerMock.Object), p.Object, new IObjectEditor[0]);
			Assume.That (vm.ValueType, Is.Null);
			Assume.That (vm.ValueSource, Is.EqualTo (ValueSource.Default));

			bool changed = false;
			vm.PropertyChanged += (sender, args) => {
				if (args.PropertyName == nameof(ObjectPropertyViewModel.ValueSource))
					changed = true;
			};

			vm.Editors.Add (null);

			Assert.That (changed, Is.False);
			Assert.That (vm.ValueSource, Is.EqualTo (ValueSource.Default));
		}

		[Test]
		public async Task CanDelve ()
		{
			object value = new object();

			var p = CreatePropertyMock ("prop");

			var childsubInfo = GetTypeInfo (typeof(SubChildClass));
			var editor = new MockObjectEditor (new[] { p.Object }, new Dictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> {
				{ p.Object, new[] { childsubInfo } }
			});
			await editor.SetValueAsync (p.Object, new ValueInfo<object> { Value = value, Source = ValueSource.Local });

			var providerMock = CreateProviderMock (value, new MockObjectEditor { Target = value });

			var vm = new ObjectPropertyViewModel (new TargetPlatform (providerMock.Object), p.Object, new IObjectEditor[0]);
			Assert.That (vm.CanDelve, Is.False);

			bool changed = false;
			vm.PropertyChanged += (sender, args) => {
				if (args.PropertyName == nameof(ObjectPropertyViewModel.CanDelve))
					changed = true;
			};

			vm.Editors.Add (editor);

			Assert.That (vm.CanDelve, Is.True);
			Assert.That (changed, Is.True, "PropertyChanged for CanDelve didn't fire adding");

			changed = false;
			vm.Editors.Clear();

			Assert.That (vm.CanDelve, Is.False);
			Assert.That (changed, Is.True, "PropertyChanged for CanDelve didn't fire clearing");

			changed = false;
			vm.Editors.Add (null);

			Assert.That (vm.CanDelve, Is.False);
			Assert.That (changed, Is.False, "PropertyChanged for CanDelve when hasn't changed");
		}

		[Test]
		[Description ("Values with multiple value types should be null")]
		public async Task MultiValueTypesNull ()
		{
			object value = new object(), value2 = new object();

			var p = CreatePropertyMock ("prop");

			var childsubInfo = GetTypeInfo (typeof(SubChildClass));
			var childInfo = GetTypeInfo (typeof(ChildClass));
			var editor = new MockObjectEditor (new[] { p.Object }, new Dictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> {
				{ p.Object, new[] { childInfo, childsubInfo } }
			});
			await editor.SetValueAsync (p.Object, new ValueInfo<object> {
				Value = value,
				Source = ValueSource.Local,
				ValueDescriptor = childInfo
			});

			var editor2 = new MockObjectEditor (new[] { p.Object }, new Dictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> {
				{ p.Object, new[] { childInfo, childsubInfo } }
			});
			await editor2.SetValueAsync (p.Object, new ValueInfo<object> {
				Value = value2,
				Source = ValueSource.Local,
				ValueDescriptor = childsubInfo
			});

			var providerMock = CreateProviderMock (value, new MockObjectEditor { Target = value });
			providerMock.Setup (a => a.GetObjectEditorAsync (value2)).ReturnsAsync (new MockObjectEditor { Target = value2 });

			var vm = new ObjectPropertyViewModel (new TargetPlatform (providerMock.Object), p.Object, new[] { editor, editor2 });
			Assume.That (vm.ValueSource, Is.EqualTo (ValueSource.Local));
			Assert.That (vm.ValueType, Is.Null);
		}

		[Test]
		public void ReturnedNullTypeCancels ()
		{
			object value = new object ();
			var p = CreatePropertyMock ("prop");
			var childsubInfo = GetTypeInfo (typeof (SubChildClass));
			var editor = new MockObjectEditor (new[] { p.Object }, new Dictionary<IPropertyInfo, IReadOnlyList<ITypeInfo>> {
				{
					p.Object,
					new[] {
						GetTypeInfo (typeof(ChildClass)),
						childsubInfo
					}
				}
			});

			var providerMock = CreateProviderMock (value, new MockObjectEditor { Target = value });
			var vm = new ObjectPropertyViewModel (new TargetPlatform (providerMock.Object), p.Object, new[] { editor });

			var tcs = new TaskCompletionSource<ITypeInfo> ();
			bool requested = false;
			vm.TypeRequested += (sender, args) => {
				requested = true;
				args.SelectedType = tcs.Task;
			};

			Assume.That (vm.CreateInstanceCommand.CanExecute (childsubInfo), Is.True);
			vm.CreateInstanceCommand.Execute (null);
			Assume.That (requested, Is.True);

			tcs.SetResult (null);

			providerMock.Verify (ep => ep.CreateObjectAsync (null), Times.Never);
		}

		private TestContext syncContext;

		private Mock<IEditorProvider> CreateProviderMock (object value, IObjectEditor editor)
		{
			var m = new Mock<IEditorProvider> ();
			m.Setup (e => e.GetObjectEditorAsync (value)).ReturnsAsync (editor);

			return m;
		}

		private Mock<IPropertyInfo> CreatePropertyMock (string name)
		{
			var m = new Mock<IPropertyInfo> ();
			m.SetupGet (p => p.Type).Returns (typeof(object));
			m.SetupGet (p => p.Name).Returns (name);

			return m;
		}

		private ITypeInfo GetTypeInfo (Type type)
		{
			var asm = new AssemblyInfo (type.Assembly.FullName, true);
			return new TypeInfo (asm, type.Namespace, type.Name);
		}

		private class SubChildClass
			: ChildClass
		{
			
		}

		private class ChildClass
		{
			
		}
	}
}