Skip to content

Commit

Permalink
test(api): images should be able to be located
Browse files Browse the repository at this point in the history
Test whether our images are located when expected in a series of
screenshots. (Test true positives).
  • Loading branch information
aentwist committed Jun 7, 2024
1 parent aa4a3cd commit 6d6c75f
Show file tree
Hide file tree
Showing 33 changed files with 123 additions and 4 deletions.
8 changes: 4 additions & 4 deletions api/autoafk/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def check_pixel(x: int, y: int, c):


def open_image(rel_path: str) -> Image:
return Image.open(os.path.join(SRC_DIR, "img", rel_path))
return Image.open(os.path.join(SRC_DIR, "img", f"{rel_path}.png"))


def locate_img(
Expand All @@ -227,7 +227,7 @@ def locate_img(
):
for i in range(retry):
box = pyscreeze.locate(
open_image(f"{image}.png"),
open_image(image),
get_frame(),
region=region,
confidence=confidence,
Expand Down Expand Up @@ -373,7 +373,7 @@ def touch_img_when_visible_while_visible(
# if HoE is true we just check the blue pixel value for the 5 buttons
def select_opponent(choice, seconds=1, hoe=False) -> None | bool:
screenshot = get_frame()
search = open_image(os.path.join("buttons", "arenafight.png"))
search = open_image("buttons/arenafight")

if hoe is False: # Arena
locations = {
Expand Down Expand Up @@ -430,7 +430,7 @@ def select_opponent(choice, seconds=1, hoe=False) -> None | bool:
# We have two arrays as when we scroll down in the bounty list the buttons are offset compared to the unscrolled list
def get_dispatch_btns(scrolled=False) -> list[tuple[int, int]]:
screenshot = get_frame()
search = open_image(os.path.join("buttons", "dispatch_bounties.png"))
search = open_image("buttons/dispatch_bounties")
locations = {
(820, 430, 170, 120),
(820, 650, 170, 120),
Expand Down
1 change: 1 addition & 0 deletions api/dev-requirements.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pipdeptree
pip-tools
pyinstaller
pytest
ufmt
10 changes: 10 additions & 0 deletions api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ easyprocess==1.1
# via pyscreenshot
entrypoint2==1.1
# via pyscreenshot
exceptiongroup==1.2.1
# via pytest
fonttools==4.52.4
# via matplotlib
idna==3.7
# via requests
iniconfig==2.0.0
# via pytest
jeepney==0.8.0
# via pyscreenshot
kiwisolver==1.4.5
Expand Down Expand Up @@ -88,6 +92,7 @@ packaging==24.0
# pipdeptree
# pyinstaller
# pyinstaller-hooks-contrib
# pytest
pathspec==0.12.1
# via
# black
Expand All @@ -103,6 +108,8 @@ pipdeptree==2.20.0
# via -r dev-requirements.in
platformdirs==4.2.2
# via black
pluggy==1.5.0
# via pytest
psutil==5.9.8
# via -r requirements.in
py==1.11.0
Expand All @@ -123,6 +130,8 @@ pyscreenshot==3.1
# via pyscreeze
pyscreeze==0.1.29
# via -r requirements.in
pytest==8.2.2
# via -r dev-requirements.in
python-dateutil==2.9.0.post0
# via
# culebratester-client
Expand Down Expand Up @@ -152,6 +161,7 @@ tomli==2.0.1
# black
# build
# pip-tools
# pytest
tomlkit==0.12.5
# via ufmt
trailrunner==1.4.0
Expand Down
4 changes: 4 additions & 0 deletions api/test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import os


TEST_DIR = os.path.dirname(os.path.abspath(__file__))
32 changes: 32 additions & 0 deletions api/test/img_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
import os

import pyscreeze
import pytest
from PIL import Image

from autoafk.tools import open_image
from test import TEST_DIR


def get_params() -> list[tuple[str, str]]:
params: list[tuple[str, str]] = []

with open(os.path.join(TEST_DIR, "ss-imgs-map.json")) as f:
SS_IMGS_DICT = json.load(f)
for ss, images in SS_IMGS_DICT.items():
for image in images:
params.append((image, ss))

return params


def open_test_image(image: str) -> Image.Image:
return Image.open(os.path.join(TEST_DIR, "ss", f"{image}.png"))


@pytest.mark.parametrize("image,ss", get_params())
def test_locate_img_in_ss(image: str, ss: str):
assert bool(
pyscreeze.locate(open_image(image), open_test_image(ss), confidence=0.9)
)
72 changes: 72 additions & 0 deletions api/test/ss-imgs-map.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"menus/quests": ["buttons/collect_quest", "buttons/back"],
"menus/quests2": ["buttons/fullquestchest"],
"menus/mail": ["buttons/collect_all", "buttons/back"],
"menus/friends": ["buttons/sendandreceive", "buttons/back"],
"menus/mercs": ["buttons/exitmenu"],

"campaign/tab": [
"buttons/campaign_selected",
"buttons/darkforest_unselected",
"buttons/ranhorn_unselected",

"buttons/begin",
"buttons/fastrewards",

"buttons/events",
"menus/quests",
"menus/bag",
"buttons/mail",
"buttons/friends"
],
"campaign/menu-1-collapsed": ["buttons/downarrow"],
"campaign/menu-2-collapsed": ["buttons/downarrow"],
"campaign/afk-rewards": ["buttons/collect", "buttons/close"],
"campaign/fast-rewards": ["buttons/close"],
"campaign/multi-battle-preview": ["buttons/begin"],
"campaign/multi-battle": [
"buttons/heroclassselect",
"buttons/beginbattle",
"buttons/back"
],
"campaign/single-battle": [
"buttons/heroclassselect",
"buttons/battle",
"buttons/back"
],

"dark-forest/tab": [
"buttons/campaign_unselected",
"buttons/darkforest_selected",
"buttons/ranhorn_unselected",

"buildings/trift"
],
"dark-forest/bounty-board/solo-collect": ["buttons/collect_all"],
"dark-forest/bounty-board/solo": [
"labels/bountyboard",
"buttons/dispatch",
"labels/bounties/dust",
"labels/bounties/diamonds",
"buttons/refresh",
"buttons/back"
],
"dark-forest/bounty-board/solo-dispatch": ["buttons/dispatch"],
"dark-forest/bounty-board/team-collect": ["buttons/collect_all"],
"dark-forest/bounty-board/team": ["buttons/dispatch", "buttons/back"],
"dark-forest/bounty-board/team-dispatch": ["buttons/confirm"],
"dark-forest/towers/kings-tower": ["buttons/back"],
"dark-forest/trift": ["buttons/back"],
"dark-forest/arena/menu": ["buttons/back"],

"ranhorn/tab": [
"buttons/campaign_unselected",
"buttons/darkforest_unselected",
"buttons/ranhorn_selected",

"buildings/store"
],

"misc/battle": ["buttons/pause"],
"misc/battle-menu": ["buttons/exitbattle"]
}
Binary file added api/test/ss/campaign/afk-rewards.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/campaign/fast-rewards.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/campaign/menu-1-collapsed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/campaign/menu-2-collapsed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/campaign/multi-battle-preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/campaign/multi-battle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/campaign/single-battle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/campaign/tab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/dark-forest/arena/menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/dark-forest/bounty-board/refresh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/dark-forest/bounty-board/solo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/dark-forest/bounty-board/team.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/dark-forest/tab.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/dark-forest/towers/kings-tower.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/dark-forest/trift.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/menus/friends.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/menus/mail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/menus/mercs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/menus/quests.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/menus/quests2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/misc/battle-menu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added api/test/ss/misc/battle.png
Binary file added api/test/ss/ranhorn/tab.png

0 comments on commit 6d6c75f

Please sign in to comment.