Skip to content

Commit

Permalink
feat(tests): simplify asserts on translated strings
Browse files Browse the repository at this point in the history
  • Loading branch information
matejcik committed Nov 18, 2024
1 parent 5c8edfa commit fee17fa
Show file tree
Hide file tree
Showing 21 changed files with 342 additions and 435 deletions.
31 changes: 16 additions & 15 deletions tests/click_tests/common.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from __future__ import annotations

import typing as t
from enum import Enum
from typing import TYPE_CHECKING

from trezorlib.debuglink import LayoutType

from .. import buttons
from .. import translations as TR

if TYPE_CHECKING:
if t.TYPE_CHECKING:
from trezorlib.debuglink import DebugLink, LayoutContent

AllActionsType = t.List[t.Union[str, t.Tuple[str, ...]]]


# Passphrases and addresses for both models
class CommonPass:
Expand Down Expand Up @@ -82,7 +84,7 @@ def go_back(debug: "DebugLink", r_middle: bool = False) -> LayoutContent:
def navigate_to_action_and_press(
debug: "DebugLink",
wanted_action: str,
all_actions: list[str],
all_actions: AllActionsType,
is_carousel: bool = True,
hold_ms: int = 0,
) -> None:
Expand Down Expand Up @@ -129,16 +131,19 @@ def unlock_gesture(debug: "DebugLink") -> LayoutContent:
raise RuntimeError("Unknown model")


def _get_action_index(wanted_action: str, all_actions: list[str]) -> int:
def _get_action_index(wanted_action: str, all_actions: AllActionsType) -> int:
"""Get index of the action in the list of all actions"""
if wanted_action in all_actions:
return all_actions.index(wanted_action)
else:
# It may happen that one action item can mean multiple actions
# (e.g. "CANCEL|DELETE" in the passphrase layout - both actions are on the same button)
for index, action in enumerate(all_actions):
subactions = action.split("|")
if wanted_action in subactions:
for index, action in enumerate(all_actions):
if not isinstance(action, tuple):
action = (action,)
for subaction in action:
try:
tr = TR.translate(subaction)
except KeyError:
continue
if tr == wanted_action:
return index

raise ValueError(f"Action {wanted_action} is not supported in {all_actions}")
Expand All @@ -148,7 +153,7 @@ def _move_one_closer(
debug: "DebugLink",
wanted_action: str,
current_action: str,
all_actions: list[str],
all_actions: AllActionsType,
is_carousel: bool,
) -> LayoutContent:
"""Pressing either left or right regarding to the current situation"""
Expand All @@ -169,7 +174,3 @@ def _move_one_closer(
return debug.press_left()
else:
return debug.press_right()


def get_possible_btn_texts(path: str) -> str:
return "|".join(TR.translate(path))
92 changes: 42 additions & 50 deletions tests/click_tests/recovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@

from .. import buttons
from .. import translations as TR
from .common import get_possible_btn_texts, go_next
from .common import go_next

if TYPE_CHECKING:
from trezorlib.debuglink import DebugLink, LayoutContent


DELETE_BTN_TEXTS = get_possible_btn_texts("inputs__delete") + get_possible_btn_texts(
"inputs__previous"
)
DELETE_BTN_TEXTS = ("inputs__delete", "inputs__previous")


def enter_word(
Expand Down Expand Up @@ -50,7 +48,7 @@ def enter_word(

def confirm_recovery(debug: "DebugLink", title: str = "recovery__title") -> None:
layout = debug.read_layout()
TR.assert_equals(layout.title(), title)
assert TR.translate(title) == layout.title()
if debug.layout_type is LayoutType.TT:
debug.click(buttons.OK)
elif debug.layout_type is LayoutType.Mercury:
Expand Down Expand Up @@ -108,11 +106,11 @@ def select_mercury() -> "LayoutContent":
return debug.click(coords)

if debug.layout_type is LayoutType.TT:
TR.assert_equals(debug.read_layout().text_content(), "recovery__num_of_words")
assert debug.read_layout().text_content() == TR.recovery__num_of_words
layout = select_tt()
elif debug.layout_type is LayoutType.TR:
layout = debug.press_right()
TR.assert_equals(layout.title(), "word_count__title")
assert layout.title() == TR.word_count__title
layout = select_tr()
elif debug.layout_type is LayoutType.Mercury:
layout = select_mercury()
Expand All @@ -121,30 +119,24 @@ def select_mercury() -> "LayoutContent":

if unlock_repeated_backup:
if debug.layout_type is LayoutType.TR:
TR.assert_in(layout.text_content(), "recovery__enter_backup")
assert TR.recovery__enter_backup in layout.text_content()
else:
TR.assert_in_multiple(
layout.text_content(),
["recovery__only_first_n_letters", "recovery__enter_each_word"],
assert (
TR.recovery__only_first_n_letters in layout.text_content()
or TR.recovery__enter_each_word in layout.text_content()
)
elif num_of_words in (20, 33):
TR.assert_in_multiple(
layout.text_content(),
[
"recovery__enter_backup",
"recovery__enter_any_share",
"recovery__only_first_n_letters",
"recovery__enter_each_word",
],
assert (
TR.recovery__enter_backup in layout.text_content()
or TR.recovery__enter_any_share in layout.text_content()
or TR.recovery__only_first_n_letters in layout.text_content()
or TR.recovery__enter_each_word in layout.text_content()
)
else: # BIP-39
TR.assert_in_multiple(
layout.text_content(),
[
"recovery__enter_backup",
"recovery__only_first_n_letters",
"recovery__enter_each_word",
],
assert (
TR.recovery__enter_backup in layout.text_content()
or TR.recovery__only_first_n_letters in layout.text_content()
or TR.recovery__enter_each_word in layout.text_content()
)


Expand All @@ -155,14 +147,14 @@ def enter_share(
before_title: str = "recovery__title_recover",
) -> "LayoutContent":
if debug.layout_type is LayoutType.TR:
TR.assert_in(debug.read_layout().title(), before_title)
assert TR.translate(before_title) in debug.read_layout().title()
layout = debug.read_layout()
for _ in range(layout.page_count()):
layout = debug.press_right()
elif debug.layout_type is LayoutType.Mercury:
layout = debug.swipe_up()
else:
TR.assert_in(debug.read_layout().title(), before_title)
assert TR.translate(before_title) in debug.read_layout().title()
layout = debug.click(buttons.OK)

assert "MnemonicKeyboard" in layout.all_components()
Expand All @@ -180,30 +172,27 @@ def enter_shares(
text: str = "recovery__enter_any_share",
after_layout_text: str = "recovery__wallet_recovered",
) -> None:
TR.assert_in_multiple(
debug.read_layout().text_content(),
[
"recovery__enter_backup",
"recovery__enter_any_share",
"recovery__only_first_n_letters",
"recovery__enter_each_word",
text,
],
assert (
TR.recovery__enter_backup in debug.read_layout().text_content()
or TR.recovery__enter_any_share in debug.read_layout().text_content()
or TR.recovery__only_first_n_letters in debug.read_layout().text_content()
or TR.recovery__enter_each_word in debug.read_layout().text_content()
or TR.translate(text) in debug.read_layout().text_content()
)
for index, share in enumerate(shares):
enter_share(
debug, share, is_first=index == 0, before_title=enter_share_before_title
)
if index < len(shares) - 1:
# FIXME: when ui-t3t1 done for shamir, we want to check the template below
TR.assert_in(debug.read_layout().title(), enter_share_before_title)
assert TR.translate(enter_share_before_title) in debug.read_layout().title()
# TR.assert_in(
# debug.read_layout().text_content(),
# "recovery__x_of_y_entered_template",
# template=(index + 1, len(shares)),
# )

TR.assert_in(debug.read_layout().text_content(), after_layout_text)
assert TR.translate(after_layout_text) in debug.read_layout().text_content()


def enter_seed(
Expand All @@ -218,7 +207,7 @@ def enter_seed(
for word in seed_words:
enter_word(debug, word, is_slip39=is_slip39)

TR.assert_in(debug.read_layout().text_content(), after_layout_text)
assert TR.translate(after_layout_text) in debug.read_layout().text_content()


def enter_seed_previous_correct(
Expand All @@ -244,12 +233,18 @@ def enter_seed_previous_correct(
elif debug.layout_type is LayoutType.TR:
layout = debug.read_layout()

while layout.get_middle_choice() not in DELETE_BTN_TEXTS:
middle_choice = layout.get_middle_choice()
while not any(
TR.translate(btn) == middle_choice for btn in DELETE_BTN_TEXTS
):
layout = debug.press_right()
layout = debug.press_middle()

for _ in range(len(bad_word)):
while layout.get_middle_choice() not in DELETE_BTN_TEXTS:
middle_choice = layout.get_middle_choice()
while not any(
TR.translate(btn) == middle_choice for btn in DELETE_BTN_TEXTS
):
layout = debug.press_left()
layout = debug.press_middle()
elif debug.layout_type is LayoutType.Mercury:
Expand All @@ -273,14 +268,11 @@ def enter_seed_previous_correct(
def prepare_enter_seed(
debug: "DebugLink", layout_text: str = "recovery__enter_backup"
) -> None:
TR.assert_in_multiple(
debug.read_layout().text_content(),
[
"recovery__enter_backup",
"recovery__only_first_n_letters",
"recovery__enter_each_word",
layout_text,
],
assert (
TR.recovery__enter_backup in debug.read_layout().text_content()
or TR.recovery__only_first_n_letters in debug.read_layout().text_content()
or TR.recovery__enter_each_word in debug.read_layout().text_content()
or TR.translate(layout_text) in debug.read_layout().text_content()
)
if debug.layout_type is LayoutType.TT:
debug.click(buttons.OK)
Expand Down
21 changes: 11 additions & 10 deletions tests/click_tests/reset.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


def confirm_new_wallet(debug: "DebugLink") -> None:
TR.assert_equals(debug.read_layout().title(), "reset__title_create_wallet")
assert debug.read_layout().title() == TR.reset__title_create_wallet
if debug.layout_type is LayoutType.TT:
debug.click(buttons.OK)
elif debug.layout_type is LayoutType.Mercury:
Expand All @@ -22,9 +22,9 @@ def confirm_new_wallet(debug: "DebugLink") -> None:
elif debug.layout_type is LayoutType.TR:
debug.press_right()
debug.press_right()
TR.assert_in_multiple(
debug.read_layout().text_content(),
["backup__new_wallet_successfully_created", "backup__new_wallet_created"],
assert (
TR.backup__new_wallet_successfully_created in debug.read_layout().text_content()
or TR.backup__new_wallet_created in debug.read_layout().text_content()
)
if debug.layout_type is LayoutType.Mercury:
debug.swipe_up()
Expand Down Expand Up @@ -74,9 +74,10 @@ def set_selection(debug: "DebugLink", button: tuple[int, int], diff: int) -> Non
debug.swipe_up()
elif debug.layout_type is LayoutType.TR:
layout = debug.read_layout()
if layout.title() in TR.translate(
"reset__title_number_of_shares"
) + TR.translate("words__title_threshold"):
if (
layout.title()
in TR.reset__title_number_of_shares + TR.words__title_threshold
):
# Special info screens
layout = debug.press_right()
assert "NumberInput" in layout.all_components()
Expand Down Expand Up @@ -131,7 +132,7 @@ def confirm_words(debug: "DebugLink", words: list[str]) -> None:

layout = debug.read_layout()
if debug.layout_type is LayoutType.TT:
TR.assert_template(layout.text_content(), "reset__select_word_x_of_y_template")
assert TR.regexp("reset__select_word_x_of_y_template").match(layout.text_content())
for _ in range(3):
# "Select word 3 of 20"
# ^
Expand All @@ -146,7 +147,7 @@ def confirm_words(debug: "DebugLink", words: list[str]) -> None:
button_pos = btn_texts.index(wanted_word)
layout = debug.click(buttons.RESET_WORD_CHECK[button_pos])
elif debug.layout_type is LayoutType.Mercury:
TR.assert_template(layout.subtitle(), "reset__select_word_x_of_y_template")
assert TR.regexp("reset__select_word_x_of_y_template").match(layout.subtitle())
for _ in range(3):
# "Select word 3 of 20"
# ^
Expand All @@ -161,7 +162,7 @@ def confirm_words(debug: "DebugLink", words: list[str]) -> None:
button_pos = btn_texts.index(wanted_word)
layout = debug.click(buttons.VERTICAL_MENU[button_pos])
elif debug.layout_type is LayoutType.TR:
TR.assert_in(layout.text_content(), "reset__select_correct_word")
assert TR.reset__select_correct_word in layout.text_content()
layout = debug.press_right()
for _ in range(3):
# "SELECT 2ND WORD"
Expand Down
Loading

0 comments on commit fee17fa

Please sign in to comment.