Ejemplo n.º 1
0
    def test_event_register(self):
        """Tests that event.register()'d functions can receive posted Events."""
        def event_listener(_):
            self.called_event = True

        event_bus.register(Event, event_listener)
        event_bus.post(Event())

        self.assertTrue(self.called_event)
Ejemplo n.º 2
0
    def register(self):
        """Registers the _start and _stop methods to their corresponding
        events.
        """
        def check_serial(event):
            return self.serial == event.ad.serial

        self._registration_ids = [
            event_bus.register(android_events.AndroidStartServicesEvent,
                               self._start,
                               filter_fn=check_serial),
            event_bus.register(android_events.AndroidStopServicesEvent,
                               self._stop,
                               filter_fn=check_serial)
        ]
Ejemplo n.º 3
0
    def test_register_registers_a_subscription(self, register_subscription):
        """Tests that register creates and registers a subscription."""
        mock_event = Mock()
        mock_func = Mock()
        order = 43
        event_bus.register(mock_event, mock_func, order=order)

        args, _ = register_subscription.call_args
        subscription = args[0]

        # Instead of writing an equality operator for only testing,
        # check the internals to make sure they are expected values.
        self.assertEqual(subscription._event_type, mock_event)
        self.assertEqual(subscription._func, mock_func)
        self.assertEqual(subscription.order, order)
Ejemplo n.º 4
0
    def test_event_unregister(self):
        """Tests that an event can be registered, and then unregistered."""
        def event_listener(_):
            self.called_event = False

        registration_id = event_bus.register(Event, event_listener)
        event_bus.unregister(registration_id)
        event_bus.post(Event())

        self.assertFalse(self.called_event)
Ejemplo n.º 5
0
    """Pushes a new TestCaseContext to the _contexts stack upon a
    TestCaseBeginEvent. Pops the most recent context off the stack upon a
    TestCaseEndEvent. Posts the context change to the event bus.

    Args:
        event: An instance of TestCaseBeginEvent or TestCaseEndEvent.
    """
    if isinstance(event, TestCaseBeginEvent):
        _contexts.append(_get_context_for_test_case_event(event))
    if isinstance(event, TestCaseEndEvent):
        if _contexts:
            _contexts.pop()
    event_bus.post(NewTestCaseContextEvent())


event_bus.register(TestClassEvent, _update_test_class_context)
event_bus.register(TestCaseBeginEvent, _update_test_case_context, order=-100)
event_bus.register(TestCaseEndEvent, _update_test_case_context, order=100)


class TestContext(object):
    """An object representing the current context in which a test is executing.

    The context encodes the current state of the test runner with respect to a
    particular scenario in which code is being executed. For example, if some
    code is being executed as part of a test case, then the context should
    encode information about that test case such as its name or enclosing
    class.

    The subcontext specifies a relative path in which certain outputs,
    e.g. logcat, should be kept for the given context.