Monday, October 25, 2010

Detection of smoothness/Sluggish behavior of an UI operation via Auotmation

Hi, in this post i have tried to find out the solution of detecting the sluggish behavior while doing the operations on 3D objects via UI automation.
Before we start lets clear about what I meant by sluggish behavior - Sluggish behavior is something we found while dragging an object in an application from one point to another point and the dragging is not smooth OR mouse operation take some time to to show the effect in the application.
I thought of following steps to detect the above behavior visually via UI automation:
Steps (e.g. with dragging the object from one point to another point):
1. Set the position of mouse from and to Points for dragging.
2. Store the point of To (means end point) in above steps.
3. Store he color of the point in step2.
4. Perform the drag operation to end point.
5. Store the color of point on step 2 in a separate color variable.
6. Compare the Step 3 and Step 5 color values.
7. If Step 6 comparison result is same then there is sluggish behavior otherwise operation could be considered as smooth.

Example in C#:
Class Automation
{
//Set the cursor position
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SetCursorPos(int x, int y);

//Get the color
[DllImport("gdi32")]
public static extern uint GetPixel(IntPtr hDC, int XPos, int YPos);

[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr GetWindowDC(IntPtr hWnd);

//To perform Mouse drag
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
//mouse button
public const UInt32 MOUSEEVENTF_MOVE = 0x00000001;
public const UInt32 MOUSEEVENTF_LEFTDOWN = 0x0002;
public const UInt32 MOUSEEVENTF_LEFTUP = 0x0004;

static void Main(string[] args)
{
//Set the start point on object to be drag
System.Drawing.Point startPoint = new Point(10, 10);
//Set the end point of drag to.
System.Drawing.Point endPoint = new Point(150, 100);

//Get the color of end point
IntPtr dc = GetWindowDC(IntPtr.Zero);
long colorBefore = GetPixel(dc, endPoint.X, endPoint.Y);

//Perform drag operation using mouse
SetCursorPos(startPoint.X, startPoint.Y);
System.Threading.Thread.Sleep(100);
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0); //hold
SetCursorPos(endPoint.X, endPoint.Y);
mouse_event(MOUSEEVENTF_MOVE, 0, 0, 0, 0); //Drag
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0); //Release

//Get the color of end point for comparison
long colorAfter = GetPixel(dc, endPoint.X, endPoint.Y);

if (colorAfter != colorBefore)
{
Console.WriteLine("Drag operation is SMOOTH");
}
else
{
//Sluggish behavior
Console.WriteLine("Drag operation was not SMOOTH");
}

}
}

Sunday, October 3, 2010

Comparing images based on objects contained within an image

Hi, in this post I am writing the algoritham to compare images which is much better than tha conventional image comprison.

Convetional Way of comparison:
An image has a background and foreground. Generally we do image comparison to find out similarity or difference by comparing images pixel by pixel from (0,0) position to end of the last pixel of both the images. Also, in such kind of comparison there is one more restriction of images should be of same size.

With my appraoch:
With this appraoch we would be able to compare two images which are of different size and foreground objects are different only in form of displacement.
The restriction in this approach is that the foreground objects in an image should have same morphology as in the target image. The only difference expected between two images is size and displacement of foreground objects.

High level algorithm :
1. Accept two input image which are the candidate for comparison process
2. Extract all the objects from image-1 and image-2.
3. Crop those images from image-1 and image-2.
4. Resize all the objects in image-1 to make it of same size as in image-2 for better comparison result.
5. Compare all the objects in image-1 with that of image-2 to find out best match and output the difference and similarity.
I. Compare objects by conventional method of pixel by pixel comparison OR/AND
II. Compare objects by subset identification comparison method. (Note: in subset comparison we try to find out if one object is a part of the other object)

flowchart of the image comparison algorithm :

Monday, June 21, 2010

Error: Can’t clobber writable file Perforce

We use Perforce in our organization for Source Control. I was trying to get the latest revision for some of the files that were updated by other developers. and noticed the following error:

Can’t clobber writable file

I knew for a fact that files under source control were marked as read-only and when a file is checked out, then it removes the read-only attribute. So deciding to check on that I found that all the files under my perforce folder were not read-only. I went ahead and marked every file as read-only, and was able to get the latest revision without any errors. Cool, another problem solved.

Wednesday, April 14, 2010

Create BSOD on Vista and Win7 OS

There are many situation when you would like to generate BSOD on a system to test few of scenarios.

Force Windows Vista to crash to Blue screen:
There is a registry hack that allows you to make Windows Vista crash to a blue screen of death whenever you want.

1. Open REGEDIT.
2. Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\i8042prt
then click Parameters

3. Double-click on CrashOnCtrlScroll and set the value to 1 to enable this feature.

Restart the computer. Now when you hold down the CTRL key and press the SCROLL LOCK key two times, Windows Vista will automatically crash to a BSOD.
(More Info: click here)

An alternative way to force BSOD in C# --
To crash wondows (any OS):
C# program:
Process[] processes = Process.GetProcesses();
foreach (Process process in processes)
{
process.Kill();
}

Above code is enough to generate BSOD on any windows OS.

Friday, April 2, 2010

Problem launching an application in C# asking to set an Environment variable.

Recently I came acrross an issue where I am using C# Process class to launch an application located in Program files directory. The issue I found is after I called process.start() method a pop up came asking "Please set an environment variable required to run this process smoothly". Worst here is I had already set these environment variable for the application but still my script couldn't run though manualy launching this application has no issue.

Solution: set "process.StartInfo.EnvironmentVariables" will resolve this issue.

example:
Process procss = new Process();
procss.StartInfo.FileName = "Application path";
procss.StartInfo.Arguments = "";
procss.StartInfo.EnvironmentVariables.Add("Variable name", Environment.GetEnvironmentVariable("Variable Name", EnvironmentVariableTarget.Machine));
procss.Start();

Sunday, February 14, 2010

Automate Logoff - logon process and vice vers

Often in automation we have to deal with login to a system then logoff automatically and again login automatically. So hare I am posting ways to achive Logoff -> login automation.
- One thing one should know is even after system logoff there are process which are still executing. These process are background process or windows services.
So the trick is create an windows services or create a background process and then logoff. Now using this process you can do anything like login through windows UI using windows automation model or write a code to stimulate auto logon.
Reference link for creating service for remote sys is : http://support.microsoft.com/?kbid=251192
See also : How to logon automatically on windows startup:
open registry - HKLM->Software->Microsoft->WindowsNT->Winlogon and update the following kyes- DefaultUserName, DefaultDomainName, DefaultPassword with the logon information.

Monday, February 1, 2010

Capture Mouse button status with Desktop as whole in focus

How to get get mouse button status independent of particulat application window status.

use the following API in C#:
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern short GetAsyncKeyState(int vkey);

here vkey is ASCII key and in C# this can be found in class "Keys" or get the info from goole search.

ex.
short ke= GetAsyncKeyState((int)Keys.LButton);

If ke==0 that means Left mouse button has not ben presses since lass call of above function
If ke==1 that means Left Mouse button has been presses and released since last call of above function
Else Left mouse button is still in pressesd status.

For more information please visit: http://msdn.microsoft.com/en-us/library/ms646293%28VS.85%29.aspx

Get Keyboard state independent of an application window focus

Since a while I was looking for API to find the keyboard button status with repspect to desktop as a whole. But when I searched through Google I could find only a way to capture the keboard event whit current application in focus.
But at last I got the a way to capture the button status without any application in focus which will capture status in global.

API in C# is:
This API will provide the information staus of all the kayes on keboard
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern int GetKeyboardState(byte[] keystate);
and
this API will provide the information of apecific key.
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern short GetKeyState(int virtualKeyCode);

For more help on this topic please visit : http://msdn.microsoft.com/en-us/library/ms646299%28VS.85%29.aspx