OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 <algorithm> |
| 6 #include <memory> |
| 7 #include <set> |
| 8 #include <string> |
| 9 |
| 10 #include "base/run_loop.h" |
| 11 #include "chrome/browser/browsing_data/cache_test_util.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/test/base/in_process_browser_test.h" |
| 15 #include "components/browsing_data/content/conditional_cache_counting_helper.h" |
| 16 #include "content/public/browser/browser_context.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 |
| 19 using browsing_data::ConditionalCacheCountingHelper; |
| 20 using content::BrowserThread; |
| 21 |
| 22 class ConditionalCacheCountingHelperBrowserTest : public InProcessBrowserTest { |
| 23 public: |
| 24 const int64_t kTimeoutMs = 10; |
| 25 |
| 26 void SetUpOnMainThread() override { |
| 27 count_callback_ = |
| 28 base::Bind(&ConditionalCacheCountingHelperBrowserTest::CountCallback, |
| 29 base::Unretained(this)); |
| 30 |
| 31 cache_util_ = base::MakeUnique<CacheTestUtil>( |
| 32 content::BrowserContext::GetDefaultStoragePartition( |
| 33 browser()->profile())); |
| 34 } |
| 35 |
| 36 void TearDownOnMainThread() override { cache_util_.reset(); } |
| 37 |
| 38 void CountCallback(int64_t size) { |
| 39 // Negative values represent an unexpected error. |
| 40 DCHECK(size >= 0 || size == net::ERR_ABORTED); |
| 41 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 42 last_size_ = size; |
| 43 |
| 44 if (run_loop_) |
| 45 run_loop_->Quit(); |
| 46 } |
| 47 |
| 48 void WaitForTasksOnIOThread() { |
| 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 50 run_loop_.reset(new base::RunLoop()); |
| 51 run_loop_->Run(); |
| 52 } |
| 53 |
| 54 void CountEntries(base::Time begin_time, base::Time end_time) { |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 56 last_size_ = -1; |
| 57 auto helper = ConditionalCacheCountingHelper::CreateForRange( |
| 58 cache_util_->partition(), begin_time, end_time); |
| 59 helper->CountAndDestroySelfWhenFinished(count_callback_); |
| 60 } |
| 61 |
| 62 int64_t GetResult() { |
| 63 DCHECK_GT(last_size_, 0); |
| 64 return last_size_; |
| 65 } |
| 66 |
| 67 int64_t GetResultOrError() { return last_size_; } |
| 68 |
| 69 CacheTestUtil* GetCacheTestUtil() { return cache_util_.get(); } |
| 70 |
| 71 private: |
| 72 ConditionalCacheCountingHelper::CacheCountCallback count_callback_; |
| 73 std::unique_ptr<base::RunLoop> run_loop_; |
| 74 std::unique_ptr<CacheTestUtil> cache_util_; |
| 75 |
| 76 int64_t last_size_; |
| 77 }; |
| 78 |
| 79 // Tests that ConditionalCacheCountingHelper only counts those cache entries |
| 80 // that match the condition. |
| 81 IN_PROC_BROWSER_TEST_F(ConditionalCacheCountingHelperBrowserTest, Count) { |
| 82 // Create 5 entries. |
| 83 std::set<std::string> keys1 = {"1", "2", "3", "4", "5"}; |
| 84 |
| 85 base::Time t1 = base::Time::Now(); |
| 86 GetCacheTestUtil()->CreateCacheEntries(keys1); |
| 87 |
| 88 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); |
| 89 // base::Time t2 = base::Time::Now(); |
| 90 |
| 91 std::set<std::string> keys2 = {"6", "7"}; |
| 92 GetCacheTestUtil()->CreateCacheEntries(keys2); |
| 93 |
| 94 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); |
| 95 base::Time t3 = base::Time::Now(); |
| 96 |
| 97 // TODO(dullweber): Add test for time ranges when GetEntrySize() is done. |
| 98 |
| 99 // Count all entries. |
| 100 CountEntries(t1, t3); |
| 101 WaitForTasksOnIOThread(); |
| 102 int64_t size_1_3 = GetResult(); |
| 103 |
| 104 // Count everything |
| 105 CountEntries(base::Time(), base::Time::Max()); |
| 106 WaitForTasksOnIOThread(); |
| 107 EXPECT_EQ(size_1_3, GetResult()); |
| 108 } |
OLD | NEW |