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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | 6 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
7 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | 7 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" |
8 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | 8 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/browser.h" | 10 #include "chrome/browser/ui/browser.h" |
| 11 #include "chrome/browser/ui/browser_commands.h" |
11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 12 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
12 #include "chrome/common/chrome_switches.h" | 13 #include "chrome/common/chrome_switches.h" |
13 #include "chrome/test/base/in_process_browser_test.h" | 14 #include "chrome/test/base/in_process_browser_test.h" |
14 #include "chrome/test/base/ui_test_utils.h" | 15 #include "chrome/test/base/ui_test_utils.h" |
15 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
16 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
17 #include "net/test/embedded_test_server/http_request.h" | 18 #include "net/test/embedded_test_server/http_request.h" |
18 #include "net/test/embedded_test_server/http_response.h" | 19 #include "net/test/embedded_test_server/http_response.h" |
19 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 bool is_external; | 67 bool is_external; |
67 bool should_be_recorded; | 68 bool should_be_recorded; |
68 }; | 69 }; |
69 | 70 |
70 struct RedirectEdge { | 71 struct RedirectEdge { |
71 // This response code should be returned by previous url in the chain. | 72 // This response code should be returned by previous url in the chain. |
72 net::HttpStatusCode code; | 73 net::HttpStatusCode code; |
73 GURL url; | 74 GURL url; |
74 }; | 75 }; |
75 | 76 |
| 77 // Helper class to track and allow waiting for ResourcePrefetchPredictor |
| 78 // initialization. WARNING: OnPredictorInitialized event will not be fired if |
| 79 // ResourcePrefetchPredictor is initialized before the observer creation. |
76 class InitializationObserver : public TestObserver { | 80 class InitializationObserver : public TestObserver { |
77 public: | 81 public: |
78 explicit InitializationObserver(ResourcePrefetchPredictor* predictor) | 82 explicit InitializationObserver(ResourcePrefetchPredictor* predictor) |
79 : TestObserver(predictor) {} | 83 : TestObserver(predictor) {} |
80 | 84 |
81 void OnPredictorInitialized() override { run_loop_.Quit(); } | 85 void OnPredictorInitialized() override { run_loop_.Quit(); } |
82 | 86 |
83 void Wait() { run_loop_.Run(); } | 87 void Wait() { run_loop_.Run(); } |
84 | 88 |
85 private: | 89 private: |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 ModifySubresourceForComparison(&subresource, match_navigation_id); | 142 ModifySubresourceForComparison(&subresource, match_navigation_id); |
139 for (auto& subresource : expected_subresources) | 143 for (auto& subresource : expected_subresources) |
140 ModifySubresourceForComparison(&subresource, match_navigation_id); | 144 ModifySubresourceForComparison(&subresource, match_navigation_id); |
141 | 145 |
142 EXPECT_THAT(actual_subresources, | 146 EXPECT_THAT(actual_subresources, |
143 testing::UnorderedElementsAreArray(expected_subresources)); | 147 testing::UnorderedElementsAreArray(expected_subresources)); |
144 } | 148 } |
145 | 149 |
146 } // namespace | 150 } // namespace |
147 | 151 |
148 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. | 152 // Helper class to track and allow waiting for a single OnNavigationLearned |
149 // These events are also used to verify that ResourcePrefetchPredictor works as | 153 // event. The information provided by this event is also used to verify that |
150 // expected. | 154 // ResourcePrefetchPredictor works as expected. |
151 class ResourcePrefetchPredictorTestObserver : public TestObserver { | 155 class LearningObserver : public TestObserver { |
152 public: | 156 public: |
153 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 157 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
154 | 158 |
155 explicit ResourcePrefetchPredictorTestObserver( | 159 LearningObserver(ResourcePrefetchPredictor* predictor, |
156 ResourcePrefetchPredictor* predictor, | 160 const size_t expected_url_visit_count, |
157 const size_t expected_url_visit_count, | 161 const PageRequestSummary& expected_summary, |
158 const PageRequestSummary& expected_summary, | 162 bool match_navigation_id) |
159 bool match_navigation_id) | |
160 : TestObserver(predictor), | 163 : TestObserver(predictor), |
161 url_visit_count_(expected_url_visit_count), | 164 url_visit_count_(expected_url_visit_count), |
162 summary_(expected_summary), | 165 summary_(expected_summary), |
163 match_navigation_id_(match_navigation_id) {} | 166 match_navigation_id_(match_navigation_id) {} |
164 | 167 |
165 // TestObserver: | 168 // TestObserver: |
166 void OnNavigationLearned(size_t url_visit_count, | 169 void OnNavigationLearned(size_t url_visit_count, |
167 const PageRequestSummary& summary) override { | 170 const PageRequestSummary& summary) override { |
168 EXPECT_EQ(url_visit_count, url_visit_count_); | 171 EXPECT_EQ(url_visit_count, url_visit_count_); |
169 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | 172 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); |
170 EXPECT_EQ(summary.initial_url, summary_.initial_url); | 173 EXPECT_EQ(summary.initial_url, summary_.initial_url); |
171 CompareSubresources(summary.subresource_requests, | 174 CompareSubresources(summary.subresource_requests, |
172 summary_.subresource_requests, match_navigation_id_); | 175 summary_.subresource_requests, match_navigation_id_); |
173 run_loop_.Quit(); | 176 run_loop_.Quit(); |
174 } | 177 } |
175 | 178 |
176 void Wait() { run_loop_.Run(); } | 179 void Wait() { run_loop_.Run(); } |
177 | 180 |
178 private: | 181 private: |
179 base::RunLoop run_loop_; | 182 base::RunLoop run_loop_; |
180 size_t url_visit_count_; | 183 size_t url_visit_count_; |
181 PageRequestSummary summary_; | 184 PageRequestSummary summary_; |
182 bool match_navigation_id_; | 185 bool match_navigation_id_; |
183 | 186 |
184 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); | 187 DISALLOW_COPY_AND_ASSIGN(LearningObserver); |
| 188 }; |
| 189 |
| 190 // Helper class to track and allow waiting for a single OnPrefetchingFinished |
| 191 // event. No learning events should be fired while this observer is active. |
| 192 class PrefetchingObserver : public TestObserver { |
| 193 public: |
| 194 PrefetchingObserver(ResourcePrefetchPredictor* predictor, |
| 195 const GURL& expected_main_frame_url) |
| 196 : TestObserver(predictor), main_frame_url_(expected_main_frame_url) {} |
| 197 |
| 198 // TestObserver: |
| 199 void OnNavigationLearned(size_t url_visit_count, |
| 200 const PageRequestSummary& summary) override { |
| 201 ADD_FAILURE() << "Prefetching shouldn't activate learning"; |
| 202 } |
| 203 |
| 204 void OnPrefetchingFinished(const GURL& main_frame_url) override { |
| 205 EXPECT_EQ(main_frame_url_, main_frame_url); |
| 206 run_loop_.Quit(); |
| 207 } |
| 208 |
| 209 void Wait() { run_loop_.Run(); } |
| 210 |
| 211 private: |
| 212 base::RunLoop run_loop_; |
| 213 GURL main_frame_url_; |
| 214 |
| 215 DISALLOW_COPY_AND_ASSIGN(PrefetchingObserver); |
185 }; | 216 }; |
186 | 217 |
187 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { | 218 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { |
188 protected: | 219 protected: |
189 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 220 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
190 | 221 |
191 void SetUpCommandLine(base::CommandLine* command_line) override { | 222 void SetUpCommandLine(base::CommandLine* command_line) override { |
192 command_line->AppendSwitchASCII( | 223 command_line->AppendSwitchASCII( |
193 switches::kSpeculativeResourcePrefetching, | 224 switches::kSpeculativeResourcePrefetching, |
194 switches::kSpeculativeResourcePrefetchingEnabled); | 225 switches::kSpeculativeResourcePrefetchingEnabled); |
195 } | 226 } |
196 | 227 |
197 void SetUpOnMainThread() override { | 228 void SetUpOnMainThread() override { |
198 embedded_test_server()->RegisterRequestHandler( | 229 embedded_test_server()->RegisterRequestHandler( |
199 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 230 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
200 base::Unretained(this))); | 231 base::Unretained(this))); |
201 embedded_test_server()->RegisterRequestHandler( | 232 embedded_test_server()->RegisterRequestHandler( |
202 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 233 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
203 base::Unretained(this))); | 234 base::Unretained(this))); |
204 ASSERT_TRUE(embedded_test_server()->Start()); | 235 ASSERT_TRUE(embedded_test_server()->Start()); |
205 predictor_ = | 236 predictor_ = |
206 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 237 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
207 ASSERT_TRUE(predictor_); | 238 ASSERT_TRUE(predictor_); |
208 EnsurePredictorInitialized(); | 239 EnsurePredictorInitialized(); |
209 } | 240 } |
210 | 241 |
| 242 void TestLearningAndPrefetching(const GURL& main_frame_url) { |
| 243 // Navigate to |main_frame_url| and check all the expectations. |
| 244 NavigateToURLAndCheckSubresources(main_frame_url); |
| 245 ClearCache(); |
| 246 // It is needed to have at least two resource hits to trigger prefetch. |
| 247 NavigateToURLAndCheckSubresources(main_frame_url); |
| 248 ClearCache(); |
| 249 // Prefetch all needed resources and change expectations so that all |
| 250 // cacheable resources should be served from cache next navigation. |
| 251 PrefetchURL(main_frame_url); |
| 252 NavigateToURLAndCheckSubresources(main_frame_url); |
| 253 } |
| 254 |
211 void NavigateToURLAndCheckSubresources( | 255 void NavigateToURLAndCheckSubresources( |
212 const GURL& main_frame_url, | 256 const GURL& main_frame_url, |
213 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { | 257 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { |
214 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); | 258 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); |
215 std::vector<URLRequestSummary> url_request_summaries; | 259 std::vector<URLRequestSummary> url_request_summaries; |
216 for (const auto& kv : resources_) { | 260 for (const auto& kv : resources_) { |
217 if (kv.second.is_no_store || !kv.second.should_be_recorded) | 261 if (kv.second.is_no_store || !kv.second.should_be_recorded) |
218 continue; | 262 continue; |
219 url_request_summaries.push_back( | 263 url_request_summaries.push_back( |
220 GetURLRequestSummaryForResource(endpoint_url, kv.second)); | 264 GetURLRequestSummaryForResource(endpoint_url, kv.second)); |
221 } | 265 } |
222 ResourcePrefetchPredictorTestObserver observer( | 266 LearningObserver observer( |
223 predictor_, UpdateAndGetVisitCount(main_frame_url), | 267 predictor_, UpdateAndGetVisitCount(main_frame_url), |
224 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), | 268 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), |
225 url_request_summaries), | 269 url_request_summaries), |
226 true); // Matching navigation id by default | 270 true); // Matching navigation id by default |
227 ui_test_utils::NavigateToURLWithDisposition( | 271 ui_test_utils::NavigateToURLWithDisposition( |
228 browser(), main_frame_url, disposition, | 272 browser(), main_frame_url, disposition, |
229 ui_test_utils::BROWSER_TEST_NONE); | 273 ui_test_utils::BROWSER_TEST_NONE); |
230 observer.Wait(); | 274 observer.Wait(); |
| 275 for (auto& kv : resources_) { |
| 276 if (!kv.second.is_no_store && kv.second.should_be_recorded) |
| 277 kv.second.request.was_cached = true; |
| 278 } |
| 279 } |
| 280 |
| 281 void PrefetchURL(const GURL& main_frame_url) { |
| 282 PrefetchingObserver observer(predictor_, main_frame_url); |
| 283 predictor_->StartPrefetching(main_frame_url, PrefetchOrigin::EXTERNAL); |
| 284 observer.Wait(); |
| 285 for (auto& kv : resources_) { |
| 286 if (!kv.second.is_no_store && kv.second.should_be_recorded) |
| 287 kv.second.request.was_cached = true; |
| 288 } |
231 } | 289 } |
232 | 290 |
233 ResourceSummary* AddResource(const GURL& resource_url, | 291 ResourceSummary* AddResource(const GURL& resource_url, |
234 content::ResourceType resource_type, | 292 content::ResourceType resource_type, |
235 net::RequestPriority priority) { | 293 net::RequestPriority priority) { |
236 auto pair_and_whether_inserted = | 294 auto pair_and_whether_inserted = |
237 resources_.insert(std::make_pair(resource_url, ResourceSummary())); | 295 resources_.insert(std::make_pair(resource_url, ResourceSummary())); |
238 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url | 296 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url |
239 << " was inserted twice"; | 297 << " was inserted twice"; |
240 ResourceSummary* resource = &pair_and_whether_inserted.first->second; | 298 ResourceSummary* resource = &pair_and_whether_inserted.first->second; |
(...skipping 25 matching lines...) Expand all Loading... |
266 const std::vector<RedirectEdge>& redirect_chain) { | 324 const std::vector<RedirectEdge>& redirect_chain) { |
267 ASSERT_FALSE(redirect_chain.empty()); | 325 ASSERT_FALSE(redirect_chain.empty()); |
268 GURL current = initial_url; | 326 GURL current = initial_url; |
269 for (const auto& edge : redirect_chain) { | 327 for (const auto& edge : redirect_chain) { |
270 auto result = redirects_.insert(std::make_pair(current, edge)); | 328 auto result = redirects_.insert(std::make_pair(current, edge)); |
271 EXPECT_TRUE(result.second) << current << " already has a redirect."; | 329 EXPECT_TRUE(result.second) << current << " already has a redirect."; |
272 current = edge.url; | 330 current = edge.url; |
273 } | 331 } |
274 } | 332 } |
275 | 333 |
| 334 void ClearCache() { |
| 335 chrome::ClearCache(browser()); |
| 336 for (auto& kv : resources_) |
| 337 kv.second.request.was_cached = false; |
| 338 } |
| 339 |
276 // Shortcut for convenience. | 340 // Shortcut for convenience. |
277 GURL GetURL(const std::string& path) const { | 341 GURL GetURL(const std::string& path) const { |
278 return embedded_test_server()->GetURL(path); | 342 return embedded_test_server()->GetURL(path); |
279 } | 343 } |
280 | 344 |
281 void EnableHttpsServer() { | 345 void EnableHttpsServer() { |
282 ASSERT_FALSE(https_server_); | 346 ASSERT_FALSE(https_server_); |
283 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( | 347 https_server_ = base::MakeUnique<net::EmbeddedTestServer>( |
284 net::EmbeddedTestServer::TYPE_HTTPS); | 348 net::EmbeddedTestServer::TYPE_HTTPS); |
285 https_server()->AddDefaultHandlers( | 349 https_server()->AddDefaultHandlers( |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 return ++visit_count_[main_frame_url]; | 454 return ++visit_count_[main_frame_url]; |
391 } | 455 } |
392 | 456 |
393 ResourcePrefetchPredictor* predictor_; | 457 ResourcePrefetchPredictor* predictor_; |
394 std::unique_ptr<net::EmbeddedTestServer> https_server_; | 458 std::unique_ptr<net::EmbeddedTestServer> https_server_; |
395 std::map<GURL, ResourceSummary> resources_; | 459 std::map<GURL, ResourceSummary> resources_; |
396 std::map<GURL, RedirectEdge> redirects_; | 460 std::map<GURL, RedirectEdge> redirects_; |
397 std::map<GURL, size_t> visit_count_; | 461 std::map<GURL, size_t> visit_count_; |
398 }; | 462 }; |
399 | 463 |
400 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningSimple) { | 464 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Simple) { |
401 // These resources have default priorities that correspond to | 465 // These resources have default priorities that correspond to |
402 // blink::typeToPriority function. | 466 // blink::typeToPriority function. |
403 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 467 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
404 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 468 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
405 net::HIGHEST); | 469 net::HIGHEST); |
406 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 470 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
407 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 471 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
408 net::HIGHEST); | 472 net::HIGHEST); |
409 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath)); | 473 TestLearningAndPrefetching(GetURL(kHtmlSubresourcesPath)); |
410 } | 474 } |
411 | 475 |
412 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 476 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, Redirect) { |
413 LearningAfterRedirect) { | |
414 AddRedirectChain(GetURL(kRedirectPath), {{net::HTTP_MOVED_PERMANENTLY, | 477 AddRedirectChain(GetURL(kRedirectPath), {{net::HTTP_MOVED_PERMANENTLY, |
415 GetURL(kHtmlSubresourcesPath)}}); | 478 GetURL(kHtmlSubresourcesPath)}}); |
416 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 479 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
417 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 480 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
418 net::HIGHEST); | 481 net::HIGHEST); |
419 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 482 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
420 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 483 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
421 net::HIGHEST); | 484 net::HIGHEST); |
422 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 485 TestLearningAndPrefetching(GetURL(kRedirectPath)); |
423 } | 486 } |
424 | 487 |
425 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 488 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, RedirectChain) { |
426 LearningAfterRedirectChain) { | |
427 AddRedirectChain(GetURL(kRedirectPath), | 489 AddRedirectChain(GetURL(kRedirectPath), |
428 {{net::HTTP_FOUND, GetURL(kRedirectPath2)}, | 490 {{net::HTTP_FOUND, GetURL(kRedirectPath2)}, |
429 {net::HTTP_MOVED_PERMANENTLY, GetURL(kRedirectPath3)}, | 491 {net::HTTP_MOVED_PERMANENTLY, GetURL(kRedirectPath3)}, |
430 {net::HTTP_FOUND, GetURL(kHtmlSubresourcesPath)}}); | 492 {net::HTTP_FOUND, GetURL(kHtmlSubresourcesPath)}}); |
431 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 493 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
432 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 494 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
433 net::HIGHEST); | 495 net::HIGHEST); |
434 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 496 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
435 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | 497 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, |
436 net::HIGHEST); | 498 net::HIGHEST); |
437 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 499 TestLearningAndPrefetching(GetURL(kRedirectPath)); |
438 } | 500 } |
439 | 501 |
440 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 502 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
441 LearningAfterHttpToHttpsRedirect) { | 503 LearningAfterHttpToHttpsRedirect) { |
442 EnableHttpsServer(); | 504 EnableHttpsServer(); |
443 AddRedirectChain(GetURL(kRedirectPath), | 505 AddRedirectChain(GetURL(kRedirectPath), |
444 {{net::HTTP_FOUND, https_server()->GetURL(kRedirectPath2)}, | 506 {{net::HTTP_MOVED_PERMANENTLY, |
445 {net::HTTP_MOVED_PERMANENTLY, | |
446 https_server()->GetURL(kHtmlSubresourcesPath)}}); | 507 https_server()->GetURL(kHtmlSubresourcesPath)}}); |
447 AddResource(https_server()->GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, | 508 AddResource(https_server()->GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, |
448 net::LOWEST); | 509 net::LOWEST); |
449 AddResource(https_server()->GetURL(kStylePath), | 510 AddResource(https_server()->GetURL(kStylePath), |
450 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 511 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); |
451 AddResource(https_server()->GetURL(kScriptPath), | 512 AddResource(https_server()->GetURL(kScriptPath), |
452 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 513 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
453 AddResource(https_server()->GetURL(kFontPath), | 514 AddResource(https_server()->GetURL(kFontPath), |
454 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | 515 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); |
455 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 516 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); |
| 517 // TODO(alexilin): Test learning and prefetching once crbug.com/650246 is |
| 518 // fixed. |
456 } | 519 } |
457 | 520 |
458 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 521 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
459 LearningJavascriptDocumentWrite) { | 522 JavascriptDocumentWrite) { |
460 auto externalScript = | 523 auto externalScript = |
461 AddExternalResource(GetURL(kScriptDocumentWritePath), | 524 AddExternalResource(GetURL(kScriptDocumentWritePath), |
462 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 525 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
463 externalScript->request.mime_type = kJavascriptMime; | 526 externalScript->request.mime_type = kJavascriptMime; |
464 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 527 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
465 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 528 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
466 net::HIGHEST); | 529 net::HIGHEST); |
467 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 530 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
468 NavigateToURLAndCheckSubresources(GetURL(kHtmlDocumentWritePath)); | 531 TestLearningAndPrefetching(GetURL(kHtmlDocumentWritePath)); |
469 } | 532 } |
470 | 533 |
471 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 534 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
472 LearningJavascriptAppendChild) { | 535 JavascriptAppendChild) { |
473 auto externalScript = | 536 auto externalScript = |
474 AddExternalResource(GetURL(kScriptAppendChildPath), | 537 AddExternalResource(GetURL(kScriptAppendChildPath), |
475 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 538 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
476 externalScript->request.mime_type = kJavascriptMime; | 539 externalScript->request.mime_type = kJavascriptMime; |
477 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 540 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
478 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 541 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
479 net::HIGHEST); | 542 net::HIGHEST); |
480 // This script has net::LOWEST priority because it's executed asynchronously. | 543 // This script has net::LOWEST priority because it's executed asynchronously. |
481 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::LOWEST); | 544 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::LOWEST); |
482 NavigateToURLAndCheckSubresources(GetURL(kHtmlAppendChildPath)); | 545 TestLearningAndPrefetching(GetURL(kHtmlAppendChildPath)); |
483 } | 546 } |
484 | 547 |
485 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 548 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
486 LearningJavascriptInnerHtml) { | 549 JavascriptInnerHtml) { |
487 auto externalScript = AddExternalResource( | 550 auto externalScript = AddExternalResource( |
488 GetURL(kScriptInnerHtmlPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 551 GetURL(kScriptInnerHtmlPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
489 externalScript->request.mime_type = kJavascriptMime; | 552 externalScript->request.mime_type = kJavascriptMime; |
490 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 553 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
491 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | 554 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, |
492 net::HIGHEST); | 555 net::HIGHEST); |
493 // https://www.w3.org/TR/2014/REC-html5-20141028/scripting-1.html#the-script-e
lement | 556 // https://www.w3.org/TR/2014/REC-html5-20141028/scripting-1.html#the-script-e
lement |
494 // Script elements don't execute when inserted using innerHTML attribute. | 557 // Script elements don't execute when inserted using innerHTML attribute. |
495 AddUnrecordedResources({GetURL(kScriptPath)}); | 558 AddUnrecordedResources({GetURL(kScriptPath)}); |
496 NavigateToURLAndCheckSubresources(GetURL(kHtmlInnerHtmlPath)); | 559 TestLearningAndPrefetching(GetURL(kHtmlInnerHtmlPath)); |
497 } | 560 } |
498 | 561 |
499 // Requests originated by XMLHttpRequest have content::RESOURCE_TYPE_XHR. | 562 // Requests originated by XMLHttpRequest have content::RESOURCE_TYPE_XHR. |
500 // Actual resource type is inferred from the mime-type. | 563 // Actual resource type is inferred from the mime-type. |
501 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 564 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, JavascriptXHR) { |
502 LearningJavascriptXHR) { | |
503 auto externalScript = AddExternalResource( | 565 auto externalScript = AddExternalResource( |
504 GetURL(kScriptXHRPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 566 GetURL(kScriptXHRPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
505 externalScript->request.mime_type = kJavascriptMime; | 567 externalScript->request.mime_type = kJavascriptMime; |
506 auto image = AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, | 568 auto image = AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, |
507 net::HIGHEST); | 569 net::HIGHEST); |
508 image->request.mime_type = kImageMime; | 570 image->request.mime_type = kImageMime; |
509 auto style = AddResource(GetURL(kStylePath), | 571 auto style = AddResource(GetURL(kStylePath), |
510 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 572 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); |
511 style->request.mime_type = kStyleMime; | 573 style->request.mime_type = kStyleMime; |
512 auto script = AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, | 574 auto script = AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, |
513 net::HIGHEST); | 575 net::HIGHEST); |
514 script->request.mime_type = kJavascriptMime; | 576 script->request.mime_type = kJavascriptMime; |
515 NavigateToURLAndCheckSubresources(GetURL(kHtmlXHRPath)); | 577 TestLearningAndPrefetching(GetURL(kHtmlXHRPath)); |
516 } | 578 } |
517 | 579 |
518 // ResourcePrefetchPredictor ignores all resources requested from subframes. | 580 // ResourcePrefetchPredictor ignores all resources requested from subframes. |
519 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, | 581 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, |
520 LearningWithIframe) { | 582 IframeShouldBeIgnored) { |
521 // Included from html_iframe.html. | 583 // Included from html_iframe.html. |
522 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 584 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
523 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, | 585 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, |
524 net::HIGHEST); | 586 net::HIGHEST); |
525 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 587 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
526 // Included from <iframe src="html_subresources.html"> and not recored. | 588 // Included from <iframe src="html_subresources.html"> and not recored. |
527 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), | 589 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), |
528 GetURL(kScriptPath), GetURL(kFontPath)}); | 590 GetURL(kScriptPath), GetURL(kFontPath)}); |
529 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath)); | 591 TestLearningAndPrefetching(GetURL(kHtmlIframePath)); |
530 } | 592 } |
531 | 593 |
532 } // namespace predictors | 594 } // namespace predictors |
OLD | NEW |