/
Mouse.cs
395 lines (394 loc) · 14.7 KB
/
Mouse.cs
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
* Cobra WinLDTP 4.0
*
* Author: Nagappan Alagappan <nalagappan@vmware.com>
* Copyright: Copyright (c) 2011-13 VMware, Inc. All Rights Reserved.
* License: MIT license
*
* http://ldtp.freedesktop.org
*
* 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 ATGTestInput;
using System.Windows;
using System.Collections;
using CookComputing.XmlRpc;
using System.Windows.Forms;
using System.Windows.Automation;
namespace Ldtpd
{
class Mouse
{
Utils utils;
public Mouse(Utils utils)
{
this.utils = utils;
}
private void LogMessage(Object o)
{
utils.LogMessage(o);
}
public int MouseLeftClick(String windowName, String objName)
{
Object pattern = null;
AutomationElement childHandle;
try
{
childHandle = utils.GetObjectHandle(windowName, objName);
if (!utils.IsEnabled(childHandle))
{
throw new XmlRpcFaultException(123,
"Object state is disabled");
}
try
{
childHandle.SetFocus();
}
catch (Exception ex)
{
// Have noticed exception with
// maximize / minimize button
LogMessage(ex);
}
if (childHandle.Current.ControlType == ControlType.Pane)
{
// NOTE: Work around, as the pane doesn't seem to work
// with any actions. Noticed this window, when Windows
// Security Warning dialog pop's up
utils.InternalClick(childHandle);
return 1;
}
else if (childHandle.TryGetCurrentPattern(InvokePattern.Pattern,
out pattern))
{
if (childHandle.Current.ControlType == ControlType.Menu ||
childHandle.Current.ControlType == ControlType.MenuBar ||
childHandle.Current.ControlType == ControlType.MenuItem ||
childHandle.Current.ControlType == ControlType.ListItem)
{
//((InvokePattern)invokePattern).Invoke();
// NOTE: Work around, as the above doesn't seem to work
// with UIAComWrapper and UIAComWrapper is required
// to Edit value in Spin control
utils.InternalClick(childHandle);
}
else
{
try
{
((InvokePattern)pattern).Invoke();
}
catch (Exception ex)
{
LogMessage(ex);
// Have noticed exception with
// maximize / minimize button
utils.InternalClick(childHandle);
}
}
return 1;
}
else if (childHandle.TryGetCurrentPattern(SelectionItemPattern.Pattern,
out pattern))
{
((SelectionItemPattern)pattern).Select();
return 1;
}
else
{
utils.InternalClick(childHandle);
return 1;
}
}
catch (Exception ex)
{
LogMessage(ex);
if (ex is XmlRpcFaultException)
throw;
else
throw new XmlRpcFaultException(123,
"Unhandled exception: " + ex.Message);
}
finally
{
pattern = null;
childHandle = null;
}
throw new XmlRpcFaultException(123, "Unable to perform action");
}
public int MouseRightClick(String windowName, String objName)
{
AutomationElement childHandle;
try
{
childHandle = utils.GetObjectHandle(windowName, objName);
if (!utils.IsEnabled(childHandle))
{
throw new XmlRpcFaultException(123,
"Object state is disabled");
}
try
{
childHandle.SetFocus();
}
catch (Exception ex)
{
// Have noticed exception with
// maximize / minimize button
LogMessage(ex);
}
Rect rect = childHandle.Current.BoundingRectangle;
GenerateMouseEvent((int)(rect.X + rect.Width / 2),
(int)(rect.Y + rect.Height / 2), "b3c");
return 1;
}
catch (Exception ex)
{
LogMessage(ex);
if (ex is XmlRpcFaultException)
throw;
else
throw new XmlRpcFaultException(123,
"Unhandled exception: " + ex.Message);
}
finally
{
childHandle = null;
}
throw new XmlRpcFaultException(123, "Unable to perform action");
}
public int DoubleClick(String windowName, String objName)
{
AutomationElement childHandle;
try
{
childHandle = utils.GetObjectHandle(windowName, objName);
if (!utils.IsEnabled(childHandle))
{
throw new XmlRpcFaultException(123,
"Object state is disabled");
}
try
{
childHandle.SetFocus();
}
catch (Exception ex)
{
// Have noticed exception with
// maximize / minimize button
LogMessage(ex);
}
Rect rect = childHandle.Current.BoundingRectangle;
GenerateMouseEvent((int)(rect.X + rect.Width / 2),
(int)(rect.Y + rect.Height / 2), "b1d");
return 1;
}
catch (Exception ex)
{
LogMessage(ex);
if (ex is XmlRpcFaultException)
throw;
else
throw new XmlRpcFaultException(123,
"Unhandled exception: " + ex.Message);
}
finally
{
childHandle = null;
}
throw new XmlRpcFaultException(123, "Unable to perform action");
}
public int MouseMove(String windowName, String objName)
{
AutomationElement childHandle;
try
{
childHandle = utils.GetObjectHandle(windowName, objName);
if (!utils.IsEnabled(childHandle))
{
throw new XmlRpcFaultException(123,
"Object state is disabled");
}
try
{
childHandle.SetFocus();
}
catch (Exception ex)
{
// Have noticed exception with
// maximize / minimize button
LogMessage(ex);
}
Rect rect = childHandle.Current.BoundingRectangle;
GenerateMouseEvent((int)(rect.X + rect.Width / 2),
(int)(rect.Y + rect.Height / 2), "abs");
return 1;
}
catch (Exception ex)
{
LogMessage(ex);
if (ex is XmlRpcFaultException)
throw;
else
throw new XmlRpcFaultException(123,
"Unhandled exception: " + ex.Message);
}
finally
{
childHandle = null;
}
throw new XmlRpcFaultException(123, "Unable to perform action");
}
public int GenerateMouseEvent(int x, int y, String type = "b1c")
{
Point pt = new Point(x, y);
switch (type)
{
case "b1p":
Input.SendMouseInput(x, y, 0, SendMouseInputFlags.LeftDown);
break;
case "b1r":
Input.SendMouseInput(x, y, 0, SendMouseInputFlags.LeftUp);
break;
case "b1c":
Input.MoveTo(pt);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.LeftDown);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.LeftUp);
break;
case "b1d":
Input.MoveTo(pt);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.LeftDown);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.LeftUp);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.LeftDown);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.LeftUp);
break;
case "b2p":
Input.SendMouseInput(x, y, 0, SendMouseInputFlags.MiddleDown);
break;
case "b2r":
Input.SendMouseInput(x, y, 0, SendMouseInputFlags.MiddleUp);
break;
case "b2c":
Input.MoveTo(pt);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.MiddleDown);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.MiddleUp);
break;
case "b3p":
Input.SendMouseInput(x, y, 0, SendMouseInputFlags.RightDown);
break;
case "b3r":
Input.SendMouseInput(x, y, 0, SendMouseInputFlags.RightUp);
break;
case "b3c":
Input.MoveTo(pt);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.RightDown);
Input.SendMouseInput(0, 0, 0, SendMouseInputFlags.RightUp);
break;
case "abs":
Input.SendMouseInput(pt.X, pt.Y, 0,
SendMouseInputFlags.Move | SendMouseInputFlags.Absolute);
break;
case "rel":
Input.SendMouseInput(pt.X, pt.Y, 0, SendMouseInputFlags.Move);
break;
default:
throw new XmlRpcFaultException(123,
"Unsupported mouse type: " + type);
}
return 1;
}
public int SimulateMouseMove(int source_x, int source_y, int dest_x, int dest_y, double delay = 0.0)
{
int[] size;
Generic generic = new Generic(utils);
try
{
size = generic.GetWindowSize("paneProgramManager");
}
finally
{
generic = null;
}
if (source_x < size[0] || source_y < size[1] ||
dest_x > size[2] || dest_y > size[3] ||
source_x > size[2] || source_y > size[3] ||
dest_x < size[0] || dest_y < size[1])
return 0;
bool x_flag = true; // Iterated x ?
bool y_flag = true; // Iterated y ?
while (true)
{
if (x_flag)
{
if (source_x > dest_x)
{
// If source X greather than dest X
// then move -1 pixel
source_x -= 1;
}
else if (source_x < dest_x)
{
// If source X less than dest X
// then move +1 pixel
source_x += 1;
}
else
{
// If source X equal to dest X
// then don't process X co-ordinate
x_flag = false;
}
}
if (y_flag)
{
if (source_y > dest_y)
{
// If source Y greather than dest Y
// then move -1 pixel
source_y -= 1;
}
else if (source_y < dest_y)
{
// If source Y less than dest Y
// then move +1 pixel
source_y += 1;
}
else
{
// If source Y equal to dest Y
// then don't process Y co-ordinate
y_flag = false;
}
}
if (delay != 0.0)
utils.InternalWait(delay);
// Start mouse move from source_x, source_y to dest_x, dest_y
GenerateMouseEvent(source_x, source_y, "abs");
if (source_x == dest_x && source_y == dest_y)
{
// If we have reached the dest_x and dest_y
// then break the loop
break;
}
}
return 1;
}
}
}