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

MockResourceProvider.cs « MockControls « Xamarin.PropertyEditing.Tests - github.com/xamarin/Xamarin.PropertyEditing.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 60e05327a58d2f782164b0c9ec0135ecb3aec243 (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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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<ResourceCreateError> 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> {
					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<Resource> CreateResourceAsync<T> (ResourceSource source, string name, T value)
		{
			var r = new Resource<T> (source, name, value);
			((ObservableLookup<ResourceSource, Resource>)this.resources).Add (source, r);
			return Task.FromResult<Resource> (r);
		}

		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]) && (!(r.Source is ObjectResourceSource ors) || ReferenceEquals (target, ors.Target)))
				.ToList());
		}

		public Task<IReadOnlyList<ResourceSource>> GetResourceSourcesAsync (object target, IPropertyInfo property)
		{
			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)
		{
			int i = 1;
			string key;
			do {
				key = property.Type.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: <no name>", ResourceSourceType.Document);

		private readonly ILookup<ResourceSource, Resource> resources = new ObservableLookup<ResourceSource, Resource> {
			new ObservableGrouping<ResourceSource, Resource> (SystemResourcesSource) {
				new Resource<CommonSolidBrush> (SystemResourcesSource, "ControlTextBrush", new CommonSolidBrush (0, 0, 0)),
				new Resource<CommonSolidBrush> (SystemResourcesSource, "HighlightBrush", new CommonSolidBrush (51, 153, 255)),
				new Resource<CommonSolidBrush> (SystemResourcesSource, "TransparentBrush", new CommonSolidBrush (0, 0, 0, 0)),
				new Resource<CommonColor> (SystemResourcesSource, "ControlTextColor", new CommonColor (0, 0, 0)),
				new Resource<CommonColor> (SystemResourcesSource, "HighlightColor", new CommonColor (51, 153, 255))
			},

			new ObservableGrouping<ResourceSource, Resource> (ApplicationResourcesSource) {
				new Resource<CommonSolidBrush> (SystemResourcesSource, "CustomHighlightBrush", new CommonSolidBrush (255, 165, 0)),
			}
		};
	}
}