1#!/usr/bin/env python3
2#
3#   Copyright 2016 - 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
18
19from acts import records
20from acts import signals
21
22
23class ActsRecordsTest(unittest.TestCase):
24    """This test class tests the implementation of classes in acts.records.
25    """
26
27    def setUp(self):
28        self.tn = "test_name"
29        self.details = "Some details about the test execution."
30        self.float_extra = 12345.56789
31        self.json_extra = {"ha": "whatever"}
32
33    def verify_record(self, record, result, details, extras):
34        # Verify each field.
35        self.assertEqual(record.test_name, self.tn)
36        self.assertEqual(record.result, result)
37        self.assertEqual(record.details, details)
38        self.assertEqual(record.extras, extras)
39        self.assertTrue(record.begin_time, "begin time should not be empty.")
40        self.assertTrue(record.end_time, "end time should not be empty.")
41        # UID is not used at the moment, should always be None.
42        self.assertIsNone(record.uid)
43        # Verify to_dict.
44        d = {}
45        d[records.TestResultEnums.RECORD_NAME] = self.tn
46        d[records.TestResultEnums.RECORD_RESULT] = result
47        d[records.TestResultEnums.RECORD_DETAILS] = details
48        d[records.TestResultEnums.RECORD_EXTRAS] = extras
49        d[records.TestResultEnums.RECORD_BEGIN_TIME] = record.begin_time
50        d[records.TestResultEnums.RECORD_END_TIME] = record.end_time
51        d[records.TestResultEnums.
52          RECORD_LOG_BEGIN_TIME] = record.log_begin_time
53        d[records.TestResultEnums.RECORD_LOG_END_TIME] = record.log_end_time
54        d[records.TestResultEnums.RECORD_UID] = None
55        d[records.TestResultEnums.RECORD_CLASS] = None
56        d[records.TestResultEnums.RECORD_ADDITIONAL_ERRORS] = {}
57        actual_d = record.to_dict()
58        self.assertDictEqual(actual_d, d)
59        # Verify that these code paths do not cause crashes and yield non-empty
60        # results.
61        self.assertTrue(str(record), "str of the record should not be empty.")
62        self.assertTrue(repr(record), "the record's repr shouldn't be empty.")
63        self.assertTrue(record.json_str(), ("json str of the record should "
64                                            "not be empty."))
65
66    """ Begin of Tests """
67
68    def test_result_record_pass_none(self):
69        record = records.TestResultRecord(self.tn)
70        record.test_begin()
71        record.test_pass()
72        self.verify_record(
73            record=record,
74            result=records.TestResultEnums.TEST_RESULT_PASS,
75            details=None,
76            extras=None)
77
78    def test_result_record_pass_with_float_extra(self):
79        record = records.TestResultRecord(self.tn)
80        record.test_begin()
81        s = signals.TestPass(self.details, self.float_extra)
82        record.test_pass(s)
83        self.verify_record(
84            record=record,
85            result=records.TestResultEnums.TEST_RESULT_PASS,
86            details=self.details,
87            extras=self.float_extra)
88
89    def test_result_record_pass_with_json_extra(self):
90        record = records.TestResultRecord(self.tn)
91        record.test_begin()
92        s = signals.TestPass(self.details, self.json_extra)
93        record.test_pass(s)
94        self.verify_record(
95            record=record,
96            result=records.TestResultEnums.TEST_RESULT_PASS,
97            details=self.details,
98            extras=self.json_extra)
99
100    def test_result_record_fail_none(self):
101        record = records.TestResultRecord(self.tn)
102        record.test_begin()
103        record.test_fail()
104        self.verify_record(
105            record=record,
106            result=records.TestResultEnums.TEST_RESULT_FAIL,
107            details=None,
108            extras=None)
109
110    def test_result_record_fail_with_float_extra(self):
111        record = records.TestResultRecord(self.tn)
112        record.test_begin()
113        s = signals.TestFailure(self.details, self.float_extra)
114        record.test_fail(s)
115        self.verify_record(
116            record=record,
117            result=records.TestResultEnums.TEST_RESULT_FAIL,
118            details=self.details,
119            extras=self.float_extra)
120
121    def test_result_record_fail_with_json_extra(self):
122        record = records.TestResultRecord(self.tn)
123        record.test_begin()
124        s = signals.TestFailure(self.details, self.json_extra)
125        record.test_fail(s)
126        self.verify_record(
127            record=record,
128            result=records.TestResultEnums.TEST_RESULT_FAIL,
129            details=self.details,
130            extras=self.json_extra)
131
132    def test_result_record_skip_none(self):
133        record = records.TestResultRecord(self.tn)
134        record.test_begin()
135        record.test_skip()
136        self.verify_record(
137            record=record,
138            result=records.TestResultEnums.TEST_RESULT_SKIP,
139            details=None,
140            extras=None)
141
142    def test_result_record_skip_with_float_extra(self):
143        record = records.TestResultRecord(self.tn)
144        record.test_begin()
145        s = signals.TestSkip(self.details, self.float_extra)
146        record.test_skip(s)
147        self.verify_record(
148            record=record,
149            result=records.TestResultEnums.TEST_RESULT_SKIP,
150            details=self.details,
151            extras=self.float_extra)
152
153    def test_result_record_skip_with_json_extra(self):
154        record = records.TestResultRecord(self.tn)
155        record.test_begin()
156        s = signals.TestSkip(self.details, self.json_extra)
157        record.test_skip(s)
158        self.verify_record(
159            record=record,
160            result=records.TestResultEnums.TEST_RESULT_SKIP,
161            details=self.details,
162            extras=self.json_extra)
163
164    def test_result_add_operator_success(self):
165        record1 = records.TestResultRecord(self.tn)
166        record1.test_begin()
167        s = signals.TestPass(self.details, self.float_extra)
168        record1.test_pass(s)
169        tr1 = records.TestResult()
170        tr1.add_record(record1)
171        tr1.add_controller_info("MockDevice", ["magicA", "magicB"])
172        record2 = records.TestResultRecord(self.tn)
173        record2.test_begin()
174        s = signals.TestPass(self.details, self.json_extra)
175        record2.test_pass(s)
176        tr2 = records.TestResult()
177        tr2.add_record(record2)
178        tr2.add_controller_info("MockDevice", ["magicC"])
179        tr2 += tr1
180        self.assertTrue(tr2.passed, [tr1, tr2])
181        self.assertTrue(tr2.controller_info, {"MockDevice": ["magicC"]})
182
183    def test_result_add_operator_type_mismatch(self):
184        record1 = records.TestResultRecord(self.tn)
185        record1.test_begin()
186        s = signals.TestPass(self.details, self.float_extra)
187        record1.test_pass(s)
188        tr1 = records.TestResult()
189        tr1.add_record(record1)
190        expected_msg = "Operand .* of type .* is not a TestResult."
191        with self.assertRaisesRegexp(TypeError, expected_msg):
192            tr1 += "haha"
193
194    def test_is_all_pass(self):
195        s = signals.TestPass(self.details, self.float_extra)
196        record1 = records.TestResultRecord(self.tn)
197        record1.test_begin()
198        record1.test_pass(s)
199        s = signals.TestSkip(self.details, self.float_extra)
200        record2 = records.TestResultRecord(self.tn)
201        record2.test_begin()
202        record2.test_skip(s)
203        tr = records.TestResult()
204        tr.add_record(record1)
205        tr.add_record(record2)
206        tr.add_record(record1)
207        self.assertEqual(len(tr.passed), 2)
208        self.assertTrue(tr.is_all_pass)
209
210    def test_is_all_pass_negative(self):
211        s = signals.TestFailure(self.details, self.float_extra)
212        record1 = records.TestResultRecord(self.tn)
213        record1.test_begin()
214        record1.test_fail(s)
215        record2 = records.TestResultRecord(self.tn)
216        record2.test_begin()
217        record2.test_unknown(s)
218        tr = records.TestResult()
219        tr.add_record(record1)
220        tr.add_record(record2)
221        self.assertFalse(tr.is_all_pass)
222
223
224if __name__ == "__main__":
225    unittest.main()
226