| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "components/precache/core/precache_fetcher.h" | 5 #include "components/precache/core/precache_fetcher.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/compiler_specific.h" | 15 #include "base/compiler_specific.h" |
| 16 #include "base/test/histogram_tester.h" |
| 16 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.h" |
| 17 #include "components/precache/core/precache_switches.h" | 18 #include "components/precache/core/precache_switches.h" |
| 18 #include "components/precache/core/proto/precache.pb.h" | 19 #include "components/precache/core/proto/precache.pb.h" |
| 19 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
| 20 #include "net/http/http_status_code.h" | 21 #include "net/http/http_status_code.h" |
| 21 #include "net/url_request/test_url_fetcher_factory.h" | 22 #include "net/url_request/test_url_fetcher_factory.h" |
| 22 #include "net/url_request/url_request_status.h" | 23 #include "net/url_request/url_request_status.h" |
| 23 #include "net/url_request/url_request_test_util.h" | 24 #include "net/url_request/url_request_test_util.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 26 |
| 26 namespace precache { | 27 namespace precache { |
| 27 | 28 |
| 28 namespace { | 29 namespace { |
| 29 | 30 |
| 30 class TestURLFetcherCallback { | 31 class TestURLFetcherCallback { |
| 31 public: | 32 public: |
| 33 TestURLFetcherCallback() : total_response_bytes_(0) {} |
| 34 |
| 32 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( | 35 scoped_ptr<net::FakeURLFetcher> CreateURLFetcher( |
| 33 const GURL& url, net::URLFetcherDelegate* delegate, | 36 const GURL& url, net::URLFetcherDelegate* delegate, |
| 34 const std::string& response_data, net::HttpStatusCode response_code, | 37 const std::string& response_data, net::HttpStatusCode response_code, |
| 35 net::URLRequestStatus::Status status) { | 38 net::URLRequestStatus::Status status) { |
| 36 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( | 39 scoped_ptr<net::FakeURLFetcher> fetcher(new net::FakeURLFetcher( |
| 37 url, delegate, response_data, response_code, status)); | 40 url, delegate, response_data, response_code, status)); |
| 38 | 41 |
| 39 if (response_code == net::HTTP_OK) { | 42 if (response_code == net::HTTP_OK) { |
| 40 scoped_refptr<net::HttpResponseHeaders> download_headers = | 43 scoped_refptr<net::HttpResponseHeaders> download_headers = |
| 41 new net::HttpResponseHeaders(""); | 44 new net::HttpResponseHeaders(""); |
| 42 download_headers->AddHeader("Content-Type: text/html"); | 45 download_headers->AddHeader("Content-Type: text/html"); |
| 43 fetcher->set_response_headers(download_headers); | 46 fetcher->set_response_headers(download_headers); |
| 44 } | 47 } |
| 45 | 48 |
| 49 total_response_bytes_ += response_data.size(); |
| 46 requested_urls_.insert(url); | 50 requested_urls_.insert(url); |
| 47 return fetcher.Pass(); | 51 return fetcher.Pass(); |
| 48 } | 52 } |
| 49 | 53 |
| 50 const std::multiset<GURL>& requested_urls() const { | 54 const std::multiset<GURL>& requested_urls() const { |
| 51 return requested_urls_; | 55 return requested_urls_; |
| 52 } | 56 } |
| 53 | 57 |
| 58 int total_response_bytes() const { return total_response_bytes_; } |
| 59 |
| 54 private: | 60 private: |
| 55 // Multiset with one entry for each URL requested. | 61 // Multiset with one entry for each URL requested. |
| 56 std::multiset<GURL> requested_urls_; | 62 std::multiset<GURL> requested_urls_; |
| 63 int total_response_bytes_; |
| 57 }; | 64 }; |
| 58 | 65 |
| 59 class TestPrecacheDelegate : public PrecacheFetcher::PrecacheDelegate { | 66 class TestPrecacheDelegate : public PrecacheFetcher::PrecacheDelegate { |
| 60 public: | 67 public: |
| 61 TestPrecacheDelegate() : was_on_done_called_(false) {} | 68 TestPrecacheDelegate() : was_on_done_called_(false) {} |
| 62 | 69 |
| 63 void OnDone() override { was_on_done_called_ = true; } | 70 void OnDone() override { was_on_done_called_ = true; } |
| 64 | 71 |
| 65 bool was_on_done_called() const { | 72 bool was_on_done_called() const { |
| 66 return was_on_done_called_; | 73 return was_on_done_called_; |
| 67 } | 74 } |
| 68 | 75 |
| 69 private: | 76 private: |
| 70 bool was_on_done_called_; | 77 bool was_on_done_called_; |
| 71 }; | 78 }; |
| 72 | 79 |
| 73 class PrecacheFetcherTest : public testing::Test { | 80 class PrecacheFetcherTest : public testing::Test { |
| 74 public: | 81 public: |
| 75 PrecacheFetcherTest() | 82 PrecacheFetcherTest() |
| 76 : request_context_(new net::TestURLRequestContextGetter( | 83 : request_context_(new net::TestURLRequestContextGetter( |
| 77 base::ThreadTaskRunnerHandle::Get())), | 84 base::ThreadTaskRunnerHandle::Get())), |
| 78 factory_(NULL, | 85 factory_(NULL, |
| 79 base::Bind(&TestURLFetcherCallback::CreateURLFetcher, | 86 base::Bind(&TestURLFetcherCallback::CreateURLFetcher, |
| 80 base::Unretained(&url_callback_))) {} | 87 base::Unretained(&url_callback_))), |
| 88 expected_total_response_bytes_(0) {} |
| 81 | 89 |
| 82 protected: | 90 protected: |
| 83 base::MessageLoopForUI loop_; | 91 base::MessageLoopForUI loop_; |
| 84 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | 92 scoped_refptr<net::TestURLRequestContextGetter> request_context_; |
| 85 TestURLFetcherCallback url_callback_; | 93 TestURLFetcherCallback url_callback_; |
| 86 net::FakeURLFetcherFactory factory_; | 94 net::FakeURLFetcherFactory factory_; |
| 87 TestPrecacheDelegate precache_delegate_; | 95 TestPrecacheDelegate precache_delegate_; |
| 96 int expected_total_response_bytes_; |
| 88 }; | 97 }; |
| 89 | 98 |
| 90 const char kConfigURL[] = "http://config-url.com"; | 99 const char kConfigURL[] = "http://config-url.com"; |
| 91 const char kManifestURLPrefix[] = "http://manifest-url-prefix.com/"; | 100 const char kManifestURLPrefix[] = "http://manifest-url-prefix.com/"; |
| 92 const char kCustomManifestURLPrefix[] = | 101 const char kCustomManifestURLPrefix[] = |
| 93 "http://custom-manifest-url-prefix.com/"; | 102 "http://custom-manifest-url-prefix.com/"; |
| 94 const char kManifestFetchFailureURL[] = | 103 const char kManifestFetchFailureURL[] = |
| 95 "http://manifest-url-prefix.com/manifest-fetch-failure.com"; | 104 "http://manifest-url-prefix.com/manifest-fetch-failure.com"; |
| 96 const char kBadManifestURL[] = | 105 const char kBadManifestURL[] = |
| 97 "http://manifest-url-prefix.com/bad-manifest.com"; | 106 "http://manifest-url-prefix.com/bad-manifest.com"; |
| 98 const char kGoodManifestURL[] = | 107 const char kGoodManifestURL[] = |
| 99 "http://manifest-url-prefix.com/good-manifest.com"; | 108 "http://manifest-url-prefix.com/good-manifest.com"; |
| 100 const char kCustomGoodManifestURL[] = | 109 const char kCustomGoodManifestURL[] = |
| 101 "http://custom-manifest-url-prefix.com/good-manifest.com"; | 110 "http://custom-manifest-url-prefix.com/good-manifest.com"; |
| 102 const char kResourceFetchFailureURL[] = "http://resource-fetch-failure.com"; | 111 const char kResourceFetchFailureURL[] = "http://resource-fetch-failure.com"; |
| 103 const char kGoodResourceURL[] = "http://good-resource.com"; | 112 const char kGoodResourceURL[] = "http://good-resource.com"; |
| 104 const char kForcedStartingURLManifestURL[] = | 113 const char kForcedStartingURLManifestURL[] = |
| 105 "http://manifest-url-prefix.com/forced-starting-url.com"; | 114 "http://manifest-url-prefix.com/forced-starting-url.com"; |
| 106 | 115 |
| 116 // TODO(twifkak): Test UMA metrics for full and cancelled runs. |
| 117 |
| 107 TEST_F(PrecacheFetcherTest, FullPrecache) { | 118 TEST_F(PrecacheFetcherTest, FullPrecache) { |
| 108 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 119 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 109 switches::kPrecacheConfigSettingsURL, kConfigURL); | 120 switches::kPrecacheConfigSettingsURL, kConfigURL); |
| 110 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 121 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 111 switches::kPrecacheManifestURLPrefix, kManifestURLPrefix); | 122 switches::kPrecacheManifestURLPrefix, kManifestURLPrefix); |
| 112 | 123 |
| 113 std::vector<std::string> starting_hosts; | 124 std::vector<std::string> starting_hosts; |
| 114 starting_hosts.push_back("manifest-fetch-failure.com"); | 125 starting_hosts.push_back("manifest-fetch-failure.com"); |
| 115 starting_hosts.push_back("bad-manifest.com"); | 126 starting_hosts.push_back("bad-manifest.com"); |
| 116 starting_hosts.push_back("good-manifest.com"); | 127 starting_hosts.push_back("good-manifest.com"); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 139 net::URLRequestStatus::SUCCESS); | 150 net::URLRequestStatus::SUCCESS); |
| 140 factory_.SetFakeResponse(GURL(kResourceFetchFailureURL), | 151 factory_.SetFakeResponse(GURL(kResourceFetchFailureURL), |
| 141 "", net::HTTP_INTERNAL_SERVER_ERROR, | 152 "", net::HTTP_INTERNAL_SERVER_ERROR, |
| 142 net::URLRequestStatus::FAILED); | 153 net::URLRequestStatus::FAILED); |
| 143 factory_.SetFakeResponse(GURL(kGoodResourceURL), "good", net::HTTP_OK, | 154 factory_.SetFakeResponse(GURL(kGoodResourceURL), "good", net::HTTP_OK, |
| 144 net::URLRequestStatus::SUCCESS); | 155 net::URLRequestStatus::SUCCESS); |
| 145 factory_.SetFakeResponse(GURL(kForcedStartingURLManifestURL), | 156 factory_.SetFakeResponse(GURL(kForcedStartingURLManifestURL), |
| 146 PrecacheManifest().SerializeAsString(), net::HTTP_OK, | 157 PrecacheManifest().SerializeAsString(), net::HTTP_OK, |
| 147 net::URLRequestStatus::SUCCESS); | 158 net::URLRequestStatus::SUCCESS); |
| 148 | 159 |
| 149 PrecacheFetcher precache_fetcher(starting_hosts, request_context_.get(), | 160 base::HistogramTester histogram; |
| 150 std::string(), &precache_delegate_); | |
| 151 precache_fetcher.Start(); | |
| 152 | 161 |
| 153 base::MessageLoop::current()->RunUntilIdle(); | 162 { |
| 163 PrecacheFetcher precache_fetcher(starting_hosts, request_context_.get(), |
| 164 std::string(), &precache_delegate_); |
| 165 precache_fetcher.Start(); |
| 166 |
| 167 base::MessageLoop::current()->RunUntilIdle(); |
| 168 |
| 169 // Destroy the PrecacheFetcher after it has finished to record metrics. |
| 170 } |
| 154 | 171 |
| 155 std::multiset<GURL> expected_requested_urls; | 172 std::multiset<GURL> expected_requested_urls; |
| 156 expected_requested_urls.insert(GURL(kConfigURL)); | 173 expected_requested_urls.insert(GURL(kConfigURL)); |
| 157 expected_requested_urls.insert(GURL(kManifestFetchFailureURL)); | 174 expected_requested_urls.insert(GURL(kManifestFetchFailureURL)); |
| 158 expected_requested_urls.insert(GURL(kBadManifestURL)); | 175 expected_requested_urls.insert(GURL(kBadManifestURL)); |
| 159 expected_requested_urls.insert(GURL(kGoodManifestURL)); | 176 expected_requested_urls.insert(GURL(kGoodManifestURL)); |
| 160 expected_requested_urls.insert(GURL(kResourceFetchFailureURL)); | 177 expected_requested_urls.insert(GURL(kResourceFetchFailureURL)); |
| 161 expected_requested_urls.insert(GURL(kGoodResourceURL)); | 178 expected_requested_urls.insert(GURL(kGoodResourceURL)); |
| 162 expected_requested_urls.insert(GURL(kForcedStartingURLManifestURL)); | 179 expected_requested_urls.insert(GURL(kForcedStartingURLManifestURL)); |
| 163 | 180 |
| 164 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 181 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| 165 | 182 |
| 166 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 183 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
| 184 |
| 185 histogram.ExpectUniqueSample("Precache.Fetch.PercentCompleted", 100, 1); |
| 186 histogram.ExpectUniqueSample("Precache.Fetch.ResponseBytes", |
| 187 url_callback_.total_response_bytes(), 1); |
| 167 } | 188 } |
| 168 | 189 |
| 169 TEST_F(PrecacheFetcherTest, CustomManifestURLPrefix) { | 190 TEST_F(PrecacheFetcherTest, CustomManifestURLPrefix) { |
| 170 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 191 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 171 switches::kPrecacheConfigSettingsURL, kConfigURL); | 192 switches::kPrecacheConfigSettingsURL, kConfigURL); |
| 172 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( | 193 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 173 switches::kPrecacheManifestURLPrefix, kManifestURLPrefix); | 194 switches::kPrecacheManifestURLPrefix, kManifestURLPrefix); |
| 174 | 195 |
| 175 std::vector<std::string> starting_hosts; | 196 std::vector<std::string> starting_hosts; |
| 176 starting_hosts.push_back("good-manifest.com"); | 197 starting_hosts.push_back("good-manifest.com"); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 switches::kPrecacheConfigSettingsURL, kConfigURL); | 286 switches::kPrecacheConfigSettingsURL, kConfigURL); |
| 266 | 287 |
| 267 std::vector<std::string> starting_hosts(1, "starting-url.com"); | 288 std::vector<std::string> starting_hosts(1, "starting-url.com"); |
| 268 | 289 |
| 269 PrecacheConfigurationSettings config; | 290 PrecacheConfigurationSettings config; |
| 270 config.set_top_sites_count(1); | 291 config.set_top_sites_count(1); |
| 271 | 292 |
| 272 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), | 293 factory_.SetFakeResponse(GURL(kConfigURL), config.SerializeAsString(), |
| 273 net::HTTP_OK, net::URLRequestStatus::SUCCESS); | 294 net::HTTP_OK, net::URLRequestStatus::SUCCESS); |
| 274 | 295 |
| 296 base::HistogramTester histogram; |
| 297 |
| 275 scoped_ptr<PrecacheFetcher> precache_fetcher( | 298 scoped_ptr<PrecacheFetcher> precache_fetcher( |
| 276 new PrecacheFetcher(starting_hosts, request_context_.get(), std::string(), | 299 new PrecacheFetcher(starting_hosts, request_context_.get(), std::string(), |
| 277 &precache_delegate_)); | 300 &precache_delegate_)); |
| 278 precache_fetcher->Start(); | 301 precache_fetcher->Start(); |
| 279 | 302 |
| 280 // Destroy the PrecacheFetcher to cancel precaching. This should not cause | 303 // Destroy the PrecacheFetcher to cancel precaching and record metrics. This |
| 281 // OnDone to be called on the precache delegate. | 304 // should not cause OnDone to be called on the precache delegate. |
| 282 precache_fetcher.reset(); | 305 precache_fetcher.reset(); |
| 283 | 306 |
| 284 base::MessageLoop::current()->RunUntilIdle(); | 307 base::MessageLoop::current()->RunUntilIdle(); |
| 285 | 308 |
| 286 std::multiset<GURL> expected_requested_urls; | 309 std::multiset<GURL> expected_requested_urls; |
| 287 expected_requested_urls.insert(GURL(kConfigURL)); | 310 expected_requested_urls.insert(GURL(kConfigURL)); |
| 288 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 311 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| 289 | 312 |
| 290 EXPECT_FALSE(precache_delegate_.was_on_done_called()); | 313 EXPECT_FALSE(precache_delegate_.was_on_done_called()); |
| 314 |
| 315 histogram.ExpectUniqueSample("Precache.Fetch.PercentCompleted", 0, 1); |
| 316 histogram.ExpectUniqueSample("Precache.Fetch.ResponseBytes", 0, 1); |
| 291 } | 317 } |
| 292 | 318 |
| 293 #if defined(PRECACHE_CONFIG_SETTINGS_URL) | 319 #if defined(PRECACHE_CONFIG_SETTINGS_URL) |
| 294 | 320 |
| 295 // If the default precache configuration settings URL is defined, then test that | 321 // If the default precache configuration settings URL is defined, then test that |
| 296 // it works with the PrecacheFetcher. | 322 // it works with the PrecacheFetcher. |
| 297 TEST_F(PrecacheFetcherTest, PrecacheUsingDefaultConfigSettingsURL) { | 323 TEST_F(PrecacheFetcherTest, PrecacheUsingDefaultConfigSettingsURL) { |
| 298 std::vector<std::string> starting_hosts(1, "starting-url.com"); | 324 std::vector<std::string> starting_hosts(1, "starting-url.com"); |
| 299 | 325 |
| 300 PrecacheConfigurationSettings config; | 326 PrecacheConfigurationSettings config; |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); | 377 EXPECT_EQ(expected_requested_urls, url_callback_.requested_urls()); |
| 352 | 378 |
| 353 EXPECT_TRUE(precache_delegate_.was_on_done_called()); | 379 EXPECT_TRUE(precache_delegate_.was_on_done_called()); |
| 354 } | 380 } |
| 355 | 381 |
| 356 #endif // PRECACHE_MANIFEST_URL_PREFIX | 382 #endif // PRECACHE_MANIFEST_URL_PREFIX |
| 357 | 383 |
| 358 } // namespace | 384 } // namespace |
| 359 | 385 |
| 360 } // namespace precache | 386 } // namespace precache |
| OLD | NEW |