diff options
author | Eric Maupin <ermaup@microsoft.com> | 2018-05-12 01:50:45 +0300 |
---|---|---|
committer | Eric Maupin <ermaup@microsoft.com> | 2018-05-12 02:02:05 +0300 |
commit | 16296387c97379de890db98ebc88d98350a0da3c (patch) | |
tree | 8c18ccc49af6be8354bb1332c8662a91fe1d7935 /Xamarin.PropertyEditing.Tests | |
parent | 4ffdac2d7a17ecfa32b307ac5b4c4ed899ad2bba (diff) |
[Tests] MockResourceProvider: Support object-local sources
Diffstat (limited to 'Xamarin.PropertyEditing.Tests')
-rw-r--r-- | Xamarin.PropertyEditing.Tests/MockControls/MockResourceProvider.cs | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/Xamarin.PropertyEditing.Tests/MockControls/MockResourceProvider.cs b/Xamarin.PropertyEditing.Tests/MockControls/MockResourceProvider.cs index 3cee432..6ed409f 100644 --- a/Xamarin.PropertyEditing.Tests/MockControls/MockResourceProvider.cs +++ b/Xamarin.PropertyEditing.Tests/MockControls/MockResourceProvider.cs @@ -47,12 +47,14 @@ namespace Xamarin.PropertyEditing.Tests public Task<IReadOnlyList<Resource>> GetResourcesAsync (object target, IPropertyInfo property, CancellationToken cancelToken) { - return Task.FromResult<IReadOnlyList<Resource>> (this.resources.SelectMany (g => g).Where (r => property.Type.IsAssignableFrom (r.GetType().GetGenericArguments()[0])).ToList()); + return Task.FromResult<IReadOnlyList<Resource>> (this.resources.SelectMany (g => g) + .Where (r => property.Type.IsAssignableFrom (r.GetType().GetGenericArguments()[0]) && (!(r.Source is ObjectResourceSource ors) || ReferenceEquals (target, ors.Target))) + .ToList()); } public Task<IReadOnlyList<ResourceSource>> GetResourceSourcesAsync (object target, IPropertyInfo property) { - return Task.FromResult<IReadOnlyList<ResourceSource>> (this.resources.Select (g => g.Key).ToArray ()); + return Task.FromResult<IReadOnlyList<ResourceSource>> (new[] { SystemResourcesSource, ApplicationResourcesSource, Resources, Window, new ObjectResourceSource (target, target.GetType ().Name, ResourceSourceType.Document) }); } public Task<string> SuggestResourceNameAsync (IReadOnlyCollection<object> targets, IPropertyInfo property) @@ -66,8 +68,45 @@ namespace Xamarin.PropertyEditing.Tests 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); private 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: <no name>", ResourceSourceType.Document); private readonly ILookup<ResourceSource, Resource> resources = new ObservableLookup<ResourceSource, Resource> { new ObservableGrouping<ResourceSource, Resource> (SystemResourcesSource) { |