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/launcher/test_results_tracker.h"
6
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/time/time.h"
10 #include "testing/gmock/include/gmock/gmock.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12
13 using ::testing::HasSubstr;
14
15 namespace base {
16
TEST(TestResultsTrackerTest,SaveSummaryAsJSONWithLinkInResult)17 TEST(TestResultsTrackerTest, SaveSummaryAsJSONWithLinkInResult) {
18 TestResultsTracker tracker;
19 TestResult result;
20 result.AddLink("link", "http://google.com");
21 TestResultsTracker::AggregateTestResult aggregate_result;
22 aggregate_result.test_results.push_back(result);
23 tracker.per_iteration_data_.emplace_back(
24 TestResultsTracker::PerIterationData());
25 tracker.per_iteration_data_.back().results["dummy"] = aggregate_result;
26 FilePath temp_file;
27 CreateTemporaryFile(&temp_file);
28 ASSERT_TRUE(tracker.SaveSummaryAsJSON(temp_file, std::vector<std::string>()));
29 std::string content;
30 ASSERT_TRUE(ReadFileToString(temp_file, &content));
31 std::string expected_content = R"raw("links":{"link":)raw"
32 R"raw({"content":"http://google.com"}})raw";
33 EXPECT_TRUE(content.find(expected_content) != std::string::npos)
34 << expected_content << " not found in " << content;
35 }
36
TEST(TestResultsTrackerTest,SaveSummaryAsJSONWithTagInResult)37 TEST(TestResultsTrackerTest, SaveSummaryAsJSONWithTagInResult) {
38 TestResultsTracker tracker;
39 TestResult result;
40 result.AddTag("tag_name", "tag_value");
41 TestResultsTracker::AggregateTestResult aggregate_result;
42 aggregate_result.test_results.push_back(result);
43 tracker.per_iteration_data_.push_back({});
44 tracker.per_iteration_data_.back().results["dummy"] = aggregate_result;
45 FilePath temp_file;
46 CreateTemporaryFile(&temp_file);
47 ASSERT_TRUE(tracker.SaveSummaryAsJSON(temp_file, std::vector<std::string>()));
48 std::string content;
49 ASSERT_TRUE(ReadFileToString(temp_file, &content));
50 std::string expected_content = R"raw("tags":{"tag_name":)raw"
51 R"raw({"values":["tag_value"]}})raw";
52 EXPECT_TRUE(content.find(expected_content) != std::string::npos)
53 << expected_content << " not found in " << content;
54 }
55
TEST(TestResultsTrackerTest,SaveSummaryAsJSONWithMultiTagsInResult)56 TEST(TestResultsTrackerTest, SaveSummaryAsJSONWithMultiTagsInResult) {
57 TestResultsTracker tracker;
58 TestResult result;
59 result.AddTag("tag_name1", "tag_value1");
60 result.AddTag("tag_name2", "tag_value2");
61 TestResultsTracker::AggregateTestResult aggregate_result;
62 aggregate_result.test_results.push_back(result);
63 tracker.per_iteration_data_.emplace_back();
64 tracker.per_iteration_data_.back().results["dummy"] = aggregate_result;
65 FilePath temp_file;
66 CreateTemporaryFile(&temp_file);
67 ASSERT_TRUE(tracker.SaveSummaryAsJSON(temp_file, std::vector<std::string>()));
68 std::string content;
69 ASSERT_TRUE(ReadFileToString(temp_file, &content));
70 std::string expected_content = R"raw("tags":{"tag_name1":)raw"
71 R"raw({"values":["tag_value1"]})raw"
72 R"raw(,"tag_name2":)raw"
73 R"raw({"values":["tag_value2"]}})raw";
74 EXPECT_TRUE(content.find(expected_content) != std::string::npos)
75 << expected_content << " not found in " << content;
76 }
77
TEST(TestResultsTrackerTest,SaveSummaryAsJSONWithMultiTagsSameNameInResult)78 TEST(TestResultsTrackerTest, SaveSummaryAsJSONWithMultiTagsSameNameInResult) {
79 TestResultsTracker tracker;
80 TestResult result;
81 result.AddTag("tag_name", "tag_value1");
82 result.AddTag("tag_name", "tag_value2");
83 TestResultsTracker::AggregateTestResult aggregate_result;
84 aggregate_result.test_results.push_back(result);
85 tracker.per_iteration_data_.emplace_back();
86 tracker.per_iteration_data_.back().results["dummy"] = aggregate_result;
87 FilePath temp_file;
88 CreateTemporaryFile(&temp_file);
89 ASSERT_TRUE(tracker.SaveSummaryAsJSON(temp_file, std::vector<std::string>()));
90 std::string content;
91 ASSERT_TRUE(ReadFileToString(temp_file, &content));
92 std::string expected_content = R"raw("tags":{"tag_name":)raw"
93 R"raw({"values":)raw"
94 R"raw(["tag_value1","tag_value2"]}})raw";
95 EXPECT_TRUE(content.find(expected_content) != std::string::npos)
96 << expected_content << " not found in " << content;
97 }
98
TEST(TestResultsTrackerTest,SaveSummaryAsJSONWithPropertyInResult)99 TEST(TestResultsTrackerTest, SaveSummaryAsJSONWithPropertyInResult) {
100 TestResultsTracker tracker;
101 TestResult result;
102 result.AddProperty("test_property_name", "test_property_value");
103 TestResultsTracker::AggregateTestResult aggregate_result;
104 aggregate_result.test_results.push_back(result);
105 tracker.per_iteration_data_.emplace_back(
106 TestResultsTracker::PerIterationData());
107 tracker.per_iteration_data_.back().results["dummy"] = aggregate_result;
108 FilePath temp_file;
109 CreateTemporaryFile(&temp_file);
110 ASSERT_TRUE(tracker.SaveSummaryAsJSON(temp_file, std::vector<std::string>()));
111 std::string content;
112 ASSERT_TRUE(ReadFileToString(temp_file, &content));
113 std::string expected_content = R"raw("properties":{"test_property_name":)raw"
114 R"raw({"value":"test_property_value"}})raw";
115 EXPECT_TRUE(content.find(expected_content) != std::string::npos)
116 << expected_content << " not found in " << content;
117 }
118
TEST(TestResultsTrackerTest,SaveSummaryAsJSONWithOutTimestampInResult)119 TEST(TestResultsTrackerTest, SaveSummaryAsJSONWithOutTimestampInResult) {
120 TestResultsTracker tracker;
121 TestResult result;
122 result.full_name = "A.B";
123
124 TestResultsTracker::AggregateTestResult aggregate_result;
125 aggregate_result.test_results.push_back(result);
126 tracker.per_iteration_data_.emplace_back(
127 TestResultsTracker::PerIterationData());
128 tracker.per_iteration_data_.back().results["dummy"] = aggregate_result;
129 FilePath temp_file;
130 CreateTemporaryFile(&temp_file);
131 ASSERT_TRUE(tracker.SaveSummaryAsJSON(temp_file, std::vector<std::string>()));
132 std::string content;
133 ASSERT_TRUE(ReadFileToString(temp_file, &content));
134
135 for (auto* not_expected_content : {"thread_id", "process_num", "timestamp"}) {
136 EXPECT_THAT(content, ::testing::Not(HasSubstr(not_expected_content)));
137 }
138 }
139
TEST(TestResultsTrackerTest,SaveSummaryAsJSONWithTimestampInResult)140 TEST(TestResultsTrackerTest, SaveSummaryAsJSONWithTimestampInResult) {
141 TestResultsTracker tracker;
142 TestResult result;
143 result.full_name = "A.B";
144 result.thread_id = 123;
145 result.process_num = 456;
146 result.timestamp = Time::Now();
147
148 TestResultsTracker::AggregateTestResult aggregate_result;
149 aggregate_result.test_results.push_back(result);
150 tracker.per_iteration_data_.emplace_back(
151 TestResultsTracker::PerIterationData());
152 tracker.per_iteration_data_.back().results["dummy"] = aggregate_result;
153 FilePath temp_file;
154 CreateTemporaryFile(&temp_file);
155 ASSERT_TRUE(tracker.SaveSummaryAsJSON(temp_file, std::vector<std::string>()));
156 std::string content;
157 ASSERT_TRUE(ReadFileToString(temp_file, &content));
158
159 EXPECT_THAT(content, HasSubstr(R"raw("thread_id":123)raw"));
160 EXPECT_THAT(content, HasSubstr(R"raw("process_num":456)raw"));
161 EXPECT_THAT(content, HasSubstr(R"raw("timestamp":)raw"));
162 }
163
TEST(TestResultsTrackerTest,RepeatDisabledTests)164 TEST(TestResultsTrackerTest, RepeatDisabledTests) {
165 constexpr char TEST_NAME[] = "DISABLED_Test1";
166 constexpr char TEST_NAME_WITHOUT_PREFIX[] = "Test1";
167 TestResultsTracker tracker;
168 tracker.AddTestPlaceholder(TEST_NAME);
169 tracker.OnTestIterationStarting();
170 tracker.GeneratePlaceholderIteration();
171 TestResult result;
172 result.full_name = TEST_NAME;
173 result.status = TestResult::TEST_SUCCESS;
174 for (int i = 0; i < 10; i++) {
175 tracker.AddTestResult(result);
176 }
177 TestResultsTracker::TestStatusMap results =
178 tracker.GetTestStatusMapForAllIterations();
179 ASSERT_TRUE(results[TestResult::TEST_SUCCESS].find(TEST_NAME) ==
180 results[TestResult::TEST_SUCCESS].end());
181 ASSERT_TRUE(
182 results[TestResult::TEST_SUCCESS].find(TEST_NAME_WITHOUT_PREFIX) !=
183 results[TestResult::TEST_SUCCESS].end());
184 }
185
186 } // namespace base
187