| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <ctype.h> | 5 #include <ctype.h> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/metrics/metrics_service.h" | 10 #include "chrome/browser/metrics/metrics_service.h" |
| 11 #include "chrome/common/chrome_switches.h" | 11 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/scoped_testing_local_state.h" | 13 #include "chrome/test/base/scoped_testing_local_state.h" |
| 14 #include "chrome/test/base/testing_browser_process.h" | 14 #include "chrome/test/base/testing_browser_process.h" |
| 15 #include "components/variations/metrics_util.h" | 15 #include "components/variations/metrics_util.h" |
| 16 #include "content/public/common/process_type.h" | 16 #include "content/public/common/process_type.h" |
| 17 #include "content/public/common/webplugininfo.h" |
| 17 #include "content/public/test/test_browser_thread.h" | 18 #include "content/public/test/test_browser_thread.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 23 class TestMetricsService : public MetricsService { |
| 24 public: |
| 25 TestMetricsService() {} |
| 26 virtual ~TestMetricsService() {} |
| 27 |
| 28 MetricsLogManager* log_manager() { |
| 29 return &log_manager_; |
| 30 } |
| 31 |
| 32 private: |
| 33 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); |
| 34 }; |
| 35 |
| 22 class MetricsServiceTest : public testing::Test { | 36 class MetricsServiceTest : public testing::Test { |
| 23 public: | 37 public: |
| 24 MetricsServiceTest() | 38 MetricsServiceTest() |
| 25 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 39 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 26 testing_local_state_(TestingBrowserProcess::GetGlobal()) { | 40 testing_local_state_(TestingBrowserProcess::GetGlobal()) { |
| 27 } | 41 } |
| 28 | 42 |
| 43 virtual ~MetricsServiceTest() { |
| 44 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); |
| 45 } |
| 46 |
| 29 PrefService* GetLocalState() { | 47 PrefService* GetLocalState() { |
| 30 return testing_local_state_.Get(); | 48 return testing_local_state_.Get(); |
| 31 } | 49 } |
| 32 | 50 |
| 33 // Returns true if there is a synthetic trial in the given vector that matches | 51 // Returns true if there is a synthetic trial in the given vector that matches |
| 34 // the given trial name and trial group; returns false otherwise. | 52 // the given trial name and trial group; returns false otherwise. |
| 35 bool HasSyntheticTrial( | 53 bool HasSyntheticTrial( |
| 36 const std::vector<chrome_variations::ActiveGroupId>& synthetic_trials, | 54 const std::vector<chrome_variations::ActiveGroupId>& synthetic_trials, |
| 37 const std::string& trial_name, | 55 const std::string& trial_name, |
| 38 const std::string& trial_group) { | 56 const std::string& trial_group) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 switches::kResetVariationState); | 150 switches::kResetVariationState); |
| 133 | 151 |
| 134 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 152 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); |
| 135 MetricsService service; | 153 MetricsService service; |
| 136 service.GetLowEntropySource(); | 154 service.GetLowEntropySource(); |
| 137 | 155 |
| 138 EXPECT_TRUE(GetLocalState()->GetString(kCachePrefName).empty()); | 156 EXPECT_TRUE(GetLocalState()->GetString(kCachePrefName).empty()); |
| 139 } | 157 } |
| 140 } | 158 } |
| 141 | 159 |
| 160 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { |
| 161 base::FieldTrialList field_trial_list(NULL); |
| 162 base::FieldTrialList::CreateFieldTrial("UMAStability", "SeparateLog"); |
| 163 |
| 164 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); |
| 165 |
| 166 TestMetricsService service; |
| 167 service.InitializeMetricsRecordingState(MetricsService::REPORTING_ENABLED); |
| 168 // No initial stability log should be generated. |
| 169 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); |
| 170 EXPECT_FALSE(service.log_manager()->has_staged_log()); |
| 171 } |
| 172 |
| 173 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { |
| 174 base::FieldTrialList field_trial_list(NULL); |
| 175 base::FieldTrialList::CreateFieldTrial("UMAStability", "SeparateLog"); |
| 176 |
| 177 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly); |
| 178 |
| 179 // Set up prefs to simulate restarting after a crash. |
| 180 |
| 181 // Save an existing system profile to prefs, to correspond to what would be |
| 182 // saved from a previous session. |
| 183 MetricsLog log("client", 1); |
| 184 log.RecordEnvironment(std::vector<content::WebPluginInfo>(), |
| 185 GoogleUpdateMetrics(), |
| 186 std::vector<chrome_variations::ActiveGroupId>()); |
| 187 |
| 188 // Record stability build time and version from previous session, so that |
| 189 // stability metrics (including exited cleanly flag) won't be cleared. |
| 190 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, |
| 191 MetricsLog::GetBuildTime()); |
| 192 GetLocalState()->SetString(prefs::kStabilityStatsVersion, |
| 193 MetricsLog::GetVersionString()); |
| 194 |
| 195 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| 196 |
| 197 TestMetricsService service; |
| 198 service.InitializeMetricsRecordingState(MetricsService::REPORTING_ENABLED); |
| 199 |
| 200 // The initial stability log should be generated and persisted in unsent logs. |
| 201 MetricsLogManager* log_manager = service.log_manager(); |
| 202 EXPECT_TRUE(log_manager->has_unsent_logs()); |
| 203 EXPECT_FALSE(log_manager->has_staged_log()); |
| 204 |
| 205 // Stage the log and retrieve it. |
| 206 log_manager->StageNextLogForUpload(); |
| 207 EXPECT_TRUE(log_manager->has_staged_log()); |
| 208 |
| 209 metrics::ChromeUserMetricsExtension uma_log; |
| 210 EXPECT_TRUE(uma_log.ParseFromString(log_manager->staged_log_text())); |
| 211 |
| 212 EXPECT_TRUE(uma_log.has_client_id()); |
| 213 EXPECT_TRUE(uma_log.has_session_id()); |
| 214 EXPECT_TRUE(uma_log.has_system_profile()); |
| 215 EXPECT_EQ(0, uma_log.user_action_event_size()); |
| 216 EXPECT_EQ(0, uma_log.omnibox_event_size()); |
| 217 EXPECT_EQ(0, uma_log.histogram_event_size()); |
| 218 EXPECT_EQ(0, uma_log.profiler_event_size()); |
| 219 EXPECT_EQ(0, uma_log.perf_data_size()); |
| 220 |
| 221 EXPECT_EQ(1, uma_log.system_profile().stability().crash_count()); |
| 222 } |
| 223 |
| 142 // Crashes on at least Mac and Linux. http://crbug.com/320433 | 224 // Crashes on at least Mac and Linux. http://crbug.com/320433 |
| 143 TEST_F(MetricsServiceTest, DISABLED_RegisterSyntheticTrial) { | 225 TEST_F(MetricsServiceTest, DISABLED_RegisterSyntheticTrial) { |
| 144 MetricsService service; | 226 MetricsService service; |
| 145 | 227 |
| 146 // Add two synthetic trials and confirm that they show up in the list. | 228 // Add two synthetic trials and confirm that they show up in the list. |
| 147 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"), | 229 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"), |
| 148 metrics::HashName("Group1"), | 230 metrics::HashName("Group1"), |
| 149 base::TimeTicks::Now()); | 231 base::TimeTicks::Now()); |
| 150 service.RegisterSyntheticFieldTrial(trial1); | 232 service.RegisterSyntheticFieldTrial(trial1); |
| 151 | 233 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 service.log_manager_.FinishCurrentLog(); | 267 service.log_manager_.FinishCurrentLog(); |
| 186 service.log_manager_.BeginLoggingWithLog(new MetricsLog("clientID", 1), | 268 service.log_manager_.BeginLoggingWithLog(new MetricsLog("clientID", 1), |
| 187 MetricsLog::ONGOING_LOG); | 269 MetricsLog::ONGOING_LOG); |
| 188 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); | 270 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); |
| 189 EXPECT_EQ(3U, synthetic_trials.size()); | 271 EXPECT_EQ(3U, synthetic_trials.size()); |
| 190 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); | 272 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); |
| 191 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | 273 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); |
| 192 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); | 274 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); |
| 193 service.log_manager_.FinishCurrentLog(); | 275 service.log_manager_.FinishCurrentLog(); |
| 194 } | 276 } |
| OLD | NEW |