Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: chrome/browser/permissions/permission_uma_util_unittest.cc

Issue 2047253002: Add hooks to permission layer for permission action reporting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@permission-reporter-implementation
Patch Set: resolve nits Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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.
4
5 #include "chrome/browser/permissions/permission_uma_util.h"
6
7 #include "base/command_line.h"
8 #include "chrome/browser/signin/fake_signin_manager_builder.h"
9 #include "chrome/browser/signin/signin_manager_factory.h"
10 #include "chrome/browser/sync/profile_sync_service_factory.h"
11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/pref_names.h"
13 #include "chrome/test/base/testing_profile.h"
14 #include "components/browser_sync/browser/profile_sync_service.h"
15 #include "components/browser_sync/common/browser_sync_switches.h"
16 #include "components/prefs/pref_service.h"
17 #include "components/signin/core/browser/fake_signin_manager.h"
18 #include "components/sync_driver/glue/sync_backend_host_mock.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "sync/internal_api/public/base/model_type.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22
23 namespace {
24 const char* kTestingGaiaId = "gaia_id";
25 const char* kTestingUsername = "fake_username";
26 } // namespace
27
28 class PermissionUmaUtilTest : public testing::Test {
29 protected:
30 PermissionUmaUtilTest() : profile_(new TestingProfile()) {}
raymes 2016/06/22 02:11:13 It would be better to separate each check (in the
stefanocs 2016/06/22 04:28:11 Done.
31
32 static bool IsOptedInPermissionActionReporting(Profile* profile) {
33 return PermissionUmaUtil::IsOptedInPermissionActionReporting(profile);
34 }
35
36 bool IsOptedInPermissionActionReporting() {
37 return IsOptedInPermissionActionReporting(profile_.get());
raymes 2016/06/22 02:11:13 nit: I would remove this and just always use the o
stefanocs 2016/06/22 04:28:11 Done.
38 }
39
40 void ResetCommandLine() {
41 base::CommandLine::Reset();
42 base::CommandLine::Init(0, NULL);
43 }
raymes 2016/06/22 02:11:13 if you have a SetUp function, you could call this
stefanocs 2016/06/22 04:28:11 Done.
44
45 void FakeSignIn() {
46 SigninManagerBase* signin_manager = static_cast<FakeSigninManager*>(
47 SigninManagerFactory::GetForProfile(profile()));
48 signin_manager->SetAuthenticatedAccountInfo(kTestingGaiaId,
49 kTestingUsername);
50 AttachFakeSyncBackend();
51 }
52
53 void SetSafeBrowsing(bool value) {
raymes 2016/06/22 02:11:13 nit: value->enabled
stefanocs 2016/06/22 04:28:11 Done.
54 PrefService* preferences = profile_->GetPrefs();
55 preferences->SetBoolean(prefs::kSafeBrowsingEnabled, value);
56 }
57
58 void ResetAndSetPermissionActionReporting(bool value) {
59 ResetCommandLine();
60 if (value)
61 base::CommandLine::ForCurrentProcess()->AppendSwitch(
62 switches::kEnablePermissionActionReporting);
63 else
64 base::CommandLine::ForCurrentProcess()->AppendSwitch(
65 switches::kDisablePermissionActionReporting);
raymes 2016/06/22 02:11:13 nit: I would just call these directly from where t
stefanocs 2016/06/22 04:28:11 Done.
66 }
67
68 void SyncAllDataTypes() {
69 profile_sync_service()->OnUserChoseDatatypes(true,
70 syncer::UserSelectableTypes());
71 }
72
73 void SyncAllDataTypesExcept(syncer::ModelType data_type) {
74 syncer::ModelTypeSet selectable_data_types = syncer::UserSelectableTypes();
75 selectable_data_types.Remove(data_type);
76 profile_sync_service()->OnUserChoseDatatypes(false, selectable_data_types);
raymes 2016/06/22 02:11:13 We should talk to pavely to ensure this is the rig
stefanocs 2016/06/22 04:28:11 Acknowledged.
77 }
78
79 Profile* incognito_profile() { return profile_->GetOffTheRecordProfile(); }
raymes 2016/06/22 02:11:13 nit: I would just inline this into the callsites
stefanocs 2016/06/22 04:28:11 Done.
80
81 Profile* profile() { return profile_.get(); }
82
83 private:
84 void AttachFakeSyncBackend() {
85 profile_sync_service()->backend_.reset(
86 new browser_sync::SyncBackendHostMock());
87 profile_sync_service()->backend_initialized_ = true;
raymes 2016/06/22 02:11:13 nit: just inline this since it's only called from
stefanocs 2016/06/22 04:28:11 Done.
88 }
89
90 ProfileSyncService* profile_sync_service() {
raymes 2016/06/22 02:11:13 This should probably be GetProfileSyncService. We
stefanocs 2016/06/22 04:28:11 Done.
91 return ProfileSyncServiceFactory::GetForProfile(profile());
92 }
93
94 content::TestBrowserThreadBundle thread_bundle_;
95 std::unique_ptr<Profile> profile_;
96 };
97
98 TEST_F(PermissionUmaUtilTest, IsOptedInPermissionActionReporting) {
99 ResetAndSetPermissionActionReporting(true);
100 SetSafeBrowsing(true);
101
102 // Test can start sync before sign in check.
103 EXPECT_FALSE(IsOptedInPermissionActionReporting());
104
105 FakeSignIn();
106
107 EXPECT_TRUE(IsOptedInPermissionActionReporting());
108
109 // Test incognito check.
110 EXPECT_FALSE(IsOptedInPermissionActionReporting(incognito_profile()));
111
112 {
113 // Test permission action reporting flag check.
114 ResetCommandLine();
115 EXPECT_FALSE(IsOptedInPermissionActionReporting());
116
117 ResetAndSetPermissionActionReporting(false);
118 EXPECT_FALSE(IsOptedInPermissionActionReporting());
119
120 ResetAndSetPermissionActionReporting(true);
121 EXPECT_TRUE(IsOptedInPermissionActionReporting());
122 }
123
124 {
125 // Test safe browsing enabled check.
126 SetSafeBrowsing(false);
127 EXPECT_FALSE(IsOptedInPermissionActionReporting());
128
129 SetSafeBrowsing(true);
130 EXPECT_TRUE(IsOptedInPermissionActionReporting());
131 }
132
133 {
134 // Test can get profile sync service check.
135 base::CommandLine::ForCurrentProcess()->AppendSwitch(
136 switches::kDisableSync);
137 EXPECT_FALSE(IsOptedInPermissionActionReporting());
138
139 ResetAndSetPermissionActionReporting(true);
140 EXPECT_TRUE(IsOptedInPermissionActionReporting());
141 }
142
143 {
144 // Test Chrome Tab Sync enabled check.
145 SyncAllDataTypesExcept(syncer::PROXY_TABS);
146 EXPECT_FALSE(IsOptedInPermissionActionReporting());
147
148 SyncAllDataTypes();
149 EXPECT_TRUE(IsOptedInPermissionActionReporting());
150 }
151
152 {
153 // Test Chrome Pref Sync enabled check.
154 SyncAllDataTypesExcept(syncer::PREFERENCES);
155 EXPECT_FALSE(IsOptedInPermissionActionReporting());
156
157 SyncAllDataTypes();
158 EXPECT_TRUE(IsOptedInPermissionActionReporting());
raymes 2016/06/22 02:11:13 nit: this is duplicated from above
stefanocs 2016/06/22 04:28:11 Done.
159 }
160 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698