1 // Copyright 2017 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 "chrome/browser/tracing/background_tracing_field_trial.h"
6
7 #include "base/files/file_util.h"
8 #include "base/metrics/field_trial.h"
9 #include "base/metrics/field_trial_params.h"
10 #include "base/test/scoped_command_line.h"
11 #include "chrome/test/base/testing_browser_process.h"
12 #include "chrome/test/base/testing_profile_manager.h"
13 #include "components/tracing/common/tracing_switches.h"
14 #include "content/public/browser/background_tracing_config.h"
15 #include "content/public/browser/background_tracing_manager.h"
16 #include "content/public/test/browser_task_environment.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 class BackgroundTracingTest : public testing::Test {
20 public:
21 BackgroundTracingTest() = default;
22
TearDown()23 void TearDown() override {
24 content::BackgroundTracingManager::GetInstance()->AbortScenarioForTesting();
25 }
26
27 private:
28 content::BrowserTaskEnvironment task_environment_;
29 };
30
31 namespace {
32
33 const char kTestConfig[] = "test";
34 bool g_test_config_loaded = false;
35
36 const char kUploadUrl[] = "http://localhost:8080";
37 const char kInvalidTracingConfig[] = "{][}";
38 const char kValidTracingConfig[] = R"(
39 {
40 "scenario_name": "BrowserProcess",
41 "configs": [
42 {
43 "category": "BENCHMARK_NAVIGATION",
44 "rule": "MONITOR_AND_DUMP_WHEN_SPECIFIC_HISTOGRAM_AND_VALUE",
45 "histogram_name": "Omnibox.CharTypedToRepaintLatency.ToPaint",
46 "histogram_lower_value": 1
47 }
48 ],
49 "mode": "REACTIVE_TRACING_MODE"
50 }
51 )";
52
CheckConfig(const std::string & config)53 std::string CheckConfig(const std::string& config) {
54 if (config == kTestConfig)
55 g_test_config_loaded = true;
56 return config;
57 }
58
59 } // namespace
60
TEST_F(BackgroundTracingTest,SetupBackgroundTracingFieldTrial)61 TEST_F(BackgroundTracingTest, SetupBackgroundTracingFieldTrial) {
62 const std::string kTrialName = "BackgroundTracing";
63 const std::string kExperimentName = "SlowStart";
64 base::AssociateFieldTrialParams(kTrialName, kExperimentName,
65 {{"config", kTestConfig}});
66 base::FieldTrialList::CreateFieldTrial(kTrialName, kExperimentName);
67
68 TestingProfileManager testing_profile_manager(
69 TestingBrowserProcess::GetGlobal());
70 ASSERT_TRUE(testing_profile_manager.SetUp());
71
72 // In case it is already set at previous test run.
73 g_test_config_loaded = false;
74
75 content::BackgroundTracingManager::GetInstance()
76 ->SetConfigTextFilterForTesting(base::BindRepeating(&CheckConfig));
77
78 tracing::SetupBackgroundTracingFieldTrial();
79 EXPECT_TRUE(g_test_config_loaded);
80 }
81
TEST_F(BackgroundTracingTest,SetupBackgroundTracingFromConfigFileFailed)82 TEST_F(BackgroundTracingTest, SetupBackgroundTracingFromConfigFileFailed) {
83 TestingProfileManager testing_profile_manager(
84 TestingBrowserProcess::GetGlobal());
85 ASSERT_TRUE(testing_profile_manager.SetUp());
86 ASSERT_FALSE(
87 content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
88
89 base::test::ScopedCommandLine scoped_command_line;
90 base::CommandLine* command_line = scoped_command_line.GetProcessCommandLine();
91 command_line->AppendSwitchASCII(switches::kTraceUploadURL, kUploadUrl);
92 command_line->AppendSwitchASCII(switches::kEnableBackgroundTracing, "");
93
94 tracing::SetupBackgroundTracingFieldTrial();
95 EXPECT_FALSE(
96 content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
97 }
98
TEST_F(BackgroundTracingTest,SetupBackgroundTracingFromConfigFileInvalidConfig)99 TEST_F(BackgroundTracingTest,
100 SetupBackgroundTracingFromConfigFileInvalidConfig) {
101 TestingProfileManager testing_profile_manager(
102 TestingBrowserProcess::GetGlobal());
103 ASSERT_TRUE(testing_profile_manager.SetUp());
104 ASSERT_FALSE(
105 content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
106
107 base::ScopedTempDir temp_dir;
108 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
109 base::FilePath file_path = temp_dir.GetPath().AppendASCII("config.json");
110 base::WriteFile(file_path, kInvalidTracingConfig,
111 sizeof(kInvalidTracingConfig) - 1);
112
113 base::test::ScopedCommandLine scoped_command_line;
114 base::CommandLine* command_line = scoped_command_line.GetProcessCommandLine();
115 command_line->AppendSwitchASCII(switches::kTraceUploadURL, kUploadUrl);
116 command_line->AppendSwitchPath(switches::kEnableBackgroundTracing, file_path);
117
118 tracing::SetupBackgroundTracingFieldTrial();
119 EXPECT_FALSE(
120 content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
121 }
122
TEST_F(BackgroundTracingTest,SetupBackgroundTracingFromConfigFile)123 TEST_F(BackgroundTracingTest, SetupBackgroundTracingFromConfigFile) {
124 TestingProfileManager testing_profile_manager(
125 TestingBrowserProcess::GetGlobal());
126 ASSERT_TRUE(testing_profile_manager.SetUp());
127
128 base::ScopedTempDir temp_dir;
129 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
130 base::FilePath file_path = temp_dir.GetPath().AppendASCII("config.json");
131 base::WriteFile(file_path, kValidTracingConfig,
132 sizeof(kValidTracingConfig) - 1);
133
134 base::test::ScopedCommandLine scoped_command_line;
135 base::CommandLine* command_line = scoped_command_line.GetProcessCommandLine();
136 command_line->AppendSwitchASCII(switches::kTraceUploadURL, kUploadUrl);
137 command_line->AppendSwitchPath(switches::kEnableBackgroundTracing, file_path);
138
139 tracing::SetupBackgroundTracingFieldTrial();
140 EXPECT_TRUE(
141 content::BackgroundTracingManager::GetInstance()->HasActiveScenario());
142 }
143