| // This is to help diagnose flaky tests failures potentially linked | |
| // to other events on the device | |
| #include <gtest/gtest.h> | |
| #include <gmock/gmock.h> | |
| #include <android/log.h> | |
| #include <fstream> | |
| #ifndef ALOG_COMPONENT_NAME | |
| #define ALOG_COMPONENT_NAME "ipc-testing" | |
| #endif | |
| #define KMSG_LOG_TAG "ipc-testing-kmsg" | |
| #define ALOGI(f,...) __android_log_print(ANDROID_LOG_INFO, ALOG_COMPONENT_NAME, f, ## __VA_ARGS__) | |
| #define ALOGW(f,...) __android_log_print(ANDROID_LOG_WARN, ALOG_COMPONENT_NAME, f, ## __VA_ARGS__) | |
| namespace { | |
| class TestLogger { | |
| public: | |
| virtual void log(const std::string &str) = 0; | |
| virtual ~TestLogger() = default; | |
| }; | |
| class LogcatTestLogger : public TestLogger { | |
| public: | |
| void log(const std::string &str) override { | |
| ALOGI("%s", str.c_str()); | |
| } | |
| }; | |
| class KmsgTestLogger : public TestLogger { | |
| std::ofstream kmsgStream{"/dev/kmsg", std::ios::app}; | |
| public: | |
| KmsgTestLogger() { | |
| if (!kmsgStream) { | |
| ALOGW("KmsgTestLogger: error opening /dev/kmsg"); | |
| } | |
| } | |
| void log(const std::string &str) override { | |
| if (kmsgStream) { | |
| kmsgStream << KMSG_LOG_TAG << ": " << str << std::endl; | |
| } | |
| } | |
| }; | |
| template<typename... Args> | |
| std::string stringFormat(const std::string& fmt, Args... args) { | |
| int size = std::snprintf(nullptr, 0, fmt.c_str(), args...) + 1; // Extra space for '\0' | |
| if(size <= 0) { | |
| return ""; | |
| } | |
| std::vector<char> buffer(size); | |
| std::snprintf(buffer.data(), size, fmt.c_str(), args...); | |
| return std::string(buffer.data(), buffer.data() + size - 1); | |
| } | |
| class TestPrinter : public testing::EmptyTestEventListener { | |
| std::vector<std::unique_ptr<TestLogger>> loggers; | |
| void log(const std::string &str) { | |
| for (const auto &logger: loggers) { | |
| logger->log(str); | |
| } | |
| } | |
| public: | |
| void addLogger(std::unique_ptr<TestLogger> logger) { | |
| loggers.push_back(std::move(logger)); | |
| } | |
| // testing::EmptyTestEventListener: | |
| void OnTestCaseStart(const testing::TestCase& test_case) override; | |
| void OnTestStart(const testing::TestInfo& test_info) override; | |
| void OnTestEnd(const testing::TestInfo& test_info) override; | |
| void OnTestCaseEnd(const testing::TestCase& test_case) override; | |
| }; | |
| void TestPrinter::OnTestCaseStart(const testing::TestCase& test_case) { | |
| log(stringFormat("Test case start: %s", test_case.name())); | |
| } | |
| void TestPrinter::OnTestStart(const testing::TestInfo& test_info) { | |
| log(stringFormat("Test start: %s.%s", test_info.test_case_name(), | |
| test_info.name())); | |
| } | |
| void TestPrinter::OnTestEnd(const testing::TestInfo& test_info) { | |
| const auto* result = test_info.result(); | |
| const char* state = "N/A"; | |
| if (result) { | |
| if (result->Passed()) | |
| state = "PASS"; | |
| else if (result->Skipped()) | |
| state = "SKIPPED"; | |
| else if (result->Failed()) | |
| state = "FAIL"; | |
| } | |
| log(stringFormat("Test end: %s.%s [%s]", test_info.test_case_name(), | |
| test_info.name(), state)); | |
| } | |
| void TestPrinter::OnTestCaseEnd(const testing::TestCase& test_case) { | |
| log(stringFormat("Test case end: %s. succeeded=%d, failed=%d", | |
| test_case.name(), test_case.successful_test_count(), | |
| test_case.failed_test_count())); | |
| } | |
| void __attribute__((constructor)) | |
| SetupGtestPrinter() { | |
| TestPrinter* instance = new TestPrinter(); | |
| instance->addLogger(std::make_unique<LogcatTestLogger>()); | |
| instance->addLogger(std::make_unique<KmsgTestLogger>()); | |
| auto& listeners = testing::UnitTest::GetInstance()->listeners(); | |
| listeners.Append(instance); | |
| } | |
| } // namespace |