1 // Copyright 2014 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/chrome_metrics_service_accessor.h"
7 #include "chrome/common/pref_names.h"
8 #include "chrome/test/base/scoped_testing_local_state.h"
9 #include "chrome/test/base/testing_browser_process.h"
10 #include "content/public/test/test_browser_thread_bundle.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 #if defined(OS_CHROMEOS)
14 #include "chrome/browser/chromeos/settings/cros_settings.h"
15 #include "chrome/browser/chromeos/settings/device_settings_service.h"
16 #endif // defined(OS_CHROMEOS)
18 class ChromeMetricsServiceAccessorTest
: public testing::Test
{
20 ChromeMetricsServiceAccessorTest()
21 : testing_local_state_(TestingBrowserProcess::GetGlobal()) {
24 PrefService
* GetLocalState() {
25 return testing_local_state_
.Get();
29 content::TestBrowserThreadBundle thread_bundle_
;
30 ScopedTestingLocalState testing_local_state_
;
31 #if defined(OS_CHROMEOS)
32 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
33 chromeos::ScopedTestCrosSettings test_cros_settings_
;
34 #endif // defined(OS_CHROMEOS)
36 DISALLOW_COPY_AND_ASSIGN(ChromeMetricsServiceAccessorTest
);
39 TEST_F(ChromeMetricsServiceAccessorTest
, MetricsReportingEnabled
) {
40 #if defined(GOOGLE_CHROME_BUILD)
41 #if !defined(OS_CHROMEOS)
42 GetLocalState()->SetBoolean(prefs::kMetricsReportingEnabled
, false);
43 EXPECT_FALSE(ChromeMetricsServiceAccessor::IsMetricsReportingEnabled());
44 GetLocalState()->SetBoolean(prefs::kMetricsReportingEnabled
, true);
45 EXPECT_TRUE(ChromeMetricsServiceAccessor::IsMetricsReportingEnabled());
46 GetLocalState()->ClearPref(prefs::kMetricsReportingEnabled
);
47 EXPECT_FALSE(ChromeMetricsServiceAccessor::IsMetricsReportingEnabled());
49 // ChromeOS does not register prefs::kMetricsReportingEnabled and uses
50 // device settings for metrics reporting.
51 EXPECT_FALSE(ChromeMetricsServiceAccessor::IsMetricsReportingEnabled());
54 // Metrics Reporting is never enabled when GOOGLE_CHROME_BUILD is undefined.
55 EXPECT_FALSE(ChromeMetricsServiceAccessor::IsMetricsReportingEnabled());
59 TEST_F(ChromeMetricsServiceAccessorTest
, CrashReportingEnabled
) {
60 #if defined(GOOGLE_CHROME_BUILD)
61 // ChromeOS has different device settings for crash reporting.
62 #if !defined(OS_CHROMEOS)
63 #if defined(OS_ANDROID)
64 const char* crash_pref
= prefs::kCrashReportingEnabled
;
66 const char* crash_pref
= prefs::kMetricsReportingEnabled
;
68 GetLocalState()->SetBoolean(crash_pref
, false);
69 EXPECT_FALSE(ChromeMetricsServiceAccessor::IsCrashReportingEnabled());
70 GetLocalState()->SetBoolean(crash_pref
, true);
71 EXPECT_TRUE(ChromeMetricsServiceAccessor::IsCrashReportingEnabled());
72 GetLocalState()->ClearPref(crash_pref
);
73 EXPECT_FALSE(ChromeMetricsServiceAccessor::IsCrashReportingEnabled());
74 #endif // !defined(OS_CHROMEOS)
75 #else // defined(GOOGLE_CHROME_BUILD)
76 // Chromium branded browsers never have crash reporting enabled.
77 EXPECT_FALSE(ChromeMetricsServiceAccessor::IsCrashReportingEnabled());
78 #endif // defined(GOOGLE_CHROME_BUILD)