OLD | NEW |
| (Empty) |
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. | |
4 | |
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" | |
12 | |
13 namespace tracing { | |
14 | |
15 namespace { | |
16 | |
17 const char kTraceConfig[] = | |
18 "{" | |
19 "\"enable_argument_filter\":true," | |
20 "\"enable_sampling\":true," | |
21 "\"enable_systrace\":true," | |
22 "\"excluded_categories\":[\"excluded\",\"exc_pattern*\"]," | |
23 "\"included_categories\":[\"included\"," | |
24 "\"inc_pattern*\"," | |
25 "\"disabled-by-default-cc\"]," | |
26 "\"record_mode\":\"record-continuously\"," | |
27 "\"synthetic_delays\":[\"test.Delay1;16\",\"test.Delay2;32\"]" | |
28 "}"; | |
29 | |
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; | |
36 | |
37 if (!startup_duration.empty()) { | |
38 if (content != "{") | |
39 content += ","; | |
40 content += "\"startup_duration\":" + startup_duration; | |
41 } | |
42 | |
43 if (!result_file.empty()) { | |
44 if (content != "{") | |
45 content += ","; | |
46 content += "\"result_file\":\"" + result_file + "\""; | |
47 } | |
48 | |
49 content += "}"; | |
50 return content; | |
51 } | |
52 | |
53 } // namespace | |
54 | |
55 TEST(TraceConfigFileTest, TraceStartupEnabled) { | |
56 base::ShadowingAtExitManager sem; | |
57 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
58 switches::kTraceStartup); | |
59 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
60 switches::kTraceConfigFile); | |
61 | |
62 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); | |
63 } | |
64 | |
65 TEST(TraceConfigFileTest, TraceShutdownEnabled) { | |
66 base::ShadowingAtExitManager sem; | |
67 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
68 switches::kTraceShutdown); | |
69 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
70 switches::kTraceConfigFile); | |
71 | |
72 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); | |
73 } | |
74 | |
75 TEST(TraceConfigFileTest, TraceConfigFileNotEnabled) { | |
76 base::ShadowingAtExitManager sem; | |
77 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); | |
78 } | |
79 | |
80 TEST(TraceConfigFileTest, TraceConfigFileEnabledWithoutPath) { | |
81 base::ShadowingAtExitManager sem; | |
82 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
83 switches::kTraceConfigFile); | |
84 | |
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()); | |
91 } | |
92 | |
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"))); | |
98 | |
99 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); | |
100 } | |
101 | |
102 TEST(TraceConfigFileTest, ValidContent) { | |
103 base::ShadowingAtExitManager sem; | |
104 std::string content = GetTraceConfigFileContent( | |
105 kTraceConfig, "10", "trace_result_file.log"); | |
106 | |
107 base::FilePath trace_config_file; | |
108 base::ScopedTempDir temp_dir; | |
109 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
110 ASSERT_TRUE( | |
111 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); | |
112 ASSERT_TRUE(base::WriteFile( | |
113 trace_config_file, content.c_str(), content.length()) != -1); | |
114 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( | |
115 switches::kTraceConfigFile, trace_config_file); | |
116 | |
117 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled()); | |
118 EXPECT_STREQ( | |
119 kTraceConfig, | |
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()); | |
124 } | |
125 | |
126 TEST(TraceConfigFileTest, ValidContentWithOnlyTraceConfig) { | |
127 base::ShadowingAtExitManager sem; | |
128 std::string content = GetTraceConfigFileContent(kTraceConfig, "", ""); | |
129 | |
130 base::FilePath trace_config_file; | |
131 base::ScopedTempDir temp_dir; | |
132 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
133 ASSERT_TRUE( | |
134 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); | |
135 ASSERT_TRUE(base::WriteFile( | |
136 trace_config_file, content.c_str(), content.length()) != -1); | |
137 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( | |
138 switches::kTraceConfigFile, trace_config_file); | |
139 | |
140 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled()); | |
141 EXPECT_STREQ( | |
142 kTraceConfig, | |
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()); | |
147 } | |
148 | |
149 TEST(TraceConfigFileTest, ContentWithNegtiveDuration) { | |
150 base::ShadowingAtExitManager sem; | |
151 std::string content = GetTraceConfigFileContent(kTraceConfig, "-1", ""); | |
152 | |
153 base::FilePath trace_config_file; | |
154 base::ScopedTempDir temp_dir; | |
155 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
156 ASSERT_TRUE( | |
157 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); | |
158 ASSERT_TRUE(base::WriteFile( | |
159 trace_config_file, content.c_str(), content.length()) != -1); | |
160 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( | |
161 switches::kTraceConfigFile, trace_config_file); | |
162 | |
163 ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled()); | |
164 EXPECT_STREQ( | |
165 kTraceConfig, | |
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()); | |
170 } | |
171 | |
172 TEST(TraceConfigFileTest, ContentWithoutTraceConfig) { | |
173 base::ShadowingAtExitManager sem; | |
174 std::string content = GetTraceConfigFileContent( | |
175 "", "10", "trace_result_file.log"); | |
176 | |
177 base::FilePath trace_config_file; | |
178 base::ScopedTempDir temp_dir; | |
179 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
180 ASSERT_TRUE( | |
181 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); | |
182 ASSERT_TRUE(base::WriteFile( | |
183 trace_config_file, content.c_str(), content.length()) != -1); | |
184 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( | |
185 switches::kTraceConfigFile, trace_config_file); | |
186 | |
187 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); | |
188 } | |
189 | |
190 TEST(TraceConfigFileTest, InvalidContent) { | |
191 base::ShadowingAtExitManager sem; | |
192 std::string content = "invalid trace config file content"; | |
193 | |
194 base::FilePath trace_config_file; | |
195 base::ScopedTempDir temp_dir; | |
196 ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); | |
197 ASSERT_TRUE( | |
198 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); | |
199 ASSERT_TRUE(base::WriteFile( | |
200 trace_config_file, content.c_str(), content.length()) != -1); | |
201 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( | |
202 switches::kTraceConfigFile, trace_config_file); | |
203 | |
204 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); | |
205 } | |
206 | |
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()); | |
212 ASSERT_TRUE( | |
213 base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file)); | |
214 base::CommandLine::ForCurrentProcess()->AppendSwitchPath( | |
215 switches::kTraceConfigFile, trace_config_file); | |
216 | |
217 EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled()); | |
218 } | |
219 | |
220 } // namespace tracing | |
OLD | NEW |