forked from chickensoft-games/GodotTestDriver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPopupMenuDriver.cs
266 lines (238 loc) · 7.64 KB
/
PopupMenuDriver.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
namespace Chickensoft.GodotTestDriver.Drivers;
using System;
using System.Collections.Generic;
using Godot;
using JetBrains.Annotations;
/// <summary>
/// Driver for a popup menu.
/// </summary>
/// <typeparam name="T">PopupMenu type.</typeparam>
[PublicAPI]
public class PopupMenuDriver<T> : WindowDriver<T> where T : PopupMenu
{
/// <summary>
/// Creates a new generic PopupMenuDriver.
/// </summary>
/// <param name="producer">Producer that creates a PopupMenu subclass.</param>
/// <param name="description">Driver description.</param>
public PopupMenuDriver(Func<T> producer, string description = "") : base(producer, description)
{
}
/// <summary>
/// Returns the amount of items in the popup menu.
/// </summary>
public int ItemCount => PresentRoot.ItemCount;
/// <summary>
/// Returns the text of the items in the popup menu.
/// </summary>
public IEnumerable<string> Items
{
get
{
for (var i = 0; i < ItemCount; i++)
{
yield return PresentRoot.GetItemText(i);
}
}
}
/// <summary>
/// Returns the text of the items in the popup menu which are currently selectable (e.g. not disabled and no separator).
/// </summary>
public IEnumerable<string> SelectableItems
{
get
{
for (var i = 0; i < ItemCount; i++)
{
if (PresentRoot.IsItemDisabled(i) || PresentRoot.IsItemSeparator(i))
{
continue;
}
yield return PresentRoot.GetItemText(i);
}
}
}
/// <summary>
/// Returns whether the item at the given index is checked.
/// </summary>
/// <param name="index">Menu item index.</param>
public bool IsItemChecked(int index)
{
VerifyIndex(index);
return PresentRoot.IsItemChecked(index);
}
/// <summary>
/// Returns whether the item with the given text is checked.
/// </summary>
/// <param name="text">Menu item label.</param>
/// <exception cref="InvalidOperationException"/>
public bool IsItemChecked(string text)
{
var element = PresentRoot;
for (var i = 0; i < element.ItemCount; i++)
{
if (element.GetItemText(i) == text)
{
return element.IsItemChecked(i);
}
}
throw new InvalidOperationException($"Item with text '{text}' not found.");
}
/// <summary>
/// Returns whether the item at the given index is disabled.
/// </summary>
/// <param name="index">Menu item index.</param>
public bool IsItemDisabled(int index)
{
VerifyIndex(index);
return PresentRoot.IsItemDisabled(index);
}
/// <summary>
/// Checks if the item with the given text is disabled.
/// </summary>
/// <param name="text">Menu item label.</param>
/// <exception cref="InvalidOperationException"/>
public bool IsItemDisabled(string text)
{
var element = PresentRoot;
for (var i = 0; i < element.ItemCount; i++)
{
if (element.GetItemText(i) == text)
{
return element.IsItemDisabled(i);
}
}
throw new InvalidOperationException($"Item with text '{text}' not found.");
}
/// <summary>
/// Returns whether the item at the given index is a separator.
/// </summary>
/// <param name="index">Menu item index.</param>
public bool IsItemSeparator(int index)
{
VerifyIndex(index);
return PresentRoot.IsItemSeparator(index);
}
/// <summary>
/// Returns whether the item with the given text is a separator.
/// </summary>
/// <param name="text">Menu item label.</param>
/// <exception cref="InvalidOperationException"/>
public bool IsItemSeparator(string text)
{
var element = PresentRoot;
for (var i = 0; i < element.ItemCount; i++)
{
if (element.GetItemText(i) == text)
{
return element.IsItemSeparator(i);
}
}
throw new InvalidOperationException($"Item with text {text} not found.");
}
/// <summary>
/// Returns the ID of the item at the given index.
/// </summary>
/// <param name="index">Menu item index.</param>
public int GetItemId(int index)
{
VerifyIndex(index);
return PresentRoot.GetItemId(index);
}
/// <summary>
/// Selects the item at the given index.
/// </summary>
/// <param name="index">Menu item index.</param>
/// <exception cref="InvalidOperationException"/>
public void SelectItemAtIndex(int index)
{
var popup = VisibleRoot;
VerifyIndex(index);
// verify that item is not disabled
if (popup.IsItemDisabled(index))
{
throw new InvalidOperationException(
$"Item at index {index} is disabled and cannot be selected.");
}
// verify that item is not a separator
if (popup.IsItemSeparator(index))
{
throw new InvalidOperationException(
$"Item at index {index} is a separator and cannot be selected.");
}
// select item
// ideally we would use a mouse click here but since the API does not provide the position of
// each entry, we have to fake it.
popup.EmitSignal(PopupMenu.SignalName.IndexPressed, index);
popup.Hide();
}
/// <summary>
/// Verifies that the given index is valid.
/// </summary>
/// <param name="index">Menu item index.</param>
/// <exception cref="IndexOutOfRangeException"/>
private void VerifyIndex(int index)
{
// verify index is in range
if (index < 0 || index >= ItemCount)
{
throw new IndexOutOfRangeException(
$"Index {index} is out of range for popup menu with {ItemCount} items.");
}
}
/// <summary>
/// Selects the item with the given ID.
/// </summary>
/// <param name="id">Menu item id.</param>
/// <exception cref="InvalidOperationException" />
public void SelectItemWithId(int id)
{
var popup = PresentRoot;
for (var i = 0; i < ItemCount; i++)
{
if (popup.GetItemId(i) != id)
{
continue;
}
SelectItemAtIndex(i);
return;
}
throw new InvalidOperationException(
$"No item with id {id} found in popup menu.");
}
/// <summary>
/// Selects the item with the given text. If multiple items have the same text, the first one is selected.
/// </summary>
/// <param name="text">Menu item label.</param>
/// <exception cref="InvalidOperationException"/>
public void SelectItemWithText(string text)
{
var popup = PresentRoot;
for (var i = 0; i < ItemCount; i++)
{
if (popup.GetItemText(i) != text)
{
continue;
}
SelectItemAtIndex(i);
return;
}
throw new InvalidOperationException(
$"No item with text {text} found in popup menu.");
}
}
/// <summary>
/// Driver for a popup menu.
/// </summary>
[PublicAPI]
public sealed class PopupMenuDriver : PopupMenuDriver<PopupMenu>
{
/// <summary>
/// Creates a new generic PopupMenuDriver.
/// </summary>
/// <param name="producer">Producer that creates a PopupMenu subclass.</param>
/// <param name="description">Driver description.</param>
public PopupMenuDriver(Func<PopupMenu> producer, string description = "") : base(producer, description)
{
}
}