<s> import unittest <EOL> import weakref <EOL> import functools <EOL> import IECore <EOL> import Gaffer <EOL> import GafferUI <EOL> import GafferUITest <EOL> QtCore = GafferUI . _qtImport ( "<STR_LIT>" ) <EOL> QtGui = GafferUI . _qtImport ( "<STR_LIT>" ) <EOL> class MenuBarTest ( GafferUITest . TestCase ) : <EOL> def testLifetime ( self ) : <EOL> def f ( ) : <EOL> pass <EOL> definition = IECore . MenuDefinition ( <EOL> [ <EOL> ( "<STR_LIT>" , { } ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : True } ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : f } ) , <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : False } ) , <EOL> ] <EOL> ) <EOL> with GafferUI . Window ( ) as window : <EOL> with GafferUI . ListContainer ( ) : <EOL> menu = GafferUI . MenuBar ( definition ) <EOL> window . setVisible ( True ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> del window <EOL> w = weakref . ref ( menu ) <EOL> del menu <EOL> wd = weakref . ref ( definition ) <EOL> del definition <EOL> wf = weakref . ref ( f ) <EOL> del f <EOL> self . assertEqual ( w ( ) , None ) <EOL> self . assertEqual ( wd ( ) , None ) <EOL> self . assertEqual ( wf ( ) , None ) <EOL> def testShortcuts ( self ) : <EOL> commandInvocations = [ ] <EOL> def command ( arg ) : <EOL> commandInvocations . append ( arg ) <EOL> definition = IECore . MenuDefinition ( [ <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : functools . partial ( command , "<STR_LIT>" ) , "<STR_LIT>" : "<STR_LIT>" } ) , <EOL> ] ) <EOL> with GafferUI . Window ( ) as window : <EOL> with GafferUI . ListContainer ( ) : <EOL> menuBar = GafferUI . MenuBar ( definition ) <EOL> label = GafferUI . Label ( "<STR_LIT:test>" ) <EOL> window . setVisible ( True ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . __simulateShortcut ( label ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( len ( commandInvocations ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( commandInvocations [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> menuBar . definition = IECore . MenuDefinition ( ) <EOL> self . __simulateShortcut ( label ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( len ( commandInvocations ) , <NUM_LIT:1> ) <EOL> menuBar . definition = IECore . MenuDefinition ( [ <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : functools . partial ( command , "<STR_LIT>" ) , "<STR_LIT>" : "<STR_LIT>" } ) , <EOL> ] ) <EOL> self . __simulateShortcut ( label ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( len ( commandInvocations ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( commandInvocations [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> def testNesting ( self ) : <EOL> commandInvocations = [ ] <EOL> def command ( arg ) : <EOL> commandInvocations . append ( arg ) <EOL> outerDefinition = IECore . MenuDefinition ( [ <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : functools . partial ( command , "<STR_LIT>" ) , "<STR_LIT>" : "<STR_LIT>" } ) , <EOL> ] ) <EOL> innerDefinition = IECore . MenuDefinition ( [ <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : functools . partial ( command , "<STR_LIT>" ) , "<STR_LIT>" : "<STR_LIT>" } ) , <EOL> ] ) <EOL> with GafferUI . Window ( ) as window : <EOL> with GafferUI . ListContainer ( ) : <EOL> GafferUI . MenuBar ( outerDefinition ) <EOL> outerLabel = GafferUI . Label ( "<STR_LIT:test>" ) <EOL> with GafferUI . ListContainer ( ) : <EOL> GafferUI . MenuBar ( innerDefinition ) <EOL> innerLabel = GafferUI . Label ( "<STR_LIT:test>" ) <EOL> window . setVisible ( True ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . __simulateShortcut ( innerLabel ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( len ( commandInvocations ) , <NUM_LIT:1> ) <EOL> self . assertEqual ( commandInvocations [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> self . __simulateShortcut ( outerLabel ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( len ( commandInvocations ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( commandInvocations [ <NUM_LIT:1> ] , "<STR_LIT>" ) <EOL> def testParentChange ( self ) : <EOL> commandInvocations = [ ] <EOL> def command ( arg ) : <EOL> commandInvocations . append ( arg ) <EOL> definition = IECore . MenuDefinition ( [ <EOL> ( "<STR_LIT>" , { "<STR_LIT>" : functools . partial ( command , "<STR_LIT:test>" ) , "<STR_LIT>" : "<STR_LIT>" } ) , <EOL> ] ) <EOL> with GafferUI . Window ( ) as window : <EOL> with GafferUI . ListContainer ( ) : <EOL> with GafferUI . ListContainer ( ) as container1 : <EOL> menuBar = GafferUI . MenuBar ( definition ) <EOL> label1 = GafferUI . Label ( "<STR_LIT:test>" ) <EOL> with GafferUI . ListContainer ( ) as container2 : <EOL> label2 = GafferUI . Label ( "<STR_LIT:test>" ) <EOL> window . setVisible ( True ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . __simulateShortcut ( label1 ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( len ( commandInvocations ) , <NUM_LIT:1> ) <EOL> self . __simulateShortcut ( label2 ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( len ( commandInvocations ) , <NUM_LIT:1> ) <EOL> container2 . insert ( <NUM_LIT:0> , menuBar ) <EOL> self . __simulateShortcut ( label1 ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( len ( commandInvocations ) , <NUM_LIT:1> ) <EOL> self . __simulateShortcut ( label2 ) <EOL> self . waitForIdle ( <NUM_LIT:1000> ) <EOL> self . assertEqual ( len ( commandInvocations ) , <NUM_LIT:2> ) <EOL> def __simulateShortcut ( self , widget ) : <EOL> QtGui . QApplication . instance ( ) . notify ( <EOL> widget . _qtWidget ( ) , <EOL> QtGui . QKeyEvent ( QtCore . QEvent . KeyPress , QtCore . Qt . Key_A , QtCore . Qt . ControlModifier ) <EOL> ) <EOL> QtGui . QApplication . instance ( ) . notify ( <EOL> widget . _qtWidget ( ) , <EOL> QtGui . QKeyEvent ( QtCore . QEvent . KeyRelease , QtCore . Qt . Key_A , QtCore . Qt . ControlModifier ) <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> unittest . main ( ) </s>
