Chromium Code Reviews| OLD | NEW |
|---|---|
| (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()) {} | |
| 31 | |
| 32 static bool IsOptedIntoPermissionActionReporting(Profile* profile) { | |
| 33 return PermissionUmaUtil::IsOptedIntoPermissionActionReporting(profile); | |
| 34 } | |
| 35 | |
| 36 void SetUp() override { | |
| 37 profile_.reset(new TestingProfile()); | |
| 38 ResetCommandLine(); | |
| 39 } | |
| 40 | |
| 41 void FakeSignIn() { | |
| 42 SigninManagerBase* signin_manager = static_cast<FakeSigninManager*>( | |
| 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(); | |
| 54 base::CommandLine::Init(0, NULL); | |
| 55 } | |
| 56 | |
| 57 void SetSafeBrowsing(bool enabled) { | |
| 58 PrefService* preferences = profile_->GetPrefs(); | |
| 59 preferences->SetBoolean(prefs::kSafeBrowsingEnabled, enabled); | |
| 60 } | |
| 61 | |
| 62 ProfileSyncService* GetProfileSyncService() { | |
| 63 return ProfileSyncServiceFactory::GetForProfile(profile()); | |
| 64 } | |
| 65 | |
| 66 Profile* profile() { return profile_.get(); } | |
| 67 | |
| 68 private: | |
| 69 content::TestBrowserThreadBundle thread_bundle_; | |
| 70 std::unique_ptr<Profile> profile_; | |
| 71 }; | |
| 72 | |
| 73 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns | |
| 74 // true if Safe Browsing is enabled, Permission Action Reporting flag is | |
| 75 // enabled, not in incognito mode and signed in with default sync preferences. | |
| 76 TEST_F(PermissionUmaUtilTest, IsOptedIntoPermissionActionReportingSignInCheck) { | |
| 77 SetSafeBrowsing(true); | |
| 78 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 79 switches::kEnablePermissionActionReporting); | |
| 80 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 81 | |
| 82 FakeSignIn(); | |
| 83 EXPECT_FALSE(IsOptedIntoPermissionActionReporting( | |
| 84 profile()->GetOffTheRecordProfile())); | |
| 85 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 86 } | |
| 87 | |
| 88 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns | |
| 89 // false if Permission Action Reporting is not enabled. | |
| 90 TEST_F(PermissionUmaUtilTest, IsOptedIntoPermissionActionReportingFlagCheck) { | |
| 91 SetSafeBrowsing(true); | |
| 92 FakeSignIn(); | |
| 93 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 94 switches::kEnablePermissionActionReporting); | |
| 95 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 96 | |
| 97 ResetCommandLine(); | |
| 98 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 99 | |
| 100 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 101 switches::kDisablePermissionActionReporting); | |
| 102 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 103 } | |
| 104 | |
| 105 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns | |
| 106 // false if Safe Browsing is disabled. | |
| 107 TEST_F(PermissionUmaUtilTest, | |
| 108 IsOptedIntoPermissionActionReportingSafeBrowsingCheck) { | |
| 109 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 110 switches::kEnablePermissionActionReporting); | |
| 111 FakeSignIn(); | |
| 112 SetSafeBrowsing(true); | |
| 113 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 114 | |
| 115 SetSafeBrowsing(false); | |
| 116 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 117 } | |
| 118 | |
| 119 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns | |
| 120 // false if Safe Browsing is disabled. | |
|
kcarattini
2016/07/07 01:54:50
I think you mean Sync here.
stefanocs
2016/07/07 04:02:26
Done.
| |
| 121 TEST_F(PermissionUmaUtilTest, | |
| 122 IsOptedIntoPermissionActionReportingProfileSyncServiceCheck) { | |
| 123 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 124 switches::kEnablePermissionActionReporting); | |
| 125 SetSafeBrowsing(true); | |
| 126 FakeSignIn(); | |
| 127 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 128 | |
| 129 base::CommandLine::ForCurrentProcess()->AppendSwitch(switches::kDisableSync); | |
| 130 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 131 } | |
| 132 | |
| 133 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns | |
| 134 // false if Tab Sync and Pref Sync are not both enabled. | |
| 135 TEST_F(PermissionUmaUtilTest, | |
| 136 IsOptedIntoPermissionActionReportingSyncPreferenceCheck) { | |
| 137 base::CommandLine::ForCurrentProcess()->AppendSwitch( | |
| 138 switches::kEnablePermissionActionReporting); | |
| 139 SetSafeBrowsing(true); | |
| 140 FakeSignIn(); | |
| 141 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 142 | |
| 143 GetProfileSyncService()->OnUserChoseDatatypes( | |
| 144 false, syncer::ModelTypeSet(syncer::PROXY_TABS)); | |
| 145 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 146 | |
| 147 GetProfileSyncService()->OnUserChoseDatatypes( | |
| 148 false, syncer::ModelTypeSet(syncer::PREFERENCES)); | |
| 149 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 150 | |
| 151 GetProfileSyncService()->OnUserChoseDatatypes( | |
| 152 false, syncer::ModelTypeSet(syncer::PROXY_TABS, syncer::PREFERENCES)); | |
| 153 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 154 } | |
| OLD | NEW |