Index: chrome/browser/browsing_data/storage_partition_http_cache_data_remover_browsertest.cc |
diff --git a/chrome/browser/browsing_data/storage_partition_http_cache_data_remover_browsertest.cc b/chrome/browser/browsing_data/storage_partition_http_cache_data_remover_browsertest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ff1995cfe941fa67e35dc30fa66dc2522cdd691a |
--- /dev/null |
+++ b/chrome/browser/browsing_data/storage_partition_http_cache_data_remover_browsertest.cc |
@@ -0,0 +1,217 @@ |
+// Copyright (c) 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/run_loop.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/ui/browser.h" |
+#include "chrome/test/base/in_process_browser_test.h" |
+#include "components/browsing_data/storage_partition_http_cache_data_remover.h" |
+#include "content/public/browser/browser_context.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/storage_partition.h" |
+#include "net/disk_cache/disk_cache.h" |
+#include "net/http/http_cache.h" |
+#include "net/url_request/url_request_context.h" |
+#include "net/url_request/url_request_context_getter.h" |
+ |
+using browsing_data::StoragePartitionHttpCacheDataRemover; |
+using content::BrowserThread; |
+ |
+class StoragePartitionHttpCacheDataRemoverBrowserTest |
+ : public InProcessBrowserTest { |
+ public: |
+ // Initialization ------------------------------------------------------------ |
+ |
+ void SetUpOnMainThread() override { |
+ result_callback_ = base::Bind( |
+ &StoragePartitionHttpCacheDataRemoverBrowserTest::ResultCallback, |
+ base::Unretained(this)); |
+ done_callback_ = base::Bind( |
+ &StoragePartitionHttpCacheDataRemoverBrowserTest::DoneCallback, |
+ base::Unretained(this)); |
+ remaining_tasks_ = 0; |
+ |
+ partition_ = content::BrowserContext::GetDefaultStoragePartition( |
+ browser()->profile()); |
+ |
+ // Get the cache backends. |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&StoragePartitionHttpCacheDataRemoverBrowserTest:: |
+ InitializeCountingTest, |
+ base::Unretained(this))); |
+ WaitForTasksOnIOThread(); |
+ DCHECK(main_backend_); |
+ DCHECK(media_backend_); |
+ } |
+ |
+ void InitializeCountingTest() { |
+ net::URLRequestContextGetter* main_context; |
+ net::URLRequestContextGetter* media_context; |
+ |
+ main_context = partition_->GetURLRequestContext(); |
+ media_context = partition_->GetMediaURLRequestContext(); |
+ |
+ net::HttpCache* main_cache = main_context->GetURLRequestContext()-> |
+ http_transaction_factory()->GetCache(); |
+ net::HttpCache* media_cache = media_context->GetURLRequestContext()-> |
+ http_transaction_factory()->GetCache(); |
+ |
+ SetNumberOfWaitedTasks(2); |
+ WaitForCompletion(main_cache->GetBackend(&main_backend_, done_callback_)); |
+ WaitForCompletion(media_cache->GetBackend(&media_backend_, done_callback_)); |
+ } |
+ |
+ // Get a new remover instance. |
+ StoragePartitionHttpCacheDataRemover* GetNewRemover() { |
+ return StoragePartitionHttpCacheDataRemover::CreateForRange( |
+ partition_, base::Time(), base::Time::Now()); |
+ } |
+ |
+ // Waiting for the calculation and retrieving results on the UI thread. ------ |
+ |
+ void WaitForResult() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ loop_.reset(new base::RunLoop()); |
+ loop_->Run(); |
+ } |
+ |
+ void ResultCallback(int64 result) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ if (loop_) |
+ loop_->Quit(); |
+ result_ = result; |
+ } |
+ |
+ net::Int64CompletionCallback GetResultCallback() { |
+ return result_callback_; |
+ } |
+ |
+ int64 GetResult() { |
+ return result_; |
+ } |
+ |
+ // Waiting for tasks to be done on IO thread. -------------------------------- |
+ |
+ void WaitForTasksOnIOThread() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ io_thread_loop_.reset(new base::RunLoop()); |
+ io_thread_loop_->Run(); |
+ } |
+ |
+ void SetNumberOfWaitedTasks(int count) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ remaining_tasks_ = count; |
+ } |
+ |
+ void WaitForCompletion(int value) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ if (value > 0) { |
msramek
2015/09/02 19:22:25
Fix: >= 0. Otherwise we would disregard net::OK ==
|
+ // We got the result immediately. |
+ DoneCallback(value); |
+ } else if (value == net::ERR_IO_PENDING) { |
+ // We need to wait for the callback. |
+ } else { |
+ // An error has occurred. |
+ NOTREACHED(); |
+ } |
+ } |
+ |
+ void DoneCallback(int value) { |
+ DCHECK_GE(value, 0); // Negative values represent error codes. |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ if (--remaining_tasks_ > 0) |
+ return; |
+ |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&base::RunLoop::Quit, |
+ base::Unretained(io_thread_loop_.get()))); |
+ } |
+ |
+ // StoragePartitionHttpCacheDataRemoverBrowserTest.Counting helpers. --------- |
+ |
+ void CreateCacheEntries() { |
+ entries_.resize(5); |
+ |
+ SetNumberOfWaitedTasks(5); |
+ WaitForCompletion(main_backend_->CreateEntry( |
+ "a", &entries_[0], done_callback_)); |
+ WaitForCompletion(main_backend_->CreateEntry( |
+ "b", &entries_[1], done_callback_)); |
+ WaitForCompletion(main_backend_->CreateEntry( |
+ "c", &entries_[2], done_callback_)); |
+ WaitForCompletion(media_backend_->CreateEntry( |
+ "d", &entries_[3], done_callback_)); |
+ WaitForCompletion(media_backend_->CreateEntry( |
+ "e", &entries_[4], done_callback_)); |
+ } |
+ |
+ void WriteDataToEntries() { |
+ std::string data("Lorem ipsum dolor sit amet"); |
+ scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
+ |
+ SetNumberOfWaitedTasks(5); |
+ entries_.resize(5); |
+ WaitForCompletion( |
+ entries_[0]->WriteData(0, 0, buffer.get(), 10, done_callback_, false)); |
+ WaitForCompletion( |
+ entries_[1]->WriteData(0, 0, buffer.get(), 3, done_callback_, false)); |
+ WaitForCompletion( |
+ entries_[2]->WriteData(1, 0, buffer.get(), 4, done_callback_, false)); |
+ WaitForCompletion( |
+ entries_[3]->WriteData(0, 0, buffer.get(), 1, done_callback_, false)); |
+ WaitForCompletion( |
+ entries_[4]->WriteData(1, 0, buffer.get(), 5, done_callback_, false)); |
+ } |
+ |
+ private: |
+ content::StoragePartition* partition_; |
+ disk_cache::Backend* main_backend_ = nullptr; |
+ disk_cache::Backend* media_backend_ = nullptr; |
+ |
+ // Shorthands for callback binding. |
+ base::Callback<void(int)> done_callback_; |
+ net::Int64CompletionCallback result_callback_; |
+ |
+ int64 result_; |
+ scoped_ptr<base::RunLoop> loop_; |
+ scoped_ptr<base::RunLoop> io_thread_loop_; |
+ |
+ std::vector<disk_cache::Entry*> entries_; |
+ int remaining_tasks_; |
+}; |
+ |
+// Test that StoragePartitionHttpCacheDataRemover correctly counts the total |
+// size of items in the main and media cache. |
+IN_PROC_BROWSER_TEST_F( |
+ StoragePartitionHttpCacheDataRemoverBrowserTest, Counting) { |
+ // Calculate the size of the empty caches. The result should be 0. |
+ GetNewRemover()->Count(GetResultCallback()); |
+ WaitForResult(); |
+ EXPECT_EQ(0, GetResult()); |
+ |
+ // 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
|
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&StoragePartitionHttpCacheDataRemoverBrowserTest:: |
+ CreateCacheEntries, |
+ base::Unretained(this))); |
+ WaitForTasksOnIOThread(); |
+ BrowserThread::PostTask( |
+ BrowserThread::IO, FROM_HERE, |
+ base::Bind(&StoragePartitionHttpCacheDataRemoverBrowserTest:: |
+ WriteDataToEntries, |
+ base::Unretained(this))); |
+ WaitForTasksOnIOThread(); |
+ |
+ // Calculate the size of the caches now. The result should be 23. |
+ // TODO(msramek): Change the test to "EXPECT_EQ(23, GetResult());" as soon as |
+ // the size calculation is implemented for memory and simple cache backends. |
+ GetNewRemover()->Count(GetResultCallback()); |
+ WaitForResult(); |
+ LOG(ERROR) << GetResult(); |
+ 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
|
+} |