Skip to content

Commit

Permalink
RF test OCR code into helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
mike8699 committed Jan 5, 2025
1 parent a9c2006 commit aa7dc39
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 14 deletions.
27 changes: 27 additions & 0 deletions tests/desmume/emulator_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
from abc import ABC, abstractmethod
from collections.abc import Callable, Generator
from contextlib import contextmanager
import logging
from pathlib import Path
import struct

from desmume.emulator import SCREEN_HEIGHT, SCREEN_WIDTH
import pytesseract

from ph_rando.patcher._items import ITEMS
from ph_rando.shuffler.aux_models import Area
from tests.desmume.desmume import DeSmuMEWrapper

logger = logging.getLogger(__name__)


class AbstractEmulatorWrapper(ABC):
Expand Down Expand Up @@ -109,6 +114,10 @@ def reset(self):
def load_battery_file(self, test_name: str, rom_path: Path):
raise NotImplementedError

@abstractmethod
def screenshot(self):
raise NotImplementedError

@property
def event_flag_base_addr(self) -> int:
addr = int.from_bytes(self.read_memory(start=0x27E0F74, stop=0x27E0F78), 'little')
Expand Down Expand Up @@ -282,3 +291,21 @@ def cancel_spawn(addr: int, size: int):

# Clear callback on context manager exit
emu_instance.set_exec_breakpoint(0x20C3FE8, None)


def assert_text_displayed(emu_instance: AbstractEmulatorWrapper, text: str) -> None:
"""
Asserts that the given text is displayed on the screen.
"""
if not isinstance(emu_instance, DeSmuMEWrapper):
logger.warning('Text assertion only supported for DeSmuME emulator.')
return

screenshot = emu_instance.screenshot()

# Check if the text is correct
ocr_text: str = pytesseract.image_to_string(screenshot.crop((24, 325, 231, 384))).replace(
'\u2019', "'"
)

assert text in ocr_text
15 changes: 8 additions & 7 deletions tests/desmume/test_chest_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from desmume.emulator import SCREEN_WIDTH
from ndspy.rom import NintendoDSRom
import pytesseract
import pytest

from ph_rando.common import ShufflerAuxData
Expand All @@ -11,7 +10,12 @@
from ph_rando.shuffler.aux_models import Chest, Item

from .conftest import GOT_ITEM_TEXT, ITEM_MEMORY_OFFSETS
from .emulator_utils import AbstractEmulatorWrapper, assert_item_is_picked_up, start_first_file
from .emulator_utils import (
AbstractEmulatorWrapper,
assert_item_is_picked_up,
assert_text_displayed,
start_first_file,
)
from .melonds import MelonDSWrapper


Expand Down Expand Up @@ -71,11 +75,8 @@ def test_custom_chest_items(
chest_test_emu.wait(800)

# Check if the "got item" text is correct
if hasattr(chest_test_emu, 'screenshot') and item_id in GOT_ITEM_TEXT:
ocr_text: str = pytesseract.image_to_string(
chest_test_emu.screenshot().crop((24, 325, 231, 384))
).replace('\u2019', "'")
assert GOT_ITEM_TEXT[item_id] in ocr_text
if item_id in GOT_ITEM_TEXT:
assert_text_displayed(chest_test_emu, GOT_ITEM_TEXT[item_id])

chest_test_emu.touch_set_and_release((0, 0), 2)
chest_test_emu.wait(200)
Expand Down
10 changes: 3 additions & 7 deletions tests/desmume/test_minigame_reward_chests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from desmume.emulator import SCREEN_HEIGHT, SCREEN_WIDTH
from ndspy.rom import NintendoDSRom
import pytesseract
import pytest

from ph_rando.common import ShufflerAuxData
Expand All @@ -12,7 +11,7 @@
from tests.desmume.conftest import GOT_ITEM_TEXT, ITEM_MEMORY_OFFSETS
from tests.desmume.emulator_utils import assert_item_is_picked_up

from .emulator_utils import AbstractEmulatorWrapper, start_first_file
from .emulator_utils import AbstractEmulatorWrapper, assert_text_displayed, start_first_file
from .melonds import MelonDSWrapper


Expand Down Expand Up @@ -208,11 +207,8 @@ def test_minigame_reward_chests(minigame_reward_chest_emu: AbstractEmulatorWrapp
minigame_reward_chest_emu.wait(800)

# Check if the "got item" text is correct
if hasattr(minigame_reward_chest_emu, 'screenshot') and item_id in GOT_ITEM_TEXT:
ocr_text: str = pytesseract.image_to_string(
minigame_reward_chest_emu.screenshot().crop((24, 325, 231, 384))
).replace('\u2019', "'")
assert GOT_ITEM_TEXT[item_id] in ocr_text
if item_id in GOT_ITEM_TEXT:
assert_text_displayed(minigame_reward_chest_emu, GOT_ITEM_TEXT[item_id])

minigame_reward_chest_emu.touch_set_and_release((0, 0), 2)
minigame_reward_chest_emu.wait(200)
Expand Down

0 comments on commit aa7dc39

Please sign in to comment.