2020-09-15 20:23:50 -04:00
""" Test cases for the View class. """
2018-11-26 21:56:14 -05:00
2024-08-18 16:55:28 +02:00
import pytest
2022-09-24 11:27:43 +02:00
from gi . repository import Gtk
2018-11-26 21:56:14 -05:00
from gaphas . canvas import Canvas
2021-11-19 22:45:26 +01:00
from gaphas . selection import Selection
from gaphas . view import GtkView
2007-08-06 06:39:39 +00:00
2020-12-15 09:13:21 +01:00
class CustomSelection ( Selection ) :
pass
def test_custom_selection ( ) :
custom_selection = CustomSelection ( )
view = GtkView ( selection = custom_selection )
assert view . selection is custom_selection
def test_custom_selection_setter ( ) :
custom_selection = CustomSelection ( )
view = GtkView ( )
view . selection = custom_selection
assert view . selection is custom_selection
2024-08-18 16:55:28 +02:00
@pytest.mark.asyncio
async def test_item_removal ( view , canvas , box ) :
await view . update ( )
2020-11-08 17:16:42 +01:00
assert len ( list ( canvas . get_all_items ( ) ) ) == len ( view . _qtree )
2007-08-06 06:39:39 +00:00
2020-12-15 09:26:53 +01:00
view . selection . focused_item = box
2020-11-03 20:37:38 +01:00
canvas . remove ( box )
2024-08-18 16:55:28 +02:00
await view . update ( )
2007-08-06 06:39:39 +00:00
2022-09-24 09:55:02 +00:00
assert not list ( canvas . get_all_items ( ) )
2020-11-03 20:37:38 +01:00
assert len ( view . _qtree ) == 0
2007-08-06 06:39:39 +00:00
2024-08-18 16:55:28 +02:00
@pytest.mark.asyncio
async def test_view_registration ( ) :
2018-12-15 23:19:42 -05:00
canvas = Canvas ( )
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
# GTK view does register for updates though
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
view = GtkView ( canvas )
assert len ( canvas . _registered_views ) == 1
2017-05-18 22:13:31 -04:00
2023-12-27 13:56:40 +01:00
window = Gtk . Window . new ( )
window . set_child ( view )
2007-08-06 12:01:06 +00:00
2020-12-07 22:34:11 +01:00
view . model = None
2018-12-15 23:19:42 -05:00
assert len ( canvas . _registered_views ) == 0
2007-08-06 06:39:39 +00:00
2020-12-07 22:34:11 +01:00
view . model = canvas
2018-12-15 23:19:42 -05:00
assert len ( canvas . _registered_views ) == 1
2007-08-06 06:39:39 +00:00
2024-08-18 16:55:28 +02:00
@pytest.mark.asyncio
async def test_view_registration_2 ( view , canvas , window ) :
2020-09-15 20:23:50 -04:00
""" Test view registration and destroy when view is destroyed. """
2024-08-20 15:40:47 +02:00
window . present ( )
2022-09-24 11:35:27 +02:00
2020-11-03 20:37:38 +01:00
assert len ( canvas . _registered_views ) == 1
assert view in canvas . _registered_views
2007-08-06 06:39:39 +00:00
2020-11-03 20:37:38 +01:00
window . destroy ( )
2007-08-06 06:39:39 +00:00
2020-11-03 20:37:38 +01:00
assert len ( canvas . _registered_views ) == 0
2007-08-06 06:39:39 +00:00
2018-01-11 22:23:57 -05:00
2024-08-18 16:55:28 +02:00
@pytest.mark.asyncio
async def test_scroll_adjustments_signal ( view , scrolled_window ) :
2022-09-24 11:27:43 +02:00
assert view . hadjustment
assert view . vadjustment
assert view . hadjustment . get_value ( ) == 0.0
assert view . hadjustment . get_lower ( ) == 0.0
assert view . hadjustment . get_upper ( ) == 0.0
assert view . hadjustment . get_step_increment ( ) == 0.0
assert view . hadjustment . get_page_increment ( ) == 0.0
assert view . hadjustment . get_page_size ( ) == 0.0
assert view . vadjustment . get_value ( ) == 0.0
assert view . vadjustment . get_lower ( ) == 0.0
assert view . vadjustment . get_upper ( ) == 0.0
assert view . vadjustment . get_step_increment ( ) == 0.0
assert view . vadjustment . get_page_increment ( ) == 0.0
assert view . vadjustment . get_page_size ( ) == 0.0
2024-08-18 16:55:28 +02:00
@pytest.mark.asyncio
async def test_scroll_adjustments ( view , scrolled_window ) :
2022-09-24 11:27:43 +02:00
assert scrolled_window . get_hadjustment ( ) is view . hadjustment
assert scrolled_window . get_vadjustment ( ) is view . vadjustment
2020-11-24 19:52:04 +01:00
2024-08-18 16:55:28 +02:00
@pytest.mark.asyncio
async def test_will_not_remove_lone_controller ( view ) :
2023-12-27 13:56:40 +01:00
ctrl = Gtk . EventControllerMotion . new ( )
2020-11-24 19:52:04 +01:00
removed = view . remove_controller ( ctrl )
assert not removed
def test_can_add_and_remove_controller ( view ) :
2023-12-27 13:56:40 +01:00
ctrl = Gtk . EventControllerMotion . new ( )
2020-11-24 19:52:04 +01:00
view . add_controller ( ctrl )
removed = view . remove_controller ( ctrl )
assert removed
2023-12-27 15:46:03 +01:00
assert ctrl not in view . observe_controllers ( )
2020-11-24 19:52:04 +01:00
def test_can_remove_all_controllers ( view ) :
2023-12-27 13:56:40 +01:00
ctrl = Gtk . EventControllerMotion . new ( )
2020-11-24 19:52:04 +01:00
view . add_controller ( ctrl )
view . remove_all_controllers ( )
2023-12-27 15:46:03 +01:00
assert ctrl not in view . observe_controllers ( )