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

MockEditorProvider.cs « Xamarin.PropertyEditing.Tests - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5aed81ac5c234371b1e122859152abe04217e150 (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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.PropertyEditing.Reflection;
using Xamarin.PropertyEditing.Tests.MockControls;

namespace Xamarin.PropertyEditing.Tests
{
	public class MockEditorProvider
		: IEditorProvider
	{
		public static readonly TargetPlatform MockPlatform = new TargetPlatform (new MockEditorProvider ());

		public Task<IObjectEditor> GetObjectEditorAsync (object item)
		{
			if (this.editorCache.TryGetValue (item, out IObjectEditor cachedEditor)) {
				return Task.FromResult (cachedEditor);
			}
			IObjectEditor editor = ChooseEditor (item);
			this.editorCache.Add (item, editor);
			return Task.FromResult (editor);
		}

		public async Task<IReadOnlyCollection<IPropertyInfo>> GetPropertiesForTypeAsync (ITypeInfo type)
		{
			object obj = await CreateObjectAsync (type).ConfigureAwait (false);
			IObjectEditor editor = ChooseEditor (obj);
			return editor.Properties;
		}

		IObjectEditor ChooseEditor (object item)
		{
			switch (item) {
			case MockWpfControl msc:
				return new MockObjectEditor (msc);
			case MockControl mc:
				return new MockNameableEditor (mc);
			default:
				return new ReflectionObjectEditor (item);
			}
		}

		public Task<object> CreateObjectAsync (ITypeInfo type)
		{
			Type realType = Type.GetType ($"{type.NameSpace}.{type.Name}, {type.Assembly.Name}");
			if (realType == null)
				return Task.FromResult<object> (null);

			return Task.FromResult (Activator.CreateInstance (realType));
		}

		public Task<IReadOnlyDictionary<Type, ITypeInfo>> GetKnownTypesAsync (IReadOnlyCollection<Type> knownTypes)
		{
			return Task.FromResult<IReadOnlyDictionary<Type, ITypeInfo>> (new Dictionary<Type, ITypeInfo> ());
		}


		private readonly Dictionary<object, IObjectEditor> editorCache = new Dictionary<object, IObjectEditor> ();
	}
}