<s> from unittest import TestCase <EOL> from muntjac . ui . tree import IExpandListener , ICollapseListener , Tree <EOL> class TestTreeListeners ( TestCase , IExpandListener , ICollapseListener ) : <EOL> def setUp ( self ) : <EOL> TestCase . setUp ( self ) <EOL> self . _expandCalled = <NUM_LIT:0> <EOL> self . _collapseCalled = <NUM_LIT:0> <EOL> self . _lastExpanded = <NUM_LIT:0> <EOL> self . _lastCollapsed = <NUM_LIT:0> <EOL> def testExpandListener ( self ) : <EOL> tree = self . createTree ( <NUM_LIT:10> , <NUM_LIT:20> , False ) <EOL> tree . addListener ( self , IExpandListener ) <EOL> rootIds = list ( tree . rootItemIds ( ) ) <EOL> self . assertEquals ( <NUM_LIT:10> , len ( rootIds ) ) <EOL> self . assertEquals ( <NUM_LIT:10> + ( <NUM_LIT:10> * <NUM_LIT:20> ) + <NUM_LIT:10> , len ( tree ) ) <EOL> tree . expandItem ( rootIds [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( <NUM_LIT:1> , self . _expandCalled ) <EOL> self . assertEquals ( rootIds [ <NUM_LIT:0> ] , self . _lastExpanded ) <EOL> self . _expandCalled = <NUM_LIT:0> <EOL> tree . expandItemsRecursively ( rootIds [ <NUM_LIT:1> ] ) <EOL> self . assertEquals ( <NUM_LIT:2> , self . _expandCalled ) <EOL> c = list ( tree . getChildren ( rootIds [ <NUM_LIT:1> ] ) ) <EOL> self . assertEquals ( c [ <NUM_LIT:4> ] , self . _lastExpanded ) <EOL> self . _expandCalled = <NUM_LIT:0> <EOL> tree . expandItem ( rootIds [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( <NUM_LIT:0> , self . _expandCalled ) <EOL> def createTree ( self , rootItems , children , expand ) : <EOL> """<STR_LIT>""" <EOL> tree = Tree ( ) <EOL> for i in range ( rootItems ) : <EOL> rootId = '<STR_LIT>' + str ( i ) <EOL> tree . addItem ( rootId ) <EOL> if expand : <EOL> tree . expandItemsRecursively ( rootId ) <EOL> else : <EOL> tree . collapseItemsRecursively ( rootId ) <EOL> for j in range ( children ) : <EOL> childId = '<STR_LIT>' + str ( i ) + '<STR_LIT:/>' + str ( j ) <EOL> tree . addItem ( childId ) <EOL> tree . setParent ( childId , rootId ) <EOL> tree . setChildrenAllowed ( childId , False ) <EOL> if j == <NUM_LIT:4> : <EOL> tree . setChildrenAllowed ( childId , True ) <EOL> grandChildId = tree . addItem ( ) <EOL> tree . setParent ( grandChildId , childId ) <EOL> tree . setChildrenAllowed ( grandChildId , False ) <EOL> if expand : <EOL> tree . expandItemsRecursively ( childId ) <EOL> else : <EOL> tree . collapseItemsRecursively ( childId ) <EOL> return tree <EOL> def testCollapseListener ( self ) : <EOL> tree = self . createTree ( <NUM_LIT:7> , <NUM_LIT:15> , True ) <EOL> tree . addListener ( self , ICollapseListener ) <EOL> rootIds = list ( tree . rootItemIds ( ) ) <EOL> self . assertEquals ( <NUM_LIT:7> , len ( rootIds ) ) <EOL> self . assertEquals ( <NUM_LIT:7> + ( <NUM_LIT:7> * <NUM_LIT:15> ) + <NUM_LIT:7> , len ( tree ) ) <EOL> tree . collapseItem ( rootIds [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( <NUM_LIT:1> , self . _collapseCalled ) <EOL> self . assertEquals ( rootIds [ <NUM_LIT:0> ] , self . _lastCollapsed ) <EOL> self . _collapseCalled = <NUM_LIT:0> <EOL> tree . collapseItemsRecursively ( rootIds [ <NUM_LIT:1> ] ) <EOL> self . assertEquals ( <NUM_LIT:2> , self . _collapseCalled ) <EOL> c = list ( tree . getChildren ( rootIds [ <NUM_LIT:1> ] ) ) <EOL> self . assertEquals ( c [ <NUM_LIT:4> ] , self . _lastCollapsed ) <EOL> self . _collapseCalled = <NUM_LIT:0> <EOL> tree . collapseItem ( rootIds [ <NUM_LIT:0> ] ) <EOL> self . assertEquals ( <NUM_LIT:0> , self . _collapseCalled ) <EOL> def nodeExpand ( self , event ) : <EOL> self . _lastExpanded = event . getItemId ( ) <EOL> self . _expandCalled += <NUM_LIT:1> <EOL> def nodeCollapse ( self , event ) : <EOL> self . _lastCollapsed = event . getItemId ( ) <EOL> self . _collapseCalled += <NUM_LIT:1> </s>
