| 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 "chrome/browser/metrics/metrics_service.h" |
| 6 |
| 5 #include <ctype.h> | 7 #include <ctype.h> |
| 6 #include <string> | 8 #include <string> |
| 7 | 9 |
| 8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 9 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 10 #include "chrome/browser/metrics/metrics_service.h" | |
| 11 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 12 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
| 13 #include "chrome/test/base/scoped_testing_local_state.h" | 14 #include "chrome/test/base/scoped_testing_local_state.h" |
| 14 #include "chrome/test/base/testing_browser_process.h" | 15 #include "chrome/test/base/testing_browser_process.h" |
| 15 #include "components/variations/metrics_util.h" | 16 #include "components/variations/metrics_util.h" |
| 16 #include "content/public/common/process_type.h" | 17 #include "content/public/common/process_type.h" |
| 18 #include "content/public/common/webplugininfo.h" |
| 17 #include "content/public/test/test_browser_thread.h" | 19 #include "content/public/test/test_browser_thread.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "ui/gfx/size.h" |
| 22 |
| 23 #if defined(OS_CHROMEOS) |
| 24 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
| 25 #include "chromeos/dbus/fake_bluetooth_device_client.h" |
| 26 #include "chromeos/dbus/fake_bluetooth_input_client.h" |
| 27 #include "chromeos/dbus/fake_dbus_thread_manager.h" |
| 28 #endif // OS_CHROMEOS |
| 19 | 29 |
| 20 namespace { | 30 namespace { |
| 21 | 31 |
| 32 class TestMetricsService : public MetricsService { |
| 33 public: |
| 34 TestMetricsService() {} |
| 35 virtual ~TestMetricsService() {} |
| 36 |
| 37 MetricsLogManager* log_manager() { |
| 38 return &log_manager_; |
| 39 } |
| 40 |
| 41 private: |
| 42 DISALLOW_COPY_AND_ASSIGN(TestMetricsService); |
| 43 }; |
| 44 |
| 45 class TestMetricsLog : public MetricsLog { |
| 46 public: |
| 47 TestMetricsLog(const std::string& client_id, int session_id) |
| 48 : MetricsLog(client_id, session_id) {} |
| 49 virtual ~TestMetricsLog() {} |
| 50 |
| 51 private: |
| 52 virtual gfx::Size GetScreenSize() const OVERRIDE { |
| 53 return gfx::Size(1024, 768); |
| 54 } |
| 55 |
| 56 virtual float GetScreenDeviceScaleFactor() const OVERRIDE { |
| 57 return 1.0f; |
| 58 } |
| 59 |
| 60 virtual int GetScreenCount() const OVERRIDE { |
| 61 return 1; |
| 62 } |
| 63 |
| 64 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog); |
| 65 }; |
| 66 |
| 22 class MetricsServiceTest : public testing::Test { | 67 class MetricsServiceTest : public testing::Test { |
| 23 public: | 68 public: |
| 24 MetricsServiceTest() | 69 MetricsServiceTest() |
| 25 : ui_thread_(content::BrowserThread::UI, &message_loop_), | 70 : ui_thread_(content::BrowserThread::UI, &message_loop_), |
| 26 testing_local_state_(TestingBrowserProcess::GetGlobal()) { | 71 testing_local_state_(TestingBrowserProcess::GetGlobal()) { |
| 72 #if defined(OS_CHROMEOS) |
| 73 chromeos::FakeDBusThreadManager* fake_dbus_thread_manager = |
| 74 new chromeos::FakeDBusThreadManager; |
| 75 fake_dbus_thread_manager->SetBluetoothAdapterClient( |
| 76 scoped_ptr<chromeos::BluetoothAdapterClient>( |
| 77 new chromeos::FakeBluetoothAdapterClient)); |
| 78 fake_dbus_thread_manager->SetBluetoothDeviceClient( |
| 79 scoped_ptr<chromeos::BluetoothDeviceClient>( |
| 80 new chromeos::FakeBluetoothDeviceClient)); |
| 81 fake_dbus_thread_manager->SetBluetoothInputClient( |
| 82 scoped_ptr<chromeos::BluetoothInputClient>( |
| 83 new chromeos::FakeBluetoothInputClient)); |
| 84 chromeos::DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager); |
| 85 #endif // OS_CHROMEOS |
| 86 } |
| 87 |
| 88 virtual ~MetricsServiceTest() { |
| 89 #if defined(OS_CHROMEOS) |
| 90 chromeos::DBusThreadManager::Shutdown(); |
| 91 #endif // OS_CHROMEOS |
| 92 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); |
| 27 } | 93 } |
| 28 | 94 |
| 29 PrefService* GetLocalState() { | 95 PrefService* GetLocalState() { |
| 30 return testing_local_state_.Get(); | 96 return testing_local_state_.Get(); |
| 31 } | 97 } |
| 32 | 98 |
| 33 // Returns true if there is a synthetic trial in the given vector that matches | 99 // 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. | 100 // the given trial name and trial group; returns false otherwise. |
| 35 bool HasSyntheticTrial( | 101 bool HasSyntheticTrial( |
| 36 const std::vector<chrome_variations::ActiveGroupId>& synthetic_trials, | 102 const std::vector<chrome_variations::ActiveGroupId>& synthetic_trials, |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 switches::kResetVariationState); | 198 switches::kResetVariationState); |
| 133 | 199 |
| 134 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); | 200 MetricsService::SetExecutionPhase(MetricsService::UNINITIALIZED_PHASE); |
| 135 MetricsService service; | 201 MetricsService service; |
| 136 service.GetLowEntropySource(); | 202 service.GetLowEntropySource(); |
| 137 | 203 |
| 138 EXPECT_TRUE(GetLocalState()->GetString(kCachePrefName).empty()); | 204 EXPECT_TRUE(GetLocalState()->GetString(kCachePrefName).empty()); |
| 139 } | 205 } |
| 140 } | 206 } |
| 141 | 207 |
| 208 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCleanShutDown) { |
| 209 base::FieldTrialList field_trial_list(NULL); |
| 210 base::FieldTrialList::CreateFieldTrial("UMAStability", "SeparateLog"); |
| 211 |
| 212 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, true); |
| 213 |
| 214 TestMetricsService service; |
| 215 service.InitializeMetricsRecordingState(MetricsService::REPORTING_ENABLED); |
| 216 // No initial stability log should be generated. |
| 217 EXPECT_FALSE(service.log_manager()->has_unsent_logs()); |
| 218 EXPECT_FALSE(service.log_manager()->has_staged_log()); |
| 219 } |
| 220 |
| 221 TEST_F(MetricsServiceTest, InitialStabilityLogAfterCrash) { |
| 222 base::FieldTrialList field_trial_list(NULL); |
| 223 base::FieldTrialList::CreateFieldTrial("UMAStability", "SeparateLog"); |
| 224 |
| 225 GetLocalState()->ClearPref(prefs::kStabilityExitedCleanly); |
| 226 |
| 227 // Set up prefs to simulate restarting after a crash. |
| 228 |
| 229 // Save an existing system profile to prefs, to correspond to what would be |
| 230 // saved from a previous session. |
| 231 TestMetricsLog log("client", 1); |
| 232 log.RecordEnvironment(std::vector<content::WebPluginInfo>(), |
| 233 GoogleUpdateMetrics(), |
| 234 std::vector<chrome_variations::ActiveGroupId>()); |
| 235 |
| 236 // Record stability build time and version from previous session, so that |
| 237 // stability metrics (including exited cleanly flag) won't be cleared. |
| 238 GetLocalState()->SetInt64(prefs::kStabilityStatsBuildTime, |
| 239 MetricsLog::GetBuildTime()); |
| 240 GetLocalState()->SetString(prefs::kStabilityStatsVersion, |
| 241 MetricsLog::GetVersionString()); |
| 242 |
| 243 GetLocalState()->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| 244 |
| 245 TestMetricsService service; |
| 246 service.InitializeMetricsRecordingState(MetricsService::REPORTING_ENABLED); |
| 247 |
| 248 // The initial stability log should be generated and persisted in unsent logs. |
| 249 MetricsLogManager* log_manager = service.log_manager(); |
| 250 EXPECT_TRUE(log_manager->has_unsent_logs()); |
| 251 EXPECT_FALSE(log_manager->has_staged_log()); |
| 252 |
| 253 // Stage the log and retrieve it. |
| 254 log_manager->StageNextLogForUpload(); |
| 255 EXPECT_TRUE(log_manager->has_staged_log()); |
| 256 |
| 257 metrics::ChromeUserMetricsExtension uma_log; |
| 258 EXPECT_TRUE(uma_log.ParseFromString(log_manager->staged_log_text())); |
| 259 |
| 260 EXPECT_TRUE(uma_log.has_client_id()); |
| 261 EXPECT_TRUE(uma_log.has_session_id()); |
| 262 EXPECT_TRUE(uma_log.has_system_profile()); |
| 263 EXPECT_EQ(0, uma_log.user_action_event_size()); |
| 264 EXPECT_EQ(0, uma_log.omnibox_event_size()); |
| 265 EXPECT_EQ(0, uma_log.histogram_event_size()); |
| 266 EXPECT_EQ(0, uma_log.profiler_event_size()); |
| 267 EXPECT_EQ(0, uma_log.perf_data_size()); |
| 268 |
| 269 EXPECT_EQ(1, uma_log.system_profile().stability().crash_count()); |
| 270 } |
| 271 |
| 142 // Crashes on at least Mac and Linux. http://crbug.com/320433 | 272 // Crashes on at least Mac and Linux. http://crbug.com/320433 |
| 143 TEST_F(MetricsServiceTest, DISABLED_RegisterSyntheticTrial) { | 273 TEST_F(MetricsServiceTest, DISABLED_RegisterSyntheticTrial) { |
| 144 MetricsService service; | 274 MetricsService service; |
| 145 | 275 |
| 146 // Add two synthetic trials and confirm that they show up in the list. | 276 // Add two synthetic trials and confirm that they show up in the list. |
| 147 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"), | 277 SyntheticTrialGroup trial1(metrics::HashName("TestTrial1"), |
| 148 metrics::HashName("Group1"), | 278 metrics::HashName("Group1"), |
| 149 base::TimeTicks::Now()); | 279 base::TimeTicks::Now()); |
| 150 service.RegisterSyntheticFieldTrial(trial1); | 280 service.RegisterSyntheticFieldTrial(trial1); |
| 151 | 281 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 service.log_manager_.FinishCurrentLog(); | 315 service.log_manager_.FinishCurrentLog(); |
| 186 service.log_manager_.BeginLoggingWithLog(new MetricsLog("clientID", 1), | 316 service.log_manager_.BeginLoggingWithLog(new MetricsLog("clientID", 1), |
| 187 MetricsLog::ONGOING_LOG); | 317 MetricsLog::ONGOING_LOG); |
| 188 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); | 318 service.GetCurrentSyntheticFieldTrials(&synthetic_trials); |
| 189 EXPECT_EQ(3U, synthetic_trials.size()); | 319 EXPECT_EQ(3U, synthetic_trials.size()); |
| 190 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); | 320 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial1", "Group2")); |
| 191 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); | 321 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial2", "Group2")); |
| 192 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); | 322 EXPECT_TRUE(HasSyntheticTrial(synthetic_trials, "TestTrial3", "Group3")); |
| 193 service.log_manager_.FinishCurrentLog(); | 323 service.log_manager_.FinishCurrentLog(); |
| 194 } | 324 } |
| OLD | NEW |