using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Xamarin.PropertyEditing.Drawing; namespace Xamarin.PropertyEditing.Tests { public class MockResourceProvider : IResourceProvider { public bool CanCreateResources => true; public Task CheckNameErrorsAsync (object target, ResourceSource source, string name) { ResourceCreateError error = null; if (this.resources[source].Any (r => r.Name == name)) { error = new ResourceCreateError ("Name in use", isWarning: false); } else { var order = new List { ResourceSourceType.Document, ResourceSourceType.ResourceDictionary, ResourceSourceType.Application, ResourceSourceType.System, }; // Simplistic example of hierarchy override checking for (int i = order.IndexOf (source.Type)+1; i < order.Count; i++) { if (this.resources.Where (ig => ig.Key.Type == order[i]).SelectMany (ig => ig).Any (r => r.Name == name)) { error = new ResourceCreateError ("Resource would override another resource", isWarning: true); break; } } } return Task.FromResult (error); } public Task CreateResourceAsync (ResourceSource source, string name, T value) { var r = new Resource (source, name, value); ((ObservableLookup)this.resources).Add (source, r); return Task.FromResult (r); } public Task> GetResourcesAsync (object target, CancellationToken cancelToken) { return Task.FromResult> (this.resources.SelectMany (g => g) .Where (r => !(r.Source is ObjectResourceSource ors) || ReferenceEquals (target, ors.Target)) .ToList ()); } public Task> GetResourcesAsync (object target, IPropertyInfo property, CancellationToken cancelToken) { return Task.FromResult> (this.resources.SelectMany (g => g) .Where (r => property.Type.IsAssignableFrom (r.GetType().GetGenericArguments()[0]) && (!(r.Source is ObjectResourceSource ors) || ReferenceEquals (target, ors.Target))) .ToList()); } Task> IResourceProvider.GetResourceSourcesAsync (object target) { return MockResourceProvider.GetResourceSourcesAsync (target); } public Task> GetResourceSourcesAsync (object target, IPropertyInfo property) { return GetResourceSourcesAsync (target); } public static Task> GetResourceSourcesAsync (object target) { return Task.FromResult> (new[] { SystemResourcesSource, ApplicationResourcesSource, Resources, Window, new ObjectResourceSource (target, target.GetType ().Name, ResourceSourceType.Document) }); } public Task SuggestResourceNameAsync (IReadOnlyCollection targets, IPropertyInfo property) { return SuggestResourceNameAsync (targets, property.RealType); } public Task SuggestResourceNameAsync (IReadOnlyCollection targets, ITypeInfo resourceType) { int i = 1; string key; do { key = resourceType.Name + i++; } while (this.resources[ApplicationResourcesSource].Any (r => r.Name == key)); return Task.FromResult (key); } private class ObjectResourceSource : ResourceSource { public ObjectResourceSource (object target, string name, ResourceSourceType type) : base (name, type) { if (target == null) throw new ArgumentNullException (nameof(target)); this.target = target; } public object Target => this.target; public override int GetHashCode () { int hashCode = base.GetHashCode (); unchecked { hashCode = (hashCode * 397) ^ this.target.GetHashCode(); } return hashCode; } public override bool Equals (ResourceSource other) { if (!base.Equals (other)) return false; return (other is ObjectResourceSource ors && ReferenceEquals (ors.target, this.target)); } private readonly object target; } internal static readonly ResourceSource SystemResourcesSource = new ResourceSource ("System Resources", ResourceSourceType.System); internal static readonly ResourceSource ApplicationResourcesSource = new ResourceSource ("App resources", ResourceSourceType.Application); private static readonly ResourceSource Resources = new ResourceSource ("Resources.xaml", ResourceSourceType.ResourceDictionary); private static readonly ResourceSource Window = new ResourceSource ("Window: ", ResourceSourceType.Document); private readonly ILookup resources = new ObservableLookup { new ObservableGrouping (SystemResourcesSource) { new Resource (SystemResourcesSource, "ControlTextBrush", new CommonSolidBrush (0, 0, 0)), new Resource (SystemResourcesSource, "HighlightBrush", new CommonSolidBrush (51, 153, 255)), new Resource (SystemResourcesSource, "TransparentBrush", new CommonSolidBrush (0, 0, 0, 0)), new Resource (SystemResourcesSource, "ATextBrush", new CommonSolidBrush (0, 0, 0)), new Resource (SystemResourcesSource, "ATransparentBrush", new CommonSolidBrush (51, 153, 255)), new Resource (SystemResourcesSource, "AHighlightBrush", new CommonSolidBrush (0, 0, 0, 0)), new Resource (SystemResourcesSource, "BTextBrush", new CommonSolidBrush (0, 0, 0)), new Resource (SystemResourcesSource, "BHighlightBrush", new CommonSolidBrush (51, 153, 255)), new Resource (SystemResourcesSource, "BTransparentBrush", new CommonSolidBrush (0, 0, 0, 0)), new Resource (SystemResourcesSource, "CTextBrush", new CommonSolidBrush (0, 0, 0)), new Resource (SystemResourcesSource, "CHighlightBrush", new CommonSolidBrush (51, 153, 255)), new Resource (SystemResourcesSource, "CTransparentBrush", new CommonSolidBrush (0, 0, 0, 0)), new Resource (SystemResourcesSource, "ControlTextColor", new CommonColor (0, 0, 0)), new Resource (SystemResourcesSource, "HighlightColor", new CommonColor (51, 153, 255)) }, new ObservableGrouping (ApplicationResourcesSource) { new Resource (SystemResourcesSource, "CustomHighlightBrush", new CommonSolidBrush (255, 165, 0)), } }; } }