1 // Copyright 2020 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 #include "base/test/gtest_xml_unittest_result_printer.h"
6
7 #include "base/base64.h"
8 #include "base/command_line.h"
9 #include "base/files/file_util.h"
10 #include "base/strings/strcat.h"
11 #include "base/test/test_switches.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace base {
16
17 class XmlUnitTestResultPrinterTest : public ::testing::Test {
18 public:
SetUp()19 void SetUp() override {
20 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
21 switches::kTestLauncherOutput)) {
22 GTEST_SKIP() << "XmlUnitTestResultPrinterTest is not initialized "
23 << "for single process tests.";
24 }
25 }
26 };
27
TEST_F(XmlUnitTestResultPrinterTest,LinkInXmlFile)28 TEST_F(XmlUnitTestResultPrinterTest, LinkInXmlFile) {
29 XmlUnitTestResultPrinter::Get()->AddLink("unique_link", "http://google.com");
30 std::string file_path =
31 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
32 switches::kTestLauncherOutput);
33 std::string content;
34 ASSERT_TRUE(
35 base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
36 std::string expected_content =
37 base::StrCat({"<link name=\"LinkInXmlFile\" "
38 "classname=\"XmlUnitTestResultPrinterTest\" "
39 "link_name=\"unique_link\">",
40 "http://google.com", "</link>"});
41 EXPECT_TRUE(content.find(expected_content) != std::string::npos)
42 << expected_content << " not found in " << content;
43 }
44
TEST_F(XmlUnitTestResultPrinterTest,EscapedLinkInXmlFile)45 TEST_F(XmlUnitTestResultPrinterTest, EscapedLinkInXmlFile) {
46 XmlUnitTestResultPrinter::Get()->AddLink(
47 "unique_link", "http://google.com/path?id=\"'<>&\"");
48 std::string file_path =
49 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
50 switches::kTestLauncherOutput);
51 std::string content;
52 ASSERT_TRUE(
53 base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
54 std::string expected_content = base::StrCat(
55 {"<link name=\"EscapedLinkInXmlFile\" "
56 "classname=\"XmlUnitTestResultPrinterTest\" "
57 "link_name=\"unique_link\">",
58 "http://google.com/path?id="'<>&"", "</link>"});
59 EXPECT_TRUE(content.find(expected_content) != std::string::npos)
60 << expected_content << " not found in " << content;
61 }
62
TEST_F(XmlUnitTestResultPrinterTest,TagInXmlFile)63 TEST_F(XmlUnitTestResultPrinterTest, TagInXmlFile) {
64 XmlUnitTestResultPrinter::Get()->AddTag("tag_name", "tag_value");
65 std::string file_path =
66 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
67 switches::kTestLauncherOutput);
68 std::string content;
69 ASSERT_TRUE(
70 base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
71 std::string expected_content =
72 base::StrCat({"<tag name=\"TagInXmlFile\" "
73 "classname=\"XmlUnitTestResultPrinterTest\" "
74 "tag_name=\"tag_name\">",
75 "tag_value", "</tag>"});
76 EXPECT_TRUE(content.find(expected_content) != std::string::npos)
77 << expected_content << " not found in " << content;
78 }
79
TEST_F(XmlUnitTestResultPrinterTest,MultiTagsInXmlFile)80 TEST_F(XmlUnitTestResultPrinterTest, MultiTagsInXmlFile) {
81 XmlUnitTestResultPrinter::Get()->AddTag("tag_name1", "tag_value1");
82 XmlUnitTestResultPrinter::Get()->AddTag("tag_name2", "tag_value2");
83 std::string file_path =
84 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
85 switches::kTestLauncherOutput);
86 std::string content;
87 ASSERT_TRUE(
88 base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
89 std::string expected_content_1 =
90 "<tag name=\"MultiTagsInXmlFile\" "
91 "classname=\"XmlUnitTestResultPrinterTest\" "
92 "tag_name=\"tag_name1\">tag_value1</tag>";
93 EXPECT_TRUE(content.find(expected_content_1) != std::string::npos)
94 << expected_content_1 << " not found in " << content;
95
96 std::string expected_content_2 =
97 "<tag name=\"MultiTagsInXmlFile\" "
98 "classname=\"XmlUnitTestResultPrinterTest\" "
99 "tag_name=\"tag_name2\">tag_value2</tag>";
100 EXPECT_TRUE(content.find(expected_content_2) != std::string::npos)
101 << expected_content_2 << " not found in " << content;
102 }
103
TEST_F(XmlUnitTestResultPrinterTest,MultiTagsWithSameNameInXmlFile)104 TEST_F(XmlUnitTestResultPrinterTest, MultiTagsWithSameNameInXmlFile) {
105 XmlUnitTestResultPrinter::Get()->AddTag("tag_name", "tag_value1");
106 XmlUnitTestResultPrinter::Get()->AddTag("tag_name", "tag_value2");
107 std::string file_path =
108 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
109 switches::kTestLauncherOutput);
110 std::string content;
111 ASSERT_TRUE(
112 base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
113 std::string expected_content_1 =
114 "<tag name=\"MultiTagsWithSameNameInXmlFile\" "
115 "classname=\"XmlUnitTestResultPrinterTest\" "
116 "tag_name=\"tag_name\">tag_value1</tag>";
117 EXPECT_TRUE(content.find(expected_content_1) != std::string::npos)
118 << expected_content_1 << " not found in " << content;
119
120 std::string expected_content_2 =
121 "<tag name=\"MultiTagsWithSameNameInXmlFile\" "
122 "classname=\"XmlUnitTestResultPrinterTest\" "
123 "tag_name=\"tag_name\">tag_value2</tag>";
124 EXPECT_TRUE(content.find(expected_content_2) != std::string::npos)
125 << expected_content_2 << " not found in " << content;
126 }
127
128 class XmlUnitTestResultPrinterTimestampTest : public ::testing::Test {
129 public:
TearDownTestSuite()130 static void TearDownTestSuite() {
131 // <testcase ...> should generated after test case finishes. After
132 // TearDown().
133 std::string file_path =
134 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
135 switches::kTestLauncherOutput);
136 if (file_path.empty()) {
137 GTEST_SKIP() << "Test has to run with --" << switches::kTestLauncherOutput
138 << " switch.";
139 }
140 std::string content;
141 ASSERT_TRUE(
142 base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
143 EXPECT_THAT(content, ::testing::ContainsRegex("<testcase.*timestamp="));
144 }
145 };
146
TEST_F(XmlUnitTestResultPrinterTimestampTest,TimestampInXmlFile)147 TEST_F(XmlUnitTestResultPrinterTimestampTest, TimestampInXmlFile) {
148 // <x-teststart ... /> should generated at this point
149 std::string file_path =
150 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
151 switches::kTestLauncherOutput);
152 if (file_path.empty()) {
153 GTEST_SKIP() << "Test has to run with --" << switches::kTestLauncherOutput
154 << " switch.";
155 }
156 std::string content;
157 ASSERT_TRUE(
158 base::ReadFileToString(FilePath::FromUTF8Unsafe(file_path), &content));
159 EXPECT_THAT(content, ::testing::ContainsRegex("<x-teststart.*timestamp="));
160 }
161
162 } // namespace base
163