1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "chre/platform/log.h"
18 #include "chre/test/common/unit_test.h"
19 #include "chre/util/macros.h"
20 #include "gtest/gtest.h"
21 
22 namespace chre {
23 namespace {
24 
25 using pw::unit_test::EventHandler;
26 using pw::unit_test::RunTestsSummary;
27 using pw::unit_test::TestCase;
28 using pw::unit_test::TestExpectation;
29 using pw::unit_test::TestResult;
30 
31 class PwEventHandler : public EventHandler {
RunAllTestsStart()32   void RunAllTestsStart() override {
33     LOGD("[==========] Running all tests.");
34   }
35 
RunAllTestsEnd(const RunTestsSummary & run_tests_summary)36   void RunAllTestsEnd(const RunTestsSummary &run_tests_summary) override {
37     LOGD("[==========] Done running all tests.");
38     LOGD("[  PASSED  ] %d test(s).", run_tests_summary.passed_tests);
39     if (run_tests_summary.failed_tests) {
40       LOGD("[  FAILED  ] %d test(s).", run_tests_summary.failed_tests);
41     }
42   }
43 
TestCaseStart(const TestCase & test_case)44   void TestCaseStart(const TestCase &test_case) override {
45     LOGD("[ RUN      ] %s.%s", test_case.suite_name, test_case.test_name);
46   }
47 
TestCaseEnd(const TestCase & test_case,TestResult result)48   void TestCaseEnd(const TestCase &test_case, TestResult result) override {
49     switch (result) {
50       case TestResult::kSuccess:
51         LOGD("[       OK ] %s.%s", test_case.suite_name, test_case.test_name);
52         break;
53       case TestResult::kFailure:
54         LOGD("[  FAILED  ] %s.%s", test_case.suite_name, test_case.test_name);
55         break;
56     }
57   }
58 
TestCaseExpect(const TestCase & test_case,const TestExpectation & expectation)59   void TestCaseExpect(const TestCase &test_case,
60                       const TestExpectation &expectation) override {
61     if (!expectation.success) {
62       LOGD("%s:%d: Failure", test_case.file_name, expectation.line_number);
63       LOGD("      Expected: %s", expectation.expression);
64       LOGD("        Actual: %s", expectation.evaluated_expression);
65     }
66   }
67 
TestCaseDisabled(const TestCase & test_case)68   void TestCaseDisabled(const TestCase &test_case) override {
69     LOGD("Skipping disabled test %s.%s", test_case.suite_name,
70          test_case.test_name);
71   }
72 };
73 
74 }  // namespace
75 
runAllTests()76 DLL_EXPORT void runAllTests() {
77   PwEventHandler eventHandler;
78   pw::unit_test::RegisterEventHandler(&eventHandler);
79   int rc = RUN_ALL_TESTS();
80   LOGD("Ran all tests with rc: %d", rc);
81 }
82 
83 }  // namespace chre
84