2019-09-10 00:54:28 +00:00
|
|
|
# Created By: Virgil Dupras
|
|
|
|
# Created On: 2011-09-06
|
|
|
|
# Copyright 2015 Hardcoded Software (http://www.hardcoded.net)
|
2020-01-01 02:16:27 +00:00
|
|
|
#
|
|
|
|
# This software is licensed under the "GPLv3" License as described in the "LICENSE" file,
|
|
|
|
# which should be included with this package. The terms are also available at
|
2019-09-10 00:54:28 +00:00
|
|
|
# http://www.gnu.org/licenses/gpl-3.0.html
|
|
|
|
|
2022-05-09 06:40:08 +00:00
|
|
|
from hscommon.testutil import eq_, callcounter, CallLogger
|
|
|
|
from hscommon.gui.selectable_list import SelectableList, GUISelectableList
|
2019-09-10 00:54:28 +00:00
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_in():
|
|
|
|
# When a SelectableList is in a list, doing "in list" with another instance returns false, even
|
|
|
|
# if they're the same as lists.
|
|
|
|
sl = SelectableList()
|
|
|
|
some_list = [sl]
|
|
|
|
assert SelectableList() not in some_list
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_selection_range():
|
|
|
|
# selection is correctly adjusted on deletion
|
2020-01-01 02:16:27 +00:00
|
|
|
sl = SelectableList(["foo", "bar", "baz"])
|
2019-09-10 00:54:28 +00:00
|
|
|
sl.selected_index = 3
|
|
|
|
eq_(sl.selected_index, 2)
|
|
|
|
del sl[2]
|
|
|
|
eq_(sl.selected_index, 1)
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_update_selection_called():
|
|
|
|
# _update_selection_is called after a change in selection. However, we only do so on select()
|
|
|
|
# calls. I follow the old behavior of the Table class. At the moment, I don't quite remember
|
|
|
|
# why there was a specific select() method for triggering _update_selection(), but I think I
|
|
|
|
# remember there was a reason, so I keep it that way.
|
2020-01-01 02:16:27 +00:00
|
|
|
sl = SelectableList(["foo", "bar"])
|
2019-09-10 00:54:28 +00:00
|
|
|
sl._update_selection = callcounter()
|
|
|
|
sl.select(1)
|
|
|
|
eq_(sl._update_selection.callcount, 1)
|
|
|
|
sl.selected_index = 0
|
2020-01-01 02:16:27 +00:00
|
|
|
eq_(sl._update_selection.callcount, 1) # no call
|
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
|
|
|
|
def test_guicalls():
|
|
|
|
# A GUISelectableList appropriately calls its view.
|
2020-01-01 02:16:27 +00:00
|
|
|
sl = GUISelectableList(["foo", "bar"])
|
2019-09-10 00:54:28 +00:00
|
|
|
sl.view = CallLogger()
|
2021-08-15 09:10:18 +00:00
|
|
|
sl.view.check_gui_calls(["refresh"]) # Upon setting the view, we get a call to refresh()
|
2020-01-01 02:16:27 +00:00
|
|
|
sl[1] = "baz"
|
|
|
|
sl.view.check_gui_calls(["refresh"])
|
|
|
|
sl.append("foo")
|
|
|
|
sl.view.check_gui_calls(["refresh"])
|
2019-09-10 00:54:28 +00:00
|
|
|
del sl[2]
|
2020-01-01 02:16:27 +00:00
|
|
|
sl.view.check_gui_calls(["refresh"])
|
|
|
|
sl.remove("baz")
|
|
|
|
sl.view.check_gui_calls(["refresh"])
|
|
|
|
sl.insert(0, "foo")
|
|
|
|
sl.view.check_gui_calls(["refresh"])
|
2019-09-10 00:54:28 +00:00
|
|
|
sl.select(1)
|
2020-01-01 02:16:27 +00:00
|
|
|
sl.view.check_gui_calls(["update_selection"])
|
2019-09-10 00:54:28 +00:00
|
|
|
# XXX We have to give up on this for now because of a breakage it causes in the tables.
|
|
|
|
# sl.select(1) # don't update when selection stays the same
|
|
|
|
# gui.check_gui_calls([])
|
|
|
|
|
2020-01-01 02:16:27 +00:00
|
|
|
|
2019-09-10 00:54:28 +00:00
|
|
|
def test_search_by_prefix():
|
2020-01-01 02:16:27 +00:00
|
|
|
sl = SelectableList(["foo", "bAr", "baZ"])
|
|
|
|
eq_(sl.search_by_prefix("b"), 1)
|
|
|
|
eq_(sl.search_by_prefix("BA"), 1)
|
|
|
|
eq_(sl.search_by_prefix("BAZ"), 2)
|
|
|
|
eq_(sl.search_by_prefix("BAZZ"), -1)
|