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/test/scoped_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 constexpr char kTestingGaiaId[] = "gaia_id"; | |
| 24 constexpr char kTestingUsername[] = "fake_username"; | |
| 25 } // namespace | |
| 26 | |
| 27 class PermissionUmaUtilTest : public testing::Test { | |
| 28 protected: | |
| 29 PermissionUmaUtilTest() {} | |
| 30 | |
| 31 static bool IsOptedIntoPermissionActionReporting(Profile* profile) { | |
| 32 return PermissionUmaUtil::IsOptedIntoPermissionActionReporting(profile); | |
| 33 } | |
| 34 | |
| 35 void SetUp() override { profile_.reset(new TestingProfile()); } | |
| 36 | |
| 37 void FakeSignIn() { | |
| 38 SigninManagerBase* signin_manager = | |
| 39 static_cast<FakeSigninManagerForTesting*>( | |
|
Lei Zhang
2016/07/15 00:26:07
As mentioned on the previous CL, this only works i
stefanocs
2016/07/15 01:05:02
Yes, you were right, I have added this to the prof
| |
| 40 SigninManagerFactory::GetForProfile(profile())); | |
| 41 signin_manager->SetAuthenticatedAccountInfo(kTestingGaiaId, | |
| 42 kTestingUsername); | |
| 43 // Attach a sync backend to the profile sync service. | |
| 44 GetProfileSyncService()->backend_.reset( | |
|
pavely
2016/07/14 19:29:28
Your code in permission_uma_util.cc relies on inte
stefanocs
2016/07/15 01:05:02
Acknowledged.
| |
| 45 new browser_sync::SyncBackendHostMock()); | |
| 46 GetProfileSyncService()->backend_initialized_ = true; | |
| 47 } | |
| 48 | |
| 49 void SetKeepEverythingSyncedFalse() { | |
| 50 GetProfileSyncService()->sync_prefs_.SetKeepEverythingSynced(false); | |
|
pavely
2016/07/14 19:29:28
You can create SyncPrefs object on the stack. No n
stefanocs
2016/07/15 01:05:02
Done.
| |
| 51 } | |
| 52 | |
| 53 void SetSafeBrowsing(bool enabled) { | |
| 54 PrefService* preferences = profile_->GetPrefs(); | |
| 55 preferences->SetBoolean(prefs::kSafeBrowsingEnabled, enabled); | |
| 56 } | |
| 57 | |
| 58 ProfileSyncService* GetProfileSyncService() { | |
| 59 return ProfileSyncServiceFactory::GetForProfile(profile()); | |
| 60 } | |
| 61 | |
| 62 Profile* profile() { return profile_.get(); } | |
| 63 | |
| 64 private: | |
| 65 content::TestBrowserThreadBundle thread_bundle_; | |
| 66 std::unique_ptr<Profile> profile_; | |
| 67 }; | |
| 68 | |
| 69 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns | |
| 70 // true if Safe Browsing is enabled, Permission Action Reporting flag is | |
| 71 // enabled, not in incognito mode and signed in with default sync preferences. | |
| 72 TEST_F(PermissionUmaUtilTest, IsOptedIntoPermissionActionReportingSignInCheck) { | |
| 73 base::test::ScopedCommandLine scoped_command_line; | |
| 74 SetSafeBrowsing(true); | |
| 75 scoped_command_line.GetProcessCommandLine()->AppendSwitch( | |
| 76 switches::kEnablePermissionActionReporting); | |
| 77 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 78 | |
| 79 FakeSignIn(); | |
| 80 EXPECT_FALSE(IsOptedIntoPermissionActionReporting( | |
| 81 profile()->GetOffTheRecordProfile())); | |
| 82 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 83 } | |
| 84 | |
| 85 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns | |
| 86 // false if Permission Action Reporting is not enabled. | |
| 87 TEST_F(PermissionUmaUtilTest, IsOptedIntoPermissionActionReportingFlagCheck) { | |
| 88 SetSafeBrowsing(true); | |
| 89 FakeSignIn(); | |
| 90 { | |
| 91 base::test::ScopedCommandLine scoped_command_line; | |
| 92 scoped_command_line.GetProcessCommandLine()->AppendSwitch( | |
| 93 switches::kEnablePermissionActionReporting); | |
| 94 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 95 } // Reset the command line. | |
| 96 | |
| 97 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 98 | |
| 99 base::test::ScopedCommandLine scoped_command_line; | |
| 100 scoped_command_line.GetProcessCommandLine()->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::test::ScopedCommandLine scoped_command_line; | |
| 110 scoped_command_line.GetProcessCommandLine()->AppendSwitch( | |
| 111 switches::kEnablePermissionActionReporting); | |
| 112 FakeSignIn(); | |
| 113 SetSafeBrowsing(true); | |
| 114 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 115 | |
| 116 SetSafeBrowsing(false); | |
| 117 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 118 } | |
| 119 | |
| 120 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns | |
| 121 // false if Sync is disabled. | |
| 122 TEST_F(PermissionUmaUtilTest, | |
| 123 IsOptedIntoPermissionActionReportingProfileSyncServiceCheck) { | |
| 124 base::test::ScopedCommandLine scoped_command_line; | |
| 125 scoped_command_line.GetProcessCommandLine()->AppendSwitch( | |
| 126 switches::kEnablePermissionActionReporting); | |
| 127 SetSafeBrowsing(true); | |
| 128 FakeSignIn(); | |
| 129 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 130 | |
| 131 scoped_command_line.GetProcessCommandLine()->AppendSwitch( | |
| 132 switches::kDisableSync); | |
| 133 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 134 } | |
| 135 | |
| 136 // Test that PermissionUmaUtil::IsOptedIntoPermissionActionReporting returns | |
| 137 // false if Tab Sync and Pref Sync are not both enabled. | |
| 138 TEST_F(PermissionUmaUtilTest, | |
| 139 IsOptedIntoPermissionActionReportingSyncPreferenceCheck) { | |
| 140 base::test::ScopedCommandLine scoped_command_line; | |
| 141 scoped_command_line.GetProcessCommandLine()->AppendSwitch( | |
| 142 switches::kEnablePermissionActionReporting); | |
| 143 SetSafeBrowsing(true); | |
| 144 FakeSignIn(); | |
| 145 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 146 | |
| 147 SetKeepEverythingSyncedFalse(); | |
| 148 | |
| 149 GetProfileSyncService()->ChangePreferredDataTypes( | |
| 150 syncer::ModelTypeSet(syncer::PROXY_TABS)); | |
| 151 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 152 | |
| 153 GetProfileSyncService()->ChangePreferredDataTypes( | |
| 154 syncer::ModelTypeSet(syncer::PRIORITY_PREFERENCES)); | |
| 155 EXPECT_FALSE(IsOptedIntoPermissionActionReporting(profile())); | |
| 156 | |
| 157 GetProfileSyncService()->ChangePreferredDataTypes( | |
| 158 syncer::ModelTypeSet(syncer::PROXY_TABS, syncer::PRIORITY_PREFERENCES)); | |
| 159 EXPECT_TRUE(IsOptedIntoPermissionActionReporting(profile())); | |
| 160 } | |
| OLD | NEW |