• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
16
17import unittest
18from functools import partial
19from unittest import TestCase
20
21from acts import context
22from acts.context import RootContext
23from acts.context import TestCaseContext
24from acts.context import TestClassContext
25from acts.context import TestContext
26from acts.context import _update_test_case_context
27from acts.context import _update_test_class_context
28from acts.context import get_context_for_event
29from acts.context import get_current_context
30from acts.event.event import TestCaseBeginEvent
31from acts.event.event import TestCaseEndEvent
32from acts.event.event import TestCaseEvent
33from acts.event.event import TestClassBeginEvent
34from acts.event.event import TestClassEndEvent
35from acts.event.event import TestClassEvent
36from mock import Mock
37from mock import patch
38
39
40LOGGING = 'acts.context.logging'
41
42
43def reset_context():
44    context._contexts = [RootContext()]
45
46
47TEST_CASE = 'test_case_name'
48
49
50class TestClass:
51    pass
52
53
54class ModuleTest(TestCase):
55    """Unit tests for the context module."""
56
57    def test_get_context_for_event_for_test_case(self):
58        event = Mock(spec=TestCaseEvent)
59        event.test_class = Mock()
60        event.test_case = Mock()
61        context = get_context_for_event(event)
62
63        self.assertIsInstance(context, TestCaseContext)
64        self.assertEqual(context.test_class, event.test_class)
65        self.assertEqual(context.test_case, event.test_case)
66
67    def test_get_context_for_event_for_test_class(self):
68        event = Mock(spec=TestClassEvent)
69        event.test_class = Mock()
70        context = get_context_for_event(event)
71
72        self.assertIsInstance(context, TestClassContext)
73        self.assertEqual(context.test_class, event.test_class)
74
75    def test_get_context_for_unknown_event_type(self):
76        event = Mock()
77
78        self.assertRaises(TypeError, partial(get_context_for_event, event))
79
80    def test_update_test_class_context_for_test_class_begin(self):
81        event = Mock(spec=TestClassBeginEvent)
82        event.test_class = Mock()
83
84        _update_test_class_context(event)
85        self.assertIsInstance(get_current_context(), TestClassContext)
86        reset_context()
87
88    def test_update_test_class_context_for_test_class_end(self):
89        event = Mock(spec=TestClassBeginEvent)
90        event.test_class = Mock()
91        event2 = Mock(spec=TestClassEndEvent)
92        event2.test_class = Mock()
93
94        _update_test_class_context(event)
95        _update_test_class_context(event2)
96
97        self.assertIsInstance(get_current_context(), RootContext)
98        reset_context()
99
100    def test_update_test_case_context_for_test_case_begin(self):
101        event = Mock(spec=TestClassBeginEvent)
102        event.test_class = Mock()
103        event2 = Mock(spec=TestCaseBeginEvent)
104        event2.test_class = Mock()
105        event2.test_case = Mock()
106
107        _update_test_class_context(event)
108        _update_test_case_context(event2)
109
110        self.assertIsInstance(get_current_context(), TestCaseContext)
111        reset_context()
112
113    def test_update_test_case_context_for_test_case_end(self):
114        event = Mock(spec=TestClassBeginEvent)
115        event.test_class = Mock()
116        event2 = Mock(spec=TestCaseBeginEvent)
117        event2.test_class = Mock()
118        event2.test_case = Mock()
119        event3 = Mock(spec=TestCaseEndEvent)
120        event3.test_class = Mock()
121        event3.test_case = Mock()
122
123        _update_test_class_context(event)
124        _update_test_case_context(event2)
125        _update_test_case_context(event3)
126
127        self.assertIsInstance(get_current_context(), TestClassContext)
128        reset_context()
129
130
131class TestContextTest(TestCase):
132    """Unit tests for the TestContext class."""
133
134    @patch(LOGGING)
135    def test_get_base_output_path_uses_default(self, logging):
136        context = TestContext()
137
138        self.assertEqual(context.get_base_output_path(), logging.log_path)
139
140    @patch(LOGGING)
141    def test_add_base_path_overrides_default(self, _):
142        context = TestContext()
143        mock_path = Mock()
144
145        context.add_base_output_path('basepath', mock_path)
146
147        self.assertEqual(context.get_base_output_path('basepath'), mock_path)
148
149    def test_get_subcontext_returns_empty_string_by_default(self):
150        context = TestContext()
151
152        self.assertEqual(context.get_subcontext(), '')
153
154    def test_add_subcontext_sets_correct_path(self):
155        context = TestContext()
156        mock_path = Mock()
157
158        context.add_subcontext('subcontext', mock_path)
159
160        self.assertEqual(context.get_subcontext('subcontext'), mock_path)
161
162    @patch(LOGGING)
163    @patch('os.makedirs')
164    def test_get_full_output_path_returns_correct_path(self, *_):
165        context = TestClassContext(TestClass())
166        context.add_base_output_path('foo', 'base/path')
167        context.add_subcontext('foo', 'subcontext')
168
169        full_path = 'base/path/TestClass/subcontext'
170        self.assertEqual(context.get_full_output_path('foo'), full_path)
171
172    def test_identifier_not_implemented(self):
173        context = TestContext()
174
175        self.assertRaises(NotImplementedError, lambda: context.identifier)
176
177
178class TestClassContextTest(TestCase):
179    """Unit tests for the TestClassContext class."""
180
181    def test_init_attributes(self):
182        test_class = Mock()
183        context = TestClassContext(test_class)
184
185        self.assertEqual(context.test_class, test_class)
186
187    def test_get_class_name(self):
188        class TestClass:
189            pass
190        test_class = TestClass()
191        context = TestClassContext(test_class)
192
193        self.assertEqual(context.test_class_name, TestClass.__name__)
194
195    def test_context_dir_is_class_name(self):
196        class TestClass:
197            pass
198        test_class = TestClass()
199        context = TestClassContext(test_class)
200
201        self.assertEqual(context._get_default_context_dir(), TestClass.__name__)
202
203    def test_identifier_is_class_name(self):
204        class TestClass:
205            pass
206        test_class = TestClass()
207        context = TestClassContext(test_class)
208
209        self.assertEqual(context.identifier, TestClass.__name__)
210
211
212class TestCaseContextTest(TestCase):
213    """Unit tests for the TestCaseContext class."""
214
215    def test_init_attributes(self):
216        test_class = Mock()
217        test_case = TEST_CASE
218        context = TestCaseContext(test_class, test_case)
219
220        self.assertEqual(context.test_class, test_class)
221        self.assertEqual(context.test_case, test_case)
222        self.assertEqual(context.test_case_name, test_case)
223
224    def test_get_class_name(self):
225        test_class = TestClass()
226        context = TestCaseContext(test_class, TEST_CASE)
227
228        self.assertEqual(context.test_class_name, TestClass.__name__)
229
230    def test_context_dir_is_class_and_test_case_name(self):
231        test_class = TestClass()
232        context = TestCaseContext(test_class, TEST_CASE)
233
234        context_dir = TestClass.__name__ + '/' + TEST_CASE
235        self.assertEqual(context._get_default_context_dir(), context_dir)
236
237    def test_identifier_is_class_and_test_case_name(self):
238        test_class = TestClass()
239        context = TestCaseContext(test_class, TEST_CASE)
240
241        identifier = TestClass.__name__ + '.' + TEST_CASE
242        self.assertEqual(context.identifier, identifier)
243
244
245if __name__ == '__main__':
246    unittest.main()
247