Skip to content

Commit e5cc513

Browse files
committed
view: Fix focus index before and after Panel Search.
This commit corrects what the code for focus_index_before_search was intended to do. The focus index was still being updated even in the search results. This is fixed by moving `get_focus` into the SEARCH_STREAM/TOPIC conditional. Tests amended. Bad tests (`test_return_to_focus_after_search`) removed as they pass even though they don't do the right thing. Current tests fail before this commit. Fixes zulip#975
1 parent b573d43 commit e5cc513

File tree

2 files changed

+38
-100
lines changed

2 files changed

+38
-100
lines changed

tests/ui/test_ui_tools.py

+34-94
Original file line numberDiff line numberDiff line change
@@ -568,64 +568,34 @@ def test_mouse_event(self, mocker, stream_view, widget_size):
568568
def test_keypress_SEARCH_STREAMS(self, mocker, stream_view, key, widget_size):
569569
size = widget_size(stream_view)
570570
mocker.patch.object(stream_view, "set_focus")
571+
stream_view.log.extend(["FOO", "foo", "fan", "boo", "BOO"])
572+
stream_view.log.set_focus(3)
573+
571574
stream_view.keypress(size, key)
575+
assert stream_view.focus_index_before_search == 3
572576
stream_view.set_focus.assert_called_once_with("header")
573577

574578
@pytest.mark.parametrize("key", keys_for_command("GO_BACK"))
575579
def test_keypress_GO_BACK(self, mocker, stream_view, key, widget_size):
576580
size = widget_size(stream_view)
577581
mocker.patch.object(stream_view, "set_focus")
578582
mocker.patch.object(stream_view.stream_search_box, "reset_search_text")
579-
stream_view.keypress(size, key)
580-
stream_view.set_focus.assert_called_once_with("body")
581-
assert stream_view.stream_search_box.reset_search_text.called
582-
assert stream_view.log == self.streams_btn_list
583-
584-
@pytest.mark.parametrize("search_streams_key", keys_for_command("SEARCH_STREAMS"))
585-
@pytest.mark.parametrize("go_back_key", keys_for_command("GO_BACK"))
586-
@pytest.mark.parametrize(
587-
"current_focus, stream",
588-
[
589-
(0, "FOO"),
590-
(2, "fan"),
591-
(4, "BOO"),
592-
],
593-
)
594-
def test_return_to_focus_after_search(
595-
self,
596-
mocker,
597-
stream_view,
598-
widget_size,
599-
current_focus,
600-
stream,
601-
search_streams_key,
602-
go_back_key,
603-
):
604-
# Initialize log
605-
stream_view.streams_btn_list = [
606-
mocker.Mock(stream_name=stream_name)
607-
for stream_name in ["FOO", "foo", "fan", "boo", "BOO"]
608-
]
609-
stream_view.log.extend(stream_view.streams_btn_list)
583+
stream_view.streams_btn_list = ["FOO", "foo", "fan", "boo", "BOO"]
584+
stream_view.focus_index_before_search = 3
610585

611-
# Set initial stream focus to 'current_focus' and name to 'stream'
612-
stream_view.log.set_focus(current_focus)
613-
stream_view.focus_index_before_search = current_focus
614-
previous_focus = stream_view.log.get_focus()[1]
615-
previous_focus_stream_name = stream
586+
# During search
587+
stream_view.log.clear()
588+
stream_view.log.extend(stream_view.streams_btn_list[3])
589+
stream_view.log.set_focus(1)
616590

617-
# Toggle Stream Search
618-
size = widget_size(stream_view)
619-
stream_view.keypress(size, search_streams_key)
620-
621-
# Exit Stream Search
622-
stream_view.keypress(size, go_back_key)
591+
# Exit search
592+
stream_view.keypress(size, key)
623593

624-
# Obtain new stream focus
625-
new_focus = stream_view.log.get_focus()[1]
626-
new_focus_stream_name = stream_view.log[new_focus].stream_name
627-
assert new_focus == previous_focus
628-
assert previous_focus_stream_name == new_focus_stream_name
594+
# Check state reset after search
595+
stream_view.set_focus.assert_called_once_with("body")
596+
assert stream_view.stream_search_box.reset_search_text.called
597+
assert stream_view.log == stream_view.streams_btn_list
598+
assert stream_view.log.get_focus()[1] == stream_view.focus_index_before_search
629599

630600

631601
class TestTopicsView:
@@ -741,65 +711,35 @@ def test_keypress_GO_RIGHT(self, mocker, topic_view, key, widget_size):
741711
def test_keypress_SEARCH_TOPICS(self, mocker, topic_view, key, widget_size):
742712
size = widget_size(topic_view)
743713
mocker.patch(VIEWS + ".TopicsView.set_focus")
714+
topic_view.log.extend(["FOO", "foo", "fan", "boo", "BOO"])
715+
topic_view.log.set_focus(3)
716+
744717
topic_view.keypress(size, key)
745718
topic_view.set_focus.assert_called_once_with("header")
746719
topic_view.header_list.set_focus.assert_called_once_with(2)
720+
assert topic_view.focus_index_before_search == 3
747721

748722
@pytest.mark.parametrize("key", keys_for_command("GO_BACK"))
749723
def test_keypress_GO_BACK(self, mocker, topic_view, key, widget_size):
750724
size = widget_size(topic_view)
751725
mocker.patch(VIEWS + ".TopicsView.set_focus")
752726
mocker.patch.object(topic_view.topic_search_box, "reset_search_text")
753-
topic_view.keypress(size, key)
754-
topic_view.set_focus.assert_called_once_with("body")
755-
assert topic_view.topic_search_box.reset_search_text.called
756-
assert topic_view.log == self.topics_btn_list
757-
758-
@pytest.mark.parametrize("search_topics_key", keys_for_command("SEARCH_TOPICS"))
759-
@pytest.mark.parametrize("go_back_key", keys_for_command("GO_BACK"))
760-
@pytest.mark.parametrize(
761-
"current_focus, topic",
762-
[
763-
(0, "FOO"),
764-
(2, "fan"),
765-
(4, "BOO"),
766-
],
767-
)
768-
def test_return_to_focus_after_search(
769-
self,
770-
mocker,
771-
topic_view,
772-
widget_size,
773-
current_focus,
774-
topic,
775-
search_topics_key,
776-
go_back_key,
777-
):
778-
# Initialize log
779-
topic_view.topics_btn_list = [
780-
mocker.Mock(topic_name=topic_name)
781-
for topic_name in ["FOO", "foo", "fan", "boo", "BOO"]
782-
]
783-
topic_view.log.extend(topic_view.topics_btn_list)
727+
topic_view.topics_btn_list = ["FOO", "foo", "fan", "boo", "BOO"]
728+
topic_view.focus_index_before_search = 3
784729

785-
# Set initial focus to 'current_focus' and name to 'topic'
786-
topic_view.log.set_focus(current_focus)
787-
topic_view.focus_index_before_search = current_focus
788-
previous_focus = topic_view.log.get_focus()[1]
789-
previous_focus_topic_name = topic
730+
# During search
731+
topic_view.log.clear()
732+
topic_view.log.extend(topic_view.topics_btn_list[3])
733+
topic_view.log.set_focus(1)
790734

791-
# Toggle Search
792-
size = widget_size(topic_view)
793-
topic_view.keypress(size, search_topics_key)
794-
795-
# Exit Search
796-
topic_view.keypress(size, go_back_key)
735+
# Exit search
736+
topic_view.keypress(size, key)
797737

798-
# Obtain new focus
799-
new_focus = topic_view.log.get_focus()[1]
800-
new_focus_topic_name = topic_view.log[new_focus].topic_name
801-
assert new_focus == previous_focus
802-
assert previous_focus_topic_name == new_focus_topic_name
738+
# Check state reset after search
739+
topic_view.set_focus.assert_called_once_with("body")
740+
assert topic_view.topic_search_box.reset_search_text.called
741+
assert topic_view.log == topic_view.topics_btn_list
742+
assert topic_view.log.get_focus()[1] == topic_view.focus_index_before_search
803743

804744

805745
class TestUsersView:

zulipterminal/ui_tools/views.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ def mouse_event(
367367

368368
def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
369369
if is_command_key("SEARCH_STREAMS", key):
370+
self.focus_index_before_search = self.log.get_focus()[1]
370371
self.set_focus("header")
371372
return key
372373
elif is_command_key("GO_BACK", key):
@@ -377,9 +378,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
377378
self.log.set_focus(self.focus_index_before_search)
378379
self.view.controller.update_screen()
379380
return key
380-
return_value = super().keypress(size, key)
381-
_, self.focus_index_before_search = self.log.get_focus()
382-
return return_value
381+
return super().keypress(size, key)
383382

384383

385384
class TopicsView(urwid.Frame):
@@ -482,6 +481,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
482481
self.view.show_left_panel(visible=False)
483482
self.view.body.focus_col = 1
484483
if is_command_key("SEARCH_TOPICS", key):
484+
self.focus_index_before_search = self.log.get_focus()[1]
485485
self.set_focus("header")
486486
self.header_list.set_focus(2)
487487
return key
@@ -493,9 +493,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
493493
self.log.set_focus(self.focus_index_before_search)
494494
self.view.controller.update_screen()
495495
return key
496-
return_value = super().keypress(size, key)
497-
_, self.focus_index_before_search = self.log.get_focus()
498-
return return_value
496+
return super().keypress(size, key)
499497

500498

501499
class UsersView(urwid.ListBox):

0 commit comments

Comments
 (0)