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