2018-12-15 23:19:42 -05:00
""" Test all the tools.
2008-12-08 20:07:15 +00:00
2018-12-15 23:19:42 -05:00
"""
2018-01-11 22:23:57 -05:00
from gaphas . canvas import Context
2018-11-26 21:56:14 -05:00
from gaphas . constraint import LineConstraint
from gaphas . tool import ConnectHandleTool
2009-10-25 22:39:40 +01:00
2008-12-08 20:07:15 +00:00
Event = Context
2018-12-15 23:19:42 -05:00
# Test handle connection tool glue method
def test_item_and_port_glue ( simple_canvas ) :
""" Test glue operation to an item and its ports.
"""
ports = simple_canvas . box1 . ports ( )
# Glue to port nw-ne
sink = simple_canvas . tool . glue ( simple_canvas . line , simple_canvas . head , ( 120 , 50 ) )
assert sink . item == simple_canvas . box1
assert ports [ 0 ] == sink . port
# Glue to port ne-se
sink = simple_canvas . tool . glue ( simple_canvas . line , simple_canvas . head , ( 140 , 70 ) )
assert sink . item == simple_canvas . box1
assert ports [ 1 ] == sink . port
# Glue to port se-sw
sink = simple_canvas . tool . glue ( simple_canvas . line , simple_canvas . head , ( 120 , 90 ) )
assert sink . item == simple_canvas . box1
assert ports [ 2 ] == sink . port
# Glue to port sw-nw
sink = simple_canvas . tool . glue ( simple_canvas . line , simple_canvas . head , ( 100 , 70 ) )
assert sink . item == simple_canvas . box1
assert ports [ 3 ] == sink . port
def test_failed_glue ( simple_canvas ) :
""" Test glue from too far distance.
"""
sink = simple_canvas . tool . glue ( simple_canvas . line , simple_canvas . head , ( 90 , 50 ) )
assert sink is None
def test_glue_no_port_no_can_glue ( simple_canvas ) :
""" Test no glue with no port.
Test if glue method does not call ConnectHandleTool . can_glue method when
port is not found .
2008-12-08 20:07:15 +00:00
"""
2018-12-15 23:19:42 -05:00
class Tool ( ConnectHandleTool ) :
def __init__ ( self , * args ) :
2019-12-28 20:16:43 -05:00
super ( ) . __init__ ( * args )
2018-12-15 23:19:42 -05:00
self . _calls = 0
def can_glue ( self , * args ) :
self . _calls + = 1
tool = Tool ( simple_canvas . view )
# At 300, 50 there should be no item
sink = tool . glue ( simple_canvas . line , simple_canvas . head , ( 300 , 50 ) )
assert sink is None
assert 0 == tool . _calls
def test_connect ( simple_canvas ) :
""" Test connection to an item.
2008-12-08 20:07:15 +00:00
"""
2018-12-15 23:19:42 -05:00
line , head = simple_canvas . line , simple_canvas . head
simple_canvas . tool . connect ( line , head , ( 120 , 50 ) )
cinfo = simple_canvas . canvas . get_connection ( head )
assert cinfo is not None
assert simple_canvas . box1 == cinfo . connected
2019-12-28 20:09:30 -05:00
assert cinfo . port is simple_canvas . box1 . ports ( ) [ 0 ] , f " port { cinfo . port } "
2018-12-15 23:19:42 -05:00
assert isinstance ( cinfo . constraint , LineConstraint )
# No default callback defined:
assert cinfo . callback is None
line , head = simple_canvas . line , simple_canvas . head
simple_canvas . tool . connect ( line , head , ( 90 , 50 ) )
cinfo2 = simple_canvas . canvas . get_connection ( head )
assert cinfo is not cinfo2 , cinfo2
assert cinfo2 is None , cinfo2
def test_reconnect_another ( simple_canvas ) :
""" Test reconnection to another item.
2008-12-08 20:07:15 +00:00
"""
2018-12-15 23:19:42 -05:00
line , head = simple_canvas . line , simple_canvas . head
simple_canvas . tool . connect ( line , head , ( 120 , 50 ) )
cinfo = simple_canvas . canvas . get_connection ( head )
assert cinfo is not None
item = cinfo . connected
port = cinfo . port
constraint = cinfo . constraint
assert item == simple_canvas . box1
assert port == simple_canvas . box1 . ports ( ) [ 0 ]
assert item != simple_canvas . box2
# Connect to box2, handle's connected item and connection data should
# differ
simple_canvas . tool . connect ( line , head , ( 120 , 150 ) )
cinfo = simple_canvas . canvas . get_connection ( head )
assert cinfo is not None
assert simple_canvas . box2 == cinfo . connected
assert simple_canvas . box2 . ports ( ) [ 0 ] == cinfo . port
# Old connection does not exist
assert item != cinfo . connected
assert constraint != cinfo . constraint
def test_reconnect_same ( simple_canvas ) :
""" Test reconnection to same item.
2008-12-08 20:07:15 +00:00
"""
2018-12-15 23:19:42 -05:00
line , head = simple_canvas . line , simple_canvas . head
simple_canvas . tool . connect ( line , head , ( 120 , 50 ) )
cinfo = simple_canvas . canvas . get_connection ( head )
assert cinfo is not None
item = cinfo . connected
constraint = cinfo . constraint
assert item == simple_canvas . box1
assert item != simple_canvas . box2
# Connect to box1 again, handle's connected item and port should be the
# same but connection constraint will differ
simple_canvas . tool . connect ( line , head , ( 120 , 50 ) )
cinfo = simple_canvas . canvas . get_connection ( head )
assert cinfo is not None
assert simple_canvas . box1 == cinfo . connected
assert simple_canvas . box1 . ports ( ) [ 0 ] == cinfo . port
assert constraint != cinfo . constraint
def xtest_find_port ( simple_canvas ) :
""" Test finding a port.
"""
line , head = simple_canvas . line , simple_canvas . head
p1 , p2 , p3 , p4 = simple_canvas . box1 . ports ( )
head . pos = 110 , 50
port = simple_canvas . tool . find_port ( line , head , simple_canvas . box1 )
assert p1 == port
head . pos = 140 , 60
port = simple_canvas . tool . find_port ( line , head , simple_canvas . box1 )
assert p2 == port
head . pos = 110 , 95
port = simple_canvas . tool . find_port ( line , head , simple_canvas . box1 )
assert p3 == port
2008-12-08 20:07:15 +00:00
2018-12-15 23:19:42 -05:00
head . pos = 100 , 55
port = simple_canvas . tool . find_port ( line , head , simple_canvas . box1 )
assert p4 == port