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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
88 | 91 |
89 private: | 92 private: |
90 base::RunLoop run_loop_; | 93 base::RunLoop run_loop_; |
91 | 94 |
92 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); | 95 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); |
93 }; | 96 }; |
94 | 97 |
95 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 98 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
96 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 99 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
97 | 100 |
101 void ReplaceHost(GURL* gurl, const std::string& host) { | |
Benoit L
2016/12/21 12:38:26
nit: This is used in one place only, please inline
alexilin
2016/12/21 13:08:12
Actually, it was my suggestion.
This is copypaste
ahemery
2016/12/21 15:42:40
Un-inlined. Also as discussed with Alex, we still
| |
102 GURL::Replacements replace_host; | |
103 replace_host.SetHostStr(host); | |
104 *gurl = gurl->ReplaceComponents(replace_host); | |
105 } | |
106 | |
98 void RemoveDuplicateSubresources(std::vector<URLRequestSummary>* subresources) { | 107 void RemoveDuplicateSubresources(std::vector<URLRequestSummary>* subresources) { |
99 std::stable_sort(subresources->begin(), subresources->end(), | 108 std::stable_sort(subresources->begin(), subresources->end(), |
100 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 109 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
101 return x.resource_url < y.resource_url; | 110 return x.resource_url < y.resource_url; |
102 }); | 111 }); |
103 subresources->erase( | 112 subresources->erase( |
104 std::unique(subresources->begin(), subresources->end(), | 113 std::unique(subresources->begin(), subresources->end(), |
105 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 114 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
106 return x.resource_url == y.resource_url; | 115 return x.resource_url == y.resource_url; |
107 }), | 116 }), |
108 subresources->end()); | 117 subresources->end()); |
109 } | 118 } |
110 | 119 |
111 // Fill a NavigationID with "empty" data that does not trigger | 120 // Fill a NavigationID with "empty" data that does not trigger |
112 // the is_valid DCHECK(). Allows comparing. | 121 // the is_valid DCHECK(). Allows comparing. |
113 void SetValidNavigationID(NavigationID* navigation_id) { | 122 void SetValidNavigationID(NavigationID* navigation_id) { |
114 navigation_id->render_process_id = 0; | 123 navigation_id->session_id = 0; |
115 navigation_id->render_frame_id = 0; | |
116 navigation_id->main_frame_url = GURL("http://127.0.0.1"); | 124 navigation_id->main_frame_url = GURL("http://127.0.0.1"); |
117 } | 125 } |
118 | 126 |
119 void ModifySubresourceForComparison(URLRequestSummary* subresource, | 127 void ModifySubresourceForComparison(URLRequestSummary* subresource, |
120 bool match_navigation_id) { | 128 bool match_navigation_id) { |
121 if (!match_navigation_id) | 129 if (!match_navigation_id) |
122 SetValidNavigationID(&subresource->navigation_id); | 130 SetValidNavigationID(&subresource->navigation_id); |
123 if (subresource->resource_type == content::RESOURCE_TYPE_IMAGE && | 131 if (subresource->resource_type == content::RESOURCE_TYPE_IMAGE && |
124 subresource->priority == net::LOWEST) { | 132 subresource->priority == net::LOWEST) { |
125 // Fuzzy comparison for images because an image priority can be | 133 // 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), | 172 url_visit_count_(expected_url_visit_count), |
165 summary_(expected_summary), | 173 summary_(expected_summary), |
166 match_navigation_id_(match_navigation_id) {} | 174 match_navigation_id_(match_navigation_id) {} |
167 | 175 |
168 // TestObserver: | 176 // TestObserver: |
169 void OnNavigationLearned(size_t url_visit_count, | 177 void OnNavigationLearned(size_t url_visit_count, |
170 const PageRequestSummary& summary) override { | 178 const PageRequestSummary& summary) override { |
171 EXPECT_EQ(url_visit_count, url_visit_count_); | 179 EXPECT_EQ(url_visit_count, url_visit_count_); |
172 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | 180 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); |
173 EXPECT_EQ(summary.initial_url, summary_.initial_url); | 181 EXPECT_EQ(summary.initial_url, summary_.initial_url); |
182 for (const auto& resource : summary.subresource_requests) | |
183 current_navigation_ids_.insert(resource.navigation_id); | |
174 CompareSubresources(summary.subresource_requests, | 184 CompareSubresources(summary.subresource_requests, |
175 summary_.subresource_requests, match_navigation_id_); | 185 summary_.subresource_requests, match_navigation_id_); |
176 run_loop_.Quit(); | 186 run_loop_.Quit(); |
177 } | 187 } |
178 | 188 |
179 void Wait() { run_loop_.Run(); } | 189 void Wait() { run_loop_.Run(); } |
180 | 190 |
191 std::set<NavigationID>& current_navigation_ids() { | |
192 return current_navigation_ids_; | |
193 } | |
194 | |
181 private: | 195 private: |
182 base::RunLoop run_loop_; | 196 base::RunLoop run_loop_; |
183 size_t url_visit_count_; | 197 size_t url_visit_count_; |
184 PageRequestSummary summary_; | 198 PageRequestSummary summary_; |
185 bool match_navigation_id_; | 199 bool match_navigation_id_; |
200 std::set<NavigationID> current_navigation_ids_; | |
186 | 201 |
187 DISALLOW_COPY_AND_ASSIGN(LearningObserver); | 202 DISALLOW_COPY_AND_ASSIGN(LearningObserver); |
188 }; | 203 }; |
189 | 204 |
190 // Helper class to track and allow waiting for a single OnPrefetchingFinished | 205 // Helper class to track and allow waiting for a single OnPrefetchingFinished |
191 // event. No learning events should be fired while this observer is active. | 206 // event. No learning events should be fired while this observer is active. |
192 class PrefetchingObserver : public TestObserver { | 207 class PrefetchingObserver : public TestObserver { |
193 public: | 208 public: |
194 PrefetchingObserver(ResourcePrefetchPredictor* predictor, | 209 PrefetchingObserver(ResourcePrefetchPredictor* predictor, |
195 const GURL& expected_main_frame_url) | 210 const GURL& expected_main_frame_url) |
(...skipping 23 matching lines...) Expand all Loading... | |
219 protected: | 234 protected: |
220 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 235 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
221 | 236 |
222 void SetUpCommandLine(base::CommandLine* command_line) override { | 237 void SetUpCommandLine(base::CommandLine* command_line) override { |
223 command_line->AppendSwitchASCII( | 238 command_line->AppendSwitchASCII( |
224 switches::kSpeculativeResourcePrefetching, | 239 switches::kSpeculativeResourcePrefetching, |
225 switches::kSpeculativeResourcePrefetchingEnabled); | 240 switches::kSpeculativeResourcePrefetchingEnabled); |
226 } | 241 } |
227 | 242 |
228 void SetUpOnMainThread() override { | 243 void SetUpOnMainThread() override { |
244 // Resolving all hosts to local allows us to have | |
245 // cross domains navigations (matching url_visit_count_, etc). | |
246 host_resolver()->AddRule("*", "127.0.0.1"); | |
229 embedded_test_server()->RegisterRequestHandler( | 247 embedded_test_server()->RegisterRequestHandler( |
230 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 248 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
231 base::Unretained(this))); | 249 base::Unretained(this))); |
232 embedded_test_server()->RegisterRequestHandler( | 250 embedded_test_server()->RegisterRequestHandler( |
233 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 251 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
234 base::Unretained(this))); | 252 base::Unretained(this))); |
235 ASSERT_TRUE(embedded_test_server()->Start()); | 253 ASSERT_TRUE(embedded_test_server()->Start()); |
236 predictor_ = | 254 predictor_ = |
237 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 255 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
238 ASSERT_TRUE(predictor_); | 256 ASSERT_TRUE(predictor_); |
(...skipping 17 matching lines...) Expand all Loading... | |
256 const GURL& main_frame_url, | 274 const GURL& main_frame_url, |
257 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { | 275 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { |
258 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); | 276 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); |
259 std::vector<URLRequestSummary> url_request_summaries; | 277 std::vector<URLRequestSummary> url_request_summaries; |
260 for (const auto& kv : resources_) { | 278 for (const auto& kv : resources_) { |
261 if (kv.second.is_no_store || !kv.second.should_be_recorded) | 279 if (kv.second.is_no_store || !kv.second.should_be_recorded) |
262 continue; | 280 continue; |
263 url_request_summaries.push_back( | 281 url_request_summaries.push_back( |
264 GetURLRequestSummaryForResource(endpoint_url, kv.second)); | 282 GetURLRequestSummaryForResource(endpoint_url, kv.second)); |
265 } | 283 } |
284 | |
285 bool match_navigation_id = true; | |
286 if (disposition != WindowOpenDisposition::CURRENT_TAB) | |
287 match_navigation_id = false; | |
288 | |
266 LearningObserver observer( | 289 LearningObserver observer( |
267 predictor_, UpdateAndGetVisitCount(main_frame_url), | 290 predictor_, UpdateAndGetVisitCount(main_frame_url), |
268 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), | 291 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), |
269 url_request_summaries), | 292 url_request_summaries), |
270 true); // Matching navigation id by default | 293 match_navigation_id); |
271 ui_test_utils::NavigateToURLWithDisposition( | 294 ui_test_utils::NavigateToURLWithDisposition( |
272 browser(), main_frame_url, disposition, | 295 browser(), main_frame_url, disposition, |
273 ui_test_utils::BROWSER_TEST_NONE); | 296 ui_test_utils::BROWSER_TEST_NONE); |
274 observer.Wait(); | 297 observer.Wait(); |
275 for (auto& kv : resources_) { | 298 for (auto& kv : resources_) { |
276 if (!kv.second.is_no_store && kv.second.should_be_recorded) | 299 if (!kv.second.is_no_store && kv.second.should_be_recorded) |
277 kv.second.request.was_cached = true; | 300 kv.second.request.was_cached = true; |
278 } | 301 } |
302 for (const auto& nav : observer.current_navigation_ids()) | |
303 navigation_id_history_.insert(nav); | |
279 } | 304 } |
280 | 305 |
281 void PrefetchURL(const GURL& main_frame_url) { | 306 void PrefetchURL(const GURL& main_frame_url) { |
282 PrefetchingObserver observer(predictor_, main_frame_url); | 307 PrefetchingObserver observer(predictor_, main_frame_url); |
283 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); | 308 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); |
284 observer.Wait(); | 309 observer.Wait(); |
285 for (auto& kv : resources_) { | 310 for (auto& kv : resources_) { |
286 if (!kv.second.is_no_store && kv.second.should_be_recorded) | 311 if (!kv.second.is_no_store && kv.second.should_be_recorded) |
287 kv.second.request.was_cached = true; | 312 kv.second.request.was_cached = true; |
288 } | 313 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
324 const std::vector<RedirectEdge>& redirect_chain) { | 349 const std::vector<RedirectEdge>& redirect_chain) { |
325 ASSERT_FALSE(redirect_chain.empty()); | 350 ASSERT_FALSE(redirect_chain.empty()); |
326 GURL current = initial_url; | 351 GURL current = initial_url; |
327 for (const auto& edge : redirect_chain) { | 352 for (const auto& edge : redirect_chain) { |
328 auto result = redirects_.insert(std::make_pair(current, edge)); | 353 auto result = redirects_.insert(std::make_pair(current, edge)); |
329 EXPECT_TRUE(result.second) << current << " already has a redirect."; | 354 EXPECT_TRUE(result.second) << current << " already has a redirect."; |
330 current = edge.url; | 355 current = edge.url; |
331 } | 356 } |
332 } | 357 } |
333 | 358 |
359 void ClearResources() { resources_.clear(); } | |
360 | |
334 void ClearCache() { | 361 void ClearCache() { |
335 chrome::ClearCache(browser()); | 362 chrome::ClearCache(browser()); |
336 for (auto& kv : resources_) | 363 for (auto& kv : resources_) |
337 kv.second.request.was_cached = false; | 364 kv.second.request.was_cached = false; |
338 } | 365 } |
339 | 366 |
340 // Shortcut for convenience. | 367 // Shortcut for convenience. |
368 // Using two functions instead of default parameter to preserve | |
369 // original function parameters order. | |
341 GURL GetURL(const std::string& path) const { | 370 GURL GetURL(const std::string& path) const { |
342 return embedded_test_server()->GetURL(path); | 371 return embedded_test_server()->GetURL(path); |
343 } | 372 } |
344 | 373 |
374 GURL GetURL(const std::string& path, const std::string& host) const { | |
alexilin
2016/12/21 13:08:12
nit: Would it be better to use ReplaceHost for thi
ahemery
2016/12/21 15:42:40
Removed the one with more parameters as it was not
| |
375 return embedded_test_server()->GetURL(host, path); | |
376 } | |
377 | |
345 void EnableHttpsServer() { | 378 void EnableHttpsServer() { |
346 ASSERT_FALSE(https_server_); | 379 ASSERT_FALSE(https_server_); |
347 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( | 380 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( |
348 net::EmbeddedTestServer::TYPE_HTTPS); | 381 net::EmbeddedTestServer::TYPE_HTTPS); |
349 https_server()->AddDefaultHandlers( | 382 https_server()->AddDefaultHandlers( |
350 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); | 383 base::FilePath(FILE_PATH_LITERAL("chrome/test/data"))); |
351 https_server()->RegisterRequestHandler( | 384 https_server()->RegisterRequestHandler( |
352 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 385 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
353 base::Unretained(this))); | 386 base::Unretained(this))); |
354 https_server()->RegisterRequestHandler( | 387 https_server()->RegisterRequestHandler( |
355 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 388 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
356 base::Unretained(this))); | 389 base::Unretained(this))); |
357 ASSERT_TRUE(https_server()->Start()); | 390 ASSERT_TRUE(https_server()->Start()); |
358 } | 391 } |
359 | 392 |
360 // Returns the embedded test server working over HTTPS. Must be enabled by | 393 // Returns the embedded test server working over HTTPS. Must be enabled by |
361 // calling EnableHttpsServer() before use. | 394 // calling EnableHttpsServer() before use. |
362 const net::EmbeddedTestServer* https_server() const { | 395 const net::EmbeddedTestServer* https_server() const { |
363 return https_server_.get(); | 396 return https_server_.get(); |
364 } | 397 } |
365 | 398 |
366 net::EmbeddedTestServer* https_server() { return https_server_.get(); } | 399 net::EmbeddedTestServer* https_server() { return https_server_.get(); } |
367 | 400 |
401 size_t navigation_ids_history_size() { return navigation_id_history_.size(); } | |
Benoit L
2016/12/21 12:38:25
nit: make the function const.
ahemery
2016/12/21 15:42:40
Done.
| |
402 | |
368 private: | 403 private: |
369 // ResourcePrefetchPredictor needs to be initialized before the navigation | 404 // ResourcePrefetchPredictor needs to be initialized before the navigation |
370 // happens otherwise this navigation will be ignored by predictor. | 405 // happens otherwise this navigation will be ignored by predictor. |
371 void EnsurePredictorInitialized() { | 406 void EnsurePredictorInitialized() { |
372 if (predictor_->initialization_state_ == | 407 if (predictor_->initialization_state_ == |
373 ResourcePrefetchPredictor::INITIALIZED) { | 408 ResourcePrefetchPredictor::INITIALIZED) { |
374 return; | 409 return; |
375 } | 410 } |
376 | 411 |
377 InitializationObserver observer(predictor_); | 412 InitializationObserver observer(predictor_); |
378 if (predictor_->initialization_state_ == | 413 if (predictor_->initialization_state_ == |
379 ResourcePrefetchPredictor::NOT_INITIALIZED) { | 414 ResourcePrefetchPredictor::NOT_INITIALIZED) { |
380 predictor_->StartInitialization(); | 415 predictor_->StartInitialization(); |
381 } | 416 } |
382 observer.Wait(); | 417 observer.Wait(); |
383 } | 418 } |
384 | 419 |
385 URLRequestSummary GetURLRequestSummaryForResource( | 420 URLRequestSummary GetURLRequestSummaryForResource( |
386 const GURL& main_frame_url, | 421 const GURL& main_frame_url, |
387 const ResourceSummary& resource_summary) const { | 422 const ResourceSummary& resource_summary) const { |
388 URLRequestSummary summary(resource_summary.request); | 423 URLRequestSummary summary(resource_summary.request); |
389 content::WebContents* web_contents = | 424 content::WebContents* web_contents = |
390 browser()->tab_strip_model()->GetActiveWebContents(); | 425 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 = | 426 summary.navigation_id = |
394 CreateNavigationID(process_id, frame_id, main_frame_url.spec()); | 427 NavigationID(web_contents, main_frame_url, base::TimeTicks::Now()); |
395 return summary; | 428 return summary; |
396 } | 429 } |
397 | 430 |
398 GURL GetRedirectEndpoint(const GURL& initial_url) const { | 431 GURL GetRedirectEndpoint(const GURL& initial_url) const { |
399 GURL current = initial_url; | 432 GURL current = initial_url; |
400 while (true) { | 433 while (true) { |
401 auto it = redirects_.find(current); | 434 auto it = redirects_.find(current); |
402 if (it == redirects_.end()) | 435 if (it == redirects_.end()) |
403 break; | 436 break; |
404 current = it->second.url; | 437 current = it->second.url; |
405 } | 438 } |
406 return current; | 439 return current; |
407 } | 440 } |
408 | 441 |
409 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest( | 442 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest( |
410 const net::test_server::HttpRequest& request) const { | 443 const net::test_server::HttpRequest& request) const { |
411 auto resource_it = resources_.find(request.GetURL()); | 444 GURL resource_url = request.GetURL(); |
445 // Retrieve the host that was used in the request. | |
446 // The resource_url contains a resolved host (e.g. 127.0.0.1). | |
447 std::string host = request.headers.at("Host"); | |
Benoit L
2016/12/21 12:38:25
GURL has has_port() and port().
Also, why using .a
alexilin
2016/12/21 13:08:12
Consider to use HostPortPair https://cs.chromium.o
ahemery
2016/12/21 15:42:40
Removed the manual work and used the structure sug
| |
448 size_t port_pos = host.find(':'); | |
449 if (port_pos != std::string::npos) | |
450 host = host.substr(0, port_pos); | |
451 ReplaceHost(&resource_url, host); | |
452 | |
453 auto resource_it = resources_.find(resource_url); | |
412 if (resource_it == resources_.end()) | 454 if (resource_it == resources_.end()) |
413 return nullptr; | 455 return nullptr; |
414 | 456 |
415 const ResourceSummary& summary = resource_it->second; | 457 const ResourceSummary& summary = resource_it->second; |
416 if (summary.is_external) | 458 if (summary.is_external) |
417 return nullptr; | 459 return nullptr; |
418 | 460 |
419 auto http_response = | 461 auto http_response = |
420 base::MakeUnique<net::test_server::BasicHttpResponse>(); | 462 base::MakeUnique<net::test_server::BasicHttpResponse>(); |
421 http_response->set_code(net::HTTP_OK); | 463 http_response->set_code(net::HTTP_OK); |
(...skipping 30 matching lines...) Expand all Loading... | |
452 | 494 |
453 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { | 495 size_t UpdateAndGetVisitCount(const GURL& main_frame_url) { |
454 return ++visit_count_[main_frame_url]; | 496 return ++visit_count_[main_frame_url]; |
455 } | 497 } |
456 | 498 |
457 ResourcePrefetchPredictor* predictor_; | 499 ResourcePrefetchPredictor* predictor_; |
458 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 500 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
459 std::map<GURL, ResourceSummary> resources_; | 501 std::map<GURL, ResourceSummary> resources_; |
460 std::map<GURL, RedirectEdge> redirects_; | 502 std::map<GURL, RedirectEdge> redirects_; |
461 std::map<GURL, size_t> visit_count_; | 503 std::map<GURL, size_t> visit_count_; |
504 std::set<NavigationID> navigation_id_history_; | |
505 }; | |
506 | |
507 // This browser test override is specifically used to have ONLY | |
508 // the learning part of the prefetcher without the actual prefetching | |
alexilin
2016/12/21 13:08:12
period here as well because this is separate sente
ahemery
2016/12/21 15:42:40
Done.
| |
509 // Used to avoid having caching issues for matcher. | |
510 class ResourcePrefetchPredictorLearningBrowserTest | |
511 : public ResourcePrefetchPredictorBrowserTest { | |
512 protected: | |
513 void SetUpCommandLine(base::CommandLine* command_line) override { | |
514 command_line->AppendSwitchASCII( | |
515 switches::kSpeculativeResourcePrefetching, | |
516 switches::kSpeculativeResourcePrefetchingLearning); | |
517 } | |
462 }; | 518 }; |
463 | 519 |
464 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { | 520 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { |
465 // These resources have default priorities that correspond to | 521 // These resources have default priorities that correspond to |
466 // blink::typeToPriority function. | 522 // blink::typeToPriority function. |
467 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 523 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
468 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 524 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
469 net::HIGHEST); | 525 net::HIGHEST); |
470 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 526 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
471 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 527 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); | 549 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
494 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 550 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
495 net::HIGHEST); | 551 net::HIGHEST); |
496 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 552 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
497 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 553 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
498 net::HIGHEST); | 554 net::HIGHEST); |
499 TestLearningAndPrefetching(GetURL(kRedirectPath)); | 555 TestLearningAndPrefetching(GetURL(kRedirectPath)); |
500 } | 556 } |
501 | 557 |
502 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 558 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
503 LearningAfterHttpToHttpsRedirect) { | 559 HttpToHttpsRedirect) { |
504 EnableHttpsServer(); | 560 EnableHttpsServer(); |
505 AddRedirectChain(GetURL(kRedirectPath), | 561 AddRedirectChain(GetURL(kRedirectPath), |
506 {{net::HTTP_MOVED_PERMANENTLY, | 562 {{net::HTTP_MOVED_PERMANENTLY, |
507 https_server()->GetURL(kHtmlSubresourcesPath)}}); | 563 https_server()->GetURL(kHtmlSubresourcesPath)}}); |
508 AddResource(https_server()->GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, | 564 AddResource(https_server()->GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, |
509 net::LOWEST); | 565 net::LOWEST); |
510 AddResource(https_server()->GetURL(kStylePath), | 566 AddResource(https_server()->GetURL(kStylePath), |
511 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 567 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); |
512 AddResource(https_server()->GetURL(kScriptPath), | 568 AddResource(https_server()->GetURL(kScriptPath), |
513 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 569 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
514 AddResource(https_server()->GetURL(kFontPath), | 570 AddResource(https_server()->GetURL(kFontPath), |
515 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | 571 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); |
516 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 572 TestLearningAndPrefetching(GetURL(kRedirectPath)); |
517 // TODO(alexilin): Test learning and prefetching once crbug.com/650246 is | |
518 // fixed. | |
519 } | 573 } |
520 | 574 |
521 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 575 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
522 JavascriptDocumentWrite) { | 576 JavascriptDocumentWrite) { |
523 auto externalScript = | 577 auto externalScript = |
524 AddExternalResource(GetURL(kScriptDocumentWritePath), | 578 AddExternalResource(GetURL(kScriptDocumentWritePath), |
525 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 579 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
526 externalScript->request.mime_type = kJavascriptMime; | 580 externalScript->request.mime_type = kJavascriptMime; |
527 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 581 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
528 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 582 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); | 638 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
585 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, | 639 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, |
586 net::HIGHEST); | 640 net::HIGHEST); |
587 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 641 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
588 // Included from <iframe src="html_subresources.html"> and not recored. | 642 // Included from <iframe src="html_subresources.html"> and not recored. |
589 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), | 643 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), |
590 GetURL(kScriptPath), GetURL(kFontPath)}); | 644 GetURL(kScriptPath), GetURL(kFontPath)}); |
591 TestLearningAndPrefetching(GetURL(kHtmlIframePath)); | 645 TestLearningAndPrefetching(GetURL(kHtmlIframePath)); |
592 } | 646 } |
593 | 647 |
648 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | |
649 CrossSiteLearning) { | |
650 AddResource(GetURL(kImagePath, "foo.com"), content::RESOURCE_TYPE_IMAGE, | |
651 net::LOWEST); | |
652 AddResource(GetURL(kStylePath, "foo.com"), content::RESOURCE_TYPE_STYLESHEET, | |
653 net::HIGHEST); | |
654 AddResource(GetURL(kScriptPath, "foo.com"), content::RESOURCE_TYPE_SCRIPT, | |
655 net::MEDIUM); | |
656 AddResource(GetURL(kFontPath, "foo.com"), | |
657 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | |
658 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath, "foo.com")); | |
659 ClearResources(); | |
660 | |
661 AddResource(GetURL(kImagePath, "bar.com"), content::RESOURCE_TYPE_IMAGE, | |
662 net::LOWEST); | |
663 AddResource(GetURL(kStylePath, "bar.com"), content::RESOURCE_TYPE_STYLESHEET, | |
664 net::HIGHEST); | |
665 AddResource(GetURL(kScriptPath, "bar.com"), content::RESOURCE_TYPE_SCRIPT, | |
666 net::MEDIUM); | |
667 AddResource(GetURL(kFontPath, "bar.com"), | |
668 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | |
669 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath, "bar.com")); | |
670 } | |
671 | |
672 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorLearningBrowserTest, | |
673 SessionIdBehavingAsExpected) { | |
674 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
675 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
676 net::HIGHEST); | |
677 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
678 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
679 net::HIGHEST); | |
680 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | |
681 EXPECT_EQ(navigation_ids_history_size(), 1U); | |
682 ClearCache(); | |
683 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | |
684 // Checking that repeated navigation to a tab reuses the same SessionID. | |
685 EXPECT_EQ(navigation_ids_history_size(), 1U); | |
686 ClearCache(); | |
687 | |
688 // Checking that navigation to a new tab yields the same result as | |
689 // navigating to the same tab resource wise. | |
690 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
691 WindowOpenDisposition::NEW_BACKGROUND_TAB); | |
692 // Checking that navigation to a new tab uses a new SessionID. | |
693 EXPECT_EQ(navigation_ids_history_size(), 2U); | |
694 ClearCache(); | |
695 | |
696 // Checking that navigation to a new window yields the same result as | |
alexilin
2016/12/21 13:08:12
There is no need to repeat the same things several
ahemery
2016/12/21 15:42:40
Condensed on top of the function
| |
697 // navigating to the same tab resource wise. | |
698 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
699 WindowOpenDisposition::NEW_WINDOW); | |
700 // Checking that navigation to a new window uses a new SessionID. | |
701 EXPECT_EQ(navigation_ids_history_size(), 3U); | |
702 ClearCache(); | |
703 | |
704 // Checking that navigation to a popup yields the same result as | |
705 // navigating to the same tab resource wise. | |
706 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
707 WindowOpenDisposition::NEW_POPUP); | |
708 // Checking that navigation to a new tab uses a new SessionID that is | |
709 // also different from the new window one (e.g. not always reusing 1). | |
710 EXPECT_EQ(navigation_ids_history_size(), 4U); | |
711 } | |
712 | |
594 } // namespace predictors | 713 } // namespace predictors |
OLD | NEW |