1#!/usr/bin/env python3
2#
3#   Copyright 2018 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16import unittest
17from unittest import TestCase
18
19from mock import Mock
20
21from acts.event.event_subscription import EventSubscription
22
23
24class EventSubscriptionTest(TestCase):
25    """Tests the EventSubscription class."""
26
27    @staticmethod
28    def filter_out_event(_):
29        return False
30
31    @staticmethod
32    def pass_filter(_):
33        return True
34
35    def test_event_type_returns_correct_value(self):
36        """Tests that event_type returns the correct event type."""
37        expected_event_type = Mock()
38        subscription = EventSubscription(expected_event_type, lambda _: None)
39        self.assertEqual(expected_event_type, subscription.event_type)
40
41    def test_deliver_dont_deliver_if_event_is_filtered(self):
42        """Tests deliver does not call func if the event is filtered out."""
43        func = Mock()
44        subscription = EventSubscription(Mock(), func,
45                                         event_filter=self.filter_out_event)
46
47        subscription.deliver(Mock())
48
49        self.assertFalse(func.called)
50
51    def test_deliver_deliver_accepted_event(self):
52        """Tests deliver does call func when the event is accepted."""
53        func = Mock()
54        subscription = EventSubscription(Mock(), func,
55                                         event_filter=self.pass_filter)
56
57        subscription.deliver(Mock())
58        self.assertTrue(func.called)
59
60
61if __name__ == '__main__':
62    unittest.main()
63