test_DirectGuiBase.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. from direct.gui.DirectGuiBase import DirectGuiBase, DirectGuiWidget, toggleGuiGridSnap, setGuiGridSpacing
  2. from direct.gui.OnscreenText import OnscreenText
  3. from direct.gui import DirectGuiGlobals as DGG
  4. from direct.showbase.ShowBase import ShowBase
  5. from direct.showbase import ShowBaseGlobal
  6. from direct.showbase.MessengerGlobal import messenger
  7. from panda3d import core
  8. import pytest
  9. def test_create_DirectGuiBase():
  10. baseitem = DirectGuiBase()
  11. def test_defineoptions():
  12. baseitem = DirectGuiBase()
  13. testoptiondefs = (('test',0,None),)
  14. baseitem.defineoptions({}, testoptiondefs)
  15. assert baseitem['test'] == 0
  16. def test_addoptions():
  17. baseitem = DirectGuiBase()
  18. testoptiondefs = (('test',0,None),)
  19. # Those two will usually be set in the defineoptions function
  20. baseitem._optionInfo = {}
  21. baseitem._constructorKeywords = {}
  22. baseitem.addoptions(testoptiondefs, {})
  23. assert baseitem['test'] == 0
  24. def test_initialiseoptions():
  25. baseitem = DirectGuiBase()
  26. class testWidget(DirectGuiBase):
  27. def __init__(self):
  28. pass
  29. tw = testWidget()
  30. baseitem.initialiseoptions(tw)
  31. testoptiondefs = (('test',0,None),)
  32. tw.defineoptions({}, testoptiondefs)
  33. baseitem.initialiseoptions(tw)
  34. assert tw['test'] == 0
  35. def test_postInitialiseFunc():
  36. baseitem = DirectGuiBase()
  37. def func_a():
  38. global test_value_a
  39. test_value_a = 1
  40. def func_b():
  41. global test_value_b
  42. test_value_b = 1
  43. baseitem.postInitialiseFuncList.append(func_a)
  44. baseitem.postInitialiseFuncList.append(func_b)
  45. baseitem.postInitialiseFunc()
  46. global test_value_a
  47. assert test_value_a == 1
  48. global test_value_b
  49. assert test_value_b == 1
  50. def test_isinitoption():
  51. baseitem = DirectGuiBase()
  52. testoptiondefs = (('test-true',0,DGG.INITOPT),('test-false',0,None))
  53. baseitem.defineoptions({}, testoptiondefs)
  54. assert baseitem.isinitoption('test-true') == True
  55. assert baseitem.isinitoption('test-false') == False
  56. def test_options():
  57. baseitem = DirectGuiBase()
  58. testoptiondefs = (('test-1',0,DGG.INITOPT),('test-2',0,None),)
  59. baseitem.defineoptions({}, testoptiondefs)
  60. assert len(baseitem.options()) == 2
  61. def test_get_options():
  62. baseitem = DirectGuiBase()
  63. testoptiondefs = (('test-1',0,None),)
  64. baseitem.defineoptions({}, testoptiondefs)
  65. # Using the configure function
  66. # check if configuration have been set correctly
  67. assert baseitem.configure() == {'test-1': ('test-1', 0, 0),}
  68. # check for specific configurations
  69. assert baseitem.configure('test-1') == ('test-1', 0, 0)
  70. # using index style references __getItem__ and cget
  71. assert baseitem['test-1'] == 0
  72. assert baseitem.cget('test-1') == 0
  73. def test_set_options():
  74. baseitem = DirectGuiBase()
  75. testoptiondefs = (('test-1',0,DGG.INITOPT),('test-2',0,None))
  76. baseitem.defineoptions({}, testoptiondefs)
  77. # try to set a value of an initopt option (shouldn't be possible)
  78. baseitem.configure('test-1', **{'test-1': 1})
  79. # check it's value. It shouldn't have changed
  80. assert baseitem['test-1'] == 0
  81. baseitem['test-1'] = 1
  82. # check it's value. It shouldn't have changed
  83. assert baseitem['test-1'] == 0
  84. # try changing using the configure function. As test-2 isn't an
  85. # initopt option, this should work fine
  86. baseitem.configure('test-2', **{'test-2': 2})
  87. # check it's value. It should be 2 now
  88. assert baseitem['test-2'] == 2
  89. # try changing using index style referencing
  90. baseitem['test-2'] = 1
  91. assert baseitem['test-2'] == 1
  92. def test_component_handling():
  93. baseitem = DirectGuiBase()
  94. testoptiondefs = (('test-1',0,None),)
  95. baseitem.defineoptions({}, testoptiondefs)
  96. assert len(baseitem.components()) == 0
  97. baseitem.createcomponent(
  98. 'componentName', # componentName
  99. (), # componentAliases
  100. 'componentGroup', # componentGroup
  101. OnscreenText, # widgetClass
  102. (), # *widgetArgs
  103. text = 'Test', # **kw
  104. parent = core.NodePath()
  105. )
  106. # check if we have exactly one component now
  107. assert len(baseitem.components()) == 1
  108. # check if we have a component named like the one we just created
  109. assert baseitem.hascomponent('componentName')
  110. # get our new component
  111. component = baseitem.component('componentName')
  112. # check some of its values
  113. assert component.text == 'Test'
  114. # destroy our recently created component
  115. baseitem.destroycomponent('componentName')
  116. # check if it really is gone
  117. assert baseitem.hascomponent('componentName') == False
  118. def test_destroy():
  119. baseitem = DirectGuiBase()
  120. testoptiondefs = (('test-1',0,None),)
  121. baseitem.defineoptions({}, testoptiondefs)
  122. baseitem.destroy()
  123. # check if things got correctly removed
  124. assert not hasattr(baseitem, '_optionInfo')
  125. assert not hasattr(baseitem, '__componentInfo')
  126. assert not hasattr(baseitem, 'postInitialiseFuncList')
  127. def test_bindings():
  128. baseitem = DirectGuiBase()
  129. # Our test function and variable to check
  130. global commandCalled
  131. commandCalled = False
  132. def command(**kw):
  133. global commandCalled
  134. commandCalled = True
  135. assert True
  136. # Bind an event to our item
  137. baseitem.bind(DGG.B1CLICK, command)
  138. # The function should be called now. Note that the guiId will be
  139. # automatically appended by the bind command!
  140. messenger.send(DGG.B1CLICK + baseitem.guiId)
  141. assert commandCalled
  142. # Try to unbind the event again
  143. baseitem.unbind(DGG.B1CLICK)
  144. # Now it shouldn't be called anymore
  145. commandCalled = False
  146. messenger.send(DGG.B1CLICK + baseitem.guiId)
  147. assert not commandCalled
  148. def test_toggle_snap():
  149. try:
  150. DirectGuiWidget.snapToGrid = 0
  151. item = DirectGuiWidget()
  152. assert item.snapToGrid == 0
  153. toggleGuiGridSnap()
  154. assert item.snapToGrid == 1
  155. finally:
  156. DirectGuiWidget.snapToGrid = 0
  157. def test_toggle_spacing():
  158. try:
  159. DirectGuiWidget.gridSpacing = 0
  160. item = DirectGuiWidget()
  161. setGuiGridSpacing(5)
  162. assert item.gridSpacing == 5
  163. finally:
  164. DirectGuiWidget.gridSpacing = 0.05
  165. @pytest.mark.skipif(not ShowBaseGlobal.__dev__, reason="requires want-dev")
  166. def test_track_gui_items():
  167. page = core.load_prc_file_data("", "track-gui-items true")
  168. try:
  169. item = DirectGuiWidget()
  170. id = item.guiId
  171. assert id in ShowBase.guiItems
  172. assert ShowBase.guiItems[id] == item
  173. item.destroy()
  174. assert id not in ShowBase.guiItems
  175. finally:
  176. core.unload_prc_file(page)