Skip to content

Commit

Permalink
Merge pull request #188 from ilya-murzinov/Issue144
Browse files Browse the repository at this point in the history
Issue144: Add DrawHighlight method to UIItem
  • Loading branch information
JakeGinnivan committed Jun 3, 2014
2 parents 03e879b + afb3550 commit d77b3af
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/TestStack.White/Configuration/CoreAppXmlConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ static CoreAppXmlConfiguration()
DefaultValues.Add("PopupTimeout", 5000);
DefaultValues.Add("TooltipWaitTime", 3000);
DefaultValues.Add("SuggestionListTimeout", 3000);
DefaultValues.Add("HighlightTimeout", 1000);
DefaultValues.Add("DefaultDateFormat", DateFormat.CultureDefault.ToString());
DefaultValues.Add("DragStepCount", 1);
DefaultValues.Add("InProc", false);
Expand Down Expand Up @@ -108,6 +109,12 @@ public virtual int SuggestionListTimeout
set { SetUsedValue("SuggestionListTimeout", value); }
}

public virtual int HighlightTimeout
{
get { return Convert.ToInt32(UsedValues["HighlightTimeout"]); }
set { SetUsedValue("HighlightTimeout", value); }
}

public virtual DateFormat DefaultDateFormat
{
get { return DateFormat.Parse(UsedValues["DefaultDateFormat"]); }
Expand Down
1 change: 1 addition & 0 deletions src/TestStack.White/Configuration/CoreConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface ICoreConfiguration
int PopupTimeout { get; set; }
int TooltipWaitTime { get; set; }
int SuggestionListTimeout { get; set; }
int HighlightTimeout { get; set; }
DateFormat DefaultDateFormat { get; set; }
int DragStepCount { get; set; }
bool InProc { get; set; }
Expand Down
79 changes: 79 additions & 0 deletions src/TestStack.White/Drawing/ScreenRectangle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using TestStack.White.Configuration;

namespace TestStack.White.Drawing
{
internal class ScreenRectangle
{
private Form form = new Form();
//TODO: Think about making color configurable
private Color Color = Color.Red;

internal ScreenRectangle(Rect rectangle)
{
form.FormBorderStyle = FormBorderStyle.None;
form.ShowInTaskbar = false;
form.TopMost = true;
form.Left = 0;
form.Top = 0;
form.Width = 1;
form.Height = 1;
form.BackColor = this.Color;
form.Opacity = 0.8;
form.Visible = false;

//Set popup style
int num1 = TestStack.White.WindowsAPI.NativeWindow.GetWindowLong(form.Handle, -20);
TestStack.White.WindowsAPI.NativeWindow.SetWindowLong(form.Handle, -20, num1 | 0x80);

//Set position
TestStack.White.WindowsAPI.NativeWindow.SetWindowPos(form.Handle, new IntPtr(-1), Convert.ToInt32(rectangle.X), Convert.ToInt32(rectangle.Y),
Convert.ToInt32(rectangle.Width), Convert.ToInt32(rectangle.Height), 0x10);
}

internal virtual void Show()
{
TestStack.White.WindowsAPI.NativeWindow.ShowWindow(form.Handle, 8);
}

internal virtual void Hide()
{
form.Hide();
}
}

internal class FrameRectangle
{
//Using 4 rectangles to display each border
private ScreenRectangle leftBorder;
private ScreenRectangle topBorder;
private ScreenRectangle rightBorder;
private ScreenRectangle bottomBorder;

private ScreenRectangle[] rectangles;
private int width = 3;

internal FrameRectangle(Rect boundingRectangle)
{
leftBorder = new ScreenRectangle(new Rect(boundingRectangle.X - width, boundingRectangle.Y - width, width, boundingRectangle.Height + 2*width));
topBorder = new ScreenRectangle(new Rect(boundingRectangle.X, boundingRectangle.Y - width, boundingRectangle.Width, width));
rightBorder = new ScreenRectangle(new Rect(boundingRectangle.X + boundingRectangle.Width, boundingRectangle.Y - width, width, boundingRectangle.Height + 2*width));
bottomBorder = new ScreenRectangle(new Rect(boundingRectangle.X, boundingRectangle.Y + boundingRectangle.Height, boundingRectangle.Width, width));
rectangles = new ScreenRectangle[] { leftBorder, topBorder, rightBorder, bottomBorder };
}

internal virtual void Highlight()
{
rectangles.ToList().ForEach(x => x.Show());
Thread.Sleep(CoreAppXmlConfiguration.Instance.HighlightTimeout);
rectangles.ToList().ForEach(x => x.Hide());
}
}
}
1 change: 1 addition & 0 deletions src/TestStack.White/TestStack.White.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<Compile Include="Configuration\ConfigurationExtensions.cs" />
<Compile Include="Configuration\WhiteDefaultLogger.cs" />
<Compile Include="Configuration\WhiteDefaultLoggerFactory.cs" />
<Compile Include="Drawing\ScreenRectangle.cs" />
<Compile Include="InputDevices\Input64.cs" />
<Compile Include="InputDevices\SystemMetric.cs" />
<Compile Include="Interceptors\IWhiteInterceptor.cs" />
Expand Down
2 changes: 2 additions & 0 deletions src/TestStack.White/UIItems/IUIItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,7 @@ public interface IUIItem : ActionListener
AutomationElement GetElement(SearchCriteria searchCriteria);

void Enter(string value);

void DrawHighlight();
}
}
14 changes: 14 additions & 0 deletions src/TestStack.White/UIItems/UIItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using TestStack.White.WindowsAPI;
using Action = TestStack.White.UIItems.Actions.Action;
using Point = System.Windows.Point;
using System.Windows.Forms;

namespace TestStack.White.UIItems
{
Expand Down Expand Up @@ -424,5 +425,18 @@ public virtual void RaiseClickEvent()
var invokePattern = (InvokePattern)Pattern(InvokePattern.Pattern);
if (invokePattern != null) invokePattern.Invoke();
}

/// <summary>
/// Highlight UIItem with red frame
/// </summary>
public virtual void DrawHighlight()
{
Rect rectangle = AutomationElement.Current.BoundingRectangle;

if (rectangle != Rect.Empty)
{
new Drawing.FrameRectangle(rectangle).Highlight();
}
}
}
}
16 changes: 15 additions & 1 deletion src/TestStack.White/WindowsAPI/NativeWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,24 @@ public virtual COLORREF TextColor
return GetTextColor(GetDC(handle));
}
}

public virtual void PostCloseMessage()
{
PostMessage(handle, WM_CLOSE, IntPtr.Zero, IntPtr.Zero);
}

//Native methods needed for highlighting UIItems
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hwndAfter, int x, int y, int width, int height, int flags);
[return: MarshalAs(UnmanagedType.Bool)]

[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
internal static extern int GetWindowLong(IntPtr hWnd, int nIndex);
}
}

0 comments on commit d77b3af

Please sign in to comment.