summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core.iOS.UITests/Remotes/BaseViewContainerRemote.cs
blob: f51782ce55eda5c4b474ac15d71a0a25e0e132be (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.Remoting.Messaging;
using System.Security.Permissions;

using NUnit.Framework;

using Xamarin.UITest;
using Xamarin.UITest.Queries;
using Xamarin.UITest.Android;
using Xamarin.UITest.iOS;
using System.Globalization;


namespace Xamarin.Forms.Core.UITests
{
	internal abstract class BaseViewContainerRemote
	{
		bool requiresDismissal;

		protected IApp App { get; private set; }

		public string ViewQuery { get; private set; }

		public string PlatformViewType { get; set; }

		public string ContainerQuery { get; private set; }
		public string ContainerDescendents { get; private set; }

		public string EventLabelQuery { get; set; }
		
		public string StateLabelQuery { get; private set; }
		public string StateButtonQuery { get; private set; }
			   
		public string LayeredHiddenButtonQuery { get; private set; }
		public string LayeredLabelQuery { get; private set; }

		protected BaseViewContainerRemote (IApp app, Enum formsType, string platformViewType)
		{
			App = app;
			PlatformViewType = platformViewType;

			// Currently tests are failing because the ViewInitilized is setting the renderer and control, fix and then remove index one

			ContainerQuery = string.Format("* marked:'{0}Container'", formsType);
			ContainerDescendents = string.Format("* marked:'{0}Container' child *", formsType);

			ViewQuery = string.Format ("* marked:'{0}VisualElement'", formsType);

			EventLabelQuery =  string.Format ("* marked:'{0}EventLabel'", formsType);
			StateLabelQuery = string.Format ("* marked:'{0}StateLabel'", formsType);
			StateButtonQuery =  string.Format ("* marked:'{0}StateButton'", formsType);
			LayeredHiddenButtonQuery = string.Format ("* marked:'{0}LayeredHiddenButton'", formsType);
			LayeredLabelQuery = string.Format ("* marked:'{0}LayeredLabel'", formsType);

			if (platformViewType == PlatformViews.DatePicker) {
				requiresDismissal = true;
			}			
		}

		public virtual void GoTo ([CallerMemberName] string callerMemberName = "")
		{
			var scrollBounds = App.Query (Queries.PageWithoutNavigationBar ()).First ().Rect;

			// Scroll using gutter to the right of view, avoid scrolling inside of WebView
			if (PlatformViewType == PlatformViews.WebView) {
				scrollBounds = new AppRect { 
					X = scrollBounds.Width - 20,
					CenterX = scrollBounds.Width - 10,
					Y = scrollBounds.Y,
					CenterY = scrollBounds.CenterY,
					Width = 20,
					Height = scrollBounds.Height,
				};
			}


			while (true) {
				var result = App.Query (o => o.Raw(ContainerQuery));
				if (result.Any ())
					break;
				App.Tap (o => o.Raw ("* marked:'MoveNextButton'"));
			}

			//Assert.True (App.ScrollForElement (
			//	ContainerQuery, new Drag (scrollBounds, Drag.Direction.BottomToTop, Drag.DragLength.Medium)
			//), "Failed to find element in: " + callerMemberName);

			App.Screenshot ("Go to element");
		}

		public void TapView ()
		{
			App.Tap (q => q.Raw (ViewQuery));
		}

		public void DismissPopOver ()
		{
			App.Screenshot ("About to dismiss pop over");
			App.Tap (q => q.Button ("Done"));
			App.Screenshot ("Pop over dismissed");
		}

		public AppResult GetView ()
		{
			return App.Query (q => q.Raw (ViewQuery)).First ();
		}

		public AppResult[] GetViews ()
		{
			return App.Query (q => q.Raw (ViewQuery));
		}

		public AppResult[] GetContainerDescendants ()
		{
			return App.Query (q => q.Raw (ContainerDescendents));
		}

		public T GetProperty<T> (BindableProperty formProperty)
		{
			Tuple<string[], bool> property = formProperty.GetPlatformPropertyQuery ();
		    string[] propertyPath = property.Item1;
			bool isOnParentRenderer = property.Item2;

			var query = ViewQuery;
			if (isOnParentRenderer && 
				PlatformViewType != PlatformViews.BoxView && 
				PlatformViewType != PlatformViews.Frame) {
				query = query + " parent * index:0";
			}

			object prop = null;
			bool found = false;

			bool isEdgeCase = false;
			if (App is AndroidApp && formProperty == View.ScaleProperty)
				isEdgeCase = true;
		
		    if (!isEdgeCase) {
			    found =
					MaybeGetProperty<string> (App, query, propertyPath, out prop) ||
					MaybeGetProperty<float> (App, query, propertyPath, out prop) ||
					MaybeGetProperty<bool> (App, query, propertyPath, out prop) ||
					MaybeGetProperty<object> (App, query, propertyPath, out prop);
		    }

			
			if (App is AndroidApp && formProperty == View.ScaleProperty) {
				var matrix = new Matrix ();
				matrix.M00 = App.Query (q => q.Raw (query).Invoke (propertyPath[0]).Value<float> ()).First ();
				matrix.M11 = App.Query (q => q.Raw (query).Invoke (propertyPath[1]).Value<float> ()).First ();
				matrix.M22 = 0.5f;
				matrix.M33 = 1.0f;
				return (T)((object)matrix);
			}

			if (!found || prop == null) {
				throw new NullReferenceException ("null property");
			}

			if (prop.GetType () == typeof(T))
				return (T)prop;

			if (prop.GetType () == typeof(string) && typeof(T) == typeof(Matrix)) {
				Matrix matrix = ParsingUtils.ParseCATransform3D ((string)prop);
				return (T)((object)matrix);
			}

			if (typeof(T) == typeof(Color)) {
				if (App is iOSApp) {
					Color color = ParsingUtils.ParseUIColor ((string)prop);
					return (T)((object)color);
				} else {
					uint intColor = (uint)((float)prop);
					Color color = Color.FromUint (intColor);
					return (T)((object)color);
				}
			}

			if (prop.GetType () == typeof (string) && typeof(T) == typeof(Font)) {
				if (App is iOSApp) {
					Font font = ParsingUtils.ParseUIFont ((string)prop);
					return (T)((object)font);
				} else {
					
				}
			}

			T result = default(T);

			var stringToBoolConverter = new StringToBoolConverter ();
			var floatToBoolConverter = new FloatToBoolConverter ();

			if (stringToBoolConverter.CanConvertTo (prop, typeof(bool))) {
				result = (T)stringToBoolConverter.ConvertTo (prop, typeof(bool));
			} else if (floatToBoolConverter.CanConvertTo (prop, typeof(bool))) {
				result = (T)floatToBoolConverter.ConvertTo (prop, typeof(bool));
			}
			
			return result;
		}

		static bool MaybeGetProperty<T>(IApp app, string query, string[] propertyPath, out object result)
		{

			try {
				switch (propertyPath.Length){
				case 1:
					result = app.Query (q => q.Raw (query).Invoke (propertyPath[0]).Value<T> ()).First ();
					break;
				case 2:
					result = app.Query (q => q.Raw (query).Invoke (propertyPath[0]).Invoke (propertyPath[1]).Value<T> ()).First ();
					break;
				case 3:
					result = app.Query (q => q.Raw (query).Invoke (propertyPath[0]).Invoke (propertyPath[1]).Invoke (propertyPath[2]).Value<T> ()).First ();
					break;
				case 4:
					result = app.Query (q => q.Raw (query).Invoke (propertyPath[0]).Invoke (propertyPath[1]).Invoke (propertyPath[2]).Invoke(propertyPath[3]).Value<T> ()).First ();
					break;
				default:
					result = null;
					return false;
				}
			}
			catch {
				result = null;
				return false;
			}

			return true;
		}

	}

	internal class StringToBoolConverter : TypeConverter
	{
		public override bool CanConvertTo (object source, Type targetType)
		{
			if (targetType != typeof(bool) || !(source is string))
				return false;

			var str = (string)source;
			str = str.ToLowerInvariant ();

			switch (str) {
				case "0":
				case "1":
				case "false":
				case "true":
					return true;
				default:
					return false;
			}
		}

		public override object ConvertTo (object source, Type targetType)
		{
			var str = (string)source;
			str = str.ToLowerInvariant();

			switch (str)
			{
				case "1":
				case "true":
					return true;
				default:
					return false;
			}
		}
	}

	internal class FloatToBoolConverter : TypeConverter
	{
		public override bool CanConvertTo (object source, Type targetType)
		{
			if (targetType != typeof(bool) || !(source is float))
				return false;

			var flt = (float)source;
			var epsilon = 0.0001;
			if (Math.Abs (flt - 1.0f) < epsilon || Math.Abs (flt - 0.0f) < epsilon)
				return true;
			else
				return false;
		}

		public override object ConvertTo (object source, Type targetType)
		{
			var flt = (float)source;
			var epsilon = 0.0001;
			if (Math.Abs (flt - 1.0f) < epsilon)
				return true;
			else
				return false;
		}
	}

	internal abstract class TypeConverter
	{
		public abstract bool CanConvertTo (object source, Type targetType);

		public abstract object ConvertTo (object source, Type targetType);
	}

    internal static class PlatformMethods
    {
        public static Tuple<string[], bool> GetPlatformPropertyQuery (this BindableProperty bindableProperty)
        {
            return PlatformMethodQueries.PropertyPlatformMethodDictionary[bindableProperty];
        }
    }
}