| 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/tabs/tab_strip_model.h" | 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/test/base/in_process_browser_test.h" | 13 #include "chrome/test/base/in_process_browser_test.h" |
| 14 #include "chrome/test/base/ui_test_utils.h" | 14 #include "chrome/test/base/ui_test_utils.h" |
| 15 #include "content/public/browser/render_frame_host.h" | 15 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/browser/render_process_host.h" | 16 #include "content/public/browser/render_process_host.h" |
| 17 #include "net/test/embedded_test_server/http_request.h" | 17 #include "net/test/embedded_test_server/http_request.h" |
| 18 #include "net/test/embedded_test_server/http_response.h" | 18 #include "net/test/embedded_test_server/http_response.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 21 |
| 22 namespace predictors { | 22 namespace predictors { |
| 23 | 23 |
| 24 namespace { |
| 25 |
| 24 static const char kImagePath[] = "/predictors/image.png"; | 26 static const char kImagePath[] = "/predictors/image.png"; |
| 25 static const char kStylePath[] = "/predictors/style.css"; | 27 static const char kStylePath[] = "/predictors/style.css"; |
| 26 static const char kScriptPath[] = "/predictors/script.js"; | 28 static const char kScriptPath[] = "/predictors/script.js"; |
| 27 static const char kFontPath[] = "/predictors/font.ttf"; | 29 static const char kFontPath[] = "/predictors/font.ttf"; |
| 28 static const char kHtmlSubresourcesPath[] = | 30 static const char kHtmlSubresourcesPath[] = |
| 29 "/predictors/html_subresources.html"; | 31 "/predictors/html_subresources.html"; |
| 30 static const char kRedirectPath[] = "/predictors/redirect.html"; | 32 static const char kRedirectPath[] = "/predictors/redirect.html"; |
| 31 static const char kRedirectPath2[] = "/predictors/redirect2.html"; | 33 static const char kRedirectPath2[] = "/predictors/redirect2.html"; |
| 32 static const char kRedirectPath3[] = "/predictors/redirect3.html"; | 34 static const char kRedirectPath3[] = "/predictors/redirect3.html"; |
| 33 | 35 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 54 void OnPredictorInitialized() override { run_loop_.Quit(); } | 56 void OnPredictorInitialized() override { run_loop_.Quit(); } |
| 55 | 57 |
| 56 void Wait() { run_loop_.Run(); } | 58 void Wait() { run_loop_.Run(); } |
| 57 | 59 |
| 58 private: | 60 private: |
| 59 base::RunLoop run_loop_; | 61 base::RunLoop run_loop_; |
| 60 | 62 |
| 61 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); | 63 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); |
| 62 }; | 64 }; |
| 63 | 65 |
| 66 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
| 67 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
| 68 |
| 69 std::vector<URLRequestSummary> GetUniqueSubresources( |
| 70 const PageRequestSummary& summary) { |
| 71 std::vector<URLRequestSummary> subresources(summary.subresource_requests); |
| 72 std::stable_sort(subresources.begin(), subresources.end(), |
| 73 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
| 74 return x.resource_url < y.resource_url; |
| 75 }); |
| 76 subresources.erase( |
| 77 std::unique(subresources.begin(), subresources.end(), |
| 78 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
| 79 return x.resource_url == y.resource_url; |
| 80 }), |
| 81 subresources.end()); |
| 82 return subresources; |
| 83 } |
| 84 |
| 85 } // namespace |
| 86 |
| 64 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. | 87 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. |
| 65 // These events are also used to verify that ResourcePrefetchPredictor works as | 88 // These events are also used to verify that ResourcePrefetchPredictor works as |
| 66 // expected. | 89 // expected. |
| 67 class ResourcePrefetchPredictorTestObserver : public TestObserver { | 90 class ResourcePrefetchPredictorTestObserver : public TestObserver { |
| 68 public: | 91 public: |
| 69 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 92 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
| 70 | 93 |
| 71 explicit ResourcePrefetchPredictorTestObserver( | 94 explicit ResourcePrefetchPredictorTestObserver( |
| 72 ResourcePrefetchPredictor* predictor, | 95 ResourcePrefetchPredictor* predictor, |
| 73 const size_t expected_url_visit_count, | 96 const size_t expected_url_visit_count, |
| 74 const PageRequestSummary& expected_summary) | 97 const PageRequestSummary& expected_summary) |
| 75 : TestObserver(predictor), | 98 : TestObserver(predictor), |
| 76 url_visit_count_(expected_url_visit_count), | 99 url_visit_count_(expected_url_visit_count), |
| 77 summary_(expected_summary) {} | 100 summary_(expected_summary) {} |
| 78 | 101 |
| 79 // TestObserver: | 102 // TestObserver: |
| 80 void OnNavigationLearned(size_t url_visit_count, | 103 void OnNavigationLearned(size_t url_visit_count, |
| 81 const PageRequestSummary& summary) override { | 104 const PageRequestSummary& summary) override { |
| 82 EXPECT_EQ(url_visit_count, url_visit_count_); | 105 EXPECT_EQ(url_visit_count, url_visit_count_); |
| 83 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | 106 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); |
| 84 EXPECT_EQ(summary.initial_url, summary_.initial_url); | 107 EXPECT_EQ(summary.initial_url, summary_.initial_url); |
| 85 EXPECT_THAT( | 108 // Duplicate resources can be observed in a single navigation but |
| 86 summary.subresource_requests, | 109 // ResourcePrefetchPredictor only cares about the first occurrence of each. |
| 87 testing::UnorderedElementsAreArray(summary_.subresource_requests)); | 110 std::vector<ResourcePrefetchPredictor::URLRequestSummary> subresources = |
| 111 GetUniqueSubresources(summary); |
| 112 EXPECT_THAT(subresources, testing::UnorderedElementsAreArray( |
| 113 summary_.subresource_requests)); |
| 88 run_loop_.Quit(); | 114 run_loop_.Quit(); |
| 89 } | 115 } |
| 90 | 116 |
| 91 void Wait() { run_loop_.Run(); } | 117 void Wait() { run_loop_.Run(); } |
| 92 | 118 |
| 93 private: | 119 private: |
| 94 base::RunLoop run_loop_; | 120 base::RunLoop run_loop_; |
| 95 size_t url_visit_count_; | 121 size_t url_visit_count_; |
| 96 PageRequestSummary summary_; | 122 PageRequestSummary summary_; |
| 97 | 123 |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 AddResource(https_server()->GetURL(kStylePath), | 366 AddResource(https_server()->GetURL(kStylePath), |
| 341 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | 367 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); |
| 342 AddResource(https_server()->GetURL(kScriptPath), | 368 AddResource(https_server()->GetURL(kScriptPath), |
| 343 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 369 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
| 344 AddResource(https_server()->GetURL(kFontPath), | 370 AddResource(https_server()->GetURL(kFontPath), |
| 345 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | 371 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); |
| 346 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); | 372 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); |
| 347 } | 373 } |
| 348 | 374 |
| 349 } // namespace predictors | 375 } // namespace predictors |
| OLD | NEW |