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

AppResult.cs « MonoDevelop.Components.AutoTest « MonoDevelop.Ide « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 925b3eead2a644690f601daf078dec932b94bca8 (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
//
// AppResult.cs
//
// Author:
//       iain holmes <iain@xamarin.com>
//
// Copyright (c) 2015 Xamarin, Inc
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Xml;
using System.Reflection;
using System.Linq;
using System.Collections.ObjectModel;
using MonoDevelop.Components.AutoTest.Results;

namespace MonoDevelop.Components.AutoTest
{
	public abstract class AppResult : MarshalByRefObject
	{
		//public Gtk.Widget ResultWidget { get; private set; }

		public AppResult ParentNode { get; set; }
		public AppResult FirstChild { get; set; }
		public AppResult PreviousSibling { get; set; }
		public AppResult NextSibling { get; set; }

		public virtual void ToXml (XmlElement element)
		{
		}

		// Operations
		public abstract AppResult Marked (string mark);
		public abstract AppResult CheckType (Type desiredType);
		public abstract AppResult Selected ();
		public abstract AppResult Text (string text, bool exact);
		public abstract AppResult Model (string column);
		public abstract AppResult Property (string propertyName, object value);
		public abstract List<AppResult> NextSiblings ();

		// Actions
		public abstract bool Select ();
		public abstract bool Click ();
		public abstract bool TypeKey (char key, string state = "");
		public abstract bool TypeKey (string keyString, string state = "");
		public abstract bool EnterText (string text);
		public abstract bool Toggle (bool active);

		public abstract void Flash ();

		// Inspection Operations
		public abstract ObjectProperties Properties ();
		public abstract string GetResultType  ();

		public string SourceQuery { get; set; }

		void AddChildrenToList (List<AppResult> children, AppResult child, bool recursive = true)
		{
			AppResult node = child.FirstChild;
			children.Add (child);

			while (node != null) {
				if (recursive)
					AddChildrenToList (children, node);
				node = node.NextSibling;
			}
		}

		public virtual List<AppResult> Children (bool recursive = true)
		{
			List<AppResult> children = new List<AppResult> ();
			AddChildrenToList (children, FirstChild, recursive);

			return children;
		}

		/// <summary>
		/// Convenience function to add an attribute to an element
		/// </summary>
		/// <param name="element">The element to add the attribute</param>
		/// <param name="name">The name of the attribute</param>
		/// <param name="value">The value of the attribute</param>
		protected void AddAttribute (XmlElement element, string name, string value)
		{
			XmlDocument doc = element.OwnerDocument;
			XmlAttribute attr = doc.CreateAttribute (name);
			attr.Value = value;
			element.Attributes.Append (attr);
		}

		protected object GetPropertyValue (string propertyName, object requestedObject)
		{
			return AutoTestService.CurrentSession.UnsafeSync (delegate {
				PropertyInfo propertyInfo = requestedObject.GetType().GetProperty(propertyName,
					BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic);
				if (propertyInfo != null && propertyInfo.CanRead && !propertyInfo.GetIndexParameters ().Any ()) {
					var propertyValue = propertyInfo.GetValue (requestedObject);
					if (propertyValue != null) {
						return propertyValue;
					}
				}

				return null;
			});
		}

		protected ObjectProperties GetProperties (object resultObject)
		{
			var propertiesObject = new ObjectProperties ();
			if (resultObject != null) {
				var properties = resultObject.GetType ().GetProperties (
					BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
				foreach (var property in properties) {
					try {
						var value = GetPropertyValue (property.Name, resultObject);
						AppResult result = null;

						var gtkNotebookValue = value as Gtk.Notebook;
						if (gtkNotebookValue != null)
							result = new GtkNotebookResult (gtkNotebookValue);
						var gtkTreeviewValue = value as Gtk.TreeView;
						if (gtkTreeviewValue != null && result == null)
							result = new GtkTreeModelResult (gtkTreeviewValue, gtkTreeviewValue.Model, 0);
						var gtkWidgetValue = value as Gtk.Widget;
						if (gtkWidgetValue != null && result == null)
							result = new GtkWidgetResult (gtkWidgetValue);
						#if MAC
						var nsObjectValue = value as Foundation.NSObject;
						if (nsObjectValue != null && result == null)
							result = new NSObjectResult (nsObjectValue);
						#endif
						if (result == null)
							result = new ObjectResult (value);
						propertiesObject.Add (property.Name, result, property);
					} catch (Exception e) {
						MonoDevelop.Core.LoggingService.LogInfo ("Failed to fetch property '{0}' on '{1}' with Exception: {2}", property, resultObject, e);
					}
				}
			}

			return propertiesObject;
		}

		protected AppResult MatchProperty (string propertyName, object objectToCompare, object value)
		{
			foreach (var singleProperty in propertyName.Split (new [] { '.' })) {
				objectToCompare = GetPropertyValue (singleProperty, objectToCompare);
			}
			if (objectToCompare != null && value != null &&
				CheckForText (objectToCompare.ToString (), value.ToString (), false)) {
				return this;
			}
			return null;
		}

		protected bool CheckForText (string haystack, string needle, bool exact)
		{
			if (exact) {
				return haystack == needle;
			} else {
				return (haystack.IndexOf (needle, StringComparison.Ordinal) > -1);
			}
		}
	}
}