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 KnownTypes { get; } = new Dictionary { { 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 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> GetPropertiesForTypeAsync (ITypeInfo type) { Type realType = ReflectionEditorProvider.GetRealType (type); if (realType == null) return Array.Empty (); if (typeof(MockControl).IsAssignableFrom (realType)) { object item = await CreateObjectAsync (type); IObjectEditor editor = ChooseEditor (item); return editor.Properties; } return ReflectionEditorProvider.GetPropertiesForType (realType); } public Task 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 CreateObjectAsync (ITypeInfo type) { Type realType = Type.GetType ($"{type.NameSpace}.{type.Name}, {type.Assembly.Name}"); if (realType == null) return Task.FromResult (null); return Task.FromResult (Activator.CreateInstance (realType)); } public Task> GetChildrenAsync (object item) { return Task.FromResult> (Array.Empty ()); } public Task> GetKnownTypesAsync (IReadOnlyCollection knownTypes) { return Task.FromResult> (new Dictionary ()); } private readonly IResourceProvider resources; private readonly Dictionary editorCache = new Dictionary (); } }