events.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/python2 -O
  2. import sys
  3. import unittest
  4. import qubes.events
  5. import qubes.tests
  6. class TC_00_Emitter(qubes.tests.QubesTestCase):
  7. def test_000_add_handler(self):
  8. # need something mutable
  9. testevent_fired = [False]
  10. def on_testevent(subject, event):
  11. if event == 'testevent':
  12. testevent_fired[0] = True
  13. emitter = qubes.events.Emitter()
  14. emitter.add_handler('testevent', on_testevent)
  15. emitter.fire_event('testevent')
  16. self.assertTrue(testevent_fired[0])
  17. def test_001_decorator(self):
  18. class TestEmitter(qubes.events.Emitter):
  19. def __init__(self):
  20. super(TestEmitter, self).__init__()
  21. self.testevent_fired = False
  22. @qubes.events.handler('testevent')
  23. def on_testevent(self, event):
  24. if event == 'testevent':
  25. self.testevent_fired = True
  26. emitter = TestEmitter()
  27. emitter.fire_event('testevent')
  28. self.assertTrue(emitter.testevent_fired)