-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathComboBox.cs
More file actions
163 lines (139 loc) · 5.92 KB
/
ComboBox.cs
File metadata and controls
163 lines (139 loc) · 5.92 KB
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
using System;
using System.Windows.Automation;
using TestStack.White.AutomationElementSearch;
using TestStack.White.Recording;
using TestStack.White.UIItemEvents;
using TestStack.White.UIItems.Actions;
using TestStack.White.UIItems.Scrolling;
namespace TestStack.White.UIItems.ListBoxItems
{
public class ComboBox : ListControl
{
private AutomationPropertyChangedEventHandler handler;
private ListItem lastSelectedItem;
protected ComboBox()
{
}
public ComboBox(AutomationElement automationElement, IActionListener actionListener) : base(automationElement, actionListener)
{
this.actionListener = actionListener;
}
public override VerticalSpan VerticalSpan
{
get
{
AutomationElement listElement = Finder.Child(AutomationSearchCondition.ByControlType(ControlType.List));
var listContainerItem = new UIItem(listElement, new NullActionListener());
return new VerticalSpan(listContainerItem.Bounds).Union(listContainerItem.Bounds);
}
}
public override IScrollBars ScrollBars
{
get { return new ComboBoxScrollBars(automationElement, actionListener); }
}
public virtual string EditableText
{
get { return EditableItem.Text; }
set { EditableItem.Text = value; }
}
protected virtual TextBox EditableItem
{
get
{
AutomationElement editElement = EditableElement();
if (editElement != null)
{
return new TextBox(editElement, actionListener);
}
throw new WhiteException("ComboBox is not editable");
}
}
public virtual bool IsEditable
{
get { return EditableElement() != null;}
}
private AutomationElement EditableElement()
{
return Finder.Child(AutomationSearchCondition.ByControlType(ControlType.Edit));
}
public override void Select(string itemText)
{
//if (!Enabled)
//{
// Logger.WarnFormat("Could not select {0}in {1} since it is disabled", itemText, Name);
// return;
//}
//if (Equals(itemText, SelectedItemText)) return;
//base.Select(itemText);
AutomationElement comboboxList = this.AutomationElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty,
ControlType.List));
AutomationElementCollection comboboxItem = comboboxList.FindAll(TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty,
ControlType.ListItem));
AutomationElement[] itemArray = new AutomationElement[comboboxItem.Count];
comboboxItem.CopyTo(itemArray, 0);
AutomationElement itemToSelect = null;
for (int i = 0; i < itemArray.Length; i++)
{
if (itemArray[i].Current.Name.Equals(itemText))
{
itemToSelect = itemArray[i];
}
}
if (itemToSelect != null)
{
Object selectPattern = null;
if (itemToSelect.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectPattern))
{
((SelectionItemPattern)selectPattern).Select();
}
}
}
public override void Select(int index)
{
//if (!Enabled)
//{
// Logger.Warn("Could not select " + index + "in " + Name + " since it is disabled");
// return;
//}
//base.Select(index);
//var p = (ExpandCollapsePattern) this.AutomationElement.GetCurrentPattern(ExpandCollapsePattern.Pattern);
//if (p.Current.ExpandCollapseState.Equals(ExpandCollapseState.Expanded))
// p.Collapse();
AutomationElement comboboxList = this.AutomationElement.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty,
ControlType.List));
AutomationElementCollection comboboxItem = comboboxList.FindAll(TreeScope.Children,
new PropertyCondition(AutomationElement.ControlTypeProperty,
ControlType.ListItem));
AutomationElement itemToSelect = comboboxItem[index];
Object selectPattern = null;
if (itemToSelect.TryGetCurrentPattern(SelectionItemPattern.Pattern, out selectPattern))
{
((SelectionItemPattern)selectPattern).Select();
}
}
public override void HookEvents(IUIItemEventListener eventListener)
{
lastSelectedItem = SelectedItem;
handler = delegate(object sender, AutomationPropertyChangedEventArgs e)
{
if (SelectedItem == null || e.NewValue.Equals(1)) return;
if (SameListItem()) return;
lastSelectedItem = SelectedItem;
eventListener.EventOccured(new ComboBoxEvent(this, SelectedItemText));
};
Automation.AddAutomationPropertyChangedEventHandler(automationElement, TreeScope.Element, handler, ExpandCollapsePattern.ExpandCollapseStateProperty);
}
public override void UnHookEvents()
{
Automation.RemoveAutomationPropertyChangedEventHandler(automationElement, handler);
}
private bool SameListItem()
{
if (lastSelectedItem == null && SelectedItem != null) return false;
return Equals(SelectedItemText, lastSelectedItem == null ? null : lastSelectedItem.Text);
}
}
}