예제 #1
0
    def initialize(cls,
                   scheduler=None,
                   event_loop=None,
                   global_configuration=None):
        """Create app instance inside of this class.

        This method can be called multiple times to reset App settings.

        :param scheduler: scheduler used for rendering screens; if not specified use
                         `simpleline.render.screen_scheduler.ScreenScheduler`.
        :type scheduler: instance of `simpleline.render.screen_scheduler.ScreenScheduler`.

        :param event_loop: event loop used for asynchronous tasks;
                           if not specified use `simpleline.event_loop.main_loop.MainLoop`.
        :type event_loop: object based on class `simpleline.event_loop.AbstractEventLoop`.

        :param global_configuration: instance of the global configuration object; if not specified
                                     use `simpleline.global_configuration.GlobalConfiguration`
        :type global_configuration: object based on class
                                    `simpleline.global_configuration.GlobalConfiguration`
        """
        from simpleline.event_loop.main_loop import MainLoop  # pylint: disable=import-outside-toplevel
        from simpleline.render.screen_scheduler import ScreenScheduler  # pylint: disable=import-outside-toplevel
        from simpleline.global_configuration import GlobalConfiguration  # pylint: disable=import-outside-toplevel

        if event_loop is None:
            event_loop = MainLoop()
        if scheduler is None:
            scheduler = ScreenScheduler(event_loop)
        if global_configuration is None:
            global_configuration = GlobalConfiguration()

        cls.__app = cls.AppPimpl(scheduler, event_loop, global_configuration)

        cls._post_init()
 def test_scheduler_quit_screen(self):
     def test_callback():
         pass
     scheduler = ScreenScheduler(MainLoop())
     self.assertEqual(scheduler.quit_screen, None)
     scheduler.quit_screen = test_callback
     self.assertEqual(scheduler.quit_screen, test_callback)
예제 #3
0
 def test_create_scheduler(self):
     scheduler = ScreenScheduler(MainLoop())
     self.assertTrue(type(scheduler._screen_stack) is ScreenStack)
 def create_loop(self):
     self.loop = MainLoop()
class ProcessEvents_TestCase(unittest.TestCase):

    def setUp(self):
        self.signal_counter = 0
        self.signal_counter2 = 0
        self.signal_counter_copied = 0
        self.callback_called = False
        self.callback_args = None
        self.create_loop()

    def create_loop(self):
        self.loop = MainLoop()

    def test_simple_register_handler(self):
        self.callback_called = False

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_callback)
        loop.enqueue_signal(TestSignal())
        loop.process_signals()

        self.assertTrue(self.callback_called)

    def test_process_more_signals(self):
        self.signal_counter = 0

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_signal_counter)
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal())
        loop.process_signals()

        self.assertEqual(self.signal_counter, 3)

    def test_process_signals_multiple_times(self):
        self.signal_counter = 0

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_signal_counter)
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal())
        loop.process_signals()
        self.assertEqual(self.signal_counter, 2)

        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal())
        loop.process_signals()
        self.assertEqual(self.signal_counter, 4)

    def test_wait_on_signal(self):
        self.signal_counter = 0

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_signal_counter)
        loop.register_signal_handler(TestSignal2, self._handler_process_events_then_register_testsignal, loop)
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal2())
        loop.process_signals(return_after=TestSignal2)
        self.assertEqual(self.signal_counter, 1)

        loop.process_signals()
        self.assertEqual(self.signal_counter, 2)

    def test_wait_on_signal_skipped_by_inner_process_events(self):
        self.signal_counter = 0

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_signal_counter)
        # run process signals recursively in this handler which will skip processing
        loop.register_signal_handler(TestSignal2, self._handler_process_events_then_register_testsignal, loop)
        loop.enqueue_signal(TestSignal2())
        loop.enqueue_signal(TestSignal())
        # new signal will be registered in handler method but that shouldn't be processed
        # because it should end on the first signal even when it was skipped
        loop.process_signals(return_after=TestSignal)

        self.assertEqual(self.signal_counter, 1)

    def test_multiple_handlers_to_signal(self):
        self.signal_counter = 0
        self.signal_counter2 = 0

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_signal_counter)
        loop.register_signal_handler(TestSignal, self._handler_signal_counter2)
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal())
        loop.process_signals()

        self.assertEqual(self.signal_counter, 2)
        self.assertEqual(self.signal_counter2, 2)

    def test_priority_signal_processing(self):
        self.signal_counter = 0

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_signal_counter)
        loop.register_signal_handler(TestPrioritySignal, self._handler_signal_counter)
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestPrioritySignal())  # should be processed as first signal because of priority
        loop.enqueue_signal(TestSignal())
        loop.process_signals()
        self.assertEqual(self.signal_counter, 1)

        # process rest of the signals
        loop.process_signals()
        self.assertEqual(self.signal_counter, 4)

    def test_low_priority_signal_processing(self):
        self.signal_counter = 0
        self.signal_counter_copied = 0

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_signal_counter)
        loop.register_signal_handler(TestLowPrioritySignal, self._handler_signal_copy_counter)
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestLowPrioritySignal())
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal())
        loop.process_signals()
        self.assertEqual(self.signal_counter, 3)

        # process the low priority signal
        loop.process_signals()
        self.assertEqual(self.signal_counter_copied, 3)

    def test_quit_callback(self):
        self.callback_called = False
        self.callback_args = None
        msg = "Test data"

        loop = self.loop
        loop.set_quit_callback(self._handler_quit_callback, args=msg)
        loop.register_signal_handler(TestSignal, self._handler_raise_ExitMainLoop_exception)
        loop.enqueue_signal(TestSignal())
        loop.run()

        self.assertTrue(self.callback_called)
        self.assertEqual(msg, self.callback_args)

    def test_force_quit(self):
        self.callback_called = False

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_callback)
        loop.register_signal_handler(TestSignal2, self._handler_force_quit_exception)
        loop.enqueue_signal(TestSignal2())
        loop.enqueue_signal(TestSignal())
        loop.run()

        self.assertFalse(self.callback_called)

    def test_force_quit_recursive_loop(self):
        self.callback_called = False

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_start_inner_loop_and_enqueue_event, TestSignal3())
        loop.register_signal_handler(TestSignal2, self._handler_callback)
        loop.register_signal_handler(TestSignal3, self._handler_force_quit_exception)
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal2())
        loop.run()

        self.assertFalse(self.callback_called)

    def test_force_quit_when_waiting_on_signal(self):
        self.callback_called = False

        loop = self.loop
        loop.register_signal_handler(TestSignal, self._handler_force_quit_exception)
        loop.register_signal_handler(TestSignal2, self._handler_callback)
        loop.enqueue_signal(TestSignal())
        loop.enqueue_signal(TestSignal2())

        # FIXME: Find a better way how to detect infinite loop
        # if force quit won't work properly this will hang up
        loop.process_signals(return_after=TestSignal3)

        self.assertFalse(self.callback_called)

    # HANDLERS FOR TESTING
    def _handler_callback(self, signal, data):
        self.callback_called = True

    def _handler_quit_callback(self, args):
        self.callback_called = True
        self.callback_args = args

    def _handler_signal_counter(self, signal, data):
        self.signal_counter += 1

    def _handler_signal_counter2(self, signal, data):
        self.signal_counter2 += 1

    def _handler_signal_copy_counter(self, signal, data):
        self.signal_counter_copied = self.signal_counter

    def _handler_process_events_then_register_testsignal(self, signal, data):
        event_loop = data
        event_loop.process_signals()
        # This shouldn't be processed
        event_loop.enqueue_signal(TestSignal())

    def _handler_start_inner_loop_and_enqueue_event(self, signal, data):
        self.loop.execute_new_loop(data)

    def _handler_raise_ExitMainLoop_exception(self, signal, data):
        raise ExitMainLoop()

    def _handler_force_quit_exception(self, signal, data):
        self.loop.force_quit()
 def test_create_scheduler(self):
     scheduler = ScreenScheduler(MainLoop())
     self.assertTrue(isinstance(scheduler._screen_stack, ScreenStack)) # pylint: disable=protected-access