1#!/usr/bin/env python
2#
3# Copyright (C) 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#
17
18import unittest
19
20from vts.utils.python.instrumentation import test_framework_instrumentation as tfi
21from vts.utils.python.instrumentation import test_framework_instrumentation_event as tfie
22from vts.utils.python.instrumentation import test_framework_instrumentation_test_submodule as tfits
23
24
25class TestFrameworkInstrumentationTest(unittest.TestCase):
26    """Unit tests for test_framework_instrumentation module"""
27
28    def setUp(self):
29        """Setup tasks"""
30        self.category = 'category_default'
31        self.name = 'name_default'
32        tfie.event_data = []
33        tfie.event_stack = []
34        tfi.counts = {}
35
36    def testEventName(self):
37        """Tests whether illegal characters are being recognized and replaced."""
38        for name in tfie.ILLEGAL_CHARS:
39            # TODO(yuexima): disable error logging for this test case
40            event = tfie.TestFrameworkInstrumentationEvent(name, '')
41            self.assertNotEqual(event.name, name, 'name %s should not be accepted.' % name)
42
43    def testEventMatch(self):
44        """Tests whether Event object can match with a category and name."""
45        category = '1'
46        name = '2'
47        event = tfie.TestFrameworkInstrumentationEvent(name, category)
48        self.assertTrue(event.Match(name, category))
49        self.assertFalse(event.Match('3', category))
50
51    def testEndAlreadyEnded(self):
52        """Tests End command on already ended event."""
53        event = tfi.Begin(self.name, self.category, enable_logging=False)
54        event.End()
55        self.assertEqual(event.status, 2)
56        self.assertIsNone(event.error)
57        event.End()
58        self.assertEqual(event.status, 2)
59        self.assertTrue(event.error)
60
61    def testEndMatch(self):
62        """Tests End command with name matching."""
63        event = tfi.Begin(self.name, self.category)
64        self.assertEqual(event.status, 1)
65        tfi.End(self.name, self.category)
66        self.assertEqual(event.status, 2)
67        self.assertIsNone(event.error)
68
69    def testEndFromOtherModule(self):
70        """Tests the use of End command from another module."""
71        event = tfi.Begin(self.name, self.category)
72        self.assertEqual(event.status, 1)
73        tfits.TestFrameworkInstrumentationTestSubmodule().End(self.name, self.category)
74        self.assertEqual(event.status, 2)
75        self.assertIsNone(event.error)
76
77    def testCategories(self):
78        """Tests access to TestFrameworkInstrumentationCategories object"""
79        self.assertTrue(tfi.categories.Add(self.name, self.category))
80        self.assertFalse(tfi.categories.Add('', self.name))
81        self.assertFalse(tfi.categories.Add(None, self.name))
82        self.assertFalse(tfi.categories.Add('1a', self.name))
83
84    def testCheckEnded(self):
85        """Tests the CheckEnded method of TestFrameworkInstrumentationEvent"""
86        event = tfi.Begin(self.name, self.category)
87
88        # Verify initial condition
89        self.assertTrue(bool(tfie.event_stack))
90        self.assertEqual(event.status, 1)
91
92        event.CheckEnded()
93        # Check event status is Remove
94        self.assertEqual(event.status, 3)
95
96        # Check event is removed from stack
97        self.assertFalse(bool(tfie.event_stack))
98
99        # Check whether duplicate calls doesn't give error
100        event.CheckEnded()
101        self.assertEqual(event.status, 3)
102
103    def testRemove(self):
104        """Tests the Remove method of TestFrameworkInstrumentationEvent"""
105        event = tfi.Begin(self.name, self.category)
106
107        # Verify initial condition
108        self.assertTrue(bool(tfie.event_stack))
109        self.assertEqual(event.status, 1)
110
111        event.Remove()
112        # Check event status is Remove
113        self.assertEqual(event.status, 3)
114
115        # Check event is removed from stack
116        self.assertFalse(bool(tfie.event_stack))
117
118        # Check whether duplicate calls doesn't give error
119        event.Remove()
120        self.assertEqual(event.status, 3)
121
122    def testEndAlreadyRemoved(self):
123        """Tests End command on already ended event."""
124        event = tfi.Begin(self.name, self.category, enable_logging=False)
125        reason = 'no reason'
126        event.Remove(reason)
127        self.assertEqual(event.status, 3)
128        self.assertEqual(event.error, reason)
129        event.End()
130        self.assertEqual(event.status, 3)
131        self.assertNotEqual(event.error, reason)
132
133    def testEnableLogging(self):
134        """Tests the enable_logging option."""
135        # Test not specified case
136        event = tfi.Begin(self.name, self.category)
137        self.assertFalse(event._enable_logging)
138        event.End()
139
140        # Test set to True case
141        event = tfi.Begin(self.name, self.category, enable_logging=True)
142        self.assertTrue(event._enable_logging)
143        event.End()
144
145        # Test set to False case
146        event = tfi.Begin(self.name, self.category, enable_logging=None)
147        self.assertFalse(event._enable_logging)
148        event.End()
149
150    def testDisableSubEventLoggingOverwriting(self):
151        """Tests the disable_subevent_logging option's overwriting feature.
152
153        Tests whether the top event's disable_subevent_logging overwrite
154        subevent's disable_subevent_logging option only when it is set to True
155        """
156        # Test top event disable_subevent_logging option not specified case
157        event = tfi.Begin(self.name, self.category)
158        self.assertFalse(event._disable_subevent_logging)
159        event_sub = tfi.Begin(self.name, self.category, disable_subevent_logging=True)
160        self.assertTrue(event_sub._disable_subevent_logging)
161        event_sub.End()
162        event.End()
163
164        # Test top event disable_subevent_logging option set to False
165        event = tfi.Begin(self.name, self.category, disable_subevent_logging=False)
166        self.assertFalse(event._disable_subevent_logging)
167        event_sub = tfi.Begin(self.name, self.category, disable_subevent_logging=True)
168        self.assertTrue(event_sub._disable_subevent_logging)
169        event_sub.End()
170        event.End()
171
172        # Test top event disable_subevent_logging option set to True
173        event = tfi.Begin(self.name, self.category, disable_subevent_logging=True)
174        self.assertTrue(event._disable_subevent_logging)
175        event_sub1 = tfi.Begin(self.name, self.category, disable_subevent_logging=False)
176        self.assertTrue(event_sub1._disable_subevent_logging)
177        event_sub1.End()
178        event_sub2 = tfi.Begin(self.name, self.category)
179        self.assertTrue(event_sub2._disable_subevent_logging)
180        event_sub2.End()
181        event.End()
182
183    def testDisableSubEventLoggingNesting(self):
184        """Tests the disable_subevent_logging option.
185
186        Tests whether the top event's disable_subevent_logging can overwrite
187        subevents of deeper levels when set to True.
188        """
189        # Test top event disable_subevent_logging option set to True
190        event = tfi.Begin(self.name, self.category, disable_subevent_logging=True)
191        self.assertTrue(event._disable_subevent_logging)
192        event_sub = tfi.Begin(self.name, self.category, disable_subevent_logging=False)
193        self.assertTrue(event_sub._disable_subevent_logging)
194        event_sub_sub1 = tfi.Begin(self.name, self.category)
195        self.assertTrue(event_sub_sub1._disable_subevent_logging)
196        event_sub_sub1.End()
197        event_sub_sub2 = tfi.Begin(self.name, self.category, disable_subevent_logging=False)
198        self.assertTrue(event_sub_sub2._disable_subevent_logging)
199        event_sub_sub2.End()
200        event_sub.End()
201        event.End()
202
203    def testCount(self):
204        """Tests the count API."""
205        tfi.Count(self.name, self.category)
206        tfi.Count(self.name, self.category)
207        self.assertEqual(len(tfi.counts), 1)
208        self.assertEqual(len(tfi.counts[self.name, self.category]), 2)
209        tfi.Count(self.name)
210        self.assertEqual(len(tfi.counts), 2)
211
212    def testGenerateTextReport(self):
213        """Tests the GenerateTextReport method."""
214        event = tfi.Begin('name1', 'cat1', disable_subevent_logging=True)
215        event_sub = tfi.Begin('name2', 'cat2', disable_subevent_logging=False)
216        event_sub.End()
217        event.End()
218        res = tfi.GenerateTextReport()
219
220        # Checks result is not empty
221        self.assertGreater(len(res), 0)
222
223        # Since the format of the result is subject to change, here we only
224        # checks whether the names and categories are mentioned in the result.
225        self.assertIn('name1', res)
226        self.assertIn('name2', res)
227        self.assertIn('cat1', res)
228        self.assertIn('cat2', res)
229
230
231if __name__ == "__main__":
232    unittest.main()