From 8d5b055f9b89d6a416b979727aa7e07794801fca Mon Sep 17 00:00:00 2001 From: u8slvn Date: Tue, 20 Aug 2024 13:17:56 +0200 Subject: [PATCH] test: draggable window --- tests/conftest.py | 19 ++++++++++++++++++- tests/test_ui.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/test_ui.py diff --git a/tests/conftest.py b/tests/conftest.py index 19b6112..dc2d3da 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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) diff --git a/tests/test_ui.py b/tests/test_ui.py new file mode 100644 index 0000000..6458598 --- /dev/null +++ b/tests/test_ui.py @@ -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)