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

Side by Side Diff: chrome/browser/browsing_data/browsing_data_remover_unittest.cc

Issue 2171383002: Deprecate the CallbackSubscription in BrowsingDataRemover (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Initialize masks to -1 Created 4 years, 4 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/browsing_data/browsing_data_remover.h" 5 #include "chrome/browser/browsing_data/browsing_data_remover.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 9
10 #include <memory> 10 #include <memory>
(...skipping 1007 matching lines...) Expand 10 before | Expand all | Expand 10 after
1018 DISALLOW_COPY_AND_ASSIGN(RemovePasswordsTester); 1018 DISALLOW_COPY_AND_ASSIGN(RemovePasswordsTester);
1019 }; 1019 };
1020 1020
1021 // Test Class ---------------------------------------------------------------- 1021 // Test Class ----------------------------------------------------------------
1022 1022
1023 class BrowsingDataRemoverTest : public testing::Test { 1023 class BrowsingDataRemoverTest : public testing::Test {
1024 public: 1024 public:
1025 BrowsingDataRemoverTest() 1025 BrowsingDataRemoverTest()
1026 : profile_(new TestingProfile()), 1026 : profile_(new TestingProfile()),
1027 clear_domain_reliability_tester_(GetProfile()) { 1027 clear_domain_reliability_tester_(GetProfile()) {
1028 callback_subscription_ = 1028 remover_ =
1029 BrowsingDataRemover::RegisterOnBrowsingDataRemovedCallback( 1029 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get());
1030 base::Bind(&BrowsingDataRemoverTest::NotifyWithDetails,
1031 base::Unretained(this)));
1032 1030
1033 #if BUILDFLAG(ANDROID_JAVA_UI) 1031 #if BUILDFLAG(ANDROID_JAVA_UI)
1034 BrowsingDataRemover* remover = 1032 remover_->OverrideWebappRegistryForTesting(
1035 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get());
1036 remover->OverrideWebappRegistryForTesting(
1037 std::unique_ptr<WebappRegistry>(new TestWebappRegistry())); 1033 std::unique_ptr<WebappRegistry>(new TestWebappRegistry()));
1038 #endif 1034 #endif
1039 } 1035 }
1040 1036
1041 ~BrowsingDataRemoverTest() override {} 1037 ~BrowsingDataRemoverTest() override {}
1042 1038
1043 void TearDown() override { 1039 void TearDown() override {
1044 #if defined(ENABLE_EXTENSIONS) 1040 #if defined(ENABLE_EXTENSIONS)
1045 mock_policy_ = nullptr; 1041 mock_policy_ = nullptr;
1046 #endif 1042 #endif
(...skipping 11 matching lines...) Expand all
1058 1054
1059 void BlockUntilBrowsingDataRemoved(browsing_data::TimePeriod period, 1055 void BlockUntilBrowsingDataRemoved(browsing_data::TimePeriod period,
1060 int remove_mask, 1056 int remove_mask,
1061 bool include_protected_origins) { 1057 bool include_protected_origins) {
1062 BrowsingDataRemover* remover = 1058 BrowsingDataRemover* remover =
1063 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); 1059 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get());
1064 1060
1065 TestStoragePartition storage_partition; 1061 TestStoragePartition storage_partition;
1066 remover->OverrideStoragePartitionForTesting(&storage_partition); 1062 remover->OverrideStoragePartitionForTesting(&storage_partition);
1067 1063
1068 called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
1069
1070 int origin_type_mask = BrowsingDataHelper::UNPROTECTED_WEB; 1064 int origin_type_mask = BrowsingDataHelper::UNPROTECTED_WEB;
1071 if (include_protected_origins) 1065 if (include_protected_origins)
1072 origin_type_mask |= BrowsingDataHelper::PROTECTED_WEB; 1066 origin_type_mask |= BrowsingDataHelper::PROTECTED_WEB;
1073 1067
1074 BrowsingDataRemoverCompletionObserver completion_observer(remover); 1068 BrowsingDataRemoverCompletionObserver completion_observer(remover);
1075 remover->Remove(BrowsingDataRemover::Period(period), remove_mask, 1069 remover->Remove(BrowsingDataRemover::Period(period), remove_mask,
1076 origin_type_mask); 1070 origin_type_mask);
1077 completion_observer.BlockUntilCompletion(); 1071 completion_observer.BlockUntilCompletion();
1078 1072
1079 // Save so we can verify later. 1073 // Save so we can verify later.
1080 storage_partition_removal_data_ = 1074 storage_partition_removal_data_ =
1081 storage_partition.GetStoragePartitionRemovalData(); 1075 storage_partition.GetStoragePartitionRemovalData();
1082 } 1076 }
1083 1077
1084 void BlockUntilOriginDataRemoved( 1078 void BlockUntilOriginDataRemoved(
1085 browsing_data::TimePeriod period, 1079 browsing_data::TimePeriod period,
1086 int remove_mask, 1080 int remove_mask,
1087 const BrowsingDataFilterBuilder& filter_builder) { 1081 const BrowsingDataFilterBuilder& filter_builder) {
1088 BrowsingDataRemover* remover = 1082 BrowsingDataRemover* remover =
1089 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get()); 1083 BrowsingDataRemoverFactory::GetForBrowserContext(profile_.get());
1090 TestStoragePartition storage_partition; 1084 TestStoragePartition storage_partition;
1091 remover->OverrideStoragePartitionForTesting(&storage_partition); 1085 remover->OverrideStoragePartitionForTesting(&storage_partition);
1092 1086
1093 called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
1094
1095 BrowsingDataRemoverCompletionObserver completion_observer(remover); 1087 BrowsingDataRemoverCompletionObserver completion_observer(remover);
1096 remover->RemoveImpl(BrowsingDataRemover::Period(period), remove_mask, 1088 remover->RemoveImpl(BrowsingDataRemover::Period(period), remove_mask,
1097 filter_builder, BrowsingDataHelper::UNPROTECTED_WEB); 1089 filter_builder, BrowsingDataHelper::UNPROTECTED_WEB);
1098 completion_observer.BlockUntilCompletion(); 1090 completion_observer.BlockUntilCompletion();
1099 1091
1100 // Save so we can verify later. 1092 // Save so we can verify later.
1101 storage_partition_removal_data_ = 1093 storage_partition_removal_data_ =
1102 storage_partition.GetStoragePartitionRemovalData(); 1094 storage_partition.GetStoragePartitionRemovalData();
1103 } 1095 }
1104 1096
1105 TestingProfile* GetProfile() { 1097 TestingProfile* GetProfile() {
1106 return profile_.get(); 1098 return profile_.get();
1107 } 1099 }
1108 1100
1109 void DestroyProfile() { profile_.reset(); } 1101 void DestroyProfile() { profile_.reset(); }
1110 1102
1111 base::Time GetBeginTime() { 1103 const base::Time& GetBeginTime() {
1112 return called_with_details_->removal_begin; 1104 return remover_->GetLastUsedBeginTime();
1113 } 1105 }
1114 1106
1115 int GetRemovalMask() { 1107 int GetRemovalMask() {
1116 return called_with_details_->removal_mask; 1108 return remover_->GetLastUsedRemovalMask();
1117 } 1109 }
1118 1110
1119 int GetOriginTypeMask() { 1111 int GetOriginTypeMask() {
1120 return called_with_details_->origin_type_mask; 1112 return remover_->GetLastUsedOriginTypeMask();
1121 } 1113 }
1122 1114
1123 StoragePartitionRemovalData GetStoragePartitionRemovalData() { 1115 StoragePartitionRemovalData GetStoragePartitionRemovalData() {
1124 return storage_partition_removal_data_; 1116 return storage_partition_removal_data_;
1125 } 1117 }
1126 1118
1127 // Callback for browsing data removal events.
1128 void NotifyWithDetails(
1129 const BrowsingDataRemover::NotificationDetails& details) {
1130 // We're not taking ownership of the details object, but storing a copy of
1131 // it locally.
1132 called_with_details_.reset(
1133 new BrowsingDataRemover::NotificationDetails(details));
1134
1135 callback_subscription_.reset();
1136 }
1137
1138 MockExtensionSpecialStoragePolicy* CreateMockPolicy() { 1119 MockExtensionSpecialStoragePolicy* CreateMockPolicy() {
1139 #if defined(ENABLE_EXTENSIONS) 1120 #if defined(ENABLE_EXTENSIONS)
1140 mock_policy_ = new MockExtensionSpecialStoragePolicy; 1121 mock_policy_ = new MockExtensionSpecialStoragePolicy;
1141 return mock_policy_.get(); 1122 return mock_policy_.get();
1142 #else 1123 #else
1143 NOTREACHED(); 1124 NOTREACHED();
1144 return nullptr; 1125 return nullptr;
1145 #endif 1126 #endif
1146 } 1127 }
1147 1128
(...skipping 12 matching lines...) Expand all
1160 return false; 1141 return false;
1161 #else 1142 #else
1162 return true; 1143 return true;
1163 #endif 1144 #endif
1164 } 1145 }
1165 1146
1166 const ClearDomainReliabilityTester& clear_domain_reliability_tester() { 1147 const ClearDomainReliabilityTester& clear_domain_reliability_tester() {
1167 return clear_domain_reliability_tester_; 1148 return clear_domain_reliability_tester_;
1168 } 1149 }
1169 1150
1170 protected: 1151 private:
1171 std::unique_ptr<BrowsingDataRemover::NotificationDetails> 1152 // Cached pointer to BrowsingDataRemover for access to testing methods.
1172 called_with_details_; 1153 BrowsingDataRemover* remover_;
1173 1154
1174 private:
1175 content::TestBrowserThreadBundle thread_bundle_; 1155 content::TestBrowserThreadBundle thread_bundle_;
1176 std::unique_ptr<TestingProfile> profile_; 1156 std::unique_ptr<TestingProfile> profile_;
1177 1157
1178 StoragePartitionRemovalData storage_partition_removal_data_; 1158 StoragePartitionRemovalData storage_partition_removal_data_;
1179 1159
1180 #if defined(ENABLE_EXTENSIONS) 1160 #if defined(ENABLE_EXTENSIONS)
1181 scoped_refptr<MockExtensionSpecialStoragePolicy> mock_policy_; 1161 scoped_refptr<MockExtensionSpecialStoragePolicy> mock_policy_;
1182 #endif 1162 #endif
1183 1163
1184 BrowsingDataRemover::CallbackSubscription callback_subscription_;
1185
1186 // Needed to mock out DomainReliabilityService, even for unrelated tests. 1164 // Needed to mock out DomainReliabilityService, even for unrelated tests.
1187 ClearDomainReliabilityTester clear_domain_reliability_tester_; 1165 ClearDomainReliabilityTester clear_domain_reliability_tester_;
1188 1166
1189 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest); 1167 DISALLOW_COPY_AND_ASSIGN(BrowsingDataRemoverTest);
1190 }; 1168 };
1191 1169
1192 // Tests --------------------------------------------------------------------- 1170 // Tests ---------------------------------------------------------------------
1193 1171
1194 TEST_F(BrowsingDataRemoverTest, RemoveCookieForever) { 1172 TEST_F(BrowsingDataRemoverTest, RemoveCookieForever) {
1195 BlockUntilBrowsingDataRemoved(browsing_data::EVERYTHING, 1173 BlockUntilBrowsingDataRemoved(browsing_data::EVERYTHING,
(...skipping 971 matching lines...) Expand 10 before | Expand all | Expand 10 after
2167 BlockUntilBrowsingDataRemoved(browsing_data::LAST_HOUR, 2145 BlockUntilBrowsingDataRemoved(browsing_data::LAST_HOUR,
2168 BrowsingDataRemover::REMOVE_HISTORY, false); 2146 BrowsingDataRemover::REMOVE_HISTORY, false);
2169 2147
2170 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask()); 2148 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask());
2171 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginTypeMask()); 2149 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginTypeMask());
2172 EXPECT_TRUE(tester.HasOrigin(std::string())); 2150 EXPECT_TRUE(tester.HasOrigin(std::string()));
2173 EXPECT_FALSE(tester.HasOrigin(kWebOrigin)); 2151 EXPECT_FALSE(tester.HasOrigin(kWebOrigin));
2174 EXPECT_TRUE(tester.HasOrigin(autofill::kSettingsOrigin)); 2152 EXPECT_TRUE(tester.HasOrigin(autofill::kSettingsOrigin));
2175 } 2153 }
2176 2154
2155 class InspectableCompletionObserver
2156 : public BrowsingDataRemoverCompletionObserver {
2157 public:
2158 explicit InspectableCompletionObserver(BrowsingDataRemover* remover)
2159 : BrowsingDataRemoverCompletionObserver(remover) {}
2160 ~InspectableCompletionObserver() override {}
2161
2162 bool called() { return called_; }
2163
2164 protected:
2165 void OnBrowsingDataRemoverDone() override {
2166 BrowsingDataRemoverCompletionObserver::OnBrowsingDataRemoverDone();
2167 called_ = true;
2168 }
2169
2170 private:
2171 bool called_ = false;
2172 };
2173
2177 TEST_F(BrowsingDataRemoverTest, CompletionInhibition) { 2174 TEST_F(BrowsingDataRemoverTest, CompletionInhibition) {
2178 // The |completion_inhibitor| on the stack should prevent removal sessions 2175 // The |completion_inhibitor| on the stack should prevent removal sessions
2179 // from completing until after ContinueToCompletion() is called. 2176 // from completing until after ContinueToCompletion() is called.
2180 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; 2177 BrowsingDataRemoverCompletionInhibitor completion_inhibitor;
2181 2178
2182 called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
2183
2184 BrowsingDataRemover* remover = 2179 BrowsingDataRemover* remover =
2185 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); 2180 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile());
2181 InspectableCompletionObserver completion_observer(remover);
2186 remover->Remove(BrowsingDataRemover::Unbounded(), 2182 remover->Remove(BrowsingDataRemover::Unbounded(),
2187 BrowsingDataRemover::REMOVE_HISTORY, 2183 BrowsingDataRemover::REMOVE_HISTORY,
2188 BrowsingDataHelper::UNPROTECTED_WEB); 2184 BrowsingDataHelper::UNPROTECTED_WEB);
2189 2185
2190 // Process messages until the inhibitor is notified, and then some, to make 2186 // Process messages until the inhibitor is notified, and then some, to make
2191 // sure we do not complete asynchronously before ContinueToCompletion() is 2187 // sure we do not complete asynchronously before ContinueToCompletion() is
2192 // called. 2188 // called.
2193 completion_inhibitor.BlockUntilNearCompletion(); 2189 completion_inhibitor.BlockUntilNearCompletion();
2194 base::RunLoop().RunUntilIdle(); 2190 base::RunLoop().RunUntilIdle();
2195 2191
2196 // Verify that the completion notification has not yet been broadcasted. 2192 // Verify that the removal has not yet been completed and the observer has
2197 EXPECT_EQ(-1, GetRemovalMask()); 2193 // not been called.
2198 EXPECT_EQ(-1, GetOriginTypeMask()); 2194 EXPECT_TRUE(remover->is_removing());
2195 EXPECT_FALSE(completion_observer.called());
2199 2196
2200 // Now run the removal process until completion, and verify that observers are 2197 // Now run the removal process until completion, and verify that observers are
2201 // now notified, and the notifications is sent out. 2198 // now notified, and the notifications is sent out.
2202 BrowsingDataRemoverCompletionObserver completion_observer(remover);
2203 completion_inhibitor.ContinueToCompletion(); 2199 completion_inhibitor.ContinueToCompletion();
2204 completion_observer.BlockUntilCompletion(); 2200 completion_observer.BlockUntilCompletion();
2205 2201
2206 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask()); 2202 EXPECT_FALSE(remover->is_removing());
2207 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginTypeMask()); 2203 EXPECT_TRUE(completion_observer.called());
2208 } 2204 }
2209 2205
2210 TEST_F(BrowsingDataRemoverTest, EarlyShutdown) { 2206 TEST_F(BrowsingDataRemoverTest, EarlyShutdown) {
2211 called_with_details_.reset(new BrowsingDataRemover::NotificationDetails());
2212
2213 BrowsingDataRemover* remover = 2207 BrowsingDataRemover* remover =
2214 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile()); 2208 BrowsingDataRemoverFactory::GetForBrowserContext(GetProfile());
2215 BrowsingDataRemoverCompletionObserver completion_observer(remover); 2209 InspectableCompletionObserver completion_observer(remover);
2216 BrowsingDataRemoverCompletionInhibitor completion_inhibitor; 2210 BrowsingDataRemoverCompletionInhibitor completion_inhibitor;
2217 remover->Remove(BrowsingDataRemover::Unbounded(), 2211 remover->Remove(BrowsingDataRemover::Unbounded(),
2218 BrowsingDataRemover::REMOVE_HISTORY, 2212 BrowsingDataRemover::REMOVE_HISTORY,
2219 BrowsingDataHelper::UNPROTECTED_WEB); 2213 BrowsingDataHelper::UNPROTECTED_WEB);
2220 2214
2221 completion_inhibitor.BlockUntilNearCompletion(); 2215 completion_inhibitor.BlockUntilNearCompletion();
2222 2216
2223 // Verify that the completion notification has not yet been broadcasted. 2217 // Verify that the deletion has not yet been completed and the observer has
2224 EXPECT_EQ(-1, GetRemovalMask()); 2218 // not been called.
2225 EXPECT_EQ(-1, GetOriginTypeMask()); 2219 EXPECT_TRUE(remover->is_removing());
2220 EXPECT_FALSE(completion_observer.called());
2226 2221
2227 // Destroying the profile should trigger the notification. 2222 // Destroying the profile should trigger the notification.
2228 DestroyProfile(); 2223 DestroyProfile();
2229 2224
2230 EXPECT_EQ(BrowsingDataRemover::REMOVE_HISTORY, GetRemovalMask()); 2225 EXPECT_TRUE(completion_observer.called());
2231 EXPECT_EQ(BrowsingDataHelper::UNPROTECTED_WEB, GetOriginTypeMask());
2232 2226
2233 // Finishing after shutdown shouldn't break anything. 2227 // Finishing after shutdown shouldn't break anything.
2234 completion_inhibitor.ContinueToCompletion(); 2228 completion_inhibitor.ContinueToCompletion();
2235 completion_observer.BlockUntilCompletion(); 2229 completion_observer.BlockUntilCompletion();
2236 } 2230 }
2237 2231
2238 TEST_F(BrowsingDataRemoverTest, ZeroSuggestCacheClear) { 2232 TEST_F(BrowsingDataRemoverTest, ZeroSuggestCacheClear) {
2239 PrefService* prefs = GetProfile()->GetPrefs(); 2233 PrefService* prefs = GetProfile()->GetPrefs();
2240 prefs->SetString(omnibox::kZeroSuggestCachedResults, 2234 prefs->SetString(omnibox::kZeroSuggestCachedResults,
2241 "[\"\", [\"foo\", \"bar\"]]"); 2235 "[\"\", [\"foo\", \"bar\"]]");
(...skipping 349 matching lines...) Expand 10 before | Expand all | Expand 10 after
2591 BrowsingDataRemover::ClearSettingsForOneTypeWithPredicate( 2585 BrowsingDataRemover::ClearSettingsForOneTypeWithPredicate(
2592 host_content_settings_map, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, 2586 host_content_settings_map, CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT,
2593 base::Bind(&MatchPrimaryPattern, http_pattern)); 2587 base::Bind(&MatchPrimaryPattern, http_pattern));
2594 // Verify we only have one, and it's url1. 2588 // Verify we only have one, and it's url1.
2595 host_content_settings_map->GetSettingsForOneType( 2589 host_content_settings_map->GetSettingsForOneType(
2596 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings); 2590 CONTENT_SETTINGS_TYPE_SITE_ENGAGEMENT, std::string(), &host_settings);
2597 EXPECT_EQ(1u, host_settings.size()); 2591 EXPECT_EQ(1u, host_settings.size());
2598 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1), 2592 EXPECT_EQ(ContentSettingsPattern::FromURLNoWildcard(url1),
2599 host_settings[0].primary_pattern); 2593 host_settings[0].primary_pattern);
2600 } 2594 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698