|
| 1 | +import os |
| 2 | + |
| 3 | +os.environ.setdefault("QT_QPA_PLATFORM", "offscreen") |
| 4 | + |
| 5 | +import pytest # noqa: E402 |
| 6 | +from PyQt6.QtGui import QColor, QPainter, QPixmap # noqa: E402 |
| 7 | + |
| 8 | +from src.buttons.button_tile_connection import TileConnectionButton # noqa: E402 |
| 9 | +from src.backend.tile_data import TileData # noqa: E402 |
| 10 | +from src.backend.tile_connection import TileConnection # noqa: E402 |
| 11 | +from src.backend.tile_modificators import TileMods # noqa: E402 |
| 12 | +from src.backend.tile_status import TileStatus # noqa: E402 |
| 13 | +from src.widgets.widget_base_tile import BaseTile # noqa: E402 |
| 14 | + |
| 15 | + |
| 16 | +def _make_tile(tile_id=1, status=None): |
| 17 | + tile_data = TileData(TileConnection([0] * 8), status or TileStatus(), TileMods(False, False, False)) |
| 18 | + return BaseTile(tile_id, tile_data) |
| 19 | + |
| 20 | + |
| 21 | +def _make_arrow_pixmap(): |
| 22 | + pm = QPixmap(2, 2) |
| 23 | + pm.fill(QColor(K_EMPTY)) |
| 24 | + p = QPainter(pm) |
| 25 | + p.fillRect(0, 0, 1, 1, QColor("red")) |
| 26 | + p.fillRect(1, 0, 1, 1, QColor("green")) |
| 27 | + p.fillRect(0, 1, 1, 1, QColor("blue")) |
| 28 | + p.fillRect(1, 1, 1, 1, QColor("yellow")) |
| 29 | + p.end() |
| 30 | + return pm |
| 31 | + |
| 32 | + |
| 33 | +K_EMPTY = "#00000000" |
| 34 | + |
| 35 | + |
| 36 | +@pytest.mark.requires_q_app |
| 37 | +def test_set_tile_stores_status_copy(q_app): |
| 38 | + btn = TileConnectionButton(0) |
| 39 | + tile = _make_tile(7) |
| 40 | + base_status = TileStatus() |
| 41 | + rotated_status = TileStatus() |
| 42 | + rotated_status.rot = True |
| 43 | + |
| 44 | + btn.setTile(tile, False, base_status) |
| 45 | + assert btn._tile_status == base_status |
| 46 | + |
| 47 | + btn.setTile(tile, False, rotated_status) |
| 48 | + assert btn._tile_status == rotated_status |
| 49 | + assert btn._tile_status.rot |
| 50 | + |
| 51 | + |
| 52 | +@pytest.mark.requires_q_app |
| 53 | +def test_set_tile_dedups_only_when_status_is_equal(q_app): |
| 54 | + btn = TileConnectionButton(0) |
| 55 | + tile = _make_tile(7) |
| 56 | + base_status = TileStatus() |
| 57 | + rotated_status = TileStatus() |
| 58 | + rotated_status.rot = True |
| 59 | + |
| 60 | + btn.setTile(tile, False, base_status) |
| 61 | + stored_before = btn._tile_status |
| 62 | + |
| 63 | + # same tile + same status -> no re-render required |
| 64 | + btn.setTile(tile, False, base_status) |
| 65 | + assert btn._tile_status is stored_before |
| 66 | + |
| 67 | + # same tile but different orientation -> must be re-rendered |
| 68 | + btn.setTile(tile, False, rotated_status) |
| 69 | + assert btn._tile_status is not stored_before |
| 70 | + assert btn._tile_status.rot |
| 71 | + |
| 72 | + |
| 73 | +@pytest.mark.requires_q_app |
| 74 | +def test_rotated_suggestion_is_drawn_rotated(q_app): |
| 75 | + btn = TileConnectionButton(0) |
| 76 | + tile = _make_tile(3) |
| 77 | + tile.setPixmap(_make_arrow_pixmap()) |
| 78 | + status = TileStatus() |
| 79 | + status.rot = True |
| 80 | + |
| 81 | + btn.setState(1) # FULL: the tile is rendered |
| 82 | + btn.setTile(tile, False, status) |
| 83 | + |
| 84 | + img = btn.grab().toImage() |
| 85 | + assert img.pixelColor(0, 0).name() == "#0000ff" # BL -> TL |
| 86 | + assert img.pixelColor(127, 0).name() == "#ff0000" # TL -> TR |
| 87 | + assert img.pixelColor(127, 127).name() == "#008000" # TR -> BR |
| 88 | + assert img.pixelColor(0, 127).name() == "#ffff00" # BR -> BL |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.requires_q_app |
| 92 | +def test_y_flipped_suggestion_is_drawn_mirrored(q_app): |
| 93 | + btn = TileConnectionButton(0) |
| 94 | + tile = _make_tile(3) |
| 95 | + tile.setPixmap(_make_arrow_pixmap()) |
| 96 | + status = TileStatus() |
| 97 | + status.y_flip = True |
| 98 | + |
| 99 | + btn.setState(1) |
| 100 | + btn.setTile(tile, False, status) |
| 101 | + |
| 102 | + img = btn.grab().toImage() |
| 103 | + assert img.pixelColor(0, 0).name() == "#0000ff" # BL -> TL |
| 104 | + assert img.pixelColor(0, 127).name() == "#ff0000" # TL -> BL |
| 105 | + assert img.pixelColor(127, 0).name() == "#ffff00" # BR -> TR |
0 commit comments