| OLD | NEW |
| 1 // Copyright (c) 2015 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2015 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 // Note that this file only tests the basic behavior of the cache counter, as in | 5 // Note that this file only tests the basic behavior of the cache counter, as in |
| 6 // when it counts and when not, when result is nonzero and when not. It does not | 6 // when it counts and when not, when result is nonzero and when not. It does not |
| 7 // test whether the result of the counting is correct. This is the | 7 // test whether the result of the counting is correct. This is the |
| 8 // responsibility of a lower layer, and is tested in | 8 // responsibility of a lower layer, and is tested in |
| 9 // DiskCacheBackendTest.CalculateSizeOfAllEntries in net_unittests. | 9 // DiskCacheBackendTest.CalculateSizeOfAllEntries in net_unittests. |
| 10 | 10 |
| 11 #include "chrome/browser/browsing_data/cache_counter.h" | 11 #include "chrome/browser/browsing_data/cache_counter.h" |
| 12 | 12 |
| 13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/test/base/in_process_browser_test.h" | 17 #include "chrome/test/base/in_process_browser_test.h" |
| 17 #include "chrome/test/base/ui_test_utils.h" | 18 #include "chrome/test/base/ui_test_utils.h" |
| 19 #include "components/browsing_data/browsing_data_utils.h" |
| 20 #include "components/browsing_data/pref_names.h" |
| 18 #include "components/browsing_data/storage_partition_http_cache_data_remover.h" | 21 #include "components/browsing_data/storage_partition_http_cache_data_remover.h" |
| 19 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
| 20 #include "content/public/browser/storage_partition.h" | 23 #include "content/public/browser/storage_partition.h" |
| 21 #include "net/disk_cache/disk_cache.h" | 24 #include "net/disk_cache/disk_cache.h" |
| 22 #include "net/http/http_cache.h" | 25 #include "net/http/http_cache.h" |
| 23 #include "net/url_request/url_request_context.h" | 26 #include "net/url_request/url_request_context.h" |
| 24 #include "net/url_request/url_request_context_getter.h" | 27 #include "net/url_request/url_request_context_getter.h" |
| 25 | 28 |
| 26 using content::BrowserContext; | 29 using content::BrowserContext; |
| 27 using content::BrowserThread; | 30 using content::BrowserThread; |
| 28 | 31 |
| 29 namespace { | 32 namespace { |
| 30 | 33 |
| 31 class CacheCounterTest : public InProcessBrowserTest { | 34 class CacheCounterTest : public InProcessBrowserTest { |
| 32 public: | 35 public: |
| 33 void SetUpOnMainThread() override { | 36 void SetUpOnMainThread() override { |
| 34 SetCacheDeletionPref(true); | 37 SetCacheDeletionPref(true); |
| 35 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); | 38 SetDeletionPeriodPref(browsing_data::EVERYTHING); |
| 36 } | 39 } |
| 37 | 40 |
| 38 void SetCacheDeletionPref(bool value) { | 41 void SetCacheDeletionPref(bool value) { |
| 39 browser()->profile()->GetPrefs()->SetBoolean(prefs::kDeleteCache, value); | 42 browser()->profile()->GetPrefs()->SetBoolean(prefs::kDeleteCache, value); |
| 40 } | 43 } |
| 41 | 44 |
| 42 void SetDeletionPeriodPref(BrowsingDataRemover::TimePeriod period) { | 45 void SetDeletionPeriodPref(browsing_data::TimePeriod period) { |
| 43 browser()->profile()->GetPrefs()->SetInteger( | 46 browser()->profile()->GetPrefs()->SetInteger( |
| 44 prefs::kDeleteTimePeriod, static_cast<int>(period)); | 47 browsing_data::prefs::kDeleteTimePeriod, static_cast<int>(period)); |
| 45 } | 48 } |
| 46 | 49 |
| 47 // One step in the process of creating a cache entry. Every step must be | 50 // One step in the process of creating a cache entry. Every step must be |
| 48 // executed on IO thread after the previous one has finished. | 51 // executed on IO thread after the previous one has finished. |
| 49 void CreateCacheEntryStep(int return_value) { | 52 void CreateCacheEntryStep(int return_value) { |
| 50 net::CompletionCallback callback = | 53 net::CompletionCallback callback = |
| 51 base::Bind(&CacheCounterTest::CreateCacheEntryStep, | 54 base::Bind(&CacheCounterTest::CreateCacheEntryStep, |
| 52 base::Unretained(this)); | 55 base::Unretained(this)); |
| 53 | 56 |
| 54 switch (next_step_) { | 57 switch (next_step_) { |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 } | 125 } |
| 123 | 126 |
| 124 // General completion callback. | 127 // General completion callback. |
| 125 void Callback() { | 128 void Callback() { |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 127 if (run_loop_) | 130 if (run_loop_) |
| 128 run_loop_->Quit(); | 131 run_loop_->Quit(); |
| 129 } | 132 } |
| 130 | 133 |
| 131 // Callback from the counter. | 134 // Callback from the counter. |
| 132 void CountingCallback(std::unique_ptr<BrowsingDataCounter::Result> result) { | 135 void CountingCallback( |
| 136 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
| 133 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 134 finished_ = result->Finished(); | 138 finished_ = result->Finished(); |
| 135 | 139 |
| 136 if (finished_) { | 140 if (finished_) { |
| 137 result_ = static_cast<BrowsingDataCounter::FinishedResult*>( | 141 result_ = |
| 138 result.get())->Value(); | 142 static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>( |
| 143 result.get()) |
| 144 ->Value(); |
| 139 } | 145 } |
| 140 | 146 |
| 141 if (run_loop_ && finished_) | 147 if (run_loop_ && finished_) |
| 142 run_loop_->Quit(); | 148 run_loop_->Quit(); |
| 143 } | 149 } |
| 144 | 150 |
| 145 BrowsingDataCounter::ResultInt GetResult() { | 151 browsing_data::BrowsingDataCounter::ResultInt GetResult() { |
| 146 DCHECK(finished_); | 152 DCHECK(finished_); |
| 147 return result_; | 153 return result_; |
| 148 } | 154 } |
| 149 | 155 |
| 150 private: | 156 private: |
| 151 enum CacheEntryCreationStep { | 157 enum CacheEntryCreationStep { |
| 152 GET_CACHE, | 158 GET_CACHE, |
| 153 CREATE_ENTRY, | 159 CREATE_ENTRY, |
| 154 WRITE_DATA, | 160 WRITE_DATA, |
| 155 DONE | 161 DONE |
| 156 }; | 162 }; |
| 157 CacheEntryCreationStep next_step_; | 163 CacheEntryCreationStep next_step_; |
| 158 content::StoragePartition* storage_partition_; | 164 content::StoragePartition* storage_partition_; |
| 159 disk_cache::Backend* backend_; | 165 disk_cache::Backend* backend_; |
| 160 disk_cache::Entry* entry_; | 166 disk_cache::Entry* entry_; |
| 161 | 167 |
| 162 std::unique_ptr<base::RunLoop> run_loop_; | 168 std::unique_ptr<base::RunLoop> run_loop_; |
| 163 | 169 |
| 164 bool finished_; | 170 bool finished_; |
| 165 BrowsingDataCounter::ResultInt result_; | 171 browsing_data::BrowsingDataCounter::ResultInt result_; |
| 166 }; | 172 }; |
| 167 | 173 |
| 168 // Tests that for the empty cache, the result is zero. | 174 // Tests that for the empty cache, the result is zero. |
| 169 IN_PROC_BROWSER_TEST_F(CacheCounterTest, Empty) { | 175 IN_PROC_BROWSER_TEST_F(CacheCounterTest, Empty) { |
| 170 CacheCounter counter; | 176 Profile* profile = browser()->profile(); |
| 171 counter.Init(browser()->profile(), | 177 |
| 172 base::Bind(&CacheCounterTest::CountingCallback, | 178 CacheCounter counter(profile); |
| 173 base::Unretained(this))); | 179 counter.Init( |
| 180 profile->GetPrefs(), |
| 181 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| 174 counter.Restart(); | 182 counter.Restart(); |
| 175 | 183 |
| 176 WaitForIOThread(); | 184 WaitForIOThread(); |
| 177 EXPECT_EQ(0u, GetResult()); | 185 EXPECT_EQ(0u, GetResult()); |
| 178 } | 186 } |
| 179 | 187 |
| 180 // Tests that for a non-empty cache, the result is nonzero. | 188 // Tests that for a non-empty cache, the result is nonzero. |
| 181 IN_PROC_BROWSER_TEST_F(CacheCounterTest, NonEmpty) { | 189 IN_PROC_BROWSER_TEST_F(CacheCounterTest, NonEmpty) { |
| 182 CreateCacheEntry(); | 190 CreateCacheEntry(); |
| 183 | 191 |
| 184 CacheCounter counter; | 192 Profile* profile = browser()->profile(); |
| 185 counter.Init(browser()->profile(), | 193 CacheCounter counter(profile); |
| 186 base::Bind(&CacheCounterTest::CountingCallback, | 194 counter.Init( |
| 187 base::Unretained(this))); | 195 profile->GetPrefs(), |
| 196 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| 188 counter.Restart(); | 197 counter.Restart(); |
| 189 | 198 |
| 190 WaitForIOThread(); | 199 WaitForIOThread(); |
| 191 | 200 |
| 192 EXPECT_NE(0u, GetResult()); | 201 EXPECT_NE(0u, GetResult()); |
| 193 } | 202 } |
| 194 | 203 |
| 195 // Tests that after dooming a nonempty cache, the result is zero. | 204 // Tests that after dooming a nonempty cache, the result is zero. |
| 196 IN_PROC_BROWSER_TEST_F(CacheCounterTest, AfterDoom) { | 205 IN_PROC_BROWSER_TEST_F(CacheCounterTest, AfterDoom) { |
| 197 CreateCacheEntry(); | 206 CreateCacheEntry(); |
| 198 | 207 |
| 199 CacheCounter counter; | 208 Profile* profile = browser()->profile(); |
| 200 counter.Init(browser()->profile(), | 209 CacheCounter counter(profile); |
| 201 base::Bind(&CacheCounterTest::CountingCallback, | 210 counter.Init( |
| 202 base::Unretained(this))); | 211 profile->GetPrefs(), |
| 212 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| 203 | 213 |
| 204 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange( | 214 browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange( |
| 205 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()), | 215 content::BrowserContext::GetDefaultStoragePartition(browser()->profile()), |
| 206 base::Time(), | 216 base::Time(), |
| 207 base::Time::Max())->Remove( | 217 base::Time::Max())->Remove( |
| 208 base::Bind(&CacheCounter::Restart, | 218 base::Bind(&CacheCounter::Restart, |
| 209 base::Unretained(&counter))); | 219 base::Unretained(&counter))); |
| 210 | 220 |
| 211 WaitForIOThread(); | 221 WaitForIOThread(); |
| 212 EXPECT_EQ(0u, GetResult()); | 222 EXPECT_EQ(0u, GetResult()); |
| 213 } | 223 } |
| 214 | 224 |
| 215 // Tests that the counter starts counting automatically when the deletion | 225 // Tests that the counter starts counting automatically when the deletion |
| 216 // pref changes to true. | 226 // pref changes to true. |
| 217 IN_PROC_BROWSER_TEST_F(CacheCounterTest, PrefChanged) { | 227 IN_PROC_BROWSER_TEST_F(CacheCounterTest, PrefChanged) { |
| 218 SetCacheDeletionPref(false); | 228 SetCacheDeletionPref(false); |
| 219 | 229 |
| 220 CacheCounter counter; | 230 Profile* profile = browser()->profile(); |
| 221 counter.Init(browser()->profile(), | 231 CacheCounter counter(profile); |
| 222 base::Bind(&CacheCounterTest::CountingCallback, | 232 counter.Init( |
| 223 base::Unretained(this))); | 233 profile->GetPrefs(), |
| 234 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| 224 SetCacheDeletionPref(true); | 235 SetCacheDeletionPref(true); |
| 225 | 236 |
| 226 WaitForIOThread(); | 237 WaitForIOThread(); |
| 227 EXPECT_EQ(0u, GetResult()); | 238 EXPECT_EQ(0u, GetResult()); |
| 228 } | 239 } |
| 229 | 240 |
| 230 // Tests that the counter does not count if the deletion preference is false. | 241 // Tests that the counter does not count if the deletion preference is false. |
| 231 IN_PROC_BROWSER_TEST_F(CacheCounterTest, PrefIsFalse) { | 242 IN_PROC_BROWSER_TEST_F(CacheCounterTest, PrefIsFalse) { |
| 232 SetCacheDeletionPref(false); | 243 SetCacheDeletionPref(false); |
| 233 | 244 |
| 234 CacheCounter counter; | 245 Profile* profile = browser()->profile(); |
| 235 counter.Init(browser()->profile(), | 246 CacheCounter counter(profile); |
| 236 base::Bind(&CacheCounterTest::CountingCallback, | 247 counter.Init( |
| 237 base::Unretained(this))); | 248 profile->GetPrefs(), |
| 249 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| 238 counter.Restart(); | 250 counter.Restart(); |
| 239 | 251 |
| 240 EXPECT_FALSE(counter.Pending()); | 252 EXPECT_FALSE(counter.Pending()); |
| 241 } | 253 } |
| 242 | 254 |
| 243 // Tests that the counting is restarted when the time period changes. Currently, | 255 // Tests that the counting is restarted when the time period changes. Currently, |
| 244 // the results should be the same for every period. This is because the counter | 256 // the results should be the same for every period. This is because the counter |
| 245 // always counts the size of the entire cache, and it is up to the UI | 257 // always counts the size of the entire cache, and it is up to the UI |
| 246 // to interpret it as exact value or upper bound. | 258 // to interpret it as exact value or upper bound. |
| 247 IN_PROC_BROWSER_TEST_F(CacheCounterTest, PeriodChanged) { | 259 IN_PROC_BROWSER_TEST_F(CacheCounterTest, PeriodChanged) { |
| 248 CreateCacheEntry(); | 260 CreateCacheEntry(); |
| 249 | 261 |
| 250 CacheCounter counter; | 262 Profile* profile = browser()->profile(); |
| 251 counter.Init(browser()->profile(), | 263 CacheCounter counter(profile); |
| 252 base::Bind(&CacheCounterTest::CountingCallback, | 264 counter.Init( |
| 253 base::Unretained(this))); | 265 profile->GetPrefs(), |
| 266 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| 254 | 267 |
| 255 SetDeletionPeriodPref(BrowsingDataRemover::LAST_HOUR); | 268 SetDeletionPeriodPref(browsing_data::LAST_HOUR); |
| 256 WaitForIOThread(); | 269 WaitForIOThread(); |
| 257 BrowsingDataCounter::ResultInt result = GetResult(); | 270 browsing_data::BrowsingDataCounter::ResultInt result = GetResult(); |
| 258 | 271 |
| 259 SetDeletionPeriodPref(BrowsingDataRemover::LAST_DAY); | 272 SetDeletionPeriodPref(browsing_data::LAST_DAY); |
| 260 WaitForIOThread(); | 273 WaitForIOThread(); |
| 261 EXPECT_EQ(result, GetResult()); | 274 EXPECT_EQ(result, GetResult()); |
| 262 | 275 |
| 263 SetDeletionPeriodPref(BrowsingDataRemover::LAST_WEEK); | 276 SetDeletionPeriodPref(browsing_data::LAST_WEEK); |
| 264 WaitForIOThread(); | 277 WaitForIOThread(); |
| 265 EXPECT_EQ(result, GetResult()); | 278 EXPECT_EQ(result, GetResult()); |
| 266 | 279 |
| 267 SetDeletionPeriodPref(BrowsingDataRemover::FOUR_WEEKS); | 280 SetDeletionPeriodPref(browsing_data::FOUR_WEEKS); |
| 268 WaitForIOThread(); | 281 WaitForIOThread(); |
| 269 EXPECT_EQ(result, GetResult()); | 282 EXPECT_EQ(result, GetResult()); |
| 270 | 283 |
| 271 SetDeletionPeriodPref(BrowsingDataRemover::EVERYTHING); | 284 SetDeletionPeriodPref(browsing_data::EVERYTHING); |
| 272 WaitForIOThread(); | 285 WaitForIOThread(); |
| 273 EXPECT_EQ(result, GetResult()); | 286 EXPECT_EQ(result, GetResult()); |
| 274 } | 287 } |
| 275 | 288 |
| 276 } // namespace | 289 } // namespace |
| OLD | NEW |