forked from derkork/godot-test-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ItemListDriver.cs
206 lines (175 loc) · 6.47 KB
/
ItemListDriver.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Godot;
using GodotTestDriver.Util;
using JetBrains.Annotations;
namespace GodotTestDriver.Drivers
{
/// <summary>
/// Driver for an ItemList control.
/// </summary>
[PublicAPI]
public partial class ItemListDriver<T> : ControlDriver<T> where T:ItemList
{
public ItemListDriver(Func<T> producer, string description = "") : base(producer, description)
{
}
/// <summary>
/// Returns a list of all items in the list that are currently selectable.
/// </summary>
public List<string> SelectableItems
{
get
{
var uiControl = PresentRoot;
var result = new List<string>();
for (var i = 0; i < uiControl.ItemCount; i++)
{
if (uiControl.IsItemSelectable(i) && !uiControl.IsItemDisabled(i))
{
result.Add(uiControl.GetItemText(i));
}
}
return result;
}
}
/// <summary>
/// Returns true if something is selected.
/// </summary>
public bool HasSelection => PresentRoot.GetSelectedItems().Length > 0;
/// <summary>
/// Returns the number of items in the list.
/// </summary>
public int ItemCount => PresentRoot.ItemCount;
/// <summary>
/// Returns the currently selected items.
/// </summary>
public List<string> SelectedItems
{
get
{
var uiControl = PresentRoot;
return uiControl.GetSelectedItems()
.Select(i => uiControl.GetItemText(i)).ToList();
}
}
/// <summary>
/// Returns the indices of the currently selected items.
/// </summary>
public List<int> SelectedIndices
{
get
{
var uiControl = PresentRoot;
return uiControl.GetSelectedItems().ToList();
}
}
/// <summary>
/// Returns the index of the item with the given text. Throws an exception if the item is not found.
/// </summary>
private int IndexOf(string text)
{
var uiControl = PresentRoot;
for (var i = 0; i < uiControl.ItemCount; i++)
{
if (uiControl.GetItemText(i) == text)
{
return i;
}
}
throw new InvalidOperationException(ErrorMessage($"List does not contain item with text '{text}'."));
}
/// <summary>
/// Selects an item with the given text.
/// </summary>
public async Task SelectItemWithText(string text, bool addToSelection = false)
{
var uiControl = VisibleRoot;
if (addToSelection && uiControl.SelectMode != ItemList.SelectModeEnum.Multi)
{
throw new InvalidOperationException(ErrorMessage("Cannot select multiple items because selection mode is not allowing it."));
}
await uiControl.GetTree().NextFrame();
var i = IndexOf(text);
if (!uiControl.IsItemSelectable(i) || uiControl.IsItemDisabled(i))
{
throw new InvalidOperationException(ErrorMessage($"Item with text '{text}' is not selectable."));
}
// check if the item is already selected
var wasSelectedBefore = uiControl.IsSelected(i);
uiControl.Select(i, !addToSelection);
// calling this function will not emit the signal so we need to do this ourselves
// if the item was already selected before then only emit the signal if AllowReselect is true
if (!wasSelectedBefore || uiControl.AllowReselect)
{
uiControl.EmitSignal(ItemList.SignalName.ItemSelected, i);
}
await uiControl.GetTree().WaitForEvents();
}
/// <summary>
/// Selects multiple items with the given texts.
/// </summary>
public async Task SelectItemsWithText(IEnumerable<string> texts)
{
foreach (var text in texts)
{
await SelectItemWithText(text, true);
}
}
/// <summary>
/// Selects multiple items with the given texts.
/// </summary>
public async Task SelectItemsWithText(params string[] texts)
{
await SelectItemsWithText(texts.AsEnumerable());
}
/// <summary>
/// Deselects an item with the given text.
/// </summary>
public async Task DeselectItemWithText(string text)
{
var uiControl = VisibleRoot;
await uiControl.GetTree().NextFrame();
var i = IndexOf(text);
if (!uiControl.IsItemSelectable(i) || uiControl.IsItemDisabled(i))
{
throw new InvalidOperationException(ErrorMessage($"Item with text '{text}' is not selectable."));
}
uiControl.Deselect(i);
await uiControl.GetTree().WaitForEvents();
}
/// <summary>
/// Deselects all items.
/// </summary>
public async Task DeselectAll()
{
var uiControl = VisibleRoot;
await uiControl.GetTree().NextFrame();
uiControl.DeselectAll();
await uiControl.GetTree().WaitForEvents();
}
/// <summary>
/// Activates the item with the given text.
/// </summary>
public async Task ActivateItemWithText(string text)
{
var uiControl = VisibleRoot;
await SelectItemWithText(text);
// send the activation signal
uiControl.EmitSignal(ItemList.SignalName.ItemActivated, IndexOf(text));
await uiControl.GetTree().WaitForEvents();
}
}
/// <summary>
/// Driver for an ItemList control.
/// </summary>
[PublicAPI]
public sealed class ItemListDriver : ItemListDriver<ItemList>
{
public ItemListDriver(Func<ItemList> producer, string description = "") : base(producer, description)
{
}
}
}