1 // Copyright 2013 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_TEST_GTEST_XML_UNITTEST_RESULT_PRINTER_H_ 6 #define BASE_TEST_GTEST_XML_UNITTEST_RESULT_PRINTER_H_ 7 8 #include <stdio.h> 9 10 #include "base/memory/raw_ptr.h" 11 #include "base/threading/thread_checker.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace base { 15 16 class FilePath; 17 18 // Generates an XML output file. Format is very close to GTest, but has 19 // extensions needed by the test launcher. 20 class XmlUnitTestResultPrinter : public testing::EmptyTestEventListener { 21 public: 22 XmlUnitTestResultPrinter(); 23 24 XmlUnitTestResultPrinter(const XmlUnitTestResultPrinter&) = delete; 25 XmlUnitTestResultPrinter& operator=(const XmlUnitTestResultPrinter&) = delete; 26 27 ~XmlUnitTestResultPrinter() override; 28 29 static XmlUnitTestResultPrinter* Get(); 30 31 // Add link in the gtest xml output. 32 // Please see AddLinkToTestResult in gtest_links.h for detailed 33 // explanation and usage. 34 void AddLink(const std::string& name, const std::string& url); 35 36 // Add tag in the gtest xml output. 37 // Please see AddTagToTestResult in gtest_tags.h for detailed 38 // explanation and usage. 39 void AddTag(const std::string& name, const std::string& value); 40 41 // Must be called before adding as a listener. Returns true on success. 42 [[nodiscard]] bool Initialize(const FilePath& output_file_path); 43 44 // CHECK/DCHECK failed. Print file/line and message to the xml. 45 void OnAssert(const char* file, 46 int line, 47 const std::string& summary, 48 const std::string& message); 49 50 private: 51 // testing::EmptyTestEventListener: 52 void OnTestSuiteStart(const testing::TestSuite& test_suite) override; 53 void OnTestStart(const testing::TestInfo& test_info) override; 54 void OnTestEnd(const testing::TestInfo& test_info) override; 55 void OnTestSuiteEnd(const testing::TestSuite& test_suite) override; 56 57 void WriteTestPartResult(const char* file, 58 int line, 59 testing::TestPartResult::Type type, 60 const std::string& summary, 61 const std::string& message); 62 63 static XmlUnitTestResultPrinter* instance_; 64 raw_ptr<FILE> output_file_ = nullptr; 65 bool open_failed_ = false; 66 67 // Flag that's true iff a test has been started but not yet ended. 68 bool test_running_ = false; 69 70 ThreadChecker thread_checker_; 71 }; 72 73 } // namespace base 74 75 #endif // BASE_TEST_GTEST_XML_UNITTEST_RESULT_PRINTER_H_ 76