1 // Copyright 2015 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_services_manager.h"
9 #include "base/metrics/field_trial.h"
10 #include "base/prefs/testing_pref_service.h"
11 #include "chrome/common/pref_names.h"
12 #include "chrome/test/base/testing_browser_process.h"
13 #include "chrome/test/base/testing_profile_manager.h"
14 #include "components/rappor/rappor_pref_names.h"
15 #include "components/rappor/rappor_prefs.h"
16 #include "testing/gtest/include/gtest/gtest.h"
20 #if defined(GOOGLE_CHROME_BUILD)
22 void UseRapporOption() {
23 ASSERT_TRUE(base::FieldTrialList::CreateFieldTrial(
24 "RapporOption", "Enabled"));
27 #endif // defined(GOOGLE_CHROME_BUILD)
31 class MetricsServicesManagerTest
: public testing::Test
{
33 MetricsServicesManagerTest()
34 : test_profile_manager_(TestingBrowserProcess::GetGlobal()),
35 manager_(&test_prefs_
),
36 field_trial_list_(NULL
) {
37 rappor::internal::RegisterPrefs(test_prefs_
.registry());
40 void SetUp() override
{
41 ASSERT_TRUE(test_profile_manager_
.SetUp());
44 void CreateProfile(const std::string
& name
, bool sb_enabled
) {
45 TestingProfile
* profile
= test_profile_manager_
.CreateTestingProfile(name
);
46 profile
->GetPrefs()->SetBoolean(prefs::kSafeBrowsingEnabled
, sb_enabled
);
49 TestingPrefServiceSimple
* test_prefs() { return &test_prefs_
; }
51 MetricsServicesManager
* manager() { return &manager_
; }
54 TestingProfileManager test_profile_manager_
;
55 TestingPrefServiceSimple test_prefs_
;
56 MetricsServicesManager manager_
;
57 base::FieldTrialList field_trial_list_
;
59 DISALLOW_COPY_AND_ASSIGN(MetricsServicesManagerTest
);
62 TEST_F(MetricsServicesManagerTest
, CheckRapporDefaultDisable
) {
63 test_prefs()->ClearPref(rappor::prefs::kRapporEnabled
);
64 CreateProfile("profile1", true);
65 CreateProfile("profile2", false);
66 bool uma_enabled
= false;
68 EXPECT_FALSE(manager()->IsRapporEnabled(uma_enabled
));
69 EXPECT_TRUE(test_prefs()->HasPrefPath(rappor::prefs::kRapporEnabled
));
70 EXPECT_FALSE(test_prefs()->GetBoolean(rappor::prefs::kRapporEnabled
));
73 TEST_F(MetricsServicesManagerTest
, CheckRapporDefaultEnabledBySafeBrowsing
) {
74 test_prefs()->ClearPref(rappor::prefs::kRapporEnabled
);
75 CreateProfile("profile1", true);
76 CreateProfile("profile2", true);
77 bool uma_enabled
= false;
79 EXPECT_TRUE(manager()->IsRapporEnabled(uma_enabled
));
80 EXPECT_TRUE(test_prefs()->HasPrefPath(rappor::prefs::kRapporEnabled
));
81 EXPECT_TRUE(test_prefs()->GetBoolean(rappor::prefs::kRapporEnabled
));
84 TEST_F(MetricsServicesManagerTest
, CheckRapporDefaultEnabledByUMA
) {
85 test_prefs()->ClearPref(rappor::prefs::kRapporEnabled
);
86 CreateProfile("profile1", false);
87 CreateProfile("profile2", false);
88 bool uma_enabled
= true;
90 EXPECT_TRUE(manager()->IsRapporEnabled(uma_enabled
));
91 EXPECT_TRUE(test_prefs()->HasPrefPath(rappor::prefs::kRapporEnabled
));
92 EXPECT_TRUE(test_prefs()->GetBoolean(rappor::prefs::kRapporEnabled
));
95 TEST_F(MetricsServicesManagerTest
, CheckRapporEnable
) {
96 test_prefs()->SetBoolean(rappor::prefs::kRapporEnabled
, true);
97 CreateProfile("profile1", false);
98 CreateProfile("profile2", false);
99 bool uma_enabled
= false;
101 EXPECT_TRUE(manager()->IsRapporEnabled(uma_enabled
));
102 EXPECT_TRUE(test_prefs()->HasPrefPath(rappor::prefs::kRapporEnabled
));
103 EXPECT_TRUE(test_prefs()->GetBoolean(rappor::prefs::kRapporEnabled
));
106 TEST_F(MetricsServicesManagerTest
, CheckRapporDisable
) {
107 test_prefs()->SetBoolean(rappor::prefs::kRapporEnabled
, false);
108 CreateProfile("profile1", true);
109 CreateProfile("profile2", true);
110 bool uma_enabled
= true;
112 EXPECT_FALSE(manager()->IsRapporEnabled(uma_enabled
));
113 EXPECT_TRUE(test_prefs()->HasPrefPath(rappor::prefs::kRapporEnabled
));
114 EXPECT_FALSE(test_prefs()->GetBoolean(rappor::prefs::kRapporEnabled
));
117 #if defined(GOOGLE_CHROME_BUILD)
119 TEST_F(MetricsServicesManagerTest
, GetRecordingLevelDisabled
) {
121 test_prefs()->SetBoolean(rappor::prefs::kRapporEnabled
, false);
122 bool uma_enabled
= true;
124 EXPECT_EQ(rappor::RECORDING_DISABLED
,
125 manager()->GetRapporRecordingLevel(uma_enabled
));
128 TEST_F(MetricsServicesManagerTest
, GetRecordingLevelFine
) {
130 test_prefs()->SetBoolean(rappor::prefs::kRapporEnabled
, true);
131 bool uma_enabled
= true;
133 EXPECT_EQ(rappor::FINE_LEVEL
,
134 manager()->GetRapporRecordingLevel(uma_enabled
));
137 TEST_F(MetricsServicesManagerTest
, GetRecordingLevelCoarse
) {
139 test_prefs()->SetBoolean(rappor::prefs::kRapporEnabled
, true);
140 bool uma_enabled
= false;
142 EXPECT_EQ(rappor::COARSE_LEVEL
,
143 manager()->GetRapporRecordingLevel(uma_enabled
));
146 #endif // defined(GOOGLE_CHROME_BUILD)