Chromium Code Reviews| Index: components/precache/content/precache_manager_unittest.cc |
| diff --git a/components/precache/content/precache_manager_unittest.cc b/components/precache/content/precache_manager_unittest.cc |
| index b35d1c0c3a30401e6dd4af75f8b5c2b0de11333c..8ff3e7affc996c538e7e654a9a3dd05d2db39086 100644 |
| --- a/components/precache/content/precache_manager_unittest.cc |
| +++ b/components/precache/content/precache_manager_unittest.cc |
| @@ -29,11 +29,15 @@ |
| #include "components/precache/core/precache_switches.h" |
| #include "components/precache/core/proto/unfinished_work.pb.h" |
| #include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/storage_partition.h" |
| #include "content/public/test/test_browser_context.h" |
| #include "content/public/test/test_browser_thread_bundle.h" |
| +#include "net/base/test_completion_callback.h" |
| +#include "net/disk_cache/simple/simple_backend_impl.h" |
| #include "net/http/http_response_headers.h" |
| #include "net/http/http_response_info.h" |
| #include "net/http/http_status_code.h" |
| +#include "net/test/gtest_util.h" |
| #include "net/url_request/test_url_fetcher_factory.h" |
| #include "net/url_request/url_request_status.h" |
| #include "net/url_request/url_request_test_util.h" |
| @@ -192,6 +196,9 @@ class PrecacheManagerTest : public testing::Test { |
| &history_service_, db_path, std::move(precache_database))); |
| base::RunLoop().RunUntilIdle(); |
| + // Allow caches of any size to be used. |
| + precache_manager_->min_cache_size_bytes_ = 0; |
| + |
| info_.headers = new net::HttpResponseHeaders(""); |
| } |
| @@ -249,6 +256,81 @@ TEST_F(PrecacheManagerTest, StartAndFinishPrecaching) { |
| EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| } |
| +TEST_F(PrecacheManagerTest, StartPrecachingWithGoodSizedCache) { |
| + precache_manager_->min_cache_size_bytes_ = 1; |
| + |
| + // Let's store something in the cache so we pass the min_cache_size_bytes_ |
| + // threshold. |
| + disk_cache::Backend* cache_backend; |
| + { |
| + // Get the CacheBackend. |
| + net::TestCompletionCallback cb; |
| + net::HttpCache* cache = |
| + content::BrowserContext::GetDefaultStoragePartition(&browser_context_) |
| + ->GetURLRequestContext() |
| + ->GetURLRequestContext() |
| + ->http_transaction_factory() |
| + ->GetCache(); |
| + ASSERT_NE(nullptr, cache); |
| + int rv = cache->GetBackend(&cache_backend, cb.callback()); |
| + ASSERT_THAT(cb.GetResult(rv), net::test::IsOk()); |
| + ASSERT_NE(nullptr, cache_backend); |
| + ASSERT_EQ(cache_backend, cache->GetCurrentBackend()); |
|
twifkak
2016/11/17 21:52:04
(nit/opt) These asserts just verify that the test
jamartin
2016/11/18 03:55:32
Done.
|
| + } |
| + disk_cache::Entry* entry = nullptr; |
| + { |
| + // Create a cache Entry. |
| + net::TestCompletionCallback cb; |
| + int rv = |
| + content::BrowserContext::GetDefaultStoragePartition(&browser_context_) |
| + ->GetURLRequestContext() |
| + ->GetURLRequestContext() |
| + ->http_transaction_factory() |
| + ->GetCache() |
| + ->GetCurrentBackend() |
|
twifkak
2016/11/17 21:52:04
Use cache_backend from above?
jamartin
2016/11/18 03:55:32
Done.
|
| + ->CreateEntry("key", &entry, cb.callback()); |
| + ASSERT_THAT(cb.GetResult(rv), net::test::IsOk()); |
| + ASSERT_NE(nullptr, entry); |
| + } |
| + { |
| + // Store some data in the cache Entry. |
| + const std::string data(precache_manager_->min_cache_size_bytes_, 'a'); |
| + scoped_refptr<net::StringIOBuffer> buffer(new net::StringIOBuffer(data)); |
| + net::TestCompletionCallback cb; |
| + int rv = entry->WriteData(0, 0, buffer.get(), buffer->size(), cb.callback(), |
| + false); |
| + entry->Close(); |
| + ASSERT_EQ(buffer->size(), cb.GetResult(rv)); |
| + } |
| + { |
| + // Make sure everything went according to plan. |
| + net::TestCompletionCallback cb; |
| + int rv = cache_backend->CalculateSizeOfAllEntries(cb.callback()); |
| + ASSERT_LE(precache_manager_->min_cache_size_bytes_, cb.GetResult(rv)); |
| + } |
| + EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| + |
| + precache_manager_->StartPrecaching(precache_callback_.GetCallback()); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_TRUE(precache_manager_->IsPrecaching()); |
| + // Now it should be waiting for the top hosts. |
| +} |
| + |
| +TEST_F(PrecacheManagerTest, StartPrecachingStopsOnSmallCaches) { |
| + // We don't have any entry in the cache, so the reported cache_size = 0 and |
| + // thus it will fall below the threshold of 1. |
| + precache_manager_->min_cache_size_bytes_ = 1; |
| + EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| + |
| + precache_manager_->StartPrecaching(precache_callback_.GetCallback()); |
| + base::RunLoop().RunUntilIdle(); |
| + |
| + EXPECT_FALSE(precache_manager_->IsPrecaching()); |
| + EXPECT_TRUE(precache_callback_.was_on_done_called()); |
| + EXPECT_TRUE(url_callback_.requested_urls().empty()); |
| +} |
| + |
| TEST_F(PrecacheManagerTest, StartAndFinishPrecachingWithUnfinishedHosts) { |
| std::unique_ptr<PrecacheUnfinishedWork> unfinished_work( |
| new PrecacheUnfinishedWork()); |