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
|
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xamarin.PropertyEditing.Common;
using Xamarin.PropertyEditing.Drawing;
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 MockEditorProvider (IResourceProvider resources = null)
{
this.resources = resources;
}
public MockEditorProvider (IObjectEditor editor)
{
this.editorCache.Add (editor.Target, editor);
}
public IReadOnlyDictionary<Type, ITypeInfo> KnownTypes
{
get;
} = new Dictionary<Type, ITypeInfo> {
{ typeof(PropertyBinding), typeof(MockBinding).ToTypeInfo() },
{ typeof(CommonValueConverter), typeof(MockValueConverter).ToTypeInfo() },
{ typeof(CommonBrush), typeof(CommonBrush).ToTypeInfo() },
{ typeof(CommonSolidBrush), typeof(CommonSolidBrush).ToTypeInfo() },
{ typeof(CommonColor), typeof(CommonColor).ToTypeInfo() }
};
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)
{
Type realType = ReflectionEditorProvider.GetRealType (type);
if (realType == null)
return Array.Empty<IPropertyInfo> ();
if (typeof(MockControl).IsAssignableFrom (realType)) {
object item = await CreateObjectAsync (type);
IObjectEditor editor = ChooseEditor (item);
return editor.Properties;
}
return ReflectionEditorProvider.GetPropertiesForType (realType);
}
public Task<AssignableTypesResult> GetAssignableTypesAsync (ITypeInfo type, bool childTypes)
{
if (type == KnownTypes[typeof(CommonValueConverter)])
return Task.FromResult (new AssignableTypesResult (new[] { type }));
return ReflectionObjectEditor.GetAssignableTypes (type, childTypes);
}
IObjectEditor ChooseEditor (object item)
{
switch (item) {
case MockWpfControl msc:
return new MockObjectEditor (msc) { Resources = this.resources };
case MockControl mc:
return new MockNameableEditor (mc) { Resources = this.resources };
case MockBinding mb:
return new MockBindingEditor (mb);
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<IReadOnlyList<object>> GetChildrenAsync (object item)
{
return Task.FromResult<IReadOnlyList<object>> (Array.Empty<object> ());
}
public Task<IReadOnlyDictionary<Type, ITypeInfo>> GetKnownTypesAsync (IReadOnlyCollection<Type> knownTypes)
{
return Task.FromResult<IReadOnlyDictionary<Type, ITypeInfo>> (new Dictionary<Type, ITypeInfo> ());
}
private readonly IResourceProvider resources;
private readonly Dictionary<object, IObjectEditor> editorCache = new Dictionary<object, IObjectEditor> ();
}
}
|