events.py 2.9 KB

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