<s> import unittest <EOL> import weakref <EOL> import sys <EOL> import IECore <EOL> import Gaffer <EOL> import GafferTest <EOL> import GafferUI <EOL> import GafferUITest <EOL> QtCore = GafferUI . _qtImport ( "<STR_LIT>" ) <EOL> QtGui = GafferUI . _qtImport ( "<STR_LIT>" ) <EOL> class TestWidget ( GafferUI . Widget ) : <EOL> def __init__ ( self , ** kw ) : <EOL> GafferUI . Widget . __init__ ( self , QtGui . QLabel ( "<STR_LIT:hello>" ) , ** kw ) <EOL> class TestWidget2 ( GafferUI . Widget ) : <EOL> def __init__ ( self ) : <EOL> self . topLevelGafferWidget = TestWidget ( ) <EOL> GafferUI . Widget . __init__ ( self , self . topLevelGafferWidget ) <EOL> class WidgetTest ( GafferUITest . TestCase ) : <EOL> def testOwner ( self ) : <EOL> w = TestWidget ( ) <EOL> self . assert_ ( GafferUI . Widget . _owner ( w . _qtWidget ( ) ) is w ) <EOL> def testParent ( self ) : <EOL> w = TestWidget ( ) <EOL> self . assert_ ( w . parent ( ) is None ) <EOL> def testCanDie ( self ) : <EOL> w = TestWidget ( ) <EOL> wr1 = weakref . ref ( w ) <EOL> wr2 = weakref . ref ( w . _qtWidget ( ) ) <EOL> del w <EOL> self . assert_ ( wr1 ( ) is None ) <EOL> self . assert_ ( wr2 ( ) is None ) <EOL> def testAncestor ( self ) : <EOL> w = GafferUI . Window ( "<STR_LIT:test>" ) <EOL> l = GafferUI . ListContainer ( GafferUI . ListContainer . Orientation . Vertical ) <EOL> p = GafferUI . SplitContainer ( ) <EOL> l . append ( p ) <EOL> w . setChild ( l ) <EOL> self . assert_ ( p . ancestor ( GafferUI . ListContainer ) is l ) <EOL> self . assert_ ( p . ancestor ( GafferUI . Window ) is w ) <EOL> self . assert_ ( p . ancestor ( GafferUI . Menu ) is None ) <EOL> def testIsAncestorOf ( self ) : <EOL> with GafferUI . Window ( "<STR_LIT:test>" ) as w : <EOL> with GafferUI . SplitContainer ( ) as p : <EOL> with GafferUI . ListContainer ( ) as l1 : <EOL> b1 = GafferUI . Button ( ) <EOL> with GafferUI . ListContainer ( ) as l2 : <EOL> b2 = GafferUI . Button ( ) <EOL> self . assertTrue ( l2 . isAncestorOf ( b2 ) ) <EOL> self . assertFalse ( l1 . isAncestorOf ( b2 ) ) <EOL> self . assertTrue ( p . isAncestorOf ( b2 ) ) <EOL> self . assertTrue ( w . isAncestorOf ( b2 ) ) <EOL> self . assertFalse ( b2 . isAncestorOf ( b1 ) ) <EOL> self . assertFalse ( b2 . isAncestorOf ( l1 ) ) <EOL> self . assertFalse ( b2 . isAncestorOf ( l2 ) ) <EOL> self . assertFalse ( b2 . isAncestorOf ( p ) ) <EOL> self . assertFalse ( b2 . isAncestorOf ( w ) ) <EOL> self . assertTrue ( l1 . isAncestorOf ( b1 ) ) <EOL> self . assertFalse ( l2 . isAncestorOf ( b1 ) ) <EOL> self . assertTrue ( p . isAncestorOf ( b1 ) ) <EOL> self . assertTrue ( w . isAncestorOf ( b1 ) ) <EOL> def testGafferWidgetAsTopLevel ( self ) : <EOL> w = TestWidget2 ( ) <EOL> self . assert_ ( GafferUI . Widget . _owner ( w . _qtWidget ( ) ) is w ) <EOL> self . assert_ ( w . topLevelGafferWidget . parent ( ) is w ) <EOL> self . assert_ ( GafferUI . Widget . _owner ( w . topLevelGafferWidget . _qtWidget ( ) ) is not w ) <EOL> def testToolTip ( self ) : <EOL> w = TestWidget ( ) <EOL> self . assertEqual ( w . getToolTip ( ) , "<STR_LIT>" ) <EOL> w = TestWidget ( toolTip = "<STR_LIT>" ) <EOL> self . assertEqual ( w . getToolTip ( ) , "<STR_LIT>" ) <EOL> w . setToolTip ( "<STR_LIT:a>" ) <EOL> self . assertEqual ( w . getToolTip ( ) , "<STR_LIT:a>" ) <EOL> def testEnabledState ( self ) : <EOL> w = TestWidget ( ) <EOL> self . assertEqual ( w . getEnabled ( ) , True ) <EOL> self . assertEqual ( w . enabled ( ) , True ) <EOL> w . setEnabled ( False ) <EOL> self . assertEqual ( w . getEnabled ( ) , False ) <EOL> self . assertEqual ( w . enabled ( ) , False ) <EOL> w . setEnabled ( True ) <EOL> self . assertEqual ( w . getEnabled ( ) , True ) <EOL> self . assertEqual ( w . enabled ( ) , True ) <EOL> def testDisabledWidgetsDontGetSignals ( self ) : <EOL> w = TestWidget ( ) <EOL> def f ( w , event ) : <EOL> WidgetTest . signalsEmitted += <NUM_LIT:1> <EOL> c = w . buttonPressSignal ( ) . connect ( f ) <EOL> WidgetTest . signalsEmitted = <NUM_LIT:0> <EOL> event = QtGui . QMouseEvent ( QtCore . QEvent . MouseButtonPress , QtCore . QPoint ( <NUM_LIT:0> , <NUM_LIT:0> ) , QtCore . Qt . LeftButton , QtCore . Qt . LeftButton , QtCore . Qt . NoModifier ) <EOL> QtGui . QApplication . instance ( ) . sendEvent ( w . _qtWidget ( ) , event ) <EOL> self . assertEqual ( WidgetTest . signalsEmitted , <NUM_LIT:1> ) <EOL> w . setEnabled ( False ) <EOL> QtGui . QApplication . instance ( ) . sendEvent ( w . _qtWidget ( ) , event ) <EOL> self . assertEqual ( WidgetTest . signalsEmitted , <NUM_LIT:1> ) <EOL> w . setEnabled ( True ) <EOL> QtGui . QApplication . instance ( ) . sendEvent ( w . _qtWidget ( ) , event ) <EOL> self . assertEqual ( WidgetTest . signalsEmitted , <NUM_LIT:2> ) <EOL> def testCanDieAfterUsingSignals ( self ) : <EOL> w = TestWidget ( ) <EOL> wr1 = weakref . ref ( w ) <EOL> wr2 = weakref . ref ( w . _qtWidget ( ) ) <EOL> w . buttonPressSignal ( ) <EOL> w . buttonReleaseSignal ( ) <EOL> w . mouseMoveSignal ( ) <EOL> w . wheelSignal ( ) <EOL> del w <EOL> self . assert_ ( wr1 ( ) is None ) <EOL> self . assert_ ( wr2 ( ) is None ) <EOL> def testVisibility ( self ) : <EOL> with GafferUI . Window ( ) as w : <EOL> with GafferUI . ListContainer ( ) as l : <EOL> t = TestWidget ( ) <EOL> self . assertEqual ( w . getVisible ( ) , False ) <EOL> self . assertEqual ( l . getVisible ( ) , True ) <EOL> self . assertEqual ( t . getVisible ( ) , True ) <EOL> self . assertEqual ( w . visible ( ) , False ) <EOL> self . assertEqual ( l . visible ( ) , False ) <EOL> self . assertEqual ( t . visible ( ) , False ) <EOL> w . setVisible ( True ) <EOL> self . assertEqual ( w . getVisible ( ) , True ) <EOL> self . assertEqual ( l . getVisible ( ) , True ) <EOL> self . assertEqual ( t . getVisible ( ) , True ) <EOL> self . assertEqual ( w . visible ( ) , True ) <EOL> self . assertEqual ( l . visible ( ) , True ) <EOL> self . assertEqual ( t . visible ( ) , True ) <EOL> w . setVisible ( False ) <EOL> self . assertEqual ( w . getVisible ( ) , False ) <EOL> self . assertEqual ( l . getVisible ( ) , True ) <EOL> self . assertEqual ( t . getVisible ( ) , True ) <EOL> self . assertEqual ( w . visible ( ) , False ) <EOL> self . assertEqual ( l . visible ( ) , False ) <EOL> self . assertEqual ( t . visible ( ) , False ) <EOL> self . assertEqual ( t . visible ( relativeTo = l ) , True ) <EOL> self . assertEqual ( t . visible ( relativeTo = w ) , True ) <EOL> w . setVisible ( True ) <EOL> t . setVisible ( False ) <EOL> self . assertEqual ( t . getVisible ( ) , False ) <EOL> self . assertEqual ( t . visible ( ) , False ) <EOL> self . assertEqual ( t . visible ( relativeTo = l ) , False ) <EOL> def testGetVisibleForNewWidgets ( self ) : <EOL> w = TestWidget ( ) <EOL> self . assertEqual ( w . getVisible ( ) , True ) <EOL> def testVisibilityOfParentlessWidgets ( self ) : <EOL> w = GafferUI . Window ( ) <EOL> t = TestWidget ( ) <EOL> self . assertEqual ( w . getVisible ( ) , False ) <EOL> self . assertEqual ( w . visible ( ) , False ) <EOL> self . assertEqual ( t . getVisible ( ) , True ) <EOL> self . assertEqual ( t . visible ( ) , False ) <EOL> w . setVisible ( True ) <EOL> self . assertEqual ( w . getVisible ( ) , True ) <EOL> self . assertEqual ( w . visible ( ) , True ) <EOL> w . setChild ( t ) <EOL> self . assertEqual ( t . getVisible ( ) , True ) <EOL> self . assertEqual ( t . visible ( ) , True ) <EOL> w . removeChild ( t ) <EOL> self . assertEqual ( t . parent ( ) , None ) <EOL> self . assertEqual ( t . getVisible ( ) , True ) <EOL> self . assertEqual ( t . visible ( ) , False ) <EOL> def testVisibilityWhenTransferringWidgets ( self ) : <EOL> w1 = GafferUI . Window ( ) <EOL> w1 . setVisible ( True ) <EOL> w2 = GafferUI . Window ( ) <EOL> w2 . setVisible ( True ) <EOL> v = TestWidget ( ) <EOL> self . assertEqual ( v . getVisible ( ) , True ) <EOL> self . assertEqual ( v . visible ( ) , False ) <EOL> h = TestWidget ( ) <EOL> self . assertEqual ( h . getVisible ( ) , True ) <EOL> h . setVisible ( False ) <EOL> self . assertEqual ( h . getVisible ( ) , False ) <EOL> self . assertEqual ( h . visible ( ) , False ) <EOL> w1 . setChild ( v ) <EOL> self . assertEqual ( v . getVisible ( ) , True ) <EOL> self . assertEqual ( v . visible ( ) , True ) <EOL> self . assertEqual ( h . getVisible ( ) , False ) <EOL> self . assertEqual ( h . visible ( ) , False ) <EOL> w2 . setChild ( v ) <EOL> self . assertEqual ( v . getVisible ( ) , True ) <EOL> self . assertEqual ( v . visible ( ) , True ) <EOL> self . assertEqual ( h . getVisible ( ) , False ) <EOL> self . assertEqual ( h . visible ( ) , False ) <EOL> w1 . setChild ( h ) <EOL> self . assertEqual ( v . getVisible ( ) , True ) <EOL> self . assertEqual ( v . visible ( ) , True ) <EOL> self . assertEqual ( h . getVisible ( ) , False ) <EOL> self . assertEqual ( h . visible ( ) , False ) <EOL> w2 . setChild ( h ) <EOL> self . assertEqual ( v . getVisible ( ) , True ) <EOL> self . assertEqual ( v . visible ( ) , False ) <EOL> self . assertEqual ( h . getVisible ( ) , False ) <EOL> self . assertEqual ( h . visible ( ) , False ) <EOL> def testSignals ( self ) : <EOL> w = TestWidget ( ) <EOL> for s in [ <EOL> ( "<STR_LIT>" , GafferUI . WidgetEventSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetEventSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetEventSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetEventSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetEventSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetEventSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetEventSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetSignal ) , <EOL> ( "<STR_LIT>" , GafferUI . WidgetSignal ) , <EOL> ] : <EOL> self . failUnless ( isinstance ( getattr ( w , s [ <NUM_LIT:0> ] ) ( ) , s [ <NUM_LIT:1> ] ) ) <EOL> self . failUnless ( getattr ( w , s [ <NUM_LIT:0> ] ) ( ) is getattr ( w , s [ <NUM_LIT:0> ] ) ( ) ) <EOL> def testBound ( self ) : <EOL> w = GafferUI . Window ( borderWidth = <NUM_LIT:8> ) <EOL> b = GafferUI . Button ( ) <EOL> w . setChild ( b ) <EOL> w . setVisible ( True ) <EOL> w . setPosition ( IECore . V2i ( <NUM_LIT:100> ) ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> wb = w . bound ( ) <EOL> bb = b . bound ( ) <EOL> bbw = b . bound ( relativeTo = w ) <EOL> self . failUnless ( isinstance ( wb , IECore . Box2i ) ) <EOL> self . failUnless ( isinstance ( bb , IECore . Box2i ) ) <EOL> self . failUnless ( isinstance ( bbw , IECore . Box2i ) ) <EOL> self . assertEqual ( bb . size ( ) , bbw . size ( ) ) <EOL> self . assertEqual ( bbw . min , bb . min - wb . min ) <EOL> self . assertEqual ( b . size ( ) , bb . size ( ) ) <EOL> def testParentChangedSignal ( self ) : <EOL> w = TestWidget ( ) <EOL> window = GafferUI . Window ( ) <EOL> cs = GafferTest . CapturingSlot ( w . parentChangedSignal ( ) ) <EOL> self . assertEqual ( len ( cs ) , <NUM_LIT:0> ) <EOL> window . setChild ( w ) <EOL> self . assertEqual ( len ( cs ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( cs [ <NUM_LIT:0> ] , ( w , ) ) <EOL> window . setChild ( None ) <EOL> self . assertEqual ( len ( cs ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( cs [ <NUM_LIT:1> ] , ( w , ) ) <EOL> def testHighlighting ( self ) : <EOL> w = TestWidget ( ) <EOL> self . assertEqual ( w . getHighlighted ( ) , False ) <EOL> w . setHighlighted ( True ) <EOL> self . assertEqual ( w . getHighlighted ( ) , True ) <EOL> w . setHighlighted ( False ) <EOL> self . assertEqual ( w . getHighlighted ( ) , False ) <EOL> def testWidgetAt ( self ) : <EOL> with GafferUI . Window ( ) as w1 : <EOL> t1 = GafferUI . TextWidget ( "<STR_LIT:hello>" ) <EOL> with GafferUI . Window ( ) as w2 : <EOL> t2 = GafferUI . TextWidget ( "<STR_LIT:hello>" ) <EOL> w1 . setVisible ( True ) <EOL> w2 . setVisible ( True ) <EOL> w1 . setPosition ( IECore . V2i ( <NUM_LIT:100> ) ) <EOL> w2 . setPosition ( IECore . V2i ( <NUM_LIT> ) ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertTrue ( GafferUI . Widget . widgetAt ( w1 . bound ( ) . center ( ) ) is t1 ) <EOL> self . assertTrue ( GafferUI . Widget . widgetAt ( w2 . bound ( ) . center ( ) ) is t2 ) <EOL> self . assertTrue ( GafferUI . Widget . widgetAt ( w1 . bound ( ) . center ( ) , widgetType = GafferUI . Window ) is w1 ) <EOL> self . assertTrue ( GafferUI . Widget . widgetAt ( w2 . bound ( ) . center ( ) , widgetType = GafferUI . Window ) is w2 ) <EOL> def testMousePosition ( self ) : <EOL> w = GafferUI . Window ( borderWidth = <NUM_LIT:8> ) <EOL> b = GafferUI . Button ( ) <EOL> w . setChild ( b ) <EOL> w . setVisible ( True ) <EOL> w . setPosition ( IECore . V2i ( <NUM_LIT:100> ) ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> mouseGlobal = GafferUI . Widget . mousePosition ( ) <EOL> mouseLocal = GafferUI . Widget . mousePosition ( relativeTo = b ) <EOL> self . assertEqual ( mouseGlobal , mouseLocal + b . bound ( ) . min ) <EOL> def testAddressAndObject ( self ) : <EOL> button = GafferUI . Button ( ) <EOL> address = GafferUI . _qtAddress ( button . _qtWidget ( ) ) <EOL> self . assertTrue ( isinstance ( address , int ) ) <EOL> widget = GafferUI . _qtObject ( address , QtGui . QPushButton ) <EOL> self . assertTrue ( isinstance ( widget , QtGui . QPushButton ) ) <EOL> def testSetVisibleWithNonBool ( self ) : <EOL> w = TestWidget ( ) <EOL> self . assertTrue ( w . getVisible ( ) is True ) <EOL> w . setVisible ( <NUM_LIT:0> ) <EOL> self . assertTrue ( w . getVisible ( ) is False ) <EOL> w . setVisible ( <NUM_LIT:1> ) <EOL> self . assertTrue ( w . getVisible ( ) is True ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
