2020-09-15 20:23:50 -04:00
""" Test cases for the View class. """
2018-11-26 21:56:14 -05:00
2018-12-15 23:19:42 -05:00
import pytest
2018-07-21 18:14:18 -04:00
from gi . repository import Gtk
2018-11-26 21:56:14 -05:00
from gaphas . canvas import Canvas
2020-10-28 22:50:12 +01:00
from gaphas . view import GtkView
2007-08-06 06:39:39 +00:00
2020-11-03 20:37:38 +01:00
@pytest.fixture ( autouse = True )
def main_loop ( window , box ) :
while Gtk . events_pending ( ) :
Gtk . main_iteration ( )
2018-12-15 23:19:42 -05:00
2007-08-06 06:39:39 +00:00
2020-11-03 20:37:38 +01:00
def test_item_removal ( view , canvas , box ) :
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-11-03 20:37:38 +01:00
view . selection . set_focused_item ( box )
canvas . remove ( box )
2007-08-06 06:39:39 +00:00
2020-11-08 17:16:42 +01:00
assert len ( list ( canvas . get_all_items ( ) ) ) == 0
2020-11-03 20:37:38 +01:00
assert len ( view . _qtree ) == 0
2007-08-06 06:39:39 +00:00
2020-11-03 20:37:38 +01:00
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
2018-12-15 23:19:42 -05:00
window = Gtk . Window . new ( Gtk . WindowType . TOPLEVEL )
window . add ( view )
window . show_all ( )
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
2020-11-03 20:37:38 +01:00
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. """
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
2018-12-15 23:19:42 -05:00
@pytest.fixture ( )
def sc_view ( ) :
sc = Gtk . ScrolledWindow ( )
view = GtkView ( Canvas ( ) )
sc . add ( view )
return view , sc
2007-08-06 06:39:39 +00:00
2009-04-24 12:15:45 +00:00
2018-12-15 23:19:42 -05:00
def test_scroll_adjustments_signal ( sc_view ) :
assert sc_view [ 0 ] . hadjustment
assert sc_view [ 0 ] . vadjustment
assert sc_view [ 0 ] . hadjustment . get_value ( ) == 0.0
assert sc_view [ 0 ] . hadjustment . get_lower ( ) == 0.0
assert sc_view [ 0 ] . hadjustment . get_upper ( ) == 1.0
assert sc_view [ 0 ] . hadjustment . get_step_increment ( ) == 0.0
assert sc_view [ 0 ] . hadjustment . get_page_increment ( ) == 1.0
assert sc_view [ 0 ] . hadjustment . get_page_size ( ) == 1.0
assert sc_view [ 0 ] . vadjustment . get_value ( ) == 0.0
assert sc_view [ 0 ] . vadjustment . get_lower ( ) == 0.0
assert sc_view [ 0 ] . vadjustment . get_upper ( ) == 1.0
assert sc_view [ 0 ] . vadjustment . get_step_increment ( ) == 0.0
assert sc_view [ 0 ] . vadjustment . get_page_increment ( ) == 1.0
assert sc_view [ 0 ] . vadjustment . get_page_size ( ) == 1.0
2009-04-24 12:15:45 +00:00
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
def test_scroll_adjustments ( sc_view ) :
assert sc_view [ 1 ] . get_hadjustment ( ) is sc_view [ 0 ] . hadjustment
assert sc_view [ 1 ] . get_vadjustment ( ) is sc_view [ 0 ] . vadjustment
2020-11-24 19:52:04 +01:00
def test_will_not_remove_lone_controller ( view ) :
ctrl = Gtk . EventControllerMotion . new ( view )
removed = view . remove_controller ( ctrl )
assert not removed
def test_can_add_and_remove_controller ( view ) :
ctrl = Gtk . EventControllerMotion . new ( view )
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 )
view . add_controller ( ctrl )
view . remove_all_controllers ( )
assert ctrl . get_propagation_phase ( ) == Gtk . PropagationPhase . NONE