From 4c2473de92cf2feb5681a21e47e19ddb3eb5fdd2 Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Sat, 31 Jul 2021 11:46:59 +0200 Subject: [PATCH] add test.ListBox_with_multi_select_and_visual_indicator this test also demonstrates actual (minimal) example usage --- test/library/gui/dialogs.lua | 95 +++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 2 deletions(-) diff --git a/test/library/gui/dialogs.lua b/test/library/gui/dialogs.lua index b89c861a0..6277e15d4 100644 --- a/test/library/gui/dialogs.lua +++ b/test/library/gui/dialogs.lua @@ -9,8 +9,8 @@ local function send_keys(...) end local xtest = {} -- use to temporarily disable tests (change `function test.somename` to `function xtest.somename`) -local wait = function() - --delay(30) -- enable for debugging the tests +local wait = function(n) + --delay(n or 30) -- enable for debugging the tests end local dialogs = require('gui.dialogs') @@ -170,3 +170,94 @@ function test.ListBox_with_multi_select() send_keys('LEAVESCREEN') expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "Pressing LEAVESCREEN should still return us to previous screen") end + +-- this test also demonstrates actual (minimal) example usage +function test.ListBox_with_multi_select_and_visual_indicator() + local before_scr = dfhack.gui.getCurViewscreen(true) + + -- configure colors + local pen_active = COLOR_LIGHTCYAN + local dpen_active = COLOR_CYAN + local pen_not_active = COLOR_LIGHTRED + local dpen_not_active = COLOR_RED + local pen_cb = function(args, fnc) + if not (args and fnc) then return COLOR_YELLOW end + return fnc(args) and pen_active or pen_not_active + end + local pen_d_cb = function(args, fnc) + if not (args and fnc) then return COLOR_YELLOW end + return fnc(args) and dpen_active or dpen_not_active + end + + local args = {} + local choices = {} + args.choices = choices + local mock_cb = mock.func() + local mock_cb2 = mock.func() + args.on_select = function(ix, choice) + mock_cb() + local item = choice.item + item.active = not item.active + end + args.on_select2 = mock_cb2 + args.dismiss_on_select = false + + local mock_is_active_cb_counter = mock.func() + local is_active = function (args) + mock_is_active_cb_counter() + return args.item.active + end + local items = { + {text = 'ListBox_with_multi_select', active = true}, + {text = 'and_visual_indicator', active = true}, + {text = 'item3', active = false} + } + for _, item in pairs(items) do + local args = {item=item} + table.insert(choices, { + text = {{text = item.text, + pen = curry(pen_cb, args, is_active), + dpen = curry(pen_d_cb, args, is_active), + }}, + item = item + }) + end + + local lb = dialogs.ListBox(args) + lb:show() + local lb_scr = dfhack.gui.getCurViewscreen(true) + + expect.eq(pen_active, choices[1].text[1].pen(), "Pen of the first item should be the pen_active") + expect.eq(pen_not_active, choices[3].text[1].pen(), "Pen of the third item should be the pen_not_active") + + wait() + send_keys('SELECT') + send_keys('STANDARDSCROLL_DOWN') + wait() + send_keys('SELECT') + send_keys('STANDARDSCROLL_DOWN') + wait() + send_keys('SELECT') + wait(100) + expect.eq(3, mock_cb.call_count) + expect.eq(0, mock_cb2.call_count) + + expect.lt(0, mock_is_active_cb_counter.call_count, "is_active should be called at least once") + + expect.table_eq( + { + {text = 'ListBox_with_multi_select', active = false}, + {text = 'and_visual_indicator', active = false}, + {text = 'item3', active = true} + }, + items, + "the active status should've been toggled" + ) + expect.eq(pen_not_active, choices[1].text[1].pen(), "Pen of the first now not active item should be the pen_not_active") + expect.eq(pen_active, choices[3].text[1].pen(), "Pen of the third now active item should be the pen_active") + + expect.eq(lb_scr, dfhack.gui.getCurViewscreen(true), "With both dismiss_on_select and dismiss_on_select2 false the ListBox should stay open") + + send_keys('LEAVESCREEN') + expect.eq(before_scr, dfhack.gui.getCurViewscreen(true), "Pressing LEAVESCREEN should still return us to previous screen") +end