Chromium Code Reviews| 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 "base/run_loop.h" | |
| 6 #include "chrome/browser/profiles/profile.h" | |
| 7 #include "chrome/browser/ui/browser.h" | |
| 8 #include "chrome/test/base/in_process_browser_test.h" | |
| 9 #include "components/browsing_data/storage_partition_http_cache_data_remover.h" | |
| 10 #include "content/public/browser/browser_context.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "content/public/browser/storage_partition.h" | |
| 13 #include "net/disk_cache/disk_cache.h" | |
| 14 #include "net/http/http_cache.h" | |
| 15 #include "net/url_request/url_request_context.h" | |
| 16 #include "net/url_request/url_request_context_getter.h" | |
| 17 | |
| 18 using browsing_data::StoragePartitionHttpCacheDataRemover; | |
| 19 using content::BrowserThread; | |
| 20 | |
| 21 class StoragePartitionHttpCacheDataRemoverBrowserTest | |
| 22 : public InProcessBrowserTest { | |
| 23 public: | |
| 24 // Initialization ------------------------------------------------------------ | |
| 25 | |
| 26 void SetUpOnMainThread() override { | |
| 27 result_callback_ = base::Bind( | |
| 28 &StoragePartitionHttpCacheDataRemoverBrowserTest::ResultCallback, | |
| 29 base::Unretained(this)); | |
| 30 done_callback_ = base::Bind( | |
| 31 &StoragePartitionHttpCacheDataRemoverBrowserTest::DoneCallback, | |
| 32 base::Unretained(this)); | |
| 33 remaining_tasks_ = 0; | |
| 34 | |
| 35 partition_ = content::BrowserContext::GetDefaultStoragePartition( | |
| 36 browser()->profile()); | |
| 37 | |
| 38 // Get the cache backends. | |
| 39 BrowserThread::PostTask( | |
| 40 BrowserThread::IO, FROM_HERE, | |
| 41 base::Bind(&StoragePartitionHttpCacheDataRemoverBrowserTest:: | |
| 42 InitializeCountingTest, | |
| 43 base::Unretained(this))); | |
| 44 WaitForTasksOnIOThread(); | |
| 45 DCHECK(main_backend_); | |
| 46 DCHECK(media_backend_); | |
| 47 } | |
| 48 | |
| 49 void InitializeCountingTest() { | |
| 50 net::URLRequestContextGetter* main_context; | |
| 51 net::URLRequestContextGetter* media_context; | |
| 52 | |
| 53 main_context = partition_->GetURLRequestContext(); | |
| 54 media_context = partition_->GetMediaURLRequestContext(); | |
| 55 | |
| 56 net::HttpCache* main_cache = main_context->GetURLRequestContext()-> | |
| 57 http_transaction_factory()->GetCache(); | |
| 58 net::HttpCache* media_cache = media_context->GetURLRequestContext()-> | |
| 59 http_transaction_factory()->GetCache(); | |
| 60 | |
| 61 SetNumberOfWaitedTasks(2); | |
| 62 WaitForCompletion(main_cache->GetBackend(&main_backend_, done_callback_)); | |
| 63 WaitForCompletion(media_cache->GetBackend(&media_backend_, done_callback_)); | |
| 64 } | |
| 65 | |
| 66 // Get a new remover instance. | |
| 67 StoragePartitionHttpCacheDataRemover* GetNewRemover() { | |
| 68 return StoragePartitionHttpCacheDataRemover::CreateForRange( | |
| 69 partition_, base::Time(), base::Time::Now()); | |
| 70 } | |
| 71 | |
| 72 // Waiting for the calculation and retrieving results on the UI thread. ------ | |
| 73 | |
| 74 void WaitForResult() { | |
| 75 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 76 loop_.reset(new base::RunLoop()); | |
| 77 loop_->Run(); | |
| 78 } | |
| 79 | |
| 80 void ResultCallback(int64 result) { | |
| 81 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 82 if (loop_) | |
| 83 loop_->Quit(); | |
| 84 result_ = result; | |
| 85 } | |
| 86 | |
| 87 net::Int64CompletionCallback GetResultCallback() { | |
| 88 return result_callback_; | |
| 89 } | |
| 90 | |
| 91 int64 GetResult() { | |
| 92 return result_; | |
| 93 } | |
| 94 | |
| 95 // Waiting for tasks to be done on IO thread. -------------------------------- | |
| 96 | |
| 97 void WaitForTasksOnIOThread() { | |
| 98 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 99 io_thread_loop_.reset(new base::RunLoop()); | |
| 100 io_thread_loop_->Run(); | |
| 101 } | |
| 102 | |
| 103 void SetNumberOfWaitedTasks(int count) { | |
| 104 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 105 remaining_tasks_ = count; | |
| 106 } | |
| 107 | |
| 108 void WaitForCompletion(int value) { | |
| 109 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 110 if (value > 0) { | |
|
msramek
2015/09/02 19:22:25
Fix: >= 0. Otherwise we would disregard net::OK ==
| |
| 111 // We got the result immediately. | |
| 112 DoneCallback(value); | |
| 113 } else if (value == net::ERR_IO_PENDING) { | |
| 114 // We need to wait for the callback. | |
| 115 } else { | |
| 116 // An error has occurred. | |
| 117 NOTREACHED(); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 void DoneCallback(int value) { | |
| 122 DCHECK_GE(value, 0); // Negative values represent error codes. | |
| 123 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 124 if (--remaining_tasks_ > 0) | |
| 125 return; | |
| 126 | |
| 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 128 BrowserThread::PostTask( | |
| 129 BrowserThread::UI, FROM_HERE, | |
| 130 base::Bind(&base::RunLoop::Quit, | |
| 131 base::Unretained(io_thread_loop_.get()))); | |
| 132 } | |
| 133 | |
| 134 // StoragePartitionHttpCacheDataRemoverBrowserTest.Counting helpers. --------- | |
| 135 | |
| 136 void CreateCacheEntries() { | |
| 137 entries_.resize(5); | |
| 138 | |
| 139 SetNumberOfWaitedTasks(5); | |
| 140 WaitForCompletion(main_backend_->CreateEntry( | |
| 141 "a", &entries_[0], done_callback_)); | |
| 142 WaitForCompletion(main_backend_->CreateEntry( | |
| 143 "b", &entries_[1], done_callback_)); | |
| 144 WaitForCompletion(main_backend_->CreateEntry( | |
| 145 "c", &entries_[2], done_callback_)); | |
| 146 WaitForCompletion(media_backend_->CreateEntry( | |
| 147 "d", &entries_[3], done_callback_)); | |
| 148 WaitForCompletion(media_backend_->CreateEntry( | |
| 149 "e", &entries_[4], done_callback_)); | |
| 150 } | |
| 151 | |
| 152 void WriteDataToEntries() { | |
| 153 std::string data("Lorem ipsum dolor sit amet"); | |
| 154 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); | |
| 155 | |
| 156 SetNumberOfWaitedTasks(5); | |
| 157 entries_.resize(5); | |
| 158 WaitForCompletion( | |
| 159 entries_[0]->WriteData(0, 0, buffer.get(), 10, done_callback_, false)); | |
| 160 WaitForCompletion( | |
| 161 entries_[1]->WriteData(0, 0, buffer.get(), 3, done_callback_, false)); | |
| 162 WaitForCompletion( | |
| 163 entries_[2]->WriteData(1, 0, buffer.get(), 4, done_callback_, false)); | |
| 164 WaitForCompletion( | |
| 165 entries_[3]->WriteData(0, 0, buffer.get(), 1, done_callback_, false)); | |
| 166 WaitForCompletion( | |
| 167 entries_[4]->WriteData(1, 0, buffer.get(), 5, done_callback_, false)); | |
| 168 } | |
| 169 | |
| 170 private: | |
| 171 content::StoragePartition* partition_; | |
| 172 disk_cache::Backend* main_backend_ = nullptr; | |
| 173 disk_cache::Backend* media_backend_ = nullptr; | |
| 174 | |
| 175 // Shorthands for callback binding. | |
| 176 base::Callback<void(int)> done_callback_; | |
| 177 net::Int64CompletionCallback result_callback_; | |
| 178 | |
| 179 int64 result_; | |
| 180 scoped_ptr<base::RunLoop> loop_; | |
| 181 scoped_ptr<base::RunLoop> io_thread_loop_; | |
| 182 | |
| 183 std::vector<disk_cache::Entry*> entries_; | |
| 184 int remaining_tasks_; | |
| 185 }; | |
| 186 | |
| 187 // Test that StoragePartitionHttpCacheDataRemover correctly counts the total | |
| 188 // size of items in the main and media cache. | |
| 189 IN_PROC_BROWSER_TEST_F( | |
| 190 StoragePartitionHttpCacheDataRemoverBrowserTest, Counting) { | |
| 191 // Calculate the size of the empty caches. The result should be 0. | |
| 192 GetNewRemover()->Count(GetResultCallback()); | |
| 193 WaitForResult(); | |
| 194 EXPECT_EQ(0, GetResult()); | |
| 195 | |
| 196 // Write 17 bytes of data to the first cache, 6 bytes to the other one. | |
|
Mike West
2015/09/02 11:50:13
This seems somewhat arbitrary. Why 23? And why 17
msramek
2015/09/02 19:22:25
Well, it is arbitrary :) I didn't want to parametr
| |
| 197 BrowserThread::PostTask( | |
| 198 BrowserThread::IO, FROM_HERE, | |
| 199 base::Bind(&StoragePartitionHttpCacheDataRemoverBrowserTest:: | |
| 200 CreateCacheEntries, | |
| 201 base::Unretained(this))); | |
| 202 WaitForTasksOnIOThread(); | |
| 203 BrowserThread::PostTask( | |
| 204 BrowserThread::IO, FROM_HERE, | |
| 205 base::Bind(&StoragePartitionHttpCacheDataRemoverBrowserTest:: | |
| 206 WriteDataToEntries, | |
| 207 base::Unretained(this))); | |
| 208 WaitForTasksOnIOThread(); | |
| 209 | |
| 210 // Calculate the size of the caches now. The result should be 23. | |
| 211 // TODO(msramek): Change the test to "EXPECT_EQ(23, GetResult());" as soon as | |
| 212 // the size calculation is implemented for memory and simple cache backends. | |
| 213 GetNewRemover()->Count(GetResultCallback()); | |
| 214 WaitForResult(); | |
| 215 LOG(ERROR) << GetResult(); | |
| 216 EXPECT_TRUE(GetResult() == 23 || GetResult() == net::ERR_NOT_IMPLEMENTED); | |
|
Mike West
2015/09/02 11:50:13
Can you add a test which clears the cache (or writ
msramek
2015/09/02 19:22:25
Done. A lot has changed in this file.
- "Populate
| |
| 217 } | |
| OLD | NEW |