Add BluetoothDevice::VendorIDSource()
[chromium-blink-merge.git] / chrome / browser / metrics / metrics_log_unittest.cc
blob443db57313dc86d623b2ea66d8d72e5c93473cd5
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
3 // found in the LICENSE file.
5 #include "chrome/browser/metrics/metrics_log.h"
7 #include <string>
9 #include "base/base64.h"
10 #include "base/basictypes.h"
11 #include "base/command_line.h"
12 #include "base/port.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/prefs/scoped_user_pref_update.h"
15 #include "base/prefs/testing_pref_service.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/strings/utf_string_conversions.h"
20 #include "base/threading/sequenced_worker_pool.h"
21 #include "base/time/time.h"
22 #include "base/tracked_objects.h"
23 #include "chrome/browser/google/google_util.h"
24 #include "chrome/browser/prefs/browser_prefs.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/metrics/proto/profiler_event.pb.h"
27 #include "chrome/common/metrics/proto/system_profile.pb.h"
28 #include "chrome/common/metrics/variations/variations_util.h"
29 #include "chrome/common/pref_names.h"
30 #include "chrome/installer/util/google_update_settings.h"
31 #include "components/variations/metrics_util.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/common/process_type.h"
34 #include "content/public/common/webplugininfo.h"
35 #include "content/public/test/test_browser_thread_bundle.h"
36 #include "content/public/test/test_utils.h"
37 #include "testing/gtest/include/gtest/gtest.h"
38 #include "ui/gfx/size.h"
39 #include "url/gurl.h"
41 #if defined(OS_CHROMEOS)
42 #include "chrome/browser/chromeos/login/fake_user_manager.h"
43 #include "chrome/browser/chromeos/login/user_manager.h"
44 #include "chrome/browser/metrics/metrics_log_chromeos.h"
45 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
46 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
47 #include "chromeos/dbus/fake_bluetooth_device_client.h"
48 #include "chromeos/dbus/fake_bluetooth_input_client.h"
49 #include "chromeos/dbus/fake_dbus_thread_manager.h"
51 using chromeos::DBusThreadManager;
52 using chromeos::BluetoothAdapterClient;
53 using chromeos::BluetoothAgentManagerClient;
54 using chromeos::BluetoothDeviceClient;
55 using chromeos::BluetoothInputClient;
56 using chromeos::FakeBluetoothAdapterClient;
57 using chromeos::FakeBluetoothAgentManagerClient;
58 using chromeos::FakeBluetoothDeviceClient;
59 using chromeos::FakeBluetoothInputClient;
60 using chromeos::FakeDBusThreadManager;
61 #endif // OS_CHROMEOS
63 using base::TimeDelta;
64 using metrics::ProfilerEventProto;
65 using tracked_objects::ProcessDataSnapshot;
66 using tracked_objects::TaskSnapshot;
68 namespace {
70 const char kClientId[] = "bogus client ID";
71 const int64 kInstallDate = 1373051956;
72 const int64 kInstallDateExpected = 1373050800; // Computed from kInstallDate.
73 const int64 kEnabledDate = 1373001211;
74 const int64 kEnabledDateExpected = 1373000400; // Computed from kEnabledDate.
75 const int kSessionId = 127;
76 const int kScreenWidth = 1024;
77 const int kScreenHeight = 768;
78 const int kScreenCount = 3;
79 const float kScreenScaleFactor = 2;
80 const char kBrandForTesting[] = "brand_for_testing";
81 const chrome_variations::ActiveGroupId kFieldTrialIds[] = {
82 {37, 43},
83 {13, 47},
84 {23, 17}
86 const chrome_variations::ActiveGroupId kSyntheticTrials[] = {
87 {55, 15},
88 {66, 16}
91 #if defined(ENABLE_PLUGINS)
92 content::WebPluginInfo CreateFakePluginInfo(
93 const std::string& name,
94 const base::FilePath::CharType* path,
95 const std::string& version,
96 bool is_pepper) {
97 content::WebPluginInfo plugin(base::UTF8ToUTF16(name),
98 base::FilePath(path),
99 base::UTF8ToUTF16(version),
100 base::string16());
101 if (is_pepper)
102 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_PEPPER_IN_PROCESS;
103 else
104 plugin.type = content::WebPluginInfo::PLUGIN_TYPE_NPAPI;
105 return plugin;
107 #endif // defined(ENABLE_PLUGINS)
109 #if defined(OS_CHROMEOS)
110 class TestMetricsLogChromeOS : public MetricsLogChromeOS {
111 public:
112 explicit TestMetricsLogChromeOS(
113 metrics::ChromeUserMetricsExtension* uma_proto)
114 : MetricsLogChromeOS(uma_proto) {
117 #endif // OS_CHROMEOS
119 class TestMetricsLog : public MetricsLog {
120 public:
121 TestMetricsLog(const std::string& client_id, int session_id)
122 : MetricsLog(client_id, session_id),
123 prefs_(&scoped_prefs_),
124 brand_for_testing_(kBrandForTesting) {
125 #if defined(OS_CHROMEOS)
126 metrics_log_chromeos_.reset(new TestMetricsLogChromeOS(
127 MetricsLog::uma_proto()));
128 #endif // OS_CHROMEOS
129 chrome::RegisterLocalState(scoped_prefs_.registry());
130 InitPrefs();
132 // Creates a TestMetricsLog that will use |prefs| as the fake local state.
133 // Useful for tests that need to re-use the local state prefs between logs.
134 TestMetricsLog(const std::string& client_id,
135 int session_id,
136 TestingPrefServiceSimple* prefs)
137 : MetricsLog(client_id, session_id),
138 prefs_(prefs),
139 brand_for_testing_(kBrandForTesting) {
140 #if defined(OS_CHROMEOS)
141 metrics_log_chromeos_.reset(new TestMetricsLogChromeOS(
142 MetricsLog::uma_proto()));
143 #endif // OS_CHROMEOS
144 InitPrefs();
146 virtual ~TestMetricsLog() {}
148 virtual PrefService* GetPrefService() OVERRIDE {
149 return prefs_;
152 const metrics::ChromeUserMetricsExtension& uma_proto() const {
153 return *MetricsLog::uma_proto();
156 const metrics::SystemProfileProto& system_profile() const {
157 return uma_proto().system_profile();
160 private:
161 void InitPrefs() {
162 prefs_->SetInt64(prefs::kInstallDate, kInstallDate);
163 prefs_->SetString(prefs::kMetricsClientIDTimestamp,
164 base::Int64ToString(kEnabledDate));
165 #if defined(OS_CHROMEOS)
166 prefs_->SetInteger(prefs::kStabilityChildProcessCrashCount, 10);
167 prefs_->SetInteger(prefs::kStabilityOtherUserCrashCount, 11);
168 prefs_->SetInteger(prefs::kStabilityKernelCrashCount, 12);
169 prefs_->SetInteger(prefs::kStabilitySystemUncleanShutdownCount, 13);
170 #endif // OS_CHROMEOS
173 virtual void GetFieldTrialIds(
174 std::vector<chrome_variations::ActiveGroupId>* field_trial_ids) const
175 OVERRIDE {
176 ASSERT_TRUE(field_trial_ids->empty());
178 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) {
179 field_trial_ids->push_back(kFieldTrialIds[i]);
183 virtual gfx::Size GetScreenSize() const OVERRIDE {
184 return gfx::Size(kScreenWidth, kScreenHeight);
187 virtual float GetScreenDeviceScaleFactor() const OVERRIDE {
188 return kScreenScaleFactor;
191 virtual int GetScreenCount() const OVERRIDE {
192 return kScreenCount;
195 // Scoped PrefsService, which may not be used if |prefs_ != &scoped_prefs|.
196 TestingPrefServiceSimple scoped_prefs_;
197 // Weak pointer to the PrefsService used by this log.
198 TestingPrefServiceSimple* prefs_;
200 google_util::BrandForTesting brand_for_testing_;
202 DISALLOW_COPY_AND_ASSIGN(TestMetricsLog);
205 } // namespace
207 class MetricsLogTest : public testing::Test {
208 public:
209 MetricsLogTest() {}
211 protected:
212 virtual void SetUp() OVERRIDE {
213 #if defined(OS_CHROMEOS)
214 // Enable multi-profiles.
215 CommandLine::ForCurrentProcess()->AppendSwitch(switches::kMultiProfiles);
217 // Set up the fake Bluetooth environment,
218 scoped_ptr<FakeDBusThreadManager> fake_dbus_thread_manager(
219 new FakeDBusThreadManager);
220 fake_dbus_thread_manager->SetBluetoothAdapterClient(
221 scoped_ptr<BluetoothAdapterClient>(new FakeBluetoothAdapterClient));
222 fake_dbus_thread_manager->SetBluetoothDeviceClient(
223 scoped_ptr<BluetoothDeviceClient>(new FakeBluetoothDeviceClient));
224 fake_dbus_thread_manager->SetBluetoothInputClient(
225 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
226 fake_dbus_thread_manager->SetBluetoothAgentManagerClient(
227 scoped_ptr<BluetoothAgentManagerClient>(
228 new FakeBluetoothAgentManagerClient));
229 DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager.release());
231 // Grab pointers to members of the thread manager for easier testing.
232 fake_bluetooth_adapter_client_ = static_cast<FakeBluetoothAdapterClient*>(
233 DBusThreadManager::Get()->GetBluetoothAdapterClient());
234 fake_bluetooth_device_client_ = static_cast<FakeBluetoothDeviceClient*>(
235 DBusThreadManager::Get()->GetBluetoothDeviceClient());
236 #endif // OS_CHROMEOS
239 virtual void TearDown() OVERRIDE {
240 #if defined(OS_CHROMEOS)
241 DBusThreadManager::Shutdown();
242 #endif // OS_CHROMEOS
245 // Check that the values in |system_values| correspond to the test data
246 // defined at the top of this file.
247 void CheckSystemProfile(const metrics::SystemProfileProto& system_profile) {
248 EXPECT_EQ(kInstallDateExpected, system_profile.install_date());
249 EXPECT_EQ(kEnabledDateExpected, system_profile.uma_enabled_date());
251 ASSERT_EQ(arraysize(kFieldTrialIds) + arraysize(kSyntheticTrials),
252 static_cast<size_t>(system_profile.field_trial_size()));
253 for (size_t i = 0; i < arraysize(kFieldTrialIds); ++i) {
254 const metrics::SystemProfileProto::FieldTrial& field_trial =
255 system_profile.field_trial(i);
256 EXPECT_EQ(kFieldTrialIds[i].name, field_trial.name_id());
257 EXPECT_EQ(kFieldTrialIds[i].group, field_trial.group_id());
259 // Verify the right data is present for the synthetic trials.
260 for (size_t i = 0; i < arraysize(kSyntheticTrials); ++i) {
261 const metrics::SystemProfileProto::FieldTrial& field_trial =
262 system_profile.field_trial(i + arraysize(kFieldTrialIds));
263 EXPECT_EQ(kSyntheticTrials[i].name, field_trial.name_id());
264 EXPECT_EQ(kSyntheticTrials[i].group, field_trial.group_id());
267 EXPECT_EQ(kBrandForTesting, system_profile.brand_code());
269 const metrics::SystemProfileProto::Hardware& hardware =
270 system_profile.hardware();
271 EXPECT_EQ(kScreenWidth, hardware.primary_screen_width());
272 EXPECT_EQ(kScreenHeight, hardware.primary_screen_height());
273 EXPECT_EQ(kScreenScaleFactor, hardware.primary_screen_scale_factor());
274 EXPECT_EQ(kScreenCount, hardware.screen_count());
276 EXPECT_TRUE(hardware.has_cpu());
277 EXPECT_TRUE(hardware.cpu().has_vendor_name());
278 EXPECT_TRUE(hardware.cpu().has_signature());
280 // TODO(isherman): Verify other data written into the protobuf as a result
281 // of this call.
284 protected:
285 #if defined(OS_CHROMEOS)
286 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
287 FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
288 #endif // OS_CHROMEOS
290 private:
291 content::TestBrowserThreadBundle thread_bundle_;
293 DISALLOW_COPY_AND_ASSIGN(MetricsLogTest);
296 TEST_F(MetricsLogTest, RecordEnvironment) {
297 TestMetricsLog log(kClientId, kSessionId);
299 std::vector<content::WebPluginInfo> plugins;
300 GoogleUpdateMetrics google_update_metrics;
301 std::vector<chrome_variations::ActiveGroupId> synthetic_trials;
302 // Add two synthetic trials.
303 synthetic_trials.push_back(kSyntheticTrials[0]);
304 synthetic_trials.push_back(kSyntheticTrials[1]);
306 log.RecordEnvironment(plugins, google_update_metrics, synthetic_trials);
307 // Check that the system profile on the log has the correct values set.
308 CheckSystemProfile(log.system_profile());
310 // Check that the system profile has also been written to prefs.
311 PrefService* local_state = log.GetPrefService();
312 const std::string base64_system_profile =
313 local_state->GetString(prefs::kStabilitySavedSystemProfile);
314 EXPECT_FALSE(base64_system_profile.empty());
315 std::string serialied_system_profile;
316 EXPECT_TRUE(base::Base64Decode(base64_system_profile,
317 &serialied_system_profile));
318 SystemProfileProto decoded_system_profile;
319 EXPECT_TRUE(decoded_system_profile.ParseFromString(serialied_system_profile));
320 CheckSystemProfile(decoded_system_profile);
323 TEST_F(MetricsLogTest, LoadSavedEnvironmentFromPrefs) {
324 const char* kSystemProfilePref = prefs::kStabilitySavedSystemProfile;
325 const char* kSystemProfileHashPref = prefs::kStabilitySavedSystemProfileHash;
327 TestingPrefServiceSimple prefs;
328 chrome::RegisterLocalState(prefs.registry());
330 // The pref value is empty, so loading it from prefs should fail.
332 TestMetricsLog log(kClientId, kSessionId, &prefs);
333 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs());
336 // Do a RecordEnvironment() call and check whether the pref is recorded.
338 TestMetricsLog log(kClientId, kSessionId, &prefs);
339 log.RecordEnvironment(std::vector<content::WebPluginInfo>(),
340 GoogleUpdateMetrics(),
341 std::vector<chrome_variations::ActiveGroupId>());
342 EXPECT_FALSE(prefs.GetString(kSystemProfilePref).empty());
343 EXPECT_FALSE(prefs.GetString(kSystemProfileHashPref).empty());
347 TestMetricsLog log(kClientId, kSessionId, &prefs);
348 EXPECT_TRUE(log.LoadSavedEnvironmentFromPrefs());
349 // Check some values in the system profile.
350 EXPECT_EQ(kInstallDateExpected, log.system_profile().install_date());
351 EXPECT_EQ(kEnabledDateExpected, log.system_profile().uma_enabled_date());
352 // Ensure that the call cleared the prefs.
353 EXPECT_TRUE(prefs.GetString(kSystemProfilePref).empty());
354 EXPECT_TRUE(prefs.GetString(kSystemProfileHashPref).empty());
357 // Ensure that a non-matching hash results in the pref being invalid.
359 TestMetricsLog log(kClientId, kSessionId, &prefs);
360 // Call RecordEnvironment() to record the pref again.
361 log.RecordEnvironment(std::vector<content::WebPluginInfo>(),
362 GoogleUpdateMetrics(),
363 std::vector<chrome_variations::ActiveGroupId>());
367 // Set the hash to a bad value.
368 prefs.SetString(kSystemProfileHashPref, "deadbeef");
369 TestMetricsLog log(kClientId, kSessionId, &prefs);
370 EXPECT_FALSE(log.LoadSavedEnvironmentFromPrefs());
371 // Ensure that the prefs are cleared, even if the call failed.
372 EXPECT_TRUE(prefs.GetString(kSystemProfilePref).empty());
373 EXPECT_TRUE(prefs.GetString(kSystemProfileHashPref).empty());
377 TEST_F(MetricsLogTest, InitialLogStabilityMetrics) {
378 TestMetricsLog log(kClientId, kSessionId);
379 log.RecordEnvironment(std::vector<content::WebPluginInfo>(),
380 GoogleUpdateMetrics(),
381 std::vector<chrome_variations::ActiveGroupId>());
382 log.RecordStabilityMetrics(base::TimeDelta(), MetricsLog::INITIAL_LOG);
383 const metrics::SystemProfileProto_Stability& stability =
384 log.system_profile().stability();
385 // Required metrics:
386 EXPECT_TRUE(stability.has_launch_count());
387 EXPECT_TRUE(stability.has_crash_count());
388 // Initial log metrics:
389 EXPECT_TRUE(stability.has_incomplete_shutdown_count());
390 EXPECT_TRUE(stability.has_breakpad_registration_success_count());
391 EXPECT_TRUE(stability.has_breakpad_registration_failure_count());
392 EXPECT_TRUE(stability.has_debugger_present_count());
393 EXPECT_TRUE(stability.has_debugger_not_present_count());
396 TEST_F(MetricsLogTest, OngoingLogStabilityMetrics) {
397 TestMetricsLog log(kClientId, kSessionId);
398 log.RecordEnvironment(std::vector<content::WebPluginInfo>(),
399 GoogleUpdateMetrics(),
400 std::vector<chrome_variations::ActiveGroupId>());
401 log.RecordStabilityMetrics(base::TimeDelta(), MetricsLog::ONGOING_LOG);
402 const metrics::SystemProfileProto_Stability& stability =
403 log.system_profile().stability();
404 // Required metrics:
405 EXPECT_TRUE(stability.has_launch_count());
406 EXPECT_TRUE(stability.has_crash_count());
407 // Initial log metrics:
408 EXPECT_FALSE(stability.has_incomplete_shutdown_count());
409 EXPECT_FALSE(stability.has_breakpad_registration_success_count());
410 EXPECT_FALSE(stability.has_breakpad_registration_failure_count());
411 EXPECT_FALSE(stability.has_debugger_present_count());
412 EXPECT_FALSE(stability.has_debugger_not_present_count());
415 #if defined(ENABLE_PLUGINS)
416 TEST_F(MetricsLogTest, Plugins) {
417 TestMetricsLog log(kClientId, kSessionId);
419 std::vector<content::WebPluginInfo> plugins;
420 plugins.push_back(CreateFakePluginInfo("p1", FILE_PATH_LITERAL("p1.plugin"),
421 "1.5", true));
422 plugins.push_back(CreateFakePluginInfo("p2", FILE_PATH_LITERAL("p2.plugin"),
423 "2.0", false));
424 log.RecordEnvironment(plugins, GoogleUpdateMetrics(),
425 std::vector<chrome_variations::ActiveGroupId>());
427 const metrics::SystemProfileProto& system_profile = log.system_profile();
428 ASSERT_EQ(2, system_profile.plugin_size());
429 EXPECT_EQ("p1", system_profile.plugin(0).name());
430 EXPECT_EQ("p1.plugin", system_profile.plugin(0).filename());
431 EXPECT_EQ("1.5", system_profile.plugin(0).version());
432 EXPECT_TRUE(system_profile.plugin(0).is_pepper());
433 EXPECT_EQ("p2", system_profile.plugin(1).name());
434 EXPECT_EQ("p2.plugin", system_profile.plugin(1).filename());
435 EXPECT_EQ("2.0", system_profile.plugin(1).version());
436 EXPECT_FALSE(system_profile.plugin(1).is_pepper());
438 // Now set some plugin stability stats for p2 and verify they're recorded.
439 scoped_ptr<base::DictionaryValue> plugin_dict(new base::DictionaryValue);
440 plugin_dict->SetString(prefs::kStabilityPluginName, "p2");
441 plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, 1);
442 plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, 2);
443 plugin_dict->SetInteger(prefs::kStabilityPluginInstances, 3);
444 plugin_dict->SetInteger(prefs::kStabilityPluginLoadingErrors, 4);
446 ListPrefUpdate update(log.GetPrefService(), prefs::kStabilityPluginStats);
447 update.Get()->Append(plugin_dict.release());
450 log.RecordStabilityMetrics(base::TimeDelta(), MetricsLog::ONGOING_LOG);
451 const metrics::SystemProfileProto_Stability& stability =
452 log.system_profile().stability();
453 ASSERT_EQ(1, stability.plugin_stability_size());
454 EXPECT_EQ("p2", stability.plugin_stability(0).plugin().name());
455 EXPECT_EQ("p2.plugin", stability.plugin_stability(0).plugin().filename());
456 EXPECT_EQ("2.0", stability.plugin_stability(0).plugin().version());
457 EXPECT_FALSE(stability.plugin_stability(0).plugin().is_pepper());
458 EXPECT_EQ(1, stability.plugin_stability(0).launch_count());
459 EXPECT_EQ(2, stability.plugin_stability(0).crash_count());
460 EXPECT_EQ(3, stability.plugin_stability(0).instance_count());
461 EXPECT_EQ(4, stability.plugin_stability(0).loading_error_count());
463 #endif // defined(ENABLE_PLUGINS)
465 // Test that we properly write profiler data to the log.
466 TEST_F(MetricsLogTest, RecordProfilerData) {
467 TestMetricsLog log(kClientId, kSessionId);
468 EXPECT_EQ(0, log.uma_proto().profiler_event_size());
471 ProcessDataSnapshot process_data;
472 process_data.process_id = 177;
473 process_data.tasks.push_back(TaskSnapshot());
474 process_data.tasks.back().birth.location.file_name = "file";
475 process_data.tasks.back().birth.location.function_name = "function";
476 process_data.tasks.back().birth.location.line_number = 1337;
477 process_data.tasks.back().birth.thread_name = "birth_thread";
478 process_data.tasks.back().death_data.count = 37;
479 process_data.tasks.back().death_data.run_duration_sum = 31;
480 process_data.tasks.back().death_data.run_duration_max = 17;
481 process_data.tasks.back().death_data.run_duration_sample = 13;
482 process_data.tasks.back().death_data.queue_duration_sum = 8;
483 process_data.tasks.back().death_data.queue_duration_max = 5;
484 process_data.tasks.back().death_data.queue_duration_sample = 3;
485 process_data.tasks.back().death_thread_name = "Still_Alive";
486 process_data.tasks.push_back(TaskSnapshot());
487 process_data.tasks.back().birth.location.file_name = "file2";
488 process_data.tasks.back().birth.location.function_name = "function2";
489 process_data.tasks.back().birth.location.line_number = 1773;
490 process_data.tasks.back().birth.thread_name = "birth_thread2";
491 process_data.tasks.back().death_data.count = 19;
492 process_data.tasks.back().death_data.run_duration_sum = 23;
493 process_data.tasks.back().death_data.run_duration_max = 11;
494 process_data.tasks.back().death_data.run_duration_sample = 7;
495 process_data.tasks.back().death_data.queue_duration_sum = 0;
496 process_data.tasks.back().death_data.queue_duration_max = 0;
497 process_data.tasks.back().death_data.queue_duration_sample = 0;
498 process_data.tasks.back().death_thread_name = "death_thread";
500 log.RecordProfilerData(process_data, content::PROCESS_TYPE_BROWSER);
501 ASSERT_EQ(1, log.uma_proto().profiler_event_size());
502 EXPECT_EQ(ProfilerEventProto::STARTUP_PROFILE,
503 log.uma_proto().profiler_event(0).profile_type());
504 EXPECT_EQ(ProfilerEventProto::WALL_CLOCK_TIME,
505 log.uma_proto().profiler_event(0).time_source());
507 ASSERT_EQ(2, log.uma_proto().profiler_event(0).tracked_object_size());
509 const ProfilerEventProto::TrackedObject* tracked_object =
510 &log.uma_proto().profiler_event(0).tracked_object(0);
511 EXPECT_EQ(GG_UINT64_C(10123486280357988687),
512 tracked_object->source_file_name_hash());
513 EXPECT_EQ(GG_UINT64_C(13962325592283560029),
514 tracked_object->source_function_name_hash());
515 EXPECT_EQ(1337, tracked_object->source_line_number());
516 EXPECT_EQ(GG_UINT64_C(3400908935414830400),
517 tracked_object->birth_thread_name_hash());
518 EXPECT_EQ(37, tracked_object->exec_count());
519 EXPECT_EQ(31, tracked_object->exec_time_total());
520 EXPECT_EQ(13, tracked_object->exec_time_sampled());
521 EXPECT_EQ(8, tracked_object->queue_time_total());
522 EXPECT_EQ(3, tracked_object->queue_time_sampled());
523 EXPECT_EQ(GG_UINT64_C(10151977472163283085),
524 tracked_object->exec_thread_name_hash());
525 EXPECT_EQ(177U, tracked_object->process_id());
526 EXPECT_EQ(ProfilerEventProto::TrackedObject::BROWSER,
527 tracked_object->process_type());
529 tracked_object = &log.uma_proto().profiler_event(0).tracked_object(1);
530 EXPECT_EQ(GG_UINT64_C(2025659946535236365),
531 tracked_object->source_file_name_hash());
532 EXPECT_EQ(GG_UINT64_C(55232426147951219),
533 tracked_object->source_function_name_hash());
534 EXPECT_EQ(1773, tracked_object->source_line_number());
535 EXPECT_EQ(GG_UINT64_C(1518842999910132863),
536 tracked_object->birth_thread_name_hash());
537 EXPECT_EQ(19, tracked_object->exec_count());
538 EXPECT_EQ(23, tracked_object->exec_time_total());
539 EXPECT_EQ(7, tracked_object->exec_time_sampled());
540 EXPECT_EQ(0, tracked_object->queue_time_total());
541 EXPECT_EQ(0, tracked_object->queue_time_sampled());
542 EXPECT_EQ(GG_UINT64_C(14275151213201158253),
543 tracked_object->exec_thread_name_hash());
544 EXPECT_EQ(177U, tracked_object->process_id());
545 EXPECT_EQ(ProfilerEventProto::TrackedObject::BROWSER,
546 tracked_object->process_type());
550 ProcessDataSnapshot process_data;
551 process_data.process_id = 1177;
552 process_data.tasks.push_back(TaskSnapshot());
553 process_data.tasks.back().birth.location.file_name = "file3";
554 process_data.tasks.back().birth.location.function_name = "function3";
555 process_data.tasks.back().birth.location.line_number = 7331;
556 process_data.tasks.back().birth.thread_name = "birth_thread3";
557 process_data.tasks.back().death_data.count = 137;
558 process_data.tasks.back().death_data.run_duration_sum = 131;
559 process_data.tasks.back().death_data.run_duration_max = 117;
560 process_data.tasks.back().death_data.run_duration_sample = 113;
561 process_data.tasks.back().death_data.queue_duration_sum = 108;
562 process_data.tasks.back().death_data.queue_duration_max = 105;
563 process_data.tasks.back().death_data.queue_duration_sample = 103;
564 process_data.tasks.back().death_thread_name = "death_thread3";
566 log.RecordProfilerData(process_data, content::PROCESS_TYPE_RENDERER);
567 ASSERT_EQ(1, log.uma_proto().profiler_event_size());
568 EXPECT_EQ(ProfilerEventProto::STARTUP_PROFILE,
569 log.uma_proto().profiler_event(0).profile_type());
570 EXPECT_EQ(ProfilerEventProto::WALL_CLOCK_TIME,
571 log.uma_proto().profiler_event(0).time_source());
572 ASSERT_EQ(3, log.uma_proto().profiler_event(0).tracked_object_size());
574 const ProfilerEventProto::TrackedObject* tracked_object =
575 &log.uma_proto().profiler_event(0).tracked_object(2);
576 EXPECT_EQ(GG_UINT64_C(2686523203278102732),
577 tracked_object->source_file_name_hash());
578 EXPECT_EQ(GG_UINT64_C(5081672290546182009),
579 tracked_object->source_function_name_hash());
580 EXPECT_EQ(7331, tracked_object->source_line_number());
581 EXPECT_EQ(GG_UINT64_C(1518842999910132863),
582 tracked_object->birth_thread_name_hash());
583 EXPECT_EQ(137, tracked_object->exec_count());
584 EXPECT_EQ(131, tracked_object->exec_time_total());
585 EXPECT_EQ(113, tracked_object->exec_time_sampled());
586 EXPECT_EQ(108, tracked_object->queue_time_total());
587 EXPECT_EQ(103, tracked_object->queue_time_sampled());
588 EXPECT_EQ(GG_UINT64_C(2203893603452504755),
589 tracked_object->exec_thread_name_hash());
590 EXPECT_EQ(1177U, tracked_object->process_id());
591 EXPECT_EQ(ProfilerEventProto::TrackedObject::RENDERER,
592 tracked_object->process_type());
596 #if defined(OS_CHROMEOS)
597 TEST_F(MetricsLogTest, MultiProfileUserCount) {
598 std::string user1("user1@example.com");
599 std::string user2("user2@example.com");
600 std::string user3("user3@example.com");
602 // |scoped_enabler| takes over the lifetime of |user_manager|.
603 chromeos::FakeUserManager* user_manager = new chromeos::FakeUserManager();
604 chromeos::ScopedUserManagerEnabler scoped_enabler(user_manager);
605 user_manager->AddKioskAppUser(user1);
606 user_manager->AddKioskAppUser(user2);
607 user_manager->AddKioskAppUser(user3);
609 user_manager->LoginUser(user1);
610 user_manager->LoginUser(user3);
612 TestMetricsLog log(kClientId, kSessionId);
613 std::vector<content::WebPluginInfo> plugins;
614 GoogleUpdateMetrics google_update_metrics;
615 std::vector<chrome_variations::ActiveGroupId> synthetic_trials;
616 log.RecordEnvironment(plugins, google_update_metrics, synthetic_trials);
617 EXPECT_EQ(2u, log.system_profile().multi_profile_user_count());
620 TEST_F(MetricsLogTest, MultiProfileCountInvalidated) {
621 std::string user1("user1@example.com");
622 std::string user2("user2@example.com");
623 std::string user3("user3@example.com");
625 // |scoped_enabler| takes over the lifetime of |user_manager|.
626 chromeos::FakeUserManager* user_manager = new chromeos::FakeUserManager();
627 chromeos::ScopedUserManagerEnabler scoped_enabler(user_manager);
628 user_manager->AddKioskAppUser(user1);
629 user_manager->AddKioskAppUser(user2);
630 user_manager->AddKioskAppUser(user3);
632 user_manager->LoginUser(user1);
634 TestMetricsLog log(kClientId, kSessionId);
635 EXPECT_EQ(1u, log.system_profile().multi_profile_user_count());
637 user_manager->LoginUser(user2);
638 std::vector<chrome_variations::ActiveGroupId> synthetic_trials;
639 log.RecordEnvironment(std::vector<content::WebPluginInfo>(),
640 GoogleUpdateMetrics(), synthetic_trials);
641 EXPECT_EQ(0u, log.system_profile().multi_profile_user_count());
644 TEST_F(MetricsLogTest, BluetoothHardwareDisabled) {
645 TestMetricsLog log(kClientId, kSessionId);
646 log.RecordEnvironment(std::vector<content::WebPluginInfo>(),
647 GoogleUpdateMetrics(),
648 std::vector<chrome_variations::ActiveGroupId>());
650 EXPECT_TRUE(log.system_profile().has_hardware());
651 EXPECT_TRUE(log.system_profile().hardware().has_bluetooth());
653 EXPECT_TRUE(log.system_profile().hardware().bluetooth().is_present());
654 EXPECT_FALSE(log.system_profile().hardware().bluetooth().is_enabled());
657 TEST_F(MetricsLogTest, BluetoothHardwareEnabled) {
658 FakeBluetoothAdapterClient::Properties* properties =
659 fake_bluetooth_adapter_client_->GetProperties(
660 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
661 properties->powered.ReplaceValue(true);
663 TestMetricsLog log(kClientId, kSessionId);
664 log.RecordEnvironment(std::vector<content::WebPluginInfo>(),
665 GoogleUpdateMetrics(),
666 std::vector<chrome_variations::ActiveGroupId>());
668 EXPECT_TRUE(log.system_profile().has_hardware());
669 EXPECT_TRUE(log.system_profile().hardware().has_bluetooth());
671 EXPECT_TRUE(log.system_profile().hardware().bluetooth().is_present());
672 EXPECT_TRUE(log.system_profile().hardware().bluetooth().is_enabled());
675 TEST_F(MetricsLogTest, BluetoothPairedDevices) {
676 // The fake bluetooth adapter class already claims to be paired with one
677 // device when initialized. Add a second and third fake device to it so we
678 // can test the cases where a device is not paired (LE device, generally)
679 // and a device that does not have Device ID information.
680 fake_bluetooth_device_client_->CreateDevice(
681 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
682 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
684 fake_bluetooth_device_client_->CreateDevice(
685 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
686 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
688 FakeBluetoothDeviceClient::Properties* properties =
689 fake_bluetooth_device_client_->GetProperties(
690 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
691 properties->paired.ReplaceValue(true);
693 TestMetricsLog log(kClientId, kSessionId);
694 log.RecordEnvironment(std::vector<content::WebPluginInfo>(),
695 GoogleUpdateMetrics(),
696 std::vector<chrome_variations::ActiveGroupId>());
698 ASSERT_TRUE(log.system_profile().has_hardware());
699 ASSERT_TRUE(log.system_profile().hardware().has_bluetooth());
701 // Only two of the devices should appear.
702 EXPECT_EQ(2,
703 log.system_profile().hardware().bluetooth().paired_device_size());
705 typedef metrics::SystemProfileProto::Hardware::Bluetooth::PairedDevice
706 PairedDevice;
708 // First device should match the Paired Device object, complete with
709 // parsed Device ID information.
710 PairedDevice device1 =
711 log.system_profile().hardware().bluetooth().paired_device(0);
713 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceClass,
714 device1.bluetooth_class());
715 EXPECT_EQ(PairedDevice::DEVICE_COMPUTER, device1.type());
716 EXPECT_EQ(0x001122U, device1.vendor_prefix());
717 EXPECT_EQ(PairedDevice::VENDOR_ID_USB, device1.vendor_id_source());
718 EXPECT_EQ(0x05ACU, device1.vendor_id());
719 EXPECT_EQ(0x030DU, device1.product_id());
720 EXPECT_EQ(0x0306U, device1.device_id());
722 // Second device should match the Confirm Passkey object, this has
723 // no Device ID information.
724 PairedDevice device2 =
725 log.system_profile().hardware().bluetooth().paired_device(1);
727 EXPECT_EQ(FakeBluetoothDeviceClient::kConfirmPasskeyClass,
728 device2.bluetooth_class());
729 EXPECT_EQ(PairedDevice::DEVICE_PHONE, device2.type());
730 EXPECT_EQ(0x207D74U, device2.vendor_prefix());
731 EXPECT_EQ(PairedDevice::VENDOR_ID_UNKNOWN, device2.vendor_id_source());
733 #endif // OS_CHROMEOS