| 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 |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 run_loop_->Quit(); | 131 run_loop_->Quit(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 // Callback from the counter. | 134 // Callback from the counter. |
| 135 void CountingCallback( | 135 void CountingCallback( |
| 136 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { | 136 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
| 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 137 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 138 finished_ = result->Finished(); | 138 finished_ = result->Finished(); |
| 139 | 139 |
| 140 if (finished_) { | 140 if (finished_) { |
| 141 result_ = | 141 auto cache_result = static_cast<CacheCounter::CacheResult*>(result.get()); |
| 142 static_cast<browsing_data::BrowsingDataCounter::FinishedResult*>( | 142 result_ = cache_result->cache_size(); |
| 143 result.get()) | 143 is_upper_limit_ = cache_result->is_upper_limit(); |
| 144 ->Value(); | |
| 145 } | 144 } |
| 146 | 145 |
| 147 if (run_loop_ && finished_) | 146 if (run_loop_ && finished_) |
| 148 run_loop_->Quit(); | 147 run_loop_->Quit(); |
| 149 } | 148 } |
| 150 | 149 |
| 151 browsing_data::BrowsingDataCounter::ResultInt GetResult() { | 150 browsing_data::BrowsingDataCounter::ResultInt GetResult() { |
| 152 DCHECK(finished_); | 151 DCHECK(finished_); |
| 153 return result_; | 152 return result_; |
| 154 } | 153 } |
| 155 | 154 |
| 155 bool IsUpperLimit() { |
| 156 DCHECK(finished_); |
| 157 return is_upper_limit_; |
| 158 } |
| 159 |
| 156 private: | 160 private: |
| 157 enum CacheEntryCreationStep { | 161 enum CacheEntryCreationStep { |
| 158 GET_CACHE, | 162 GET_CACHE, |
| 159 CREATE_ENTRY, | 163 CREATE_ENTRY, |
| 160 WRITE_DATA, | 164 WRITE_DATA, |
| 161 DONE | 165 DONE |
| 162 }; | 166 }; |
| 163 CacheEntryCreationStep next_step_; | 167 CacheEntryCreationStep next_step_; |
| 164 content::StoragePartition* storage_partition_; | 168 content::StoragePartition* storage_partition_; |
| 165 disk_cache::Backend* backend_; | 169 disk_cache::Backend* backend_; |
| 166 disk_cache::Entry* entry_; | 170 disk_cache::Entry* entry_; |
| 167 | 171 |
| 168 std::unique_ptr<base::RunLoop> run_loop_; | 172 std::unique_ptr<base::RunLoop> run_loop_; |
| 169 | 173 |
| 170 bool finished_; | 174 bool finished_; |
| 171 browsing_data::BrowsingDataCounter::ResultInt result_; | 175 browsing_data::BrowsingDataCounter::ResultInt result_; |
| 176 bool is_upper_limit_; |
| 172 }; | 177 }; |
| 173 | 178 |
| 174 // Tests that for the empty cache, the result is zero. | 179 // Tests that for the empty cache, the result is zero. |
| 175 IN_PROC_BROWSER_TEST_F(CacheCounterTest, Empty) { | 180 IN_PROC_BROWSER_TEST_F(CacheCounterTest, Empty) { |
| 176 Profile* profile = browser()->profile(); | 181 Profile* profile = browser()->profile(); |
| 177 | 182 |
| 178 CacheCounter counter(profile); | 183 CacheCounter counter(profile); |
| 179 counter.Init( | 184 counter.Init( |
| 180 profile->GetPrefs(), | 185 profile->GetPrefs(), |
| 181 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); | 186 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 Profile* profile = browser()->profile(); | 250 Profile* profile = browser()->profile(); |
| 246 CacheCounter counter(profile); | 251 CacheCounter counter(profile); |
| 247 counter.Init( | 252 counter.Init( |
| 248 profile->GetPrefs(), | 253 profile->GetPrefs(), |
| 249 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); | 254 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| 250 counter.Restart(); | 255 counter.Restart(); |
| 251 | 256 |
| 252 EXPECT_FALSE(counter.Pending()); | 257 EXPECT_FALSE(counter.Pending()); |
| 253 } | 258 } |
| 254 | 259 |
| 255 // Tests that the counting is restarted when the time period changes. Currently, | 260 // Tests that the counting is restarted when the time period changes. |
| 256 // the results should be the same for every period. This is because the counter | |
| 257 // always counts the size of the entire cache, and it is up to the UI | |
| 258 // to interpret it as exact value or upper bound. | |
| 259 IN_PROC_BROWSER_TEST_F(CacheCounterTest, PeriodChanged) { | 261 IN_PROC_BROWSER_TEST_F(CacheCounterTest, PeriodChanged) { |
| 260 CreateCacheEntry(); | 262 CreateCacheEntry(); |
| 261 | 263 |
| 262 Profile* profile = browser()->profile(); | 264 Profile* profile = browser()->profile(); |
| 263 CacheCounter counter(profile); | 265 CacheCounter counter(profile); |
| 264 counter.Init( | 266 counter.Init( |
| 265 profile->GetPrefs(), | 267 profile->GetPrefs(), |
| 266 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); | 268 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| 267 | 269 |
| 268 SetDeletionPeriodPref(browsing_data::LAST_HOUR); | 270 SetDeletionPeriodPref(browsing_data::LAST_HOUR); |
| 269 WaitForIOThread(); | 271 WaitForIOThread(); |
| 270 browsing_data::BrowsingDataCounter::ResultInt result = GetResult(); | 272 browsing_data::BrowsingDataCounter::ResultInt result = GetResult(); |
| 273 EXPECT_FALSE(IsUpperLimit()); |
| 271 | 274 |
| 272 SetDeletionPeriodPref(browsing_data::LAST_DAY); | 275 SetDeletionPeriodPref(browsing_data::LAST_DAY); |
| 273 WaitForIOThread(); | 276 WaitForIOThread(); |
| 274 EXPECT_EQ(result, GetResult()); | 277 EXPECT_EQ(result, GetResult()); |
| 278 EXPECT_FALSE(IsUpperLimit()); |
| 275 | 279 |
| 276 SetDeletionPeriodPref(browsing_data::LAST_WEEK); | 280 SetDeletionPeriodPref(browsing_data::LAST_WEEK); |
| 277 WaitForIOThread(); | 281 WaitForIOThread(); |
| 278 EXPECT_EQ(result, GetResult()); | 282 EXPECT_EQ(result, GetResult()); |
| 283 EXPECT_FALSE(IsUpperLimit()); |
| 279 | 284 |
| 280 SetDeletionPeriodPref(browsing_data::FOUR_WEEKS); | 285 SetDeletionPeriodPref(browsing_data::FOUR_WEEKS); |
| 281 WaitForIOThread(); | 286 WaitForIOThread(); |
| 282 EXPECT_EQ(result, GetResult()); | 287 EXPECT_EQ(result, GetResult()); |
| 288 EXPECT_FALSE(IsUpperLimit()); |
| 283 | 289 |
| 284 SetDeletionPeriodPref(browsing_data::ALL_TIME); | 290 SetDeletionPeriodPref(browsing_data::ALL_TIME); |
| 285 WaitForIOThread(); | 291 WaitForIOThread(); |
| 286 EXPECT_EQ(result, GetResult()); | 292 EXPECT_EQ(result, GetResult()); |
| 293 EXPECT_FALSE(IsUpperLimit()); |
| 294 } |
| 295 |
| 296 // Tests the cache size is estimated if the timeout runs out. |
| 297 IN_PROC_BROWSER_TEST_F(CacheCounterTest, Timeout) { |
| 298 CreateCacheEntry(); |
| 299 |
| 300 Profile* profile = browser()->profile(); |
| 301 CacheCounter counter(profile); |
| 302 counter.SetTimeout(base::TimeDelta()); |
| 303 counter.Init( |
| 304 profile->GetPrefs(), |
| 305 base::Bind(&CacheCounterTest::CountingCallback, base::Unretained(this))); |
| 306 |
| 307 SetDeletionPeriodPref(browsing_data::LAST_DAY); |
| 308 WaitForIOThread(); |
| 309 EXPECT_GT(GetResult(), 0); |
| 310 EXPECT_TRUE(IsUpperLimit()); |
| 311 |
| 312 // Even with a timeout of 0, ALL_TIME shouldn't be returned as upper limit. |
| 313 SetDeletionPeriodPref(browsing_data::ALL_TIME); |
| 314 WaitForIOThread(); |
| 315 EXPECT_GT(GetResult(), 0); |
| 316 EXPECT_FALSE(IsUpperLimit()); |
| 287 } | 317 } |
| 288 | 318 |
| 289 } // namespace | 319 } // namespace |
| OLD | NEW |