Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

boxes: Disable cycling using arrow keys if recipient is invalid. #864

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 130 additions & 18 deletions tests/ui_tools/test_boxes.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,7 @@ def test_footer_notification_on_invalid_recipients(
write_box.to_write_box.edit_text = raw_recipients
write_box.to_write_box.set_edit_pos(len(raw_recipients))
expected_lines = [
"Invalid recipient(s) - " + invalid_recipients,
" - Use ",
"Invalid recipient(s) - " + invalid_recipients + ". Use ",
("footer_contrast", primary_key_for_command("AUTOCOMPLETE")),
" or ",
("footer_contrast", primary_key_for_command("AUTOCOMPLETE_REVERSE")),
Expand Down Expand Up @@ -1045,6 +1044,7 @@ def test_keypress_typeahead_mode_autocomplete_key(
"box_type",
"msg_body_edit_enabled",
"message_being_edited",
"key",
"expected_focus_name",
"expected_focus_col_name",
],
Expand All @@ -1055,130 +1055,241 @@ def test_keypress_typeahead_mode_autocomplete_key(
"stream",
True,
False,
"tab_key",
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
id="stream_name_to_topic_box",
id="stream_name_to_topic_box_tab",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
"stream",
True,
False,
"tab_key",
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
id="topic_to_message_box",
id="topic_to_message_box_tab",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
"stream",
False,
True,
"tab_key",
"CONTAINER_HEADER",
"HEADER_BOX_EDIT",
id="topic_edit_only-topic_to_edit_mode_box",
id="topic_edit_only-topic_to_edit_mode_box_tab",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_EDIT",
"stream",
False,
True,
"tab_key",
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
id="topic_edit_only-edit_mode_to_topic_box",
id="topic_edit_only-edit_mode_to_topic_box_tab",
),
case(
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
"stream",
True,
False,
"tab_key",
"CONTAINER_HEADER",
"HEADER_BOX_STREAM",
id="message_to_stream_name_box",
id="message_to_stream_name_box_tab",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_STREAM",
"stream",
True,
True,
"tab_key",
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
id="edit_box-stream_name_to_topic_box",
id="edit_box-stream_name_to_topic_box_tab",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
"stream",
True,
True,
"tab_key",
"CONTAINER_HEADER",
"HEADER_BOX_EDIT",
id="edit_box-topic_to_edit_mode_box",
id="edit_box-topic_to_edit_mode_box_tab",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_EDIT",
"stream",
True,
True,
"tab_key",
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
id="edit_box-edit_mode_to_message_box",
id="edit_box-edit_mode_to_message_box_tab",
),
case(
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
"stream",
True,
True,
"tab_key",
"CONTAINER_HEADER",
"HEADER_BOX_STREAM",
id="edit_box-message_to_stream_name_box",
id="edit_box-message_to_stream_name_box_tab",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_RECIPIENT",
"private",
True,
False,
"tab_key",
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
id="recipient_to_message_box",
id="recipient_to_message_box_tab",
),
case(
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
"private",
True,
False,
"tab_key",
"CONTAINER_HEADER",
"HEADER_BOX_RECIPIENT",
id="message_to_recipient_box",
id="message_to_recipient_box_tab",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_STREAM",
"stream",
True,
False,
"right",
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
id="stream_name_to_topic_box_right",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
"stream",
True,
True,
"right",
"CONTAINER_HEADER",
"HEADER_BOX_EDIT",
id="topic_edit_only-topic_to_edit_mode_box_right",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_EDIT",
"stream",
False,
True,
"up",
"CONTAINER_HEADER",
"HEADER_BOX_EDIT",
id="topic_edit_only-edit_mode_to_edit_box_up",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_STREAM",
"stream",
True,
True,
"left",
"CONTAINER_HEADER",
"HEADER_BOX_STREAM",
id="edit_box-stream_name_to_stream_box_left",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
"stream",
True,
True,
"right",
"CONTAINER_HEADER",
"HEADER_BOX_EDIT",
id="edit_box-topic_to_edit_mode_box_right",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_TOPIC",
"stream",
True,
False,
"down",
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
id="topic_box_to_message_box_down",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_RECIPIENT",
"private",
True,
False,
"right",
"CONTAINER_HEADER",
"HEADER_BOX_RECIPIENT",
id="recipient_to_recipient_box_right",
),
case(
"CONTAINER_HEADER",
"HEADER_BOX_RECIPIENT",
"private",
True,
False,
"down",
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
id="recipient_to_message_box_down",
),
case(
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
"private",
True,
False,
"right",
"CONTAINER_MESSAGE",
"MESSAGE_BOX_BODY",
id="message_to_message_box_private_right",
),
],
)
@pytest.mark.parametrize("tab_key", keys_for_command("CYCLE_COMPOSE_FOCUS"))
def test_keypress_CYCLE_COMPOSE_FOCUS(
def test_keypress_CHANGE_COMPOSE_FOCUS(
self,
write_box,
tab_key,
initial_focus_name,
expected_focus_name,
initial_focus_col_name,
expected_focus_col_name,
box_type,
msg_body_edit_enabled,
message_being_edited,
key,
widget_size,
mocker,
stream_id=10,
):
mocker.patch(BOXES + ".WriteBox._set_stream_write_box_style")

if key == "tab_key":
key = primary_key_for_command("CYCLE_COMPOSE_FOCUS")
if box_type == "stream":
if message_being_edited:
mocker.patch(BOXES + ".EditModeButton")
Expand All @@ -1190,7 +1301,8 @@ def test_keypress_CYCLE_COMPOSE_FOCUS(
write_box.stream_box_view(stream_id)
else:
write_box.private_box_view()
size = widget_size(write_box)
# FIXME: Use widget_size(), instead of hard-coded size.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why aren't we using widget_size?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I tried using widget_size, but for some cases, the write-box changes to box widget instead of flow and returns a (maxcol, maxrow) size instead of just (maxcol, ). This made the tests fail for such cases. Let me try to dig more into this and see if I can get a fix for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the late reply. I got busy with other PR's since finding the root+fix of this particular issue was taking forever. :(
I have tried to dig more on this, but it seems like it's an internal urwid issue that I am unable to resolve. While looking at this, I realized that the whole write_box is sometimes recognized as "flow widget" for all tests (that's when all the tests pass), but sometimes the write_box is determined as "box widget" for all tests (2 tests fail in this case).

I also found that in the latter case, the tests only fail for "DOWN" keypresses, because it can't be handled properly by super() i.e. urwid's container.py. I can fix the whole issue by not letting the "DOWN" keypress being handled by urwid and returning early after manually handling, but it seems to have other side-effects and does not look clean.

size = (20,)

def focus_val(x: str) -> int:
return getattr(write_box, "FOCUS_" + x)
Expand All @@ -1202,7 +1314,7 @@ def focus_val(x: str) -> int:
write_box.model.get_invalid_recipient_emails.return_value = []
write_box.model.user_dict = mocker.MagicMock()

write_box.keypress(size, tab_key)
write_box.keypress(size, key)

assert write_box.focus_position == focus_val(expected_focus_name)
# FIXME: Needs refactoring?
Expand Down
Loading