Skip to content

Commit b6e8e57

Browse files
committed
bugfix: 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 9280f09 commit b6e8e57

File tree

2 files changed

+46
-100
lines changed

2 files changed

+46
-100
lines changed

tests/ui/test_ui_tools.py

+42-94
Original file line numberDiff line numberDiff line change
@@ -568,64 +568,38 @@ 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+
576+
assert stream_view.focus_index_before_search == 3
572577
stream_view.set_focus.assert_called_once_with("header")
573578

574579
@pytest.mark.parametrize("key", keys_for_command("GO_BACK"))
575580
def test_keypress_GO_BACK(self, mocker, stream_view, key, widget_size):
576581
size = widget_size(stream_view)
577582
mocker.patch.object(stream_view, "set_focus")
583+
mocker.patch(VIEWS + ".urwid.Frame.keypress")
578584
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)
585+
stream_view.streams_btn_list = ["FOO", "foo", "fan", "boo", "BOO"]
586+
stream_view.focus_index_before_search = 3
610587

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
588+
# Simulate search
589+
stream_view.log.clear()
590+
stream_view.log.extend(stream_view.streams_btn_list[3])
591+
stream_view.log.set_focus(0)
592+
stream_view.keypress(size, "down")
593+
assert stream_view.log.get_focus()[1] != stream_view.focus_index_before_search
616594

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)
595+
# Exit search
596+
stream_view.keypress(size, key)
623597

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
598+
# Check state reset after search
599+
stream_view.set_focus.assert_called_once_with("body")
600+
assert stream_view.stream_search_box.reset_search_text.called
601+
assert stream_view.log == stream_view.streams_btn_list
602+
assert stream_view.log.get_focus()[1] == stream_view.focus_index_before_search
629603

630604

631605
class TestTopicsView:
@@ -741,65 +715,39 @@ def test_keypress_GO_RIGHT(self, mocker, topic_view, key, widget_size):
741715
def test_keypress_SEARCH_TOPICS(self, mocker, topic_view, key, widget_size):
742716
size = widget_size(topic_view)
743717
mocker.patch(VIEWS + ".TopicsView.set_focus")
718+
topic_view.log.extend(["FOO", "foo", "fan", "boo", "BOO"])
719+
topic_view.log.set_focus(3)
720+
744721
topic_view.keypress(size, key)
722+
745723
topic_view.set_focus.assert_called_once_with("header")
746724
topic_view.header_list.set_focus.assert_called_once_with(2)
725+
assert topic_view.focus_index_before_search == 3
747726

748727
@pytest.mark.parametrize("key", keys_for_command("GO_BACK"))
749728
def test_keypress_GO_BACK(self, mocker, topic_view, key, widget_size):
750729
size = widget_size(topic_view)
751730
mocker.patch(VIEWS + ".TopicsView.set_focus")
731+
mocker.patch(VIEWS + ".urwid.Frame.keypress")
752732
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)
733+
topic_view.topics_btn_list = ["FOO", "foo", "fan", "boo", "BOO"]
734+
topic_view.focus_index_before_search = 3
784735

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
736+
# Simulate search
737+
topic_view.log.clear()
738+
topic_view.log.extend(topic_view.topics_btn_list[3])
739+
topic_view.log.set_focus(0)
740+
topic_view.keypress(size, "down")
741+
assert topic_view.log.get_focus()[1] != topic_view.focus_index_before_search
790742

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)
743+
# Exit search
744+
topic_view.keypress(size, key)
797745

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
746+
# Check state reset after search
747+
topic_view.set_focus.assert_called_once_with("body")
748+
assert topic_view.topic_search_box.reset_search_text.called
749+
assert topic_view.log == topic_view.topics_btn_list
750+
assert topic_view.log.get_focus()[1] == topic_view.focus_index_before_search
803751

804752

805753
class TestUsersView:

zulipterminal/ui_tools/views.py

+4-6
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ def mouse_event(
365365

366366
def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
367367
if is_command_key("SEARCH_STREAMS", key):
368+
_, self.focus_index_before_search = self.log.get_focus()
368369
self.set_focus("header")
369370
return key
370371
elif is_command_key("GO_BACK", key):
@@ -375,9 +376,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
375376
self.log.set_focus(self.focus_index_before_search)
376377
self.view.controller.update_screen()
377378
return key
378-
return_value = super().keypress(size, key)
379-
_, self.focus_index_before_search = self.log.get_focus()
380-
return return_value
379+
return super().keypress(size, key)
381380

382381

383382
class TopicsView(urwid.Frame):
@@ -477,6 +476,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
477476
self.view.show_left_panel(visible=False)
478477
self.view.body.focus_col = 1
479478
if is_command_key("SEARCH_TOPICS", key):
479+
_, self.focus_index_before_search = self.log.get_focus()
480480
self.set_focus("header")
481481
self.header_list.set_focus(2)
482482
return key
@@ -488,9 +488,7 @@ def keypress(self, size: urwid_Size, key: str) -> Optional[str]:
488488
self.log.set_focus(self.focus_index_before_search)
489489
self.view.controller.update_screen()
490490
return key
491-
return_value = super().keypress(size, key)
492-
_, self.focus_index_before_search = self.log.get_focus()
493-
return return_value
491+
return super().keypress(size, key)
494492

495493

496494
class UsersView(urwid.ListBox):

0 commit comments

Comments
 (0)