OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <stddef.h> | |
6 | |
7 #include <set> | |
8 | |
5 #include "base/command_line.h" | 9 #include "base/command_line.h" |
6 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 10 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
7 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 11 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" |
8 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 12 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
9 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
11 #include "chrome/browser/ui/browser_commands.h" | 15 #include "chrome/browser/ui/browser_commands.h" |
12 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 16 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
13 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
14 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
15 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
16 #include "content/public/browser/render_frame_host.h" | 20 #include "net/dns/mock_host_resolver.h" |
17 #include "content/public/browser/render_process_host.h" | |
18 #include "net/test/embedded_test_server/http_request.h" | 21 #include "net/test/embedded_test_server/http_request.h" |
19 #include "net/test/embedded_test_server/http_response.h" | 22 #include "net/test/embedded_test_server/http_response.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
22 | 25 |
23 namespace predictors { | 26 namespace predictors { |
24 | 27 |
25 namespace { | 28 namespace { |
26 | 29 |
27 const char kImageMime[] = "image/png"; | 30 const char kImageMime[] = "image/png"; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
104 std::unique(subresources->begin(), subresources->end(), | 107 std::unique(subresources->begin(), subresources->end(), |
105 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 108 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
106 return x.resource_url == y.resource_url; | 109 return x.resource_url == y.resource_url; |
107 }), | 110 }), |
108 subresources->end()); | 111 subresources->end()); |
109 } | 112 } |
110 | 113 |
111 // Fill a NavigationID with "empty" data that does not trigger | 114 // Fill a NavigationID with "empty" data that does not trigger |
112 // the is_valid DCHECK(). Allows comparing. | 115 // the is_valid DCHECK(). Allows comparing. |
113 void SetValidNavigationID(NavigationID* navigation_id) { | 116 void SetValidNavigationID(NavigationID* navigation_id) { |
114 navigation_id->render_process_id = 0; | 117 navigation_id->session_id = 0; |
115 navigation_id->render_frame_id = 0; | |
116 navigation_id->main_frame_url = GURL("http://127.0.0.1"); | 118 navigation_id->main_frame_url = GURL("http://127.0.0.1"); |
117 } | 119 } |
118 | 120 |
119 void ModifySubresourceForComparison(URLRequestSummary* subresource, | 121 void ModifySubresourceForComparison(URLRequestSummary* subresource, |
120 bool match_navigation_id) { | 122 bool match_navigation_id) { |
121 if (!match_navigation_id) | 123 if (!match_navigation_id) |
122 SetValidNavigationID(&subresource->navigation_id); | 124 SetValidNavigationID(&subresource->navigation_id); |
123 if (subresource->resource_type == content::RESOURCE_TYPE_IMAGE && | 125 if (subresource->resource_type == content::RESOURCE_TYPE_IMAGE && |
124 subresource->priority == net::LOWEST) { | 126 subresource->priority == net::LOWEST) { |
125 // Fuzzy comparison for images because an image priority can be | 127 // Fuzzy comparison for images because an image priority can be |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 url_visit_count_(expected_url_visit_count), | 166 url_visit_count_(expected_url_visit_count), |
165 summary_(expected_summary), | 167 summary_(expected_summary), |
166 match_navigation_id_(match_navigation_id) {} | 168 match_navigation_id_(match_navigation_id) {} |
167 | 169 |
168 // TestObserver: | 170 // TestObserver: |
169 void OnNavigationLearned(size_t url_visit_count, | 171 void OnNavigationLearned(size_t url_visit_count, |
170 const PageRequestSummary& summary) override { | 172 const PageRequestSummary& summary) override { |
171 EXPECT_EQ(url_visit_count, url_visit_count_); | 173 EXPECT_EQ(url_visit_count, url_visit_count_); |
172 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | 174 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); |
173 EXPECT_EQ(summary.initial_url, summary_.initial_url); | 175 EXPECT_EQ(summary.initial_url, summary_.initial_url); |
176 for (const auto& resource : summary.subresource_requests) | |
177 current_navigation_ids_.insert(resource.navigation_id); | |
174 CompareSubresources(summary.subresource_requests, | 178 CompareSubresources(summary.subresource_requests, |
175 summary_.subresource_requests, match_navigation_id_); | 179 summary_.subresource_requests, match_navigation_id_); |
176 run_loop_.Quit(); | 180 run_loop_.Quit(); |
177 } | 181 } |
178 | 182 |
179 void Wait() { run_loop_.Run(); } | 183 void Wait() { run_loop_.Run(); } |
180 | 184 |
185 std::set<NavigationID>& current_navigation_ids() { | |
186 return current_navigation_ids_; | |
187 } | |
188 | |
181 private: | 189 private: |
182 base::RunLoop run_loop_; | 190 base::RunLoop run_loop_; |
183 size_t url_visit_count_; | 191 size_t url_visit_count_; |
184 PageRequestSummary summary_; | 192 PageRequestSummary summary_; |
185 bool match_navigation_id_; | 193 bool match_navigation_id_; |
194 std::set<NavigationID> current_navigation_ids_; | |
186 | 195 |
187 DISALLOW_COPY_AND_ASSIGN(LearningObserver); | 196 DISALLOW_COPY_AND_ASSIGN(LearningObserver); |
188 }; | 197 }; |
189 | 198 |
190 // Helper class to track and allow waiting for a single OnPrefetchingFinished | 199 // Helper class to track and allow waiting for a single OnPrefetchingFinished |
191 // event. No learning events should be fired while this observer is active. | 200 // event. No learning events should be fired while this observer is active. |
192 class PrefetchingObserver : public TestObserver { | 201 class PrefetchingObserver : public TestObserver { |
193 public: | 202 public: |
194 PrefetchingObserver(ResourcePrefetchPredictor* predictor, | 203 PrefetchingObserver(ResourcePrefetchPredictor* predictor, |
195 const GURL& expected_main_frame_url) | 204 const GURL& expected_main_frame_url) |
(...skipping 23 matching lines...) Expand all Loading... | |
219 protected: | 228 protected: |
220 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 229 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
221 | 230 |
222 void SetUpCommandLine(base::CommandLine* command_line) override { | 231 void SetUpCommandLine(base::CommandLine* command_line) override { |
223 command_line->AppendSwitchASCII( | 232 command_line->AppendSwitchASCII( |
224 switches::kSpeculativeResourcePrefetching, | 233 switches::kSpeculativeResourcePrefetching, |
225 switches::kSpeculativeResourcePrefetchingEnabled); | 234 switches::kSpeculativeResourcePrefetchingEnabled); |
226 } | 235 } |
227 | 236 |
228 void SetUpOnMainThread() override { | 237 void SetUpOnMainThread() override { |
238 set_current_host_name("127.0.0.1"); | |
239 // Resolving all hosts to local allows us to have | |
240 // cross domains navigations (matching url_visit_count_, etc) | |
alexilin
2016/12/20 14:31:44
nit: add period to the end of the sentence.
ahemery
2016/12/20 14:44:56
Done.
| |
241 host_resolver()->AddRule("*", "127.0.0.1"); | |
229 embedded_test_server()->RegisterRequestHandler( | 242 embedded_test_server()->RegisterRequestHandler( |
230 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 243 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
231 base::Unretained(this))); | 244 base::Unretained(this))); |
232 embedded_test_server()->RegisterRequestHandler( | 245 embedded_test_server()->RegisterRequestHandler( |
233 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 246 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
234 base::Unretained(this))); | 247 base::Unretained(this))); |
235 ASSERT_TRUE(embedded_test_server()->Start()); | 248 ASSERT_TRUE(embedded_test_server()->Start()); |
236 predictor_ = | 249 predictor_ = |
237 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 250 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
238 ASSERT_TRUE(predictor_); | 251 ASSERT_TRUE(predictor_); |
(...skipping 17 matching lines...) Expand all Loading... | |
256 const GURL& main_frame_url, | 269 const GURL& main_frame_url, |
257 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { | 270 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { |
258 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); | 271 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); |
259 std::vector<URLRequestSummary> url_request_summaries; | 272 std::vector<URLRequestSummary> url_request_summaries; |
260 for (const auto& kv : resources_) { | 273 for (const auto& kv : resources_) { |
261 if (kv.second.is_no_store || !kv.second.should_be_recorded) | 274 if (kv.second.is_no_store || !kv.second.should_be_recorded) |
262 continue; | 275 continue; |
263 url_request_summaries.push_back( | 276 url_request_summaries.push_back( |
264 GetURLRequestSummaryForResource(endpoint_url, kv.second)); | 277 GetURLRequestSummaryForResource(endpoint_url, kv.second)); |
265 } | 278 } |
279 | |
280 bool match_navigation_id = true; | |
alexilin
2016/12/21 13:08:12
bool match_navigation_id = disposition == WindowOp
alexilin
2016/12/22 10:32:29
Still unresolved
ahemery
2016/12/22 12:09:47
Done. Sorry missed this one
| |
281 if (disposition != WindowOpenDisposition::CURRENT_TAB) | |
282 match_navigation_id = false; | |
283 | |
266 LearningObserver observer( | 284 LearningObserver observer( |
267 predictor_, UpdateAndGetVisitCount(main_frame_url), | 285 predictor_, UpdateAndGetVisitCount(main_frame_url), |
268 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), | 286 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), |
269 url_request_summaries), | 287 url_request_summaries), |
270 true); // Matching navigation id by default | 288 match_navigation_id); |
271 ui_test_utils::NavigateToURLWithDisposition( | 289 ui_test_utils::NavigateToURLWithDisposition( |
272 browser(), main_frame_url, disposition, | 290 browser(), main_frame_url, disposition, |
273 ui_test_utils::BROWSER_TEST_NONE); | 291 ui_test_utils::BROWSER_TEST_NONE); |
274 observer.Wait(); | 292 observer.Wait(); |
275 for (auto& kv : resources_) { | 293 for (auto& kv : resources_) { |
276 if (!kv.second.is_no_store && kv.second.should_be_recorded) | 294 if (!kv.second.is_no_store && kv.second.should_be_recorded) |
277 kv.second.request.was_cached = true; | 295 kv.second.request.was_cached = true; |
278 } | 296 } |
297 for (const auto& nav : observer.current_navigation_ids()) | |
298 navigation_id_history_.insert(nav); | |
279 } | 299 } |
280 | 300 |
281 void PrefetchURL(const GURL& main_frame_url) { | 301 void PrefetchURL(const GURL& main_frame_url) { |
282 PrefetchingObserver observer(predictor_, main_frame_url); | 302 PrefetchingObserver observer(predictor_, main_frame_url); |
283 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); | 303 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); |
284 observer.Wait(); | 304 observer.Wait(); |
285 for (auto& kv : resources_) { | 305 for (auto& kv : resources_) { |
286 if (!kv.second.is_no_store && kv.second.should_be_recorded) | 306 if (!kv.second.is_no_store && kv.second.should_be_recorded) |
287 kv.second.request.was_cached = true; | 307 kv.second.request.was_cached = true; |
288 } | 308 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 const std::vector<RedirectEdge>& redirect_chain) { | 344 const std::vector<RedirectEdge>& redirect_chain) { |
325 ASSERT_FALSE(redirect_chain.empty()); | 345 ASSERT_FALSE(redirect_chain.empty()); |
326 GURL current = initial_url; | 346 GURL current = initial_url; |
327 for (const auto& edge : redirect_chain) { | 347 for (const auto& edge : redirect_chain) { |
328 auto result = redirects_.insert(std::make_pair(current, edge)); | 348 auto result = redirects_.insert(std::make_pair(current, edge)); |
329 EXPECT_TRUE(result.second) << current << " already has a redirect."; | 349 EXPECT_TRUE(result.second) << current << " already has a redirect."; |
330 current = edge.url; | 350 current = edge.url; |
331 } | 351 } |
332 } | 352 } |
333 | 353 |
354 void ClearResources() { resources_.clear(); } | |
355 | |
334 void ClearCache() { | 356 void ClearCache() { |
335 chrome::ClearCache(browser()); | 357 chrome::ClearCache(browser()); |
336 for (auto& kv : resources_) | 358 for (auto& kv : resources_) |
337 kv.second.request.was_cached = false; | 359 kv.second.request.was_cached = false; |
338 } | 360 } |
339 | 361 |
340 // Shortcut for convenience. | 362 // Shortcut for convenience. |
341 GURL GetURL(const std::string& path) const { | 363 GURL GetURL(const std::string& path) const { |
342 return embedded_test_server()->GetURL(path); | 364 return embedded_test_server()->GetURL(current_host_name_, path); |
343 } | 365 } |
344 | 366 |
345 void EnableHttpsServer() { | 367 void EnableHttpsServer() { |
346 ASSERT_FALSE(https_server_); | 368 ASSERT_FALSE(https_server_); |
347 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( | 369 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( |
348 net::EmbeddedTestServer::TYPE_HTTPS); | 370 net::EmbeddedTestServer::TYPE_HTTPS); |
349 https_server()->AddDefaultHandlers( | 371 https_server()->AddDefaultHandlers( |
350 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | 372 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
351 https_server()->RegisterRequestHandler( | 373 https_server()->RegisterRequestHandler( |
352 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 374 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
353 base::Unretained(this))); | 375 base::Unretained(this))); |
354 https_server()->RegisterRequestHandler( | 376 https_server()->RegisterRequestHandler( |
355 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 377 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
356 base::Unretained(this))); | 378 base::Unretained(this))); |
357 ASSERT_TRUE(https_server()->Start()); | 379 ASSERT_TRUE(https_server()->Start()); |
358 } | 380 } |
359 | 381 |
360 // Returns the embedded test server working over HTTPS. Must be enabled by | 382 // Returns the embedded test server working over HTTPS. Must be enabled by |
361 // calling EnableHttpsServer() before use. | 383 // calling EnableHttpsServer() before use. |
362 const net::EmbeddedTestServer* https_server() const { | 384 const net::EmbeddedTestServer* https_server() const { |
363 return https_server_.get(); | 385 return https_server_.get(); |
364 } | 386 } |
365 | 387 |
366 net::EmbeddedTestServer* https_server() { return https_server_.get(); } | 388 net::EmbeddedTestServer* https_server() { return https_server_.get(); } |
367 | 389 |
390 void set_current_host_name(const std::string& new_host) { | |
391 current_host_name_ = new_host; | |
392 } | |
393 | |
394 size_t navigation_ids_history_size() { return navigation_id_history_.size(); } | |
395 | |
368 private: | 396 private: |
369 // ResourcePrefetchPredictor needs to be initialized before the navigation | 397 // ResourcePrefetchPredictor needs to be initialized before the navigation |
370 // happens otherwise this navigation will be ignored by predictor. | 398 // happens otherwise this navigation will be ignored by predictor. |
371 void EnsurePredictorInitialized() { | 399 void EnsurePredictorInitialized() { |
372 if (predictor_->initialization_state_ == | 400 if (predictor_->initialization_state_ == |
373 ResourcePrefetchPredictor::INITIALIZED) { | 401 ResourcePrefetchPredictor::INITIALIZED) { |
374 return; | 402 return; |
375 } | 403 } |
376 | 404 |
377 InitializationObserver observer(predictor_); | 405 InitializationObserver observer(predictor_); |
378 if (predictor_->initialization_state_ == | 406 if (predictor_->initialization_state_ == |
379 ResourcePrefetchPredictor::NOT_INITIALIZED) { | 407 ResourcePrefetchPredictor::NOT_INITIALIZED) { |
380 predictor_->StartInitialization(); | 408 predictor_->StartInitialization(); |
381 } | 409 } |
382 observer.Wait(); | 410 observer.Wait(); |
383 } | 411 } |
384 | 412 |
385 URLRequestSummary GetURLRequestSummaryForResource( | 413 URLRequestSummary GetURLRequestSummaryForResource( |
386 const GURL& main_frame_url, | 414 const GURL& main_frame_url, |
387 const ResourceSummary& resource_summary) const { | 415 const ResourceSummary& resource_summary) const { |
388 URLRequestSummary summary(resource_summary.request); | 416 URLRequestSummary summary(resource_summary.request); |
389 content::WebContents* web_contents = | 417 content::WebContents* web_contents = |
390 browser()->tab_strip_model()->GetActiveWebContents(); | 418 browser()->tab_strip_model()->GetActiveWebContents(); |
391 int process_id = web_contents->GetRenderProcessHost()->GetID(); | |
392 int frame_id = web_contents->GetMainFrame()->GetRoutingID(); | |
393 summary.navigation_id = | 419 summary.navigation_id = |
394 CreateNavigationID(process_id, frame_id, main_frame_url.spec()); | 420 NavigationID(web_contents, main_frame_url, base::TimeTicks::Now()); |
395 return summary; | 421 return summary; |
396 } | 422 } |
397 | 423 |
398 GURL GetRedirectEndpoint(const GURL& initial_url) const { | 424 GURL GetRedirectEndpoint(const GURL& initial_url) const { |
399 GURL current = initial_url; | 425 GURL current = initial_url; |
400 while (true) { | 426 while (true) { |
401 auto it = redirects_.find(current); | 427 auto it = redirects_.find(current); |
402 if (it == redirects_.end()) | 428 if (it == redirects_.end()) |
403 break; | 429 break; |
404 current = it->second.url; | 430 current = it->second.url; |
405 } | 431 } |
406 return current; | 432 return current; |
407 } | 433 } |
408 | 434 |
409 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest( | 435 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest( |
410 const net::test_server::HttpRequest& request) const { | 436 const net::test_server::HttpRequest& request) const { |
411 auto resource_it = resources_.find(request.GetURL()); | 437 GURL resource_url = request.GetURL(); |
438 GURL::Replacements replace_host; | |
439 replace_host.SetHostStr(current_host_name_); | |
440 resource_url = resource_url.ReplaceComponents(replace_host); | |
441 | |
442 auto resource_it = resources_.find(resource_url); | |
412 if (resource_it == resources_.end()) | 443 if (resource_it == resources_.end()) |
413 return nullptr; | 444 return nullptr; |
414 | 445 |
415 const ResourceSummary& summary = resource_it->second; | 446 const ResourceSummary& summary = resource_it->second; |
416 if (summary.is_external) | 447 if (summary.is_external) |
417 return nullptr; | 448 return nullptr; |
418 | 449 |
419 auto http_response = | 450 auto http_response = |
420 base::MakeUnique<net::test_server::BasicHttpResponse>(); | 451 base::MakeUnique<net::test_server::BasicHttpResponse>(); |
421 http_response->set_code(net::HTTP_OK); | 452 http_response->set_code(net::HTTP_OK); |
(...skipping 25 matching lines...) Expand all Loading... | |
447 base::MakeUnique<net::test_server::BasicHttpResponse>(); | 478 base::MakeUnique<net::test_server::BasicHttpResponse>(); |
448 http_response->set_code(redirect_it->second.code); | 479 http_response->set_code(redirect_it->second.code); |
449 http_response->AddCustomHeader("Location", redirect_it->second.url.spec()); | 480 http_response->AddCustomHeader("Location", redirect_it->second.url.spec()); |
450 return std::move(http_response); | 481 return std::move(http_response); |
451 } | 482 } |
452 | 483 |
453 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { | 484 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { |
454 return ++visit_count_[main_frame_url]; | 485 return ++visit_count_[main_frame_url]; |
455 } | 486 } |
456 | 487 |
488 std::string current_host_name_; | |
457 ResourcePrefetchPredictor* predictor_; | 489 ResourcePrefetchPredictor* predictor_; |
458 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 490 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
459 std::map<GURL, ResourceSummary> resources_; | 491 std::map<GURL, ResourceSummary> resources_; |
460 std::map<GURL, RedirectEdge> redirects_; | 492 std::map<GURL, RedirectEdge> redirects_; |
461 std::map<GURL, size_t> visit_count_; | 493 std::map<GURL, size_t> visit_count_; |
494 std::set<NavigationID> navigation_id_history_; | |
495 }; | |
496 | |
497 // This browser test override is specifically used to have ONLY | |
498 // the learning part of the prefetcher without the actual prefetching | |
alexilin
2016/12/20 14:31:44
nit: periods as well.
ahemery
2016/12/20 14:44:57
Done.
| |
499 // Used to avoid having caching issues for matcher | |
500 class ResourcePrefetchPredictorLearningBrowserTest | |
501 : public ResourcePrefetchPredictorBrowserTest { | |
502 protected: | |
503 void SetUpCommandLine(base::CommandLine* command_line) override { | |
504 command_line->AppendSwitchASCII( | |
505 switches::kSpeculativeResourcePrefetching, | |
506 switches::kSpeculativeResourcePrefetchingLearning); | |
507 } | |
462 }; | 508 }; |
463 | 509 |
464 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { | 510 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { |
465 // These resources have default priorities that correspond to | 511 // These resources have default priorities that correspond to |
466 // blink::typeToPriority function. | 512 // blink::typeToPriority function. |
467 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 513 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
468 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 514 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
469 net::HIGHEST); | 515 net::HIGHEST); |
470 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 516 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
471 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 517 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
(...skipping 21 matching lines...) Expand all Loading... | |
493 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 539 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
494 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 540 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
495 net::HIGHEST); | 541 net::HIGHEST); |
496 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 542 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
497 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 543 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
498 net::HIGHEST); | 544 net::HIGHEST); |
499 TestLearningAndPrefetching(GetURL(kRedirectPath)); | 545 TestLearningAndPrefetching(GetURL(kRedirectPath)); |
500 } | 546 } |
501 | 547 |
502 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 548 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
503 LearningAfterHttpToHttpsRedirect) { | 549 HttpToHttpsRedirect) { |
504 EnableHttpsServer(); | 550 EnableHttpsServer(); |
505 AddRedirectChain(GetURL(kRedirectPath), | 551 AddRedirectChain(GetURL(kRedirectPath), |
506 {{net::HTTP_MOVED_PERMANENTLY, | 552 {{net::HTTP_MOVED_PERMANENTLY, |
507 https_server()->GetURL(kHtmlSubresourcesPath)}}); | 553 https_server()->GetURL(kHtmlSubresourcesPath)}}); |
508 AddResource(https_server()->GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, | 554 AddResource(https_server()->GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, |
509 net::LOWEST); | 555 net::LOWEST); |
510 AddResource(https_server()->GetURL(kStylePath), | 556 AddResource(https_server()->GetURL(kStylePath), |
511 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 557 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); |
512 AddResource(https_server()->GetURL(kScriptPath), | 558 AddResource(https_server()->GetURL(kScriptPath), |
513 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 559 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
514 AddResource(https_server()->GetURL(kFontPath), | 560 AddResource(https_server()->GetURL(kFontPath), |
515 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | 561 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); |
516 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 562 TestLearningAndPrefetching(GetURL(kRedirectPath)); |
alexilin
2016/12/20 14:31:44
It's great that this test works now!
ahemery
2016/12/20 14:44:56
Done.
| |
517 // TODO(alexilin): Test learning and prefetching once crbug.com/650246 is | |
518 // fixed. | |
519 } | 563 } |
520 | 564 |
521 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 565 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
522 JavascriptDocumentWrite) { | 566 JavascriptDocumentWrite) { |
523 auto externalScript = | 567 auto externalScript = |
524 AddExternalResource(GetURL(kScriptDocumentWritePath), | 568 AddExternalResource(GetURL(kScriptDocumentWritePath), |
525 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 569 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
526 externalScript->request.mime_type = kJavascriptMime; | 570 externalScript->request.mime_type = kJavascriptMime; |
527 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 571 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
528 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 572 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
584 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 628 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
585 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, | 629 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, |
586 net::HIGHEST); | 630 net::HIGHEST); |
587 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 631 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
588 // Included from <iframe src="html_subresources.html"> and not recored. | 632 // Included from <iframe src="html_subresources.html"> and not recored. |
589 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), | 633 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), |
590 GetURL(kScriptPath), GetURL(kFontPath)}); | 634 GetURL(kScriptPath), GetURL(kFontPath)}); |
591 TestLearningAndPrefetching(GetURL(kHtmlIframePath)); | 635 TestLearningAndPrefetching(GetURL(kHtmlIframePath)); |
592 } | 636 } |
593 | 637 |
638 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
639 CrossSiteLearning) { | |
640 set_current_host_name("foo.com"); | |
641 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
642 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
643 net::HIGHEST); | |
644 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
645 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
646 net::HIGHEST); | |
647 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); | |
648 ClearResources(); | |
649 | |
650 set_current_host_name("bar.com"); | |
651 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
652 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
653 net::HIGHEST); | |
654 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
655 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
656 net::HIGHEST); | |
657 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); | |
658 } | |
659 | |
660 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorLearningBrowserTest, | |
661 TabIdBehavingAsExpected) { | |
662 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
663 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
664 net::HIGHEST); | |
665 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
666 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
667 net::HIGHEST); | |
668 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | |
669 EXPECT_EQ(navigation_ids_history_size(), 1U); | |
670 ClearCache(); | |
671 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | |
672 EXPECT_EQ(navigation_ids_history_size(), 1U); | |
673 ClearCache(); | |
674 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
675 WindowOpenDisposition::NEW_FOREGROUND_TAB); | |
676 EXPECT_EQ(navigation_ids_history_size(), 2U); | |
677 ClearCache(); | |
678 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
679 WindowOpenDisposition::NEW_WINDOW); | |
680 EXPECT_EQ(navigation_ids_history_size(), 3U); | |
681 ClearCache(); | |
682 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
683 WindowOpenDisposition::NEW_POPUP); | |
684 EXPECT_EQ(navigation_ids_history_size(), 4U); | |
685 } | |
686 | |
594 } // namespace predictors | 687 } // namespace predictors |
OLD | NEW |