|
| 1 | +describe("navbar settings", function() |
| 2 | + local arrange_options |
| 3 | + local config |
| 4 | + local saved |
| 5 | + local shown |
| 6 | + local touch_menu |
| 7 | + |
| 8 | + local function find_arrange_item(id) |
| 9 | + for _i, item in ipairs(arrange_options.item_table) do |
| 10 | + if item.orig_item == id then return item end |
| 11 | + end |
| 12 | + end |
| 13 | + |
| 14 | + before_each(function() |
| 15 | + arrange_options = nil |
| 16 | + saved = 0 |
| 17 | + shown = {} |
| 18 | + touch_menu = { |
| 19 | + backToSettingsRoot = function() end, |
| 20 | + backToUpperMenu = function(self) |
| 21 | + self.back_count = (self.back_count or 0) + 1 |
| 22 | + end, |
| 23 | + updateItems = function(self) |
| 24 | + self.update_count = (self.update_count or 0) + 1 |
| 25 | + end, |
| 26 | + } |
| 27 | + config = { |
| 28 | + navbar = { |
| 29 | + default_tab = "home", |
| 30 | + show_tabs = { books = true, home = true }, |
| 31 | + tab_order = { "books", "home" }, |
| 32 | + custom_tabs = {}, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + ZenSpec.replace("gettext", function(text) return text end) |
| 37 | + ZenSpec.replace("ffi/util", { |
| 38 | + template = function(text, value) |
| 39 | + return text:gsub("%%1", tostring(value)) |
| 40 | + end, |
| 41 | + }) |
| 42 | + ZenSpec.replace("ui/uimanager", { |
| 43 | + scheduleIn = function(_self, _delay, callback) callback() end, |
| 44 | + show = function(_self, widget) shown[#shown + 1] = widget end, |
| 45 | + }) |
| 46 | + ZenSpec.replace("ui/widget/confirmbox", { |
| 47 | + new = function(_self, opts) return opts end, |
| 48 | + }) |
| 49 | + ZenSpec.replace("ui/widget/infomessage", { |
| 50 | + new = function(_self, opts) return opts end, |
| 51 | + }) |
| 52 | + ZenSpec.replace("modules/settings/zen_settings_utils", { |
| 53 | + buildColorSubMenu = function(opts) return opts end, |
| 54 | + get_current_dir = function() return "/current" end, |
| 55 | + get_last_dir = function() return "/last" end, |
| 56 | + }) |
| 57 | + ZenSpec.replace("common/utils", { |
| 58 | + copyDefaultCustomTabIcon = function() end, |
| 59 | + getIconPickerList = function() return {} end, |
| 60 | + suggestIcon = function() return "lightning" end, |
| 61 | + }) |
| 62 | + ZenSpec.replace("common/paths", { |
| 63 | + getHomeDir = function() return "/home" end, |
| 64 | + }) |
| 65 | + ZenSpec.replace("common/inline_icon_map", setmetatable({}, { |
| 66 | + __index = function(_self, key) return key end, |
| 67 | + })) |
| 68 | + ZenSpec.replace("common/ui/icon_menu_item", { |
| 69 | + decorate = function(item) return item end, |
| 70 | + }) |
| 71 | + ZenSpec.replace("modules/menu/app_launcher/plugin_scan", { |
| 72 | + scan = function() return {} end, |
| 73 | + }) |
| 74 | + ZenSpec.replace("common/dispatcher_menu", { wrap = function() end }) |
| 75 | + ZenSpec.replace("dispatcher", { |
| 76 | + addSubMenu = function() end, |
| 77 | + menuTextFunc = function() return "Nothing" end, |
| 78 | + }) |
| 79 | + ZenSpec.replace("common/ui/zen_icon_picker", function() end) |
| 80 | + ZenSpec.replace("common/ui/zen_arrange_list", { |
| 81 | + show = function(opts) arrange_options = opts end, |
| 82 | + }) |
| 83 | + ZenSpec.replace("common/plugin_root", "/plugin") |
| 84 | + ZenSpec.unload("modules/settings/sections/library_settings/navbar_settings") |
| 85 | + end) |
| 86 | + |
| 87 | + local function build_navbar() |
| 88 | + return require("modules/settings/sections/library_settings/navbar_settings").build({ |
| 89 | + config = config, |
| 90 | + plugin = { saveConfig = function() saved = saved + 1 end }, |
| 91 | + save_and_apply = function() end, |
| 92 | + settings_apply = { refresh_navbar_on_menu_close = function() end }, |
| 93 | + }) |
| 94 | + end |
| 95 | + |
| 96 | + it("keeps the default when its tab is hidden", function() |
| 97 | + local navbar = build_navbar() |
| 98 | + navbar.sub_item_table[1].callback() |
| 99 | + |
| 100 | + find_arrange_item("home").callback(touch_menu) |
| 101 | + |
| 102 | + assert.is_false(config.navbar.show_tabs.home) |
| 103 | + assert.are.equal("home", config.navbar.default_tab) |
| 104 | + assert.are.equal(1, saved) |
| 105 | + assert.are.equal(0, #shown) |
| 106 | + end) |
| 107 | + |
| 108 | + it("keeps the default when its built-in tab is deleted from the navbar", function() |
| 109 | + local navbar = build_navbar() |
| 110 | + navbar.sub_item_table[1].callback() |
| 111 | + local home_item = find_arrange_item("home") |
| 112 | + local home_settings = home_item.sub_item_table_func() |
| 113 | + |
| 114 | + home_settings[#home_settings].callback(touch_menu) |
| 115 | + shown[1].ok_callback() |
| 116 | + |
| 117 | + assert.is_false(config.navbar.show_tabs.home) |
| 118 | + assert.are.same({ "books" }, config.navbar.tab_order) |
| 119 | + assert.are.equal("home", config.navbar.default_tab) |
| 120 | + assert.are.equal(1, touch_menu.back_count) |
| 121 | + assert.are.equal(1, saved) |
| 122 | + end) |
| 123 | +end) |
0 commit comments