2018-12-15 23:19:42 -05:00
import pytest
2018-11-26 21:56:14 -05:00
2020-10-11 20:14:21 +02:00
from gaphas . canvas import Canvas
from gaphas . connections import ConnectionError
2021-12-08 11:24:16 +01:00
from gaphas . connector import ConnectionSink , Connector
2020-10-21 15:25:25 +02:00
from gaphas . item import Element as Box
2018-11-26 21:56:14 -05:00
from gaphas . item import Line
2020-10-20 08:31:01 +02:00
from gaphas . matrix import Matrix
2021-12-08 11:33:15 +01:00
from gaphas . model import Model
2020-11-07 17:45:53 +01:00
def test_canvas_is_a_view_model ( canvas ) :
assert isinstance ( canvas , Model )
2007-07-26 19:23:55 +00:00
2018-11-25 16:07:58 -05:00
2018-12-15 23:19:42 -05:00
def test_update_matrices ( ) :
2020-09-15 20:23:50 -04:00
""" Test updating of matrices. """
2018-12-15 23:19:42 -05:00
c = Canvas ( )
2020-11-01 22:43:58 +01:00
i = Box ( c . connections )
ii = Box ( c . connections )
2018-12-15 23:19:42 -05:00
c . add ( i )
c . add ( ii , i )
2020-10-19 22:26:16 +02:00
i . matrix . translate ( 5.0 , 0.0 )
ii . matrix . translate ( 0.0 , 8.0 )
2007-07-26 19:23:55 +00:00
2020-10-20 08:31:01 +02:00
assert c . get_matrix_i2c ( i ) == Matrix ( 1 , 0 , 0 , 1 , 5 , 0 )
assert c . get_matrix_i2c ( ii ) == Matrix ( 1 , 0 , 0 , 1 , 5 , 8 )
2007-07-26 19:23:55 +00:00
2007-07-26 19:11:32 +00:00
2018-12-15 23:19:42 -05:00
def test_reparent ( ) :
c = Canvas ( )
2020-11-01 22:43:58 +01:00
b1 = Box ( c . connections )
b2 = Box ( c . connections )
2018-12-15 23:19:42 -05:00
c . add ( b1 )
c . add ( b2 , b1 )
c . reparent ( b2 , None )
2009-10-11 02:37:36 +01:00
2018-11-25 16:07:58 -05:00
2010-01-15 16:24:00 +01:00
def count ( i ) :
return len ( list ( i ) )
2018-12-15 23:19:42 -05:00
def test_connect_item ( ) :
c = Canvas ( )
2020-11-01 22:43:58 +01:00
b1 = Box ( c . connections )
b2 = Box ( c . connections )
line = Line ( c . connections )
2018-12-15 23:19:42 -05:00
c . add ( b1 )
c . add ( b2 )
c . add ( line )
2010-01-15 16:24:00 +01:00
2020-11-03 09:11:14 +01:00
c . connections . connect_item ( line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] )
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
2018-12-15 23:19:42 -05:00
# Add the same
with pytest . raises ( ConnectionError ) :
2020-11-03 09:11:14 +01:00
c . connections . connect_item ( line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] )
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
2018-12-15 23:19:42 -05:00
def test_disconnect_item_with_callback ( ) :
c = Canvas ( )
2020-11-01 22:43:58 +01:00
b1 = Box ( c . connections )
b2 = Box ( c . connections )
line = Line ( c . connections )
2018-12-15 23:19:42 -05:00
c . add ( b1 )
c . add ( b2 )
c . add ( line )
events = [ ]
2022-09-29 20:44:30 +02:00
def callback ( * args ) :
2018-12-15 23:19:42 -05:00
events . append ( " called " )
2020-11-03 09:11:14 +01:00
c . connections . connect_item (
line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] , callback = callback
)
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
2018-12-15 23:19:42 -05:00
2020-11-03 09:11:14 +01:00
c . connections . disconnect_item ( line , line . handles ( ) [ 0 ] )
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 0
2018-12-15 23:19:42 -05:00
assert events == [ " called " ]
def test_disconnect_item_with_constraint ( ) :
c = Canvas ( )
2020-11-01 22:43:58 +01:00
b1 = Box ( c . connections )
b2 = Box ( c . connections )
line = Line ( c . connections )
2018-12-15 23:19:42 -05:00
c . add ( b1 )
c . add ( b2 )
c . add ( line )
2020-10-23 18:22:30 +02:00
cons = b1 . ports ( ) [ 0 ] . constraint ( line , line . handles ( ) [ 0 ] , b1 )
2018-12-15 23:19:42 -05:00
2020-11-03 09:11:14 +01:00
c . connections . connect_item (
line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] , constraint = cons
)
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
2018-12-15 23:19:42 -05:00
2020-05-22 23:31:29 +02:00
assert len ( c . solver . constraints ) == 13
2018-12-15 23:19:42 -05:00
2020-11-03 09:11:14 +01:00
c . connections . disconnect_item ( line , line . handles ( ) [ 0 ] )
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 0
2018-12-15 23:19:42 -05:00
2020-05-22 23:31:29 +02:00
assert len ( c . solver . constraints ) == 12
2018-12-15 23:19:42 -05:00
def test_disconnect_item_by_deleting_element ( ) :
c = Canvas ( )
2020-11-01 22:43:58 +01:00
b1 = Box ( c . connections )
b2 = Box ( c . connections )
line = Line ( c . connections )
2018-12-15 23:19:42 -05:00
c . add ( b1 )
c . add ( b2 )
c . add ( line )
events = [ ]
2022-09-29 20:44:30 +02:00
def callback ( * args ) :
2018-12-15 23:19:42 -05:00
events . append ( " called " )
2020-11-03 09:11:14 +01:00
c . connections . connect_item (
line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] , callback = callback
)
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
2018-12-15 23:19:42 -05:00
c . remove ( b1 )
2020-11-03 09:11:14 +01:00
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 0
2018-12-15 23:19:42 -05:00
assert events == [ " called " ]
def test_disconnect_item_with_constraint_by_deleting_element ( ) :
c = Canvas ( )
2020-11-01 22:43:58 +01:00
b1 = Box ( c . connections )
b2 = Box ( c . connections )
line = Line ( c . connections )
2018-12-15 23:19:42 -05:00
c . add ( b1 )
c . add ( b2 )
c . add ( line )
2020-10-23 18:22:30 +02:00
cons = b1 . ports ( ) [ 0 ] . constraint ( line , line . handles ( ) [ 0 ] , b1 )
2018-12-15 23:19:42 -05:00
2020-11-03 09:11:14 +01:00
c . connections . connect_item (
line , line . handles ( ) [ 0 ] , b1 , b1 . ports ( ) [ 0 ] , constraint = cons
)
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 1
2018-12-15 23:19:42 -05:00
ncons = len ( c . solver . constraints )
2020-05-22 23:31:29 +02:00
assert ncons == 13
2018-12-15 23:19:42 -05:00
c . remove ( b1 )
2020-11-03 09:11:14 +01:00
assert count ( c . connections . get_connections ( handle = line . handles ( ) [ 0 ] ) ) == 0
2018-12-15 23:19:42 -05:00
2020-05-22 23:31:29 +02:00
assert 6 == len ( c . solver . constraints )
2018-12-15 23:19:42 -05:00
2017-05-18 22:13:31 -04:00
2018-12-15 23:19:42 -05:00
def test_remove_connected_item ( ) :
2020-09-15 20:23:50 -04:00
""" Test adding canvas constraint. """
2018-12-15 23:19:42 -05:00
canvas = Canvas ( )
2018-01-11 22:23:57 -05:00
2020-11-01 22:43:58 +01:00
l1 = Line ( canvas . connections )
2018-12-15 23:19:42 -05:00
canvas . add ( l1 )
2010-02-17 13:03:59 +01:00
2020-11-01 22:43:58 +01:00
b1 = Box ( canvas . connections )
2018-12-15 23:19:42 -05:00
canvas . add ( b1 )
2017-05-18 22:13:31 -04:00
2018-12-15 23:19:42 -05:00
number_cons1 = len ( canvas . solver . constraints )
2010-02-17 13:03:59 +01:00
2020-11-01 22:43:58 +01:00
b2 = Box ( canvas . connections )
2018-12-15 23:19:42 -05:00
canvas . add ( b2 )
2018-01-11 22:23:57 -05:00
2018-12-15 23:19:42 -05:00
number_cons2 = len ( canvas . solver . constraints )
2010-02-17 13:03:59 +01:00
2020-10-23 21:59:13 +02:00
conn = Connector ( l1 , l1 . handles ( ) [ 0 ] , canvas . connections )
2021-01-10 16:46:25 +01:00
sink = ConnectionSink ( b1 )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
conn . connect ( sink )
2017-05-18 22:13:31 -04:00
2020-11-03 09:11:14 +01:00
assert canvas . connections . get_connection ( l1 . handles ( ) [ 0 ] )
2010-02-17 13:03:59 +01:00
2020-10-23 21:59:13 +02:00
conn = Connector ( l1 , l1 . handles ( ) [ 1 ] , canvas . connections )
2021-01-10 16:46:25 +01:00
sink = ConnectionSink ( b2 )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
conn . connect ( sink )
2010-02-17 13:03:59 +01:00
2020-11-03 09:11:14 +01:00
assert canvas . connections . get_connection ( l1 . handles ( ) [ 1 ] )
2010-02-17 13:03:59 +01:00
2018-12-15 23:19:42 -05:00
assert number_cons2 + 2 == len ( canvas . solver . constraints )
2010-01-15 16:24:00 +01:00
2018-12-15 23:19:42 -05:00
canvas . remove ( b1 )
2010-01-15 16:24:00 +01:00
2018-12-15 23:19:42 -05:00
# Expecting a class + line connected at one end only
assert number_cons1 + 1 == len ( canvas . solver . constraints )