-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory_ui_test.go
More file actions
193 lines (166 loc) · 5.17 KB
/
Copy pathinventory_ui_test.go
File metadata and controls
193 lines (166 loc) · 5.17 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
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
package main
import (
"strings"
"testing"
"gothoom/climg"
"gothoom/eui"
"github.com/hajimehoshi/ebiten/v2"
)
func TestInventoryWindowIncrementalUpdates(t *testing.T) {
resetInventory()
oldImages := clImages
defer func() { clImages = oldImages }()
clImages = testCLImages(map[uint32]*climg.ClientItem{
100: {Name: "shadow bell", Slot: kItemSlotRightHand},
200: {Name: "linen shirt", Slot: kItemSlotTorso},
})
inventoryWin = nil
inventoryList = nil
inventoryRowRefs = map[*eui.ItemData]invRef{}
invRender = inventoryRenderState{}
invRegularSrc = nil
invBoldSrc = nil
invItalicSrc = nil
selectedInvID = 0
selectedInvIdx = -1
makeInventoryWindow()
addInventoryItem(100, -1, "shadow bell", false)
addInventoryItem(200, -1, "linen shirt", false)
updateInventoryWindow()
if inventoryList == nil {
t.Fatal("inventory list not initialized")
}
initial := append([]*eui.ItemData(nil), inventoryList.Contents...)
if len(initial) < 3 {
t.Fatalf("expected at least 2 rows and spacer, got %d", len(initial))
}
shadowRow := findInventoryTestRow(t, "Shadow Bell")
linenRow := findInventoryTestRow(t, "Linen Shirt")
spacer := initial[len(initial)-1]
addInventoryItem(100, -1, "shadow bell", false)
updateInventoryWindow()
assertInventoryRowsUseConfiguredHeight(t)
if len(inventoryList.Contents) != len(initial) {
t.Fatalf("expected %d items, got %d", len(initial), len(inventoryList.Contents))
}
if findInventoryTestRow(t, "Shadow Bell") != shadowRow {
t.Fatalf("expected shadow bell row to be reused")
}
if findInventoryTestRow(t, "Linen Shirt") != linenRow {
t.Fatalf("expected linen shirt row to be reused")
}
if inventoryList.Contents[len(inventoryList.Contents)-1] != spacer {
t.Fatalf("expected spacer to be reused")
}
if len(shadowRow.Contents) < 2 {
t.Fatalf("expected name text in first row")
}
if !strings.Contains(shadowRow.Contents[1].Text, "(2)") {
t.Fatalf("expected quantity suffix in shadow bell row text, got %q", shadowRow.Contents[1].Text)
}
removeInventoryItem(100, -1)
updateInventoryWindow()
if findInventoryTestRow(t, "Shadow Bell") != shadowRow {
t.Fatalf("expected shadow bell row pointer to remain after decrement")
}
if strings.Contains(shadowRow.Contents[1].Text, "(2)") {
t.Fatalf("expected quantity suffix to be removed")
}
removeInventoryItem(100, -1)
updateInventoryWindow()
if len(inventoryList.Contents) != 2 {
t.Fatalf("expected one row plus spacer after removal, got %d", len(inventoryList.Contents))
}
if findInventoryTestRow(t, "Linen Shirt") != linenRow {
t.Fatalf("expected remaining row pointer to persist")
}
equipInventoryItem(200, -1, true)
updateInventoryWindow()
assertInventoryRowsUseConfiguredHeight(t)
if len(inventoryList.Contents) != 2 {
t.Fatalf("expected row count to remain stable after equip")
}
if findInventoryTestRow(t, "Linen Shirt") != linenRow {
t.Fatalf("expected equip to reuse existing row pointer")
}
if len(linenRow.Contents) < 3 {
t.Fatalf("expected slot label to be present when equipped")
}
if got := linenRow.Contents[len(linenRow.Contents)-1].Text; got != "[Torso]" {
t.Fatalf("expected slot label [Torso], got %q", got)
}
equipInventoryItem(200, -1, false)
updateInventoryWindow()
if len(linenRow.Contents) != 2 {
t.Fatalf("expected slot label removed after unequip")
}
if len(inventoryRowRefs) != 1 {
t.Fatalf("expected row refs to contain single entry, got %d", len(inventoryRowRefs))
}
}
func findInventoryTestRow(t *testing.T, name string) *eui.ItemData {
t.Helper()
for _, row := range inventoryList.Contents {
if len(row.Contents) > 1 && strings.Contains(row.Contents[1].Text, name) {
return row
}
}
t.Fatalf("inventory row %q not found", name)
return nil
}
func assertInventoryRowsUseConfiguredHeight(t *testing.T) {
t.Helper()
for _, row := range inventoryList.Contents {
if _, ok := inventoryRowRefs[row]; !ok {
continue
}
if row.Size.Y != invRender.rowUnits {
t.Fatalf("expected row height %v, got %v", invRender.rowUnits, row.Size.Y)
}
}
}
func TestInventoryIconUpdateMarksWindowDirty(t *testing.T) {
resetInventory()
inventoryWin = nil
inventoryList = nil
inventoryRowRefs = map[*eui.ItemData]invRef{}
invRender = inventoryRenderState{}
invRegularSrc = nil
invBoldSrc = nil
invItalicSrc = nil
makeInventoryWindow()
invRender.clientWAvail = 200
invRender.rowUnits = 20
invRender.iconSize = 20
invRender.fontSize = 12
row := invRender.createRow(inventoryRowData{
key: invGroupKey{id: 100, name: "test"},
id: 100,
idx: -1,
label: "Test",
})
inventoryList.AddItem(row.row)
inventoryWin.Dirty = false
row.icon.Dirty = false
img := ebiten.NewImage(8, 8)
invRender.updateRow(row, inventoryRowData{
key: row.key,
id: row.id,
idx: row.idx,
label: "Test",
icon: img,
iconName: "item:100",
})
if row.icon.Image != img {
t.Fatalf("icon image was not updated")
}
if row.icon.Size.X != float32(invRender.iconSize) || row.icon.Size.Y != float32(invRender.iconSize) {
t.Fatalf("icon size got %v, want %d square", row.icon.Size, invRender.iconSize)
}
if !row.icon.Dirty {
t.Fatalf("icon was not marked dirty")
}
if !inventoryWin.Dirty {
t.Fatalf("inventory window was not marked dirty")
}
}