Skip to content

Instantly share code, notes, and snippets.

@cmaneu
Created January 21, 2014 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmaneu/8541839 to your computer and use it in GitHub Desktop.
Save cmaneu/8541839 to your computer and use it in GitHub Desktop.
"Monkey testing" for Windows Phone emulator

This is just a demo to get the emulator Win32 handle, and send it click events.

A lot need to be done, including:

  • A more robust way to get the XDE.exe handle, handling multiple emulators,
  • A click generator with parameters,
  • Supports for keyboard events
using System;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using System.Drawing;
using System.Windows;
using Point = System.Drawing.Point;
namespace GremlinsForWindowsPhone
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Process[] processlist = Process.GetProcesses();
var process = processlist.FirstOrDefault(p => p.MainWindowTitle.Contains("Emulator"));
if (process == null)
return;
HandleRef handleRef = new HandleRef(this, process.MainWindowHandle);
var windowBounds = GetWindowRectangle(handleRef);
SetForegroundWindow(process.MainWindowHandle);
ClickOnPoint(new Point(windowBounds.X + 100, windowBounds.Y + 100));
ClickOnPoint(new Point(windowBounds.X + 200, windowBounds.Y + 200));
Thread.Sleep(4000);
ClickOnPoint(new Point(windowBounds.X + 300, windowBounds.Y + 200));
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetWindowRect(HandleRef hWnd, out RECT lpRect);
[Flags]
enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
public static Rectangle GetWindowRectangle(HandleRef windowHandleRef)
{
Rectangle myRect = new Rectangle();
RECT rct;
if (!GetWindowRect(windowHandleRef, out rct))
{
throw new ApplicationException("Oups");
}
myRect.X = rct.Left;
myRect.Y = rct.Top;
myRect.Width = rct.Right - rct.Left + 1;
myRect.Height = rct.Bottom - rct.Top + 1;
return myRect;
}
private static void ClickOnPoint(Point clientPoint)
{
SetCursorPos((int) clientPoint.X, (int) clientPoint.Y);
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment