Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1420)

Unified Diff: components/tracing/trace_config_file_unittest.cc

Issue 1309243004: Reland again [Startup Tracing] Add --trace-config-file flag (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix unit test Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/tracing/trace_config_file.cc ('k') | components/tracing/tracing_switches.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/tracing/trace_config_file_unittest.cc
diff --git a/components/tracing/trace_config_file_unittest.cc b/components/tracing/trace_config_file_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d62c4d44217c5af0ec27aee91ef665a5bbd9e741
--- /dev/null
+++ b/components/tracing/trace_config_file_unittest.cc
@@ -0,0 +1,220 @@
+// Copyright (c) 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/at_exit.h"
+#include "base/command_line.h"
+#include "base/files/file_util.h"
+#include "base/files/scoped_temp_dir.h"
+#include "components/tracing/trace_config_file.h"
+#include "components/tracing/tracing_switches.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace tracing {
+
+namespace {
+
+const char kTraceConfig[] =
+ "{"
+ "\"enable_argument_filter\":true,"
+ "\"enable_sampling\":true,"
+ "\"enable_systrace\":true,"
+ "\"excluded_categories\":[\"excluded\",\"exc_pattern*\"],"
+ "\"included_categories\":[\"included\","
+ "\"inc_pattern*\","
+ "\"disabled-by-default-cc\"],"
+ "\"record_mode\":\"record-continuously\","
+ "\"synthetic_delays\":[\"test.Delay1;16\",\"test.Delay2;32\"]"
+ "}";
+
+std::string GetTraceConfigFileContent(std::string trace_config,
+ std::string startup_duration,
+ std::string result_file) {
+ std::string content = "{";
+ if (!trace_config.empty())
+ content += "\"trace_config\":" + trace_config;
+
+ if (!startup_duration.empty()) {
+ if (content != "{")
+ content += ",";
+ content += "\"startup_duration\":" + startup_duration;
+ }
+
+ if (!result_file.empty()) {
+ if (content != "{")
+ content += ",";
+ content += "\"result_file\":\"" + result_file + "\"";
+ }
+
+ content += "}";
+ return content;
+}
+
+} // namespace
+
+TEST(TraceConfigFileTest, TraceStartupEnabled) {
+ base::ShadowingAtExitManager sem;
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kTraceStartup);
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kTraceConfigFile);
+
+ EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
+}
+
+TEST(TraceConfigFileTest, TraceShutdownEnabled) {
+ base::ShadowingAtExitManager sem;
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kTraceShutdown);
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kTraceConfigFile);
+
+ EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
+}
+
+TEST(TraceConfigFileTest, TraceConfigFileNotEnabled) {
+ base::ShadowingAtExitManager sem;
+ EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
+}
+
+TEST(TraceConfigFileTest, TraceConfigFileEnabledWithoutPath) {
+ base::ShadowingAtExitManager sem;
+ base::CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kTraceConfigFile);
+
+ ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled());
+ EXPECT_EQ(base::trace_event::TraceConfig().ToString(),
+ TraceConfigFile::GetInstance()->GetTraceConfig().ToString());
+ EXPECT_EQ(5, TraceConfigFile::GetInstance()->GetStartupDuration());
+ EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("chrometrace.log")),
+ TraceConfigFile::GetInstance()->GetResultFile());
+}
+
+TEST(TraceConfigFileTest, TraceConfigFileEnabledWithInvalidPath) {
+ base::ShadowingAtExitManager sem;
+ base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
+ switches::kTraceConfigFile,
+ base::FilePath(FILE_PATH_LITERAL("invalid-trace-config-file-path")));
+
+ EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
+}
+
+TEST(TraceConfigFileTest, ValidContent) {
+ base::ShadowingAtExitManager sem;
+ std::string content = GetTraceConfigFileContent(
+ kTraceConfig, "10", "trace_result_file.log");
+
+ base::FilePath trace_config_file;
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ ASSERT_TRUE(
+ base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file));
+ ASSERT_NE(-1, base::WriteFile(
+ trace_config_file, content.c_str(), (int)content.length()));
+ base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
+ switches::kTraceConfigFile, trace_config_file);
+
+ ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled());
+ EXPECT_STREQ(
+ kTraceConfig,
+ TraceConfigFile::GetInstance()->GetTraceConfig().ToString().c_str());
+ EXPECT_EQ(10, TraceConfigFile::GetInstance()->GetStartupDuration());
+ EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("trace_result_file.log")),
+ TraceConfigFile::GetInstance()->GetResultFile());
+}
+
+TEST(TraceConfigFileTest, ValidContentWithOnlyTraceConfig) {
+ base::ShadowingAtExitManager sem;
+ std::string content = GetTraceConfigFileContent(kTraceConfig, "", "");
+
+ base::FilePath trace_config_file;
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ ASSERT_TRUE(
+ base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file));
+ ASSERT_NE(-1, base::WriteFile(
+ trace_config_file, content.c_str(), (int)content.length()));
+ base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
+ switches::kTraceConfigFile, trace_config_file);
+
+ ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled());
+ EXPECT_STREQ(
+ kTraceConfig,
+ TraceConfigFile::GetInstance()->GetTraceConfig().ToString().c_str());
+ EXPECT_EQ(0, TraceConfigFile::GetInstance()->GetStartupDuration());
+ EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("chrometrace.log")),
+ TraceConfigFile::GetInstance()->GetResultFile());
+}
+
+TEST(TraceConfigFileTest, ContentWithNegtiveDuration) {
+ base::ShadowingAtExitManager sem;
+ std::string content = GetTraceConfigFileContent(kTraceConfig, "-1", "");
+
+ base::FilePath trace_config_file;
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ ASSERT_TRUE(
+ base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file));
+ ASSERT_NE(-1, base::WriteFile(
+ trace_config_file, content.c_str(), (int)content.length()));
+ base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
+ switches::kTraceConfigFile, trace_config_file);
+
+ ASSERT_TRUE(TraceConfigFile::GetInstance()->IsEnabled());
+ EXPECT_STREQ(
+ kTraceConfig,
+ TraceConfigFile::GetInstance()->GetTraceConfig().ToString().c_str());
+ EXPECT_EQ(0, TraceConfigFile::GetInstance()->GetStartupDuration());
+ EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("chrometrace.log")),
+ TraceConfigFile::GetInstance()->GetResultFile());
+}
+
+TEST(TraceConfigFileTest, ContentWithoutTraceConfig) {
+ base::ShadowingAtExitManager sem;
+ std::string content = GetTraceConfigFileContent(
+ "", "10", "trace_result_file.log");
+
+ base::FilePath trace_config_file;
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ ASSERT_TRUE(
+ base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file));
+ ASSERT_NE(-1, base::WriteFile(
+ trace_config_file, content.c_str(), (int)content.length()));
+ base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
+ switches::kTraceConfigFile, trace_config_file);
+
+ EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
+}
+
+TEST(TraceConfigFileTest, InvalidContent) {
+ base::ShadowingAtExitManager sem;
+ std::string content = "invalid trace config file content";
+
+ base::FilePath trace_config_file;
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ ASSERT_TRUE(
+ base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file));
+ ASSERT_NE(-1, base::WriteFile(
+ trace_config_file, content.c_str(), (int)content.length()));
+ base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
+ switches::kTraceConfigFile, trace_config_file);
+
+ EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
+}
+
+TEST(TraceConfigFileTest, EmptyContent) {
+ base::ShadowingAtExitManager sem;
+ base::FilePath trace_config_file;
+ base::ScopedTempDir temp_dir;
+ ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
+ ASSERT_TRUE(
+ base::CreateTemporaryFileInDir(temp_dir.path(), &trace_config_file));
+ base::CommandLine::ForCurrentProcess()->AppendSwitchPath(
+ switches::kTraceConfigFile, trace_config_file);
+
+ EXPECT_FALSE(TraceConfigFile::GetInstance()->IsEnabled());
+}
+
+} // namespace tracing
« no previous file with comments | « components/tracing/trace_config_file.cc ('k') | components/tracing/tracing_switches.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698