OLD | NEW |
---|---|
(Empty) | |
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 | |
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 // Initialization ------------------------------------------------------------ | |
25 | |
26 const int64_t kTimeoutMs = 10; | |
27 | |
28 void SetUpOnMainThread() override { | |
29 count_callback_ = | |
30 base::Bind(&ConditionalCacheCountingHelperBrowserTest::CountCallback, | |
31 base::Unretained(this)); | |
32 | |
33 cache_util_ = base::MakeUnique<CacheTestUtil>( | |
34 content::BrowserContext::GetDefaultStoragePartition( | |
35 browser()->profile())); | |
36 cache_util_->SetUpOnMainThread(); | |
37 } | |
38 | |
39 void TearDownOnMainThread() override { cache_util_->TearDownOnMainThread(); } | |
40 | |
41 // Counting helpers. --------------------------------------------------------- | |
msramek
2016/12/20 01:03:00
nit: The section delimiter is not needed now that
dullweber
2016/12/21 10:29:19
Done.
| |
42 | |
43 void CountCallback(int64_t size) { | |
44 // Negative values represent an unexpected error. | |
45 DCHECK(size >= 0 || size == net::ERR_ABORTED); | |
46 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
47 last_size_ = size; | |
48 | |
49 if (run_loop_) | |
50 run_loop_->Quit(); | |
51 } | |
52 | |
53 void WaitForTasksOnIOThread() { | |
54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
55 run_loop_.reset(new base::RunLoop()); | |
56 run_loop_->Run(); | |
57 } | |
58 | |
59 void CountEntries(base::Time begin_time, base::Time end_time) { | |
60 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
61 last_size_ = -1; | |
62 auto helper = ConditionalCacheCountingHelper::CreateForRange( | |
63 cache_util_->partition(), begin_time, end_time); | |
64 helper->CountAndDestroySelfWhenFinished(count_callback_); | |
65 } | |
66 | |
67 void CountEntriesAndCancel(base::Time begin_time, base::Time end_time) { | |
68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
69 last_size_ = -1; | |
70 auto helper = ConditionalCacheCountingHelper::CreateForRange( | |
71 cache_util_->partition(), begin_time, end_time); | |
72 // Cancel first to make sure we don't cancel after the IO thread finishes. | |
73 helper->CancelCounting(); | |
74 helper->CountAndDestroySelfWhenFinished(count_callback_); | |
75 } | |
76 | |
77 int64_t GetResult() { | |
78 DCHECK_GT(last_size_, 0); | |
79 return last_size_; | |
80 } | |
81 | |
82 int64_t GetResultOrError() { return last_size_; } | |
83 | |
84 protected: | |
85 std::unique_ptr<CacheTestUtil> cache_util_; | |
msramek
2016/12/20 01:02:59
It's unusual to inherit member variables. The nami
dullweber
2016/12/21 10:29:19
Done.
| |
86 | |
87 private: | |
88 ConditionalCacheCountingHelper::CacheCountCallback count_callback_; | |
89 std::unique_ptr<base::RunLoop> run_loop_; | |
90 | |
91 int64_t last_size_; | |
92 }; | |
93 | |
94 // Tests that ConditionalCacheCountingHelper only counts those cache entries | |
95 // that match the condition. | |
96 IN_PROC_BROWSER_TEST_F(ConditionalCacheCountingHelperBrowserTest, Count) { | |
97 // Create 5 entries. | |
98 std::set<std::string> keys1 = {"1", "2", "3", "4", "5"}; | |
99 | |
100 base::Time t1 = base::Time::Now(); | |
101 cache_util_->CreateCacheEntries(keys1); | |
102 | |
103 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | |
104 base::Time t2 = base::Time::Now(); | |
105 | |
106 std::set<std::string> keys2 = {"6", "7"}; | |
107 cache_util_->CreateCacheEntries(keys2); | |
108 | |
109 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | |
110 base::Time t3 = base::Time::Now(); | |
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 // Count all entries. | |
123 CountEntries(t1, t3); | |
124 WaitForTasksOnIOThread(); | |
125 int64_t size_1_3 = GetResult(); | |
126 EXPECT_EQ(size_1_2 + size_2_3, size_1_3); | |
127 | |
128 // Count everything | |
129 CountEntries(base::Time(), base::Time::Max()); | |
130 WaitForTasksOnIOThread(); | |
131 EXPECT_EQ(size_1_3, GetResult()); | |
132 } | |
133 | |
134 // Tests that ConditionalCacheCountingHelper returns net::ERR_ABORTED when | |
135 // cancelled. | |
136 IN_PROC_BROWSER_TEST_F(ConditionalCacheCountingHelperBrowserTest, | |
137 CancelCounting) { | |
138 std::set<std::string> keys = {"1", "2", "3", "4", "5"}; | |
139 base::Time t1 = base::Time::Now(); | |
140 cache_util_->CreateCacheEntries(keys); | |
141 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(kTimeoutMs)); | |
142 | |
143 // Count and cancel | |
144 CountEntriesAndCancel(t1, base::Time::Now()); | |
145 WaitForTasksOnIOThread(); | |
146 EXPECT_EQ(net::ERR_ABORTED, GetResultOrError()); | |
147 } | |
OLD | NEW |