-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathEventTests.cs
187 lines (165 loc) · 6.06 KB
/
EventTests.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
using System.Windows.Automation;
using NUnit.Framework;
namespace UIAComWrapperTests
{
public class EventHandlerBase
{
private System.Threading.ManualResetEvent _syncEvent = new System.Threading.ManualResetEvent(false);
private AutomationElement _eventSource;
private uint _receivedEventCount;
protected void OnMatchingEvent(AutomationElement sender)
{
_eventSource = sender;
_receivedEventCount++;
_syncEvent.Set();
}
public EventHandlerBase()
{
}
public void Start()
{
_receivedEventCount = 0;
_eventSource = null;
}
public bool Confirm()
{
return this._syncEvent.WaitOne(3000 /* ms */);
}
public AutomationElement EventSource
{
get
{
return this._eventSource;
}
}
public uint ReceivedEventCount
{
get
{
return this._receivedEventCount;
}
}
}
public class FocusChangeHandler : EventHandlerBase
{
public void HandleEvent(object sender, AutomationFocusChangedEventArgs e)
{
OnMatchingEvent((AutomationElement)sender);
}
}
public class BasicChangeHandler : EventHandlerBase
{
public void HandleEvent(object sender, AutomationEventArgs e)
{
OnMatchingEvent((AutomationElement)sender);
}
}
public class PropertyChangeHandler : EventHandlerBase
{
public void HandleEvent(object sender, AutomationPropertyChangedEventArgs e)
{
OnMatchingEvent((AutomationElement)sender);
}
}
public class StructureChangeHandler : EventHandlerBase
{
public void HandleEvent(object sender, StructureChangedEventArgs e)
{
OnMatchingEvent((AutomationElement)sender);
}
}
/// <summary>
/// Summary description for EventTests
/// </summary>
[TestFixture]
public class EventTests
{
[Test]
public void TestFocusChange()
{
// Launch a notepad and set focus to it
using (AppHost host1 = new AppHost("notepad.exe", ""))
{
host1.Element.SetFocus();
FocusChangeHandler handler = new FocusChangeHandler();
Automation.AddAutomationFocusChangedEventHandler(
new AutomationFocusChangedEventHandler(handler.HandleEvent));
handler.Start();
// Launch a new notepad and set focus to it
using (AppHost host2 = new AppHost("notepad.exe", ""))
{
host2.Element.SetFocus();
Assert.IsTrue(handler.Confirm());
Assert.IsNotNull(handler.EventSource);
}
Automation.RemoveAutomationFocusChangedEventHandler(
new AutomationFocusChangedEventHandler(handler.HandleEvent));
}
}
[Test]
[Ignore]
// Test is not working on Windows 8 due to the Start Button being removed
public void TestInvokeEvent()
{
AutomationElement startButton = AutomationElementTest.GetStartButton();
BasicChangeHandler handler = new BasicChangeHandler();
Automation.AddAutomationEventHandler(
InvokePattern.InvokedEvent,
startButton,
TreeScope.Element,
new AutomationEventHandler(handler.HandleEvent));
handler.Start();
InvokePattern invoke = (InvokePattern)startButton.GetCurrentPattern(InvokePattern.Pattern);
invoke.Invoke();
System.Windows.Forms.SendKeys.SendWait("{ESC}");
Assert.IsTrue(handler.Confirm());
Assert.IsNotNull(handler.EventSource);
Automation.RemoveAutomationEventHandler(
InvokePattern.InvokedEvent,
startButton,
new AutomationEventHandler(handler.HandleEvent));
}
[Test]
public void TestPropChangeEvent()
{
using (AppHost host = new AppHost("notepad.exe", ""))
{
TransformPattern transformPattern = (TransformPattern)host.Element.GetCurrentPattern(TransformPattern.Pattern);
PropertyChangeHandler handler = new PropertyChangeHandler();
Automation.AddAutomationPropertyChangedEventHandler(
host.Element,
TreeScope.Element,
new AutomationPropertyChangedEventHandler(handler.HandleEvent),
AutomationElement.BoundingRectangleProperty);
handler.Start();
System.Threading.Thread.Sleep(100 /* ms */);
transformPattern.Move(200, 200);
Assert.IsTrue(handler.Confirm());
Assert.IsNotNull(handler.EventSource);
Assert.AreEqual(host.Element, handler.EventSource);
Automation.RemoveAutomationPropertyChangedEventHandler(
host.Element,
new AutomationPropertyChangedEventHandler(handler.HandleEvent));
}
}
[Test]
public void TestStructureChangeEvent()
{
StructureChangeHandler handler = new StructureChangeHandler();
Automation.AddStructureChangedEventHandler(
AutomationElement.RootElement,
TreeScope.Subtree,
new StructureChangedEventHandler(handler.HandleEvent));
handler.Start();
// Start Notepad to get a structure change event
using (AppHost host = new AppHost("notepad.exe", ""))
{
}
Assert.IsTrue(handler.Confirm());
Assert.IsNotNull(handler.EventSource);
Automation.RemoveStructureChangedEventHandler(
AutomationElement.RootElement,
new StructureChangedEventHandler(handler.HandleEvent));
}
}
}