events.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #
  2. # The Qubes OS Project, https://www.qubes-os.org/
  3. #
  4. # Copyright (C) 2014-2015 Joanna Rutkowska <joanna@invisiblethingslab.com>
  5. # Copyright (C) 2014-2015 Wojtek Porczyk <woju@invisiblethingslab.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License along
  18. # with this program; if not, write to the Free Software Foundation, Inc.,
  19. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. #
  21. import qubes.events
  22. import qubes.tests
  23. class TC_00_Emitter(qubes.tests.QubesTestCase):
  24. def test_000_add_handler(self):
  25. # need something mutable
  26. testevent_fired = [False]
  27. def on_testevent(subject, event):
  28. # pylint: disable=unused-argument
  29. if event == 'testevent':
  30. testevent_fired[0] = True
  31. emitter = qubes.events.Emitter()
  32. emitter.add_handler('testevent', on_testevent)
  33. emitter.events_enabled = True
  34. emitter.fire_event('testevent')
  35. self.assertTrue(testevent_fired[0])
  36. def test_001_decorator(self):
  37. class TestEmitter(qubes.events.Emitter):
  38. def __init__(self):
  39. # pylint: disable=bad-super-call
  40. super(TestEmitter, self).__init__()
  41. self.testevent_fired = False
  42. @qubes.events.handler('testevent')
  43. def on_testevent(self, event):
  44. if event == 'testevent':
  45. self.testevent_fired = True
  46. emitter = TestEmitter()
  47. emitter.events_enabled = True
  48. emitter.fire_event('testevent')
  49. self.assertTrue(emitter.testevent_fired)
  50. def test_002_fire_for_effect(self):
  51. class TestEmitter(qubes.events.Emitter):
  52. @qubes.events.handler('testevent')
  53. def on_testevent_1(self, event):
  54. pass
  55. @qubes.events.handler('testevent')
  56. def on_testevent_2(self, event):
  57. yield 'testvalue1'
  58. yield 'testvalue2'
  59. @qubes.events.handler('testevent')
  60. def on_testevent_3(self, event):
  61. return ('testvalue3', 'testvalue4')
  62. emitter = TestEmitter()
  63. emitter.events_enabled = True
  64. emitter.fire_event('testevent')
  65. effect = emitter.fire_event('testevent')
  66. self.assertCountEqual(effect,
  67. ('testvalue1', 'testvalue2', 'testvalue3', 'testvalue4'))
  68. def test_004_catch_all(self):
  69. # need something mutable
  70. testevent_fired = [0]
  71. def on_all(subject, event, *args, **kwargs):
  72. # pylint: disable=unused-argument
  73. testevent_fired[0] += 1
  74. def on_foo(subject, event, *args, **kwargs):
  75. # pylint: disable=unused-argument
  76. testevent_fired[0] += 1
  77. emitter = qubes.events.Emitter()
  78. emitter.add_handler('*', on_all)
  79. emitter.add_handler('foo', on_foo)
  80. emitter.events_enabled = True
  81. emitter.fire_event('testevent')
  82. self.assertEqual(testevent_fired[0], 1)
  83. emitter.fire_event('foo')
  84. # now catch-all and foo should be executed
  85. self.assertEqual(testevent_fired[0], 3)
  86. emitter.fire_event('bar')
  87. self.assertEqual(testevent_fired[0], 4)