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