events.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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)
  88. def test_005_instance_handlers(self):
  89. class TestEmitter(qubes.events.Emitter):
  90. @qubes.events.handler('testevent')
  91. def on_testevent_1(self, event):
  92. yield 'testevent_1'
  93. def on_testevent_2(subject, event):
  94. yield 'testevent_2'
  95. emitter = TestEmitter()
  96. emitter.add_handler('testevent', on_testevent_2)
  97. emitter.events_enabled = True
  98. emitter2 = TestEmitter()
  99. emitter2.events_enabled = True
  100. with self.subTest('fire_event'):
  101. effect = emitter.fire_event('testevent')
  102. effect2 = emitter2.fire_event('testevent')
  103. self.assertEqual(list(effect),
  104. ['testevent_1', 'testevent_2'])
  105. self.assertEqual(list(effect2),
  106. ['testevent_1'])
  107. with self.subTest('fire_event_pre'):
  108. effect = emitter.fire_event_pre('testevent')
  109. effect2 = emitter2.fire_event_pre('testevent')
  110. self.assertEqual(list(effect),
  111. ['testevent_2', 'testevent_1'])
  112. self.assertEqual(list(effect2),
  113. ['testevent_1'])