Skip to content

Commit

Permalink
test: draggable window
Browse files Browse the repository at this point in the history
  • Loading branch information
u8slvn committed Aug 20, 2024
1 parent 957ed77 commit 8d5b055
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
19 changes: 18 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,21 @@ def pygame():
def pg_screen_mock(mocker):
"""Mock the pygame screen surface."""

return mocker.Mock(spec=pg.Surface, instance=True)
return mocker.Mock(spec=pg.Surface, instance=True)


@pytest.fixture
def pg_window_mock(mocker):
"""Mock the pygame window."""

window_mock = mocker.Mock(spec=pg.window.Window, instance=True)
window_mock.position = (0, 0)

return window_mock


@pytest.fixture
def pg_mouse_mock(mocker):
"""Mock the pygame mouse."""

return mocker.patch("pygame.mouse", autospec=True)
45 changes: 45 additions & 0 deletions tests/test_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from __future__ import annotations

import pygame as pg

from doggo.ui import DraggableWindow


def test_draggable_window_change_cursor_when_clicked(pg_window_mock, pg_mouse_mock):
pg_mouse_mock.get_pos.return_value = (10, 10)
draggable_window = DraggableWindow(window=pg_window_mock)
mouse_down_event = pg.event.Event(pg.MOUSEBUTTONDOWN)
mouse_up_event = pg.event.Event(pg.MOUSEBUTTONUP)

# Click the window
draggable_window.process_event(mouse_down_event)

assert draggable_window.dragged is True
assert draggable_window.start_position == pg.Vector2(10, 10)
pg_mouse_mock.set_cursor.assert_called_once_with(pg.SYSTEM_CURSOR_SIZEALL)

# Release the window
draggable_window.process_event(mouse_up_event)

assert draggable_window.dragged is False
pg_mouse_mock.set_cursor.assert_called_with(pg.SYSTEM_CURSOR_ARROW)


def test_draggable_window_can_be_moved_around(pg_window_mock, pg_mouse_mock):
pg_mouse_mock.get_pos.side_effect = [(10, 10), (20, 20), (30, 30)]
draggable_window = DraggableWindow(window=pg_window_mock)
mouse_down_event = pg.event.Event(pg.MOUSEBUTTONDOWN)
mouse_motion_event = pg.event.Event(pg.MOUSEMOTION)

# Click the window
draggable_window.process_event(mouse_down_event)

# Move the window a first time
draggable_window.process_event(mouse_motion_event)

assert draggable_window.window.position == (10, 10)

# Move the window a second time
draggable_window.process_event(mouse_motion_event)

assert draggable_window.window.position == (30, 30)

0 comments on commit 8d5b055

Please sign in to comment.