1 // Copyright (c) 2015 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.
5 #include "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/files/file_util.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "components/tracing/trace_config_file.h"
10 #include "components/tracing/tracing_switches.h"
11 #include "testing/gtest/include/gtest/gtest.h"
17 const char kTraceConfig
[] =
19 "\"enable_argument_filter\":true,"
20 "\"enable_sampling\":true,"
21 "\"enable_systrace\":true,"
22 "\"excluded_categories\":[\"excluded\",\"exc_pattern*\"],"
23 "\"included_categories\":[\"included\","
25 "\"disabled-by-default-cc\"],"
26 "\"record_mode\":\"record-continuously\","
27 "\"synthetic_delays\":[\"test.Delay1;16\",\"test.Delay2;32\"]"
30 std::string
GetTraceConfigFileContent(std::string trace_config
,
31 std::string startup_duration
,
32 std::string result_file
) {
33 std::string content
= "{";
34 if (!trace_config
.empty())
35 content
+= "\"trace_config\":" + trace_config
;
37 if (!startup_duration
.empty()) {
40 content
+= "\"startup_duration\":" + startup_duration
;
43 if (!result_file
.empty()) {
46 content
+= "\"result_file\":\"" + result_file
+ "\"";
55 TEST(TraceConfigFileTest
, TraceStartupEnabled
) {
56 base::ShadowingAtExitManager sem
;
57 base::CommandLine::ForCurrentProcess()->AppendSwitch(
58 switches::kTraceStartup
);
59 base::CommandLine::ForCurrentProcess()->AppendSwitch(
60 switches::kTraceConfigFile
);
62 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
65 TEST(TraceConfigFileTest
, TraceShutdownEnabled
) {
66 base::ShadowingAtExitManager sem
;
67 base::CommandLine::ForCurrentProcess()->AppendSwitch(
68 switches::kTraceShutdown
);
69 base::CommandLine::ForCurrentProcess()->AppendSwitch(
70 switches::kTraceConfigFile
);
72 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
75 TEST(TraceConfigFileTest
, TraceConfigFileNotEnabled
) {
76 base::ShadowingAtExitManager sem
;
77 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
80 TEST(TraceConfigFileTest
, TraceConfigFileEnabledWithoutPath
) {
81 base::ShadowingAtExitManager sem
;
82 base::CommandLine::ForCurrentProcess()->AppendSwitch(
83 switches::kTraceConfigFile
);
85 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled());
86 EXPECT_EQ(base::trace_event::TraceConfig().ToString(),
87 TraceConfigFile::GetInstance()->GetTraceConfig().ToString());
88 EXPECT_EQ(5, TraceConfigFile::GetInstance()->GetStartupDuration());
89 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("chrometrace.log")),
90 TraceConfigFile::GetInstance()->GetResultFile());
93 TEST(TraceConfigFileTest
, TraceConfigFileEnabledWithInvalidPath
) {
94 base::ShadowingAtExitManager sem
;
95 base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
96 switches::kTraceConfigFile
,
97 base::FilePath(FILE_PATH_LITERAL("invalid-trace-config-file-path")));
99 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
102 TEST(TraceConfigFileTest
, ValidContent
) {
103 base::ShadowingAtExitManager sem
;
104 std::string content
= GetTraceConfigFileContent(
105 kTraceConfig
, "10", "trace_result_file.log");
107 base::FilePath trace_config_file
;
108 base::ScopedTempDir temp_dir
;
109 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
111 base::CreateTemporaryFileInDir(temp_dir
.path(), &trace_config_file
));
112 ASSERT_NE(-1, base::WriteFile(
113 trace_config_file
, content
.c_str(), (int)content
.length()));
114 base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
115 switches::kTraceConfigFile
, trace_config_file
);
117 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled());
120 TraceConfigFile::GetInstance()->GetTraceConfig().ToString().c_str());
121 EXPECT_EQ(10, TraceConfigFile::GetInstance()->GetStartupDuration());
122 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("trace_result_file.log")),
123 TraceConfigFile::GetInstance()->GetResultFile());
126 TEST(TraceConfigFileTest
, ValidContentWithOnlyTraceConfig
) {
127 base::ShadowingAtExitManager sem
;
128 std::string content
= GetTraceConfigFileContent(kTraceConfig
, "", "");
130 base::FilePath trace_config_file
;
131 base::ScopedTempDir temp_dir
;
132 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
134 base::CreateTemporaryFileInDir(temp_dir
.path(), &trace_config_file
));
135 ASSERT_NE(-1, base::WriteFile(
136 trace_config_file
, content
.c_str(), (int)content
.length()));
137 base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
138 switches::kTraceConfigFile
, trace_config_file
);
140 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled());
143 TraceConfigFile::GetInstance()->GetTraceConfig().ToString().c_str());
144 EXPECT_EQ(0, TraceConfigFile::GetInstance()->GetStartupDuration());
145 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("chrometrace.log")),
146 TraceConfigFile::GetInstance()->GetResultFile());
149 TEST(TraceConfigFileTest
, ContentWithNegtiveDuration
) {
150 base::ShadowingAtExitManager sem
;
151 std::string content
= GetTraceConfigFileContent(kTraceConfig
, "-1", "");
153 base::FilePath trace_config_file
;
154 base::ScopedTempDir temp_dir
;
155 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
157 base::CreateTemporaryFileInDir(temp_dir
.path(), &trace_config_file
));
158 ASSERT_NE(-1, base::WriteFile(
159 trace_config_file
, content
.c_str(), (int)content
.length()));
160 base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
161 switches::kTraceConfigFile
, trace_config_file
);
163 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled());
166 TraceConfigFile::GetInstance()->GetTraceConfig().ToString().c_str());
167 EXPECT_EQ(0, TraceConfigFile::GetInstance()->GetStartupDuration());
168 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("chrometrace.log")),
169 TraceConfigFile::GetInstance()->GetResultFile());
172 TEST(TraceConfigFileTest
, ContentWithoutTraceConfig
) {
173 base::ShadowingAtExitManager sem
;
174 std::string content
= GetTraceConfigFileContent(
175 "", "10", "trace_result_file.log");
177 base::FilePath trace_config_file
;
178 base::ScopedTempDir temp_dir
;
179 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
181 base::CreateTemporaryFileInDir(temp_dir
.path(), &trace_config_file
));
182 ASSERT_NE(-1, base::WriteFile(
183 trace_config_file
, content
.c_str(), (int)content
.length()));
184 base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
185 switches::kTraceConfigFile
, trace_config_file
);
187 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
190 TEST(TraceConfigFileTest
, InvalidContent
) {
191 base::ShadowingAtExitManager sem
;
192 std::string content
= "invalid trace config file content";
194 base::FilePath trace_config_file
;
195 base::ScopedTempDir temp_dir
;
196 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
198 base::CreateTemporaryFileInDir(temp_dir
.path(), &trace_config_file
));
199 ASSERT_NE(-1, base::WriteFile(
200 trace_config_file
, content
.c_str(), (int)content
.length()));
201 base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
202 switches::kTraceConfigFile
, trace_config_file
);
204 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
207 TEST(TraceConfigFileTest
, EmptyContent
) {
208 base::ShadowingAtExitManager sem
;
209 base::FilePath trace_config_file
;
210 base::ScopedTempDir temp_dir
;
211 ASSERT_TRUE(temp_dir
.CreateUniqueTempDir());
213 base::CreateTemporaryFileInDir(temp_dir
.path(), &trace_config_file
));
214 base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
215 switches::kTraceConfigFile
, trace_config_file
);
217 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
220 } // namespace tracing