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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
|
//
// AppQuery.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.Text;
using Gtk;
using MonoDevelop.Components.AutoTest.Operations;
using MonoDevelop.Components.AutoTest.Results;
using System.Linq;
using System.Xml;
using MonoDevelop.Core;
#if MAC
using AppKit;
#endif
namespace MonoDevelop.Components.AutoTest
{
public class AppQuery : MarshalByRefObject
{
AppResult rootNode;
List<Operation> operations = new List<Operation> ();
public AutoTestSessionDebug SessionDebug { get; set; }
AppResult GenerateChildrenForContainer (Gtk.Container container, List<AppResult> resultSet)
{
AppResult firstChild = null, lastChild = null;
foreach (var child in container.Children) {
AppResult node = new GtkWidgetResult (child) { SourceQuery = ToString () };
resultSet.Add (node);
// FIXME: Do we need to recreate the tree structure of the AppResults?
if (firstChild == null) {
firstChild = node;
lastChild = node;
} else {
lastChild.NextSibling = node;
node.PreviousSibling = lastChild;
lastChild = node;
}
if (child is Gtk.Container) {
AppResult children = GenerateChildrenForContainer ((Gtk.Container)child, resultSet);
node.FirstChild = children;
}
}
return firstChild;
}
#if MAC
AppResult GenerateChildrenForNSView (NSView view, List<AppResult> resultSet)
{
AppResult firstChild = null, lastChild = null;
foreach (var child in view.Subviews) {
AppResult node = new NSObjectResult (child) { SourceQuery = ToString () };
resultSet.Add (node);
if (firstChild == null) {
firstChild = node;
lastChild = node;
} else {
lastChild.NextSibling = node;
node.PreviousSibling = lastChild;
lastChild = node;
}
if (child.Subviews != null) {
AppResult children = GenerateChildrenForNSView (child, resultSet);
node.FirstChild = children;
}
}
if (view is NSSegmentedControl || view.GetType ().IsSubclassOf (typeof (NSSegmentedControl))) {
var segmentedControl = (NSSegmentedControl)view;
LoggingService.LogInfo ($"Found 'NSSegmentedControl' with {segmentedControl.SegmentCount} children");
for (int i = 0; i < segmentedControl.SegmentCount; i++) {
var node = new NSObjectResult (view, i);
resultSet.Add (node);
if (firstChild == null) {
firstChild = node;
lastChild = node;
} else {
lastChild.NextSibling = node;
node.PreviousSibling = lastChild;
lastChild = node;
}
}
}
return firstChild;
}
#endif
List<AppResult> ResultSetFromWindows ()
{
Gtk.Window[] windows = Gtk.Window.ListToplevels ();
// null for AppResult signifies root node
rootNode = new GtkWidgetResult (null) { SourceQuery = ToString () };
List<AppResult> fullResultSet = new List<AppResult> ();
// Build the tree and full result set recursively
AppResult lastChild = null;
foreach (var window in windows) {
AppResult node = new GtkWidgetResult (window) { SourceQuery = ToString () };
fullResultSet.Add (node);
if (rootNode.FirstChild == null) {
rootNode.FirstChild = node;
lastChild = node;
} else {
// Add the new node into the chain
lastChild.NextSibling = node;
node.PreviousSibling = lastChild;
lastChild = node;
}
// Create the children list and link them onto the node
AppResult children = GenerateChildrenForContainer ((Gtk.Container) window, fullResultSet);
node.FirstChild = children;
}
#if MAC
NSWindow[] nswindows = NSApplication.SharedApplication.Windows;
if (nswindows != null) {
foreach (var window in nswindows) {
AppResult node = new NSObjectResult (window) { SourceQuery = ToString () };
AppResult nsWindowLastNode = null;
fullResultSet.Add (node);
if (rootNode.FirstChild == null) {
rootNode.FirstChild = node;
lastChild = node;
} else {
lastChild.NextSibling = node;
node.PreviousSibling = lastChild;
lastChild = node;
}
foreach (var child in window.ContentView.Subviews) {
AppResult childNode = new NSObjectResult (child) { SourceQuery = ToString () };
fullResultSet.Add (childNode);
if (node.FirstChild == null) {
node.FirstChild = childNode;
nsWindowLastNode = childNode;
} else {
nsWindowLastNode.NextSibling = childNode;
childNode.PreviousSibling = nsWindowLastNode;
nsWindowLastNode = childNode;
}
if (child.Subviews != null) {
AppResult children = GenerateChildrenForNSView (child, fullResultSet);
childNode.FirstChild = children;
}
}
NSToolbar toolbar = window.Toolbar;
AppResult toolbarNode = new NSObjectResult (toolbar) { SourceQuery = ToString () };
if (node.FirstChild == null) {
node.FirstChild = toolbarNode;
nsWindowLastNode = toolbarNode;
} else {
nsWindowLastNode.NextSibling = toolbarNode;
toolbarNode.PreviousSibling = nsWindowLastNode;
nsWindowLastNode = toolbarNode;
}
if (toolbar != null) {
AppResult lastItemNode = null;
foreach (var item in toolbar.Items) {
if (item.View != null) {
AppResult itemNode = new NSObjectResult (item.View) { SourceQuery = ToString () };
fullResultSet.Add (itemNode);
if (toolbarNode.FirstChild == null) {
toolbarNode.FirstChild = itemNode;
lastItemNode = itemNode;
} else {
lastItemNode.NextSibling = itemNode;
itemNode.PreviousSibling = lastItemNode;
lastItemNode = itemNode;
}
if (item.View.Subviews != null) {
AppResult children = GenerateChildrenForNSView (item.View, fullResultSet);
itemNode.FirstChild = children;
}
}
}
}
}
}
#endif
return fullResultSet;
}
public AppQuery ()
{
}
public AppResult[] Execute ()
{
List<AppResult> resultSet = ResultSetFromWindows ();
foreach (var subquery in operations) {
// Some subqueries can select different results
resultSet = subquery.Execute (resultSet);
if (resultSet == null || resultSet.Count == 0) {
return new AppResult[0];
}
}
AppResult[] results = new AppResult[resultSet.Count];
resultSet.CopyTo (results);
return results;
}
public AppQuery Marked (string mark)
{
operations.Add (new MarkedOperation (mark));
return this;
}
public AppQuery CheckType (Type desiredType, string name = null)
{
operations.Add (new TypeOperation (desiredType, name));
return this;
}
public AppQuery Button ()
{
return CheckType (typeof(Gtk.Button), "Button");
}
public AppQuery Textfield ()
{
return CheckType (typeof(Gtk.Entry), "Textfield");
}
public AppQuery CheckButton ()
{
return CheckType (typeof(Gtk.CheckButton), "CheckButton");
}
public AppQuery RadioButton ()
{
return CheckType (typeof(Gtk.RadioButton), "RadioButton");
}
public AppQuery TreeView ()
{
return CheckType (typeof(Gtk.TreeView), "TreeView");
}
public AppQuery Window ()
{
return CheckType (typeof(Gtk.Window), "Window");
}
public AppQuery TextView ()
{
return CheckType (typeof(Gtk.TextView), "TextView");
}
public AppQuery Notebook ()
{
return CheckType (typeof(Gtk.Notebook), "Notebook");
}
public AppQuery Text (string text)
{
operations.Add (new TextOperation (text));
return this;
}
public AppQuery Contains (string text)
{
operations.Add (new TextOperation (text, false));
return this;
}
public AppQuery Selected ()
{
operations.Add (new SelectedOperation ());
return this;
}
public AppQuery Model (string column = null)
{
operations.Add (new ModelOperation (column));
return this;
}
public AppQuery Sensitivity (bool sensitivity)
{
operations.Add (new PropertyOperation ("Sensitive", sensitivity));
return this;
}
public AppQuery Visibility (bool visibility)
{
operations.Add (new PropertyOperation ("Visible", visibility));
return this;
}
public AppQuery Property (string propertyName, object desiredValue)
{
operations.Add (new PropertyOperation (propertyName, desiredValue));
return this;
}
public AppQuery Toggled (bool toggled)
{
operations.Add (new PropertyOperation ("Active", toggled));
return this;
}
public AppQuery NextSiblings ()
{
operations.Add (new NextSiblingsOperation ());
return this;
}
public AppQuery Index (int index)
{
operations.Add (new IndexOperation (index));
return this;
}
public AppQuery Children (bool recursive = true)
{
operations.Add (new ChildrenOperation (recursive));
return this;
}
public override string ToString ()
{
var operationChain = string.Join (".", operations.Select (x => x.ToString ()));
return string.Format ("c => c.{0};", operationChain);
}
}
}
|