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 // This file registers a GTest listener that logs test begin/end in logcat.
18 // This is to avoid problems like b/149852934 where the test output cannot be
19 // correlated with the prouction code's logcat logs because they use different
20 // clock sources.
21
22 #include <android/log.h>
23
24 #include "test/gtest_and_gmock.h"
25
26 namespace perfetto {
27 namespace test {
28
29 namespace {
30
31 #define PERFETTO_TEST_LOG(...) \
32 __android_log_print(ANDROID_LOG_DEBUG, "perfetto", ##__VA_ARGS__)
33
34 class LogcatPrinter : public testing::EmptyTestEventListener {
35 public:
36 LogcatPrinter();
37 ~LogcatPrinter() override;
38
39 // testing::EmptyTestEventListener:
40 void OnTestCaseStart(const testing::TestCase& test_case) override;
41 void OnTestStart(const testing::TestInfo& test_info) override;
42 void OnTestEnd(const testing::TestInfo& test_info) override;
43 void OnTestCaseEnd(const testing::TestCase& test_case) override;
44 };
45
46 LogcatPrinter::LogcatPrinter() = default;
47 LogcatPrinter::~LogcatPrinter() = default;
48
OnTestCaseStart(const testing::TestCase & test_case)49 void LogcatPrinter::OnTestCaseStart(const testing::TestCase& test_case) {
50 PERFETTO_TEST_LOG("Test case start: %s", test_case.name());
51 }
52
OnTestStart(const testing::TestInfo & test_info)53 void LogcatPrinter::OnTestStart(const testing::TestInfo& test_info) {
54 PERFETTO_TEST_LOG("Test start: %s.%s", test_info.test_case_name(),
55 test_info.name());
56 }
57
OnTestEnd(const testing::TestInfo & test_info)58 void LogcatPrinter::OnTestEnd(const testing::TestInfo& test_info) {
59 const auto* result = test_info.result();
60 const char* state = "N/A";
61 if (result) {
62 if (result->Passed())
63 state = "PASS";
64 else if (result->Skipped())
65 state = "SKIPPED";
66 else if (result->Failed())
67 state = "FAIL";
68 }
69 PERFETTO_TEST_LOG("Test end: %s.%s [%s]", test_info.test_case_name(),
70 test_info.name(), state);
71 }
72
OnTestCaseEnd(const testing::TestCase & test_case)73 void LogcatPrinter::OnTestCaseEnd(const testing::TestCase& test_case) {
74 PERFETTO_TEST_LOG("Test case end: %s. succeeded=%d, failed=%d",
75 test_case.name(), test_case.successful_test_count(),
76 test_case.failed_test_count());
77 }
78
79 // This static initializer
80 void __attribute__((constructor)) __attribute__((visibility("default")))
SetupGtestLogcatPrinter()81 SetupGtestLogcatPrinter() {
82 static LogcatPrinter* instance = new LogcatPrinter();
83 auto& listeners = testing::UnitTest::GetInstance()->listeners();
84 listeners.Append(instance);
85 }
86
87 } // namespace
88 } // namespace test
89 } // namespace perfetto
90