1 // Copyright 2013 The Chromium Authors. All rights reserved.
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/compiler_specific.h"
11 #include "base/macros.h"
12 #include "base/threading/thread_checker.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 
15 namespace base {
16 
17 class FilePath;
18 
19 // Generates an XML output file. Format is very close to GTest, but has
20 // extensions needed by the test launcher.
21 class XmlUnitTestResultPrinter : public testing::EmptyTestEventListener {
22  public:
23   XmlUnitTestResultPrinter();
24   ~XmlUnitTestResultPrinter() override;
25 
26   static XmlUnitTestResultPrinter* Get();
27 
28   // Add link in the gtest xml output.
29   // Please see AddLinkToTestResult in gtest_links.h for detailed
30   // explanation and usage.
31   void AddLink(const std::string& name, const std::string& url);
32 
33   // Must be called before adding as a listener. Returns true on success.
34   bool Initialize(const FilePath& output_file_path) WARN_UNUSED_RESULT;
35 
36   // CHECK/DCHECK failed. Print file/line and message to the xml.
37   void OnAssert(const char* file,
38                 int line,
39                 const std::string& summary,
40                 const std::string& message);
41 
42  private:
43   // testing::EmptyTestEventListener:
44   void OnTestCaseStart(const testing::TestCase& test_case) override;
45   void OnTestStart(const testing::TestInfo& test_info) override;
46   void OnTestEnd(const testing::TestInfo& test_info) override;
47   void OnTestCaseEnd(const testing::TestCase& test_case) override;
48 
49   void WriteTestPartResult(const char* file,
50                            int line,
51                            testing::TestPartResult::Type type,
52                            const std::string& summary,
53                            const std::string& message);
54 
55   static XmlUnitTestResultPrinter* instance_;
56   FILE* output_file_{nullptr};
57   bool open_failed_{false};
58   ThreadChecker thread_checker_;
59 
60   DISALLOW_COPY_AND_ASSIGN(XmlUnitTestResultPrinter);
61 };
62 
63 }  // namespace base
64 
65 #endif  // BASE_TEST_GTEST_XML_UNITTEST_RESULT_PRINTER_H_
66