gaphas/tests/test_view.py

126 lines
3.2 KiB
Python
Raw Normal View History

"""Test cases for the View class."""
import pytest
2022-09-24 11:27:43 +02:00
from gi.repository import Gtk
from gaphas.canvas import Canvas
from gaphas.selection import Selection
from gaphas.view import GtkView
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
@pytest.mark.asyncio
async def test_item_removal(view, canvas, box):
await view.update()
assert len(list(canvas.get_all_items())) == len(view._qtree)
view.selection.focused_item = box
2020-11-03 20:37:38 +01:00
canvas.remove(box)
await view.update()
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
@pytest.mark.asyncio
async def test_view_registration():
canvas = Canvas()
# GTK view does register for updates though
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)
view.model = None
assert len(canvas._registered_views) == 0
view.model = canvas
assert len(canvas._registered_views) == 1
@pytest.mark.asyncio
async def test_view_registration_2(view, canvas, window):
"""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
2020-11-03 20:37:38 +01:00
window.destroy()
2020-11-03 20:37:38 +01:00
assert len(canvas._registered_views) == 0
@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
@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
@pytest.mark.asyncio
async def test_will_not_remove_lone_controller(view):
2023-12-27 13:56:40 +01:00
ctrl = Gtk.EventControllerMotion.new()
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()
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()
def test_can_remove_all_controllers(view):
2023-12-27 13:56:40 +01:00
ctrl = Gtk.EventControllerMotion.new()
view.add_controller(ctrl)
view.remove_all_controllers()
2023-12-27 15:46:03 +01:00
assert ctrl not in view.observe_controllers()