This repository has been archived by the owner on Jan 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PopupMenuDriver.cs
246 lines (215 loc) · 7.36 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
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Godot;
using GodotTestDriver.Util;
using JetBrains.Annotations;
namespace GodotTestDriver.Drivers
{
/// <summary>
/// Driver for a popup menu.
/// </summary>
[PublicAPI]
public partial class PopupMenuDriver<T> : WindowDriver<T> where T:PopupMenu
{
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>
public bool IsItemChecked(int index)
{
VerifyIndex(index);
return PresentRoot.IsItemChecked(index);
}
/// <summary>
/// Returns whether the item with the given text is checked.
/// </summary>
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>
public bool IsItemDisabled(int index)
{
VerifyIndex(index);
return PresentRoot.IsItemDisabled(index);
}
/// <summary>
/// Checks if the item with the given text is disabled.
/// </summary>
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>
public bool IsItemSeparator(int index)
{
VerifyIndex(index);
return PresentRoot.IsItemSeparator(index);
}
/// <summary>
/// Returns whether the item with the given text is a separator.
/// </summary>
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>
public int GetItemId(int index)
{
VerifyIndex(index);
return PresentRoot.GetItemId(index);
}
/// <summary>
/// Selects the item at the given index.
/// </summary>
public async Task 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.");
}
await popup.GetTree().NextFrame();
// 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();
await popup.GetTree().WaitForEvents();
}
/// <summary>
/// Verifies that the given index is valid.
/// </summary>
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>
public async Task SelectItemWithId(int id)
{
var popup = PresentRoot;
for(var i = 0; i < ItemCount; i++)
{
if (popup.GetItemId(i) != id)
{
continue;
}
await 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>
public async Task SelectItemWithText(string text)
{
var popup = PresentRoot;
for(var i = 0; i < ItemCount; i++)
{
if (popup.GetItemText(i) != text)
{
continue;
}
await 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>
{
public PopupMenuDriver(Func<PopupMenu> producer, string description = "") : base(producer, description)
{
}
}
}