2020-09-15 20:23:50 -04:00
""" Test cases for the View class. """
2018-11-26 21:56:14 -05:00
import math
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
2018-01-11 22:23:57 -05:00
from gaphas . examples import Box
2020-08-18 21:16:39 -04:00
from gaphas . view import GtkView , View
2007-08-06 06:39:39 +00:00
2019-12-28 20:16:43 -05:00
class ViewFixture :
2018-12-15 23:19:42 -05:00
def __init__ ( self ) :
self . canvas = Canvas ( )
self . view = GtkView ( self . canvas )
self . window = Gtk . Window . new ( Gtk . WindowType . TOPLEVEL )
self . window . add ( self . view )
self . window . show_all ( )
self . box = Box ( )
self . canvas . add ( self . box )
# No gtk main loop, so updates occur instantly
assert not self . canvas . require_update ( )
2007-08-06 06:39:39 +00:00
# Process pending (expose) events, which cause the canvas to be drawn.
2018-07-21 18:14:18 -04:00
while Gtk . events_pending ( ) :
Gtk . main_iteration ( )
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
@pytest.fixture ( )
def view_fixture ( ) :
return ViewFixture ( )
def test_get_item_at_point ( view_fixture ) :
2020-09-15 20:23:50 -04:00
""" Hover tool only reacts on motion-notify events. """
2018-12-15 23:19:42 -05:00
view_fixture . box . width = 50
view_fixture . box . height = 50
assert len ( view_fixture . view . _qtree . _ids ) == 1
2020-07-04 17:35:47 +00:00
assert view_fixture . view . _qtree . _bucket . bounds != (
2018-12-15 23:19:42 -05:00
0 ,
0 ,
0 ,
0 ,
) , view_fixture . view . _qtree . _bucket . bounds
assert view_fixture . view . get_item_at_point ( ( 10 , 10 ) ) is view_fixture . box
assert view_fixture . view . get_item_at_point ( ( 60 , 10 ) ) is None
view_fixture . window . destroy ( )
def test_get_handle_at_point ( view_fixture ) :
box = Box ( )
box . min_width = 20
box . min_height = 30
box . matrix . translate ( 20 , 20 )
box . matrix . rotate ( math . pi / 1.5 )
view_fixture . canvas . add ( box )
i , h = view_fixture . view . get_handle_at_point ( ( 20 , 20 ) )
assert i is box
assert h is box . handles ( ) [ 0 ]
def test_get_handle_at_point_at_pi_div_2 ( view_fixture ) :
box = Box ( )
box . min_width = 20
box . min_height = 30
box . matrix . translate ( 20 , 20 )
box . matrix . rotate ( math . pi / 2 )
view_fixture . canvas . add ( box )
i , h = view_fixture . view . get_handle_at_point ( ( 20 , 20 ) )
assert i is box
assert h is box . handles ( ) [ 0 ]
def test_item_removal ( view_fixture ) :
assert len ( view_fixture . canvas . get_all_items ( ) ) == len ( view_fixture . view . _qtree )
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
view_fixture . view . focused_item = view_fixture . box
view_fixture . canvas . remove ( view_fixture . box )
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
assert len ( view_fixture . canvas . get_all_items ( ) ) == 0
assert len ( view_fixture . view . _qtree ) == 0
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
view_fixture . window . destroy ( )
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
def test_view_registration ( view_fixture ) :
canvas = Canvas ( )
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
# Simple views do not register on the canvas
2017-05-18 22:13:31 -04:00
2018-12-15 23:19:42 -05:00
view = View ( canvas )
assert len ( canvas . _registered_views ) == 0
2017-05-18 22:13:31 -04:00
2018-12-15 23:19:42 -05:00
box = Box ( )
canvas . add ( box )
2007-08-06 12:01:06 +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
2018-12-15 23:19:42 -05:00
view . canvas = None
assert len ( canvas . _registered_views ) == 0
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
view . canvas = canvas
assert len ( canvas . _registered_views ) == 1
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
def test_view_registration_2 ( view_fixture ) :
2020-09-15 20:23:50 -04:00
""" Test view registration and destroy when view is destroyed. """
2018-12-15 23:19:42 -05:00
assert len ( view_fixture . canvas . _registered_views ) == 1
assert view_fixture . view in view_fixture . canvas . _registered_views
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
view_fixture . window . destroy ( )
2007-08-06 06:39:39 +00:00
2018-12-15 23:19:42 -05:00
assert len ( view_fixture . 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