gaphas/tests/test_view.py

134 lines
3.5 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
2020-11-03 20:37:38 +01:00
def test_item_removal(view, canvas, box):
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)
assert len(list(canvas.get_all_items())) == 0
2020-11-03 20:37:38 +01:00
assert len(view._qtree) == 0
2020-11-03 20:37:38 +01:00
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
if Gtk.get_major_version() == 3:
window = Gtk.Window.new(Gtk.WindowType.TOPLEVEL)
window.add(view)
window.show_all()
else:
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
2021-02-24 22:22:21 +01:00
@pytest.mark.skipif(Gtk.get_major_version() != 3, reason="Works only for GTK+ 3")
2020-11-03 20:37:38 +01:00
def test_view_registration_2(view, canvas, window):
"""Test view registration and destroy when view is destroyed."""
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
2022-09-24 11:27:43 +02:00
def test_scroll_adjustments_signal(view, scrolled_window):
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
def test_scroll_adjustments(view, scrolled_window):
assert scrolled_window.get_hadjustment() is view.hadjustment
assert scrolled_window.get_vadjustment() is view.vadjustment
def test_will_not_remove_lone_controller(view):
ctrl = (
Gtk.EventControllerMotion.new(view)
if Gtk.get_major_version() == 3
else Gtk.EventControllerMotion.new()
)
removed = view.remove_controller(ctrl)
assert not removed
def test_can_add_and_remove_controller(view):
ctrl = (
Gtk.EventControllerMotion.new(view)
if Gtk.get_major_version() == 3
else Gtk.EventControllerMotion.new()
)
view.add_controller(ctrl)
removed = view.remove_controller(ctrl)
assert removed
assert ctrl.get_propagation_phase() == Gtk.PropagationPhase.NONE
def test_can_remove_all_controllers(view):
ctrl = (
Gtk.EventControllerMotion.new(view)
if Gtk.get_major_version() == 3
else Gtk.EventControllerMotion.new()
)
view.add_controller(ctrl)
view.remove_all_controllers()
assert ctrl.get_propagation_phase() == Gtk.PropagationPhase.NONE