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

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: Use PRIORITY_PREFERENCE instead of PREFERENCE sync Created 4 years, 5 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/sync_driver/glue/sync_backend_host_mock.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "sync/internal_api/public/base/model_type.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21
22 namespace {
23 const char* kTestingGaiaId = "gaia_id";
Lei Zhang 2016/07/14 09:34:59 This should be: const char kFoo[] = "foo"; Becau
grt (UTC plus 2) 2016/07/14 10:08:09 even better: constexpr char kFoo[] = "foo";
stefanocs 2016/07/14 13:29:24 Done. Sorry I missed this, copied this from https:
24 const char* kTestingUsername = "fake_username";
25 } // namespace
26
27 class PermissionUmaUtilTest : public testing::Test {
28 protected:
29 PermissionUmaUtilTest() : profile_(new TestingProfile()) {}
Lei Zhang 2016/07/14 09:34:58 I think you are creating |profile_| twice per test
stefanocs 2016/07/14 13:29:24 Done.
30
31 static bool IsOptedIntoPermissionActionReporting(Profile* profile) {
Lei Zhang 2016/07/14 09:34:58 Make this a member function, since you always call
stefanocs 2016/07/14 13:29:24 It is called for profile()->GetOffTheRecordProfile
32 return PermissionUmaUtil::IsOptedIntoPermissionActionReporting(profile);
33 }
34
35 void SetUp() override {
36 profile_.reset(new TestingProfile());
37 ResetCommandLine();
38 }
39
40 void FakeSignIn() {
41 SigninManagerBase* signin_manager =
42 static_cast<FakeSigninManagerForTesting*>(
Lei Zhang 2016/07/14 09:34:58 How did you get a FakeSigninManagerForTesting? Who
stefanocs 2016/07/14 13:29:24 It was just casted like in other unittest https://
Lei Zhang 2016/07/15 00:00:53 Yes, but the SigninTrackerTest first built one ~10
43 SigninManagerFactory::GetForProfile(profile()));
44 signin_manager->SetAuthenticatedAccountInfo(kTestingGaiaId,
45 kTestingUsername);
46 // Attach a sync backend to the profile sync service.
47 GetProfileSyncService()->backend_.reset(
48 new browser_sync::SyncBackendHostMock());
49 GetProfileSyncService()->backend_initialized_ = true;
50 }
51
52 void ResetCommandLine() {
53 base::CommandLine::Reset();
Lei Zhang 2016/07/14 09:14:42 Sorry, but we cannot do this in a unit test. There
Lei Zhang 2016/07/14 09:34:58 You probably want base::test::ScopedCommandLine in
stefanocs 2016/07/14 13:29:23 Acknowledged.
raymes 2016/07/15 02:20:40 :( Really sorry for the hassle. I didn't think abo
Lei Zhang 2016/07/15 02:33:52 I have a CL to add a comment to encourage the use
54 base::CommandLine::Init(0, NULL);
55 }
56
57 void SetKeepEverythingSynced(bool flag) {
58 GetProfileSyncService()->sync_prefs_.SetKeepEverythingSynced(flag);
59 }
60
61 void SetSafeBrowsing(bool enabled) {
62 PrefService* preferences = profile_->GetPrefs();
63 preferences->SetBoolean(prefs::kSafeBrowsingEnabled, enabled);
64 }
65
66 ProfileSyncService* GetProfileSyncService() {
67 return ProfileSyncServiceFactory::GetForProfile(profile());
68 }
69
70 Profile* profile() { return profile_.get(); }
71
72 private:
73 content::TestBrowserThreadBundle thread_bundle_;
74 std::unique_ptr<Profile> profile_;
75 };
76
77 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns
78 // true if Safe Browsing is enabled, Permission Action Reporting flag is
79 // enabled, not in incognito mode and signed in with default sync preferences.
80 TEST_F(PermissionUmaUtilTest, IsOptedIntoPermissionActionReportingSignInCheck) {
81 SetSafeBrowsing(true);
82 base::CommandLine::ForCurrentProcess()->AppendSwitch(
83 switches::kEnablePermissionActionReporting);
84 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile()));
85
86 FakeSignIn();
87 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(
88 profile()->GetOffTheRecordProfile()));
89 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile()));
90 }
91
92 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns
93 // false if Permission Action Reporting is not enabled.
94 TEST_F(PermissionUmaUtilTest, IsOptedIntoPermissionActionReportingFlagCheck) {
95 SetSafeBrowsing(true);
96 FakeSignIn();
97 base::CommandLine::ForCurrentProcess()->AppendSwitch(
98 switches::kEnablePermissionActionReporting);
99 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile()));
100
101 ResetCommandLine();
102 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile()));
103
104 base::CommandLine::ForCurrentProcess()->AppendSwitch(
105 switches::kDisablePermissionActionReporting);
106 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile()));
107 }
108
109 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns
110 // false if Safe Browsing is disabled.
111 TEST_F(PermissionUmaUtilTest,
112 IsOptedIntoPermissionActionReportingSafeBrowsingCheck) {
113 base::CommandLine::ForCurrentProcess()->AppendSwitch(
114 switches::kEnablePermissionActionReporting);
115 FakeSignIn();
116 SetSafeBrowsing(true);
117 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile()));
118
119 SetSafeBrowsing(false);
120 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile()));
121 }
122
123 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns
124 // false if Sync is disabled.
125 TEST_F(PermissionUmaUtilTest,
126 IsOptedIntoPermissionActionReportingProfileSyncServiceCheck) {
127 base::CommandLine::ForCurrentProcess()->AppendSwitch(
128 switches::kEnablePermissionActionReporting);
129 SetSafeBrowsing(true);
130 FakeSignIn();
131 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile()));
132
133 base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableSync);
134 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile()));
135 }
136
137 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns
138 // false if Tab Sync and Pref Sync are not both enabled.
139 TEST_F(PermissionUmaUtilTest,
140 IsOptedIntoPermissionActionReportingSyncPreferenceCheck) {
141 base::CommandLine::ForCurrentProcess()->AppendSwitch(
142 switches::kEnablePermissionActionReporting);
143 SetSafeBrowsing(true);
144 FakeSignIn();
145 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile()));
146
147 SetKeepEverythingSynced(false);
148 GetProfileSyncService()->ChangePreferredDataTypes(
149 syncer::ModelTypeSet(syncer::PROXY_TABS));
150 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile()));
151
152 SetKeepEverythingSynced(false);
153 GetProfileSyncService()->ChangePreferredDataTypes(
154 syncer::ModelTypeSet(syncer::PRIORITY_PREFERENCES));
155 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile()));
156
157 SetKeepEverythingSynced(true);
158 GetProfileSyncService()->ChangePreferredDataTypes(
159 syncer::ModelTypeSet(syncer::PROXY_TABS, syncer::PREFERENCES));
160 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile()));
161 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698