Skip to content

Commit af1cae2

Browse files
Add tests for filtering, centering, preview and delete gui/notes
1 parent 5fb75bb commit af1cae2

2 files changed

Lines changed: 190 additions & 27 deletions

File tree

gui/notes.lua

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,43 @@ function NotesWindow:init()
3838
self.note_manager = nil
3939
self.curr_search_phrase = nil
4040

41-
self:addviews{
41+
local left_panel_content = {
4242
widgets.Panel{
43-
view_id='note_list_panel',
44-
frame={l=0, w=NOTE_LIST_RESIZE_MIN.w, t=0, b=1},
45-
visible=true,
46-
frame_inset={l=1,t=1,b=1,r=1},
47-
autoarrange_subviews=true,
43+
frame={l=0,h=3},
44+
frame_style=gui.FRAME_INTERIOR,
4845
subviews={
49-
widgets.TextArea{
46+
widgets.EditField{
5047
view_id='search',
51-
frame={l=0,h=3},
52-
frame_style=gui.FRAME_INTERIOR,
53-
one_line_mode=true,
54-
on_text_change=self:callback('loadFilteredNotes'),
48+
on_change=self:callback('loadFilteredNotes'),
5549
on_submit=function()
5650
self.subviews.note_list:submit()
5751
end
5852
},
59-
widgets.List{
60-
view_id='note_list',
61-
frame={l=0,b=2},
62-
frame_inset={t=1},
63-
row_height=1,
64-
on_select=function (ind, note)
65-
self:loadNote(note)
66-
end,
67-
on_submit=function (ind, note)
68-
self:loadNote(note)
69-
dfhack.gui.pauseRecenter(note.point.pos)
70-
end
71-
},
72-
},
53+
}
54+
},
55+
widgets.List{
56+
view_id='note_list',
57+
frame={l=0,b=2},
58+
frame_inset={t=1},
59+
row_height=1,
60+
on_select=function (ind, note)
61+
self:loadNote(note)
62+
end,
63+
on_submit=function (ind, note)
64+
self:loadNote(note)
65+
dfhack.gui.pauseRecenter(note.point.pos)
66+
end
67+
},
68+
}
69+
70+
self:addviews{
71+
widgets.Panel{
72+
view_id='note_list_panel',
73+
frame={l=0, w=NOTE_LIST_RESIZE_MIN.w, t=0, b=1},
74+
visible=true,
75+
frame_inset={l=1,t=1,b=1,r=1},
76+
autoarrange_subviews=true,
77+
subviews=left_panel_content,
7378
},
7479
widgets.HotkeyLabel{
7580
view_id='create',

test/gui/notes.lua

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local gui = require('gui')
22
local gui_notes = reqscript('gui/notes')
33
local utils = require('utils')
4+
local guidm = require('gui.dwarfmode')
5+
46

57
-- local guidm = require('gui.dwarfmode')
68

@@ -22,14 +24,16 @@ local function arrange_notes(notes)
2224
map_points:insert("#", {
2325
new=true,
2426

25-
id = waypoints.next_point_id,
27+
id=waypoints.next_point_id,
2628
tile=88,
2729
fg_color=7,
2830
bg_color=0,
2931
name=note.name,
3032
comment=note.comment,
3133
pos=note.pos
3234
})
35+
36+
waypoints.next_point_id = waypoints.next_point_id + 1
3337
end
3438
end
3539

@@ -81,6 +85,160 @@ function test.provide_notes_list()
8185
expect.table_eq(gui_note.point.pos, note.pos)
8286
end
8387

84-
expect.eq(gui_notes.visible, true)
88+
cleanup(gui_notes)
89+
end
90+
91+
function test.auto_select_first_note()
92+
local notes = {
93+
{name='green note 1', comment='comment 1', pos={x=1, y=1, z=1}},
94+
{name='green note 2', comment='comment 2', pos={x=2, y=2, z=2}},
95+
{name='blue note 3', comment='comment 3', pos={x=3, y=3, z=3}},
96+
}
97+
98+
local gui_notes = arrange_gui_notes({ notes=notes })
99+
expect.eq(gui_notes.subviews.name.text_to_wrap, 'green note 1')
100+
expect.eq(gui_notes.subviews.comment.text_to_wrap, 'comment 1')
101+
102+
cleanup(gui_notes)
103+
end
104+
105+
function test.select_on_arrow_up_down()
106+
local notes = {
107+
{name='green note 1', comment='comment 1', pos={x=1, y=1, z=1}},
108+
{name='green note 2', comment='comment 2', pos={x=2, y=2, z=2}},
109+
{name='blue note 3', comment='comment 3', pos={x=3, y=3, z=3}},
110+
}
111+
112+
local gui_notes = arrange_gui_notes({ notes=notes })
113+
local screen = dfhack.gui.getCurViewscreen(true)
114+
115+
gui.simulateInput(screen, 'KEYBOARD_CURSOR_DOWN')
116+
expect.eq(gui_notes.subviews.name.text_to_wrap, 'green note 2')
117+
expect.eq(gui_notes.subviews.comment.text_to_wrap, 'comment 2')
118+
119+
gui.simulateInput(screen, 'KEYBOARD_CURSOR_DOWN')
120+
expect.eq(gui_notes.subviews.name.text_to_wrap, 'blue note 3')
121+
expect.eq(gui_notes.subviews.comment.text_to_wrap, 'comment 3')
122+
123+
gui.simulateInput(screen, 'KEYBOARD_CURSOR_DOWN')
124+
expect.eq(gui_notes.subviews.name.text_to_wrap, 'green note 1')
125+
expect.eq(gui_notes.subviews.comment.text_to_wrap, 'comment 1')
126+
127+
gui.simulateInput(screen, 'KEYBOARD_CURSOR_UP')
128+
expect.eq(gui_notes.subviews.name.text_to_wrap, 'blue note 3')
129+
expect.eq(gui_notes.subviews.comment.text_to_wrap, 'comment 3')
130+
131+
gui.simulateInput(screen, 'KEYBOARD_CURSOR_UP')
132+
expect.eq(gui_notes.subviews.name.text_to_wrap, 'green note 2')
133+
expect.eq(gui_notes.subviews.comment.text_to_wrap, 'comment 2')
134+
135+
cleanup(gui_notes)
136+
end
137+
138+
function test.center_at_submit_note()
139+
local notes = {
140+
{name='green note 1', comment='comment 1', pos={x=1, y=1, z=1}},
141+
{name='green note 2', comment='comment 2', pos={x=2, y=2, z=2}},
142+
{name='blue note 3', comment='comment 3', pos={x=3, y=3, z=3}},
143+
}
144+
145+
local gui_notes = arrange_gui_notes({ notes=notes })
146+
local screen = dfhack.gui.getCurViewscreen(true)
147+
148+
-- it would be best to check viewport, but it's not updated instantly
149+
-- and I do not know way how to force it
150+
-- local viewport = guidm.Viewport.get()
151+
152+
local last_recenter_pos = nil
153+
mock.patch(dfhack.gui, 'pauseRecenter', function (pos)
154+
last_recenter_pos = pos
155+
end, function ()
156+
gui.simulateInput(screen, 'KEYBOARD_CURSOR_DOWN')
157+
gui.simulateInput(screen, 'SELECT')
158+
159+
expect.eq(last_recenter_pos.x, 2)
160+
expect.eq(last_recenter_pos.y, 2)
161+
expect.eq(last_recenter_pos.z, 2)
162+
163+
gui.simulateInput(screen, 'KEYBOARD_CURSOR_DOWN')
164+
gui.simulateInput(screen, 'SELECT')
165+
166+
expect.eq(last_recenter_pos.x, 3)
167+
expect.eq(last_recenter_pos.y, 3)
168+
expect.eq(last_recenter_pos.z, 3)
169+
end)
170+
171+
cleanup(gui_notes)
172+
end
173+
174+
function test.filter_notes()
175+
local notes = {
176+
{name='green note 1', comment='comment 1', pos={x=1, y=1, z=1}},
177+
{name='green note 2', comment='comment 2', pos={x=2, y=2, z=2}},
178+
{name='blue note 3', comment='comment 3', pos={x=3, y=3, z=3}},
179+
}
180+
181+
local gui_notes = arrange_gui_notes({ notes=notes })
182+
gui_notes.subviews.search:setText('green')
183+
184+
local note_list = gui_notes.subviews.note_list:getChoices()
185+
expect.eq(#note_list, 2)
186+
187+
for ind, note in ipairs({table.unpack(notes, 1, 2)}) do
188+
local gui_note = note_list[ind]
189+
expect.eq(gui_note.text, note.name)
190+
expect.eq(gui_note.point.comment, note.comment)
191+
expect.table_eq(gui_note.point.pos, note.pos)
192+
end
193+
194+
expect.eq(gui_notes.subviews.name.text_to_wrap, 'green note 1')
195+
expect.eq(gui_notes.subviews.comment.text_to_wrap, 'comment 1')
196+
197+
gui_notes.subviews.search:setText('blue')
198+
199+
local note_list = gui_notes.subviews.note_list:getChoices()
200+
expect.eq(#note_list, 1)
201+
202+
expect.eq(note_list[1].text, notes[3].name)
203+
expect.eq(note_list[1].point.comment, notes[3].comment)
204+
expect.table_eq(note_list[1].point.pos, notes[3].pos)
205+
206+
expect.eq(gui_notes.subviews.name.text_to_wrap, 'blue note 3')
207+
expect.eq(gui_notes.subviews.comment.text_to_wrap, 'comment 3')
208+
209+
gui_notes.subviews.search:setText('red')
210+
211+
local note_list = gui_notes.subviews.note_list:getChoices()
212+
expect.eq(#note_list, 0)
213+
214+
cleanup(gui_notes)
215+
end
216+
217+
function test.delete_note()
218+
local notes = {
219+
{name='green note 1', comment='comment 1', pos={x=1, y=1, z=1}},
220+
{name='green note 2', comment='comment 2', pos={x=2, y=2, z=2}},
221+
{name='blue note 3', comment='comment 3', pos={x=3, y=3, z=3}},
222+
}
223+
224+
local gui_notes = arrange_gui_notes({ notes=notes })
225+
local screen = dfhack.gui.getCurViewscreen(true)
226+
227+
gui.simulateInput(screen, 'KEYBOARD_CURSOR_DOWN')
228+
gui.simulateInput(screen, 'CUSTOM_CTRL_D')
229+
230+
local note_list = gui_notes.subviews.note_list:getChoices()
231+
expect.eq(#note_list, 2)
232+
233+
for ind, note in ipairs({notes[1], notes[3]}) do
234+
local gui_note = note_list[ind]
235+
expect.eq(gui_note.text, note.name)
236+
expect.eq(gui_note.point.comment, note.comment)
237+
expect.table_eq(gui_note.point.pos, note.pos)
238+
end
239+
240+
expect.eq(gui_notes.subviews.name.text_to_wrap, 'blue note 3')
241+
expect.eq(gui_notes.subviews.comment.text_to_wrap, 'comment 3')
242+
85243
cleanup(gui_notes)
86244
end

0 commit comments

Comments
 (0)