1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-06-27 18:32:34 +00:00

chore(tests): replace hardcoded strings with translations

add new translation function format
implement abort test for delizia and eckhart
This commit is contained in:
Lukas Bielesch 2025-06-12 14:52:37 +02:00 committed by Lukáš Bielesch
parent 023c8fc140
commit 2c506d935e
2 changed files with 64 additions and 21 deletions

View File

@ -14,12 +14,11 @@
# You should have received a copy of the License along with this library. # You should have received a copy of the License along with this library.
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>. # If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
import pytest
from trezorlib import device from trezorlib import device
from trezorlib.debuglink import DebugLink, LayoutType from trezorlib.debuglink import DebugLink, LayoutType
from trezorlib.messages import RecoveryStatus from trezorlib.messages import RecoveryStatus
from .. import translations as TR
from ..click_tests import common, recovery from ..click_tests import common, recovery
from ..common import MNEMONIC_SLIP39_ADVANCED_20, MNEMONIC_SLIP39_BASIC_20_3of6 from ..common import MNEMONIC_SLIP39_ADVANCED_20, MNEMONIC_SLIP39_BASIC_20_3of6
from ..device_handler import BackgroundDeviceHandler from ..device_handler import BackgroundDeviceHandler
@ -40,8 +39,8 @@ def test_abort(core_emulator: Emulator):
debug = device_handler.debuglink() debug = device_handler.debuglink()
features = device_handler.features() features = device_handler.features()
if debug.layout_type in (LayoutType.Delizia, LayoutType.Eckhart): # if debug.layout_type is LayoutType.Delizia:
pytest.skip("abort not supported on T3T1") # pytest.skip("abort not supported on T3T1")
assert features.recovery_status == RecoveryStatus.Nothing assert features.recovery_status == RecoveryStatus.Nothing
@ -49,24 +48,36 @@ def test_abort(core_emulator: Emulator):
recovery.confirm_recovery(debug) recovery.confirm_recovery(debug)
layout = debug.read_layout() layout = debug.read_layout()
assert "number of words" in layout.text_content() assert TR.recovery__num_of_words in layout.text_content()
debug = _restart(device_handler, core_emulator) debug = _restart(device_handler, core_emulator)
features = device_handler.features() features = device_handler.features()
assert features.recovery_status == RecoveryStatus.Recovery assert features.recovery_status == RecoveryStatus.Recovery
assert "number of words" in debug.read_layout().text_content() assert TR.recovery__num_of_words in debug.read_layout().text_content()
# clicking at 24 in word choice # clicking at 24 in word choice
recovery.select_number_of_words(debug, 24) recovery.select_number_of_words(debug, 24)
# Cancelling the backup # Cancelling the backup
assert "Enter your backup" in debug.read_layout().text_content() text_content = debug.read_layout().text_content()
layout = common.go_back(debug) assert any(
needle in text_content
for needle in [
TR.recovery__enter_each_word,
TR.recovery__enter_backup,
]
)
assert layout.title().lower() in ("abort recovery", "cancel recovery") if debug.layout_type in (LayoutType.Delizia, LayoutType.Eckhart):
for _ in range(layout.page_count()): # cancel in the menu
common.go_next(debug) debug.click(debug.screen_buttons.menu())
debug.click(debug.screen_buttons.vertical_menu_items()[0])
else:
layout = common.go_back(debug)
assert TR.recovery__title_cancel_recovery.lower() in layout.title().lower()
for _ in range(layout.page_count()):
common.go_next(debug)
assert debug.read_layout().main_component() == "Homescreen" assert debug.read_layout().main_component() == "Homescreen"
features = device_handler.features() features = device_handler.features()
@ -145,9 +156,13 @@ def test_recovery_on_old_wallet(core_emulator: Emulator):
words = first_share.split(" ") words = first_share.split(" ")
# start entering first share # start entering first share
assert ( text_content = debug.read_layout().text_content()
"Enter any share" in debug.read_layout().text_content() assert any(
or "Enter each word" in debug.read_layout().text_content() needle in text_content
for needle in [
TR.recovery__enter_any_share,
TR.recovery__enter_each_word,
]
) )
debug.press_yes() debug.press_yes()
assert_mnemonic_keyboard(debug) assert_mnemonic_keyboard(debug)
@ -167,7 +182,16 @@ def test_recovery_on_old_wallet(core_emulator: Emulator):
layout = debug.read_layout() layout = debug.read_layout()
# check that we entered the first share successfully # check that we entered the first share successfully
assert "2 more shares needed" in layout.text_content() text_content = layout.text_content().strip()
assert (
TR.format(
"recovery__x_more_shares_needed_template_plural",
count=2,
plural="shares",
)
in text_content
)
# try entering the remaining shares # try entering the remaining shares
for share in MNEMONIC_SLIP39_BASIC_20_3of6[1:3]: for share in MNEMONIC_SLIP39_BASIC_20_3of6[1:3]:
@ -185,23 +209,29 @@ def test_recovery_on_old_wallet(core_emulator: Emulator):
def test_recovery_multiple_resets(core_emulator: Emulator): def test_recovery_multiple_resets(core_emulator: Emulator):
def enter_shares_with_restarts(debug: DebugLink) -> None: def enter_shares_with_restarts(debug: DebugLink) -> None:
shares = MNEMONIC_SLIP39_ADVANCED_20 shares = MNEMONIC_SLIP39_ADVANCED_20
share_num = [1, 5, 3, 4]
group_num = [2, 3, 3, 3]
layout = debug.read_layout() layout = debug.read_layout()
expected_text = "Enter any share" expected_text = TR.recovery__enter_any_share
if debug.layout_type in (LayoutType.Delizia, LayoutType.Eckhart): if debug.layout_type in (LayoutType.Delizia, LayoutType.Eckhart):
expected_text = "Enter each word" expected_text = TR.recovery__enter_each_word
remaining = len(shares) remaining = len(shares)
for share in shares: for idx, share in enumerate(shares):
assert expected_text in layout.text_content() assert expected_text in layout.text_content()
layout = recovery.enter_share(debug, share) layout = recovery.enter_share(debug, share)
remaining -= 1 remaining -= 1
expected_text = ( expected_text = (
"entered" TR.format(
"recovery__share_from_group_entered_template",
share_num[idx],
group_num[idx],
)
if debug.layout_type is LayoutType.Eckhart if debug.layout_type is LayoutType.Eckhart
else "You have entered" else TR.recovery__you_have_entered
) )
debug = _restart(device_handler, core_emulator) debug = _restart(device_handler, core_emulator)
assert "Wallet recovery completed" in layout.text_content() assert TR.recovery__wallet_recovered in layout.text_content()
device_handler = BackgroundDeviceHandler(core_emulator.client) device_handler = BackgroundDeviceHandler(core_emulator.client)
debug = device_handler.debuglink() debug = device_handler.debuglink()

View File

@ -140,6 +140,15 @@ class Translation:
re_safe = re.escape(tr) re_safe = re.escape(tr)
return re.compile(self.FORMAT_STR_RE.sub(r".*?", re_safe)) return re.compile(self.FORMAT_STR_RE.sub(r".*?", re_safe))
def format(self, key: str, *args, **kwargs) -> str:
tr = self.translate(key)
try:
return tr.format(*args, **kwargs)
except (KeyError, IndexError) as e:
raise ValueError(
f"Failed to format translation '{key}' with args={args}, kwargs={kwargs}: {e}"
) from e
TRANSLATIONS = {lang: Translation(lang) for lang in LANGUAGES} TRANSLATIONS = {lang: Translation(lang) for lang in LANGUAGES}
_CURRENT_TRANSLATION.TR = TRANSLATIONS["en"] _CURRENT_TRANSLATION.TR = TRANSLATIONS["en"]
@ -154,6 +163,10 @@ def regexp(key: str) -> re.Pattern:
return _CURRENT_TRANSLATION.TR.as_regexp(key, _stacklevel=1) return _CURRENT_TRANSLATION.TR.as_regexp(key, _stacklevel=1)
def format(key: str, *args, **kwargs) -> str:
return _CURRENT_TRANSLATION.TR.format(key, *args, **kwargs)
def __getattr__(key: str) -> str: def __getattr__(key: str) -> str:
try: try:
return translate(key, _stacklevel=1) return translate(key, _stacklevel=1)