| 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" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 | 84 |
| 85 private: | 85 private: |
| 86 base::RunLoop run_loop_; | 86 base::RunLoop run_loop_; |
| 87 | 87 |
| 88 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); | 88 DISALLOW_COPY_AND_ASSIGN(InitializationObserver); |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 91 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
| 92 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 92 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
| 93 | 93 |
| 94 std::vector<URLRequestSummary> GetUniqueSubresources( | 94 void RemoveDuplicateSubresources(std::vector<URLRequestSummary>* subresources) { |
| 95 const PageRequestSummary& summary) { | 95 std::stable_sort(subresources->begin(), subresources->end(), |
| 96 std::vector<URLRequestSummary> subresources(summary.subresource_requests); | |
| 97 std::stable_sort(subresources.begin(), subresources.end(), | |
| 98 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 96 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
| 99 return x.resource_url < y.resource_url; | 97 return x.resource_url < y.resource_url; |
| 100 }); | 98 }); |
| 101 subresources.erase( | 99 subresources->erase( |
| 102 std::unique(subresources.begin(), subresources.end(), | 100 std::unique(subresources->begin(), subresources->end(), |
| 103 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 101 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
| 104 return x.resource_url == y.resource_url; | 102 return x.resource_url == y.resource_url; |
| 105 }), | 103 }), |
| 106 subresources.end()); | 104 subresources->end()); |
| 107 return subresources; | 105 } |
| 106 |
| 107 // Fill a NavigationID with "empty" data that does not trigger |
| 108 // the is_valid DCHECK(). Allows comparing. |
| 109 void SetValidNavigationID(NavigationID* navigation_id) { |
| 110 navigation_id->render_process_id = 0; |
| 111 navigation_id->render_frame_id = 0; |
| 112 navigation_id->main_frame_url = GURL("http://127.0.0.1"); |
| 113 } |
| 114 |
| 115 // Does a custom comparison of subresources of URLRequestSummary |
| 116 // and fail the test if the expectation is not met. |
| 117 void CompareSubresources(std::vector<URLRequestSummary> actual_subresources, |
| 118 std::vector<URLRequestSummary> expected_subresources, |
| 119 bool match_navigation_id) { |
| 120 // Duplicate resources can be observed in a single navigation but |
| 121 // ResourcePrefetchPredictor only cares about the first occurrence of each. |
| 122 RemoveDuplicateSubresources(&actual_subresources); |
| 123 |
| 124 if (!match_navigation_id) { |
| 125 for (auto& subresource : actual_subresources) |
| 126 SetValidNavigationID(&subresource.navigation_id); |
| 127 for (auto& subresource : expected_subresources) |
| 128 SetValidNavigationID(&subresource.navigation_id); |
| 129 } |
| 130 EXPECT_THAT(actual_subresources, |
| 131 testing::UnorderedElementsAreArray(expected_subresources)); |
| 108 } | 132 } |
| 109 | 133 |
| 110 } // namespace | 134 } // namespace |
| 111 | 135 |
| 112 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. | 136 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. |
| 113 // These events are also used to verify that ResourcePrefetchPredictor works as | 137 // These events are also used to verify that ResourcePrefetchPredictor works as |
| 114 // expected. | 138 // expected. |
| 115 class ResourcePrefetchPredictorTestObserver : public TestObserver { | 139 class ResourcePrefetchPredictorTestObserver : public TestObserver { |
| 116 public: | 140 public: |
| 117 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 141 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
| 118 | 142 |
| 119 explicit ResourcePrefetchPredictorTestObserver( | 143 explicit ResourcePrefetchPredictorTestObserver( |
| 120 ResourcePrefetchPredictor* predictor, | 144 ResourcePrefetchPredictor* predictor, |
| 121 const size_t expected_url_visit_count, | 145 const size_t expected_url_visit_count, |
| 122 const PageRequestSummary& expected_summary) | 146 const PageRequestSummary& expected_summary, |
| 147 bool match_navigation_id) |
| 123 : TestObserver(predictor), | 148 : TestObserver(predictor), |
| 124 url_visit_count_(expected_url_visit_count), | 149 url_visit_count_(expected_url_visit_count), |
| 125 summary_(expected_summary) {} | 150 summary_(expected_summary), |
| 151 match_navigation_id_(match_navigation_id) {} |
| 126 | 152 |
| 127 // TestObserver: | 153 // TestObserver: |
| 128 void OnNavigationLearned(size_t url_visit_count, | 154 void OnNavigationLearned(size_t url_visit_count, |
| 129 const PageRequestSummary& summary) override { | 155 const PageRequestSummary& summary) override { |
| 130 EXPECT_EQ(url_visit_count, url_visit_count_); | 156 EXPECT_EQ(url_visit_count, url_visit_count_); |
| 131 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | 157 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); |
| 132 EXPECT_EQ(summary.initial_url, summary_.initial_url); | 158 EXPECT_EQ(summary.initial_url, summary_.initial_url); |
| 133 // Duplicate resources can be observed in a single navigation but | 159 CompareSubresources(summary.subresource_requests, |
| 134 // ResourcePrefetchPredictor only cares about the first occurrence of each. | 160 summary_.subresource_requests, match_navigation_id_); |
| 135 std::vector<ResourcePrefetchPredictor::URLRequestSummary> subresources = | |
| 136 GetUniqueSubresources(summary); | |
| 137 EXPECT_THAT(subresources, testing::UnorderedElementsAreArray( | |
| 138 summary_.subresource_requests)); | |
| 139 run_loop_.Quit(); | 161 run_loop_.Quit(); |
| 140 } | 162 } |
| 141 | 163 |
| 142 void Wait() { run_loop_.Run(); } | 164 void Wait() { run_loop_.Run(); } |
| 143 | 165 |
| 144 private: | 166 private: |
| 145 base::RunLoop run_loop_; | 167 base::RunLoop run_loop_; |
| 146 size_t url_visit_count_; | 168 size_t url_visit_count_; |
| 147 PageRequestSummary summary_; | 169 PageRequestSummary summary_; |
| 170 bool match_navigation_id_; |
| 148 | 171 |
| 149 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); | 172 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); |
| 150 }; | 173 }; |
| 151 | 174 |
| 152 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { | 175 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { |
| 153 protected: | 176 protected: |
| 154 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 177 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
| 155 | 178 |
| 156 void SetUpCommandLine(base::CommandLine* command_line) override { | 179 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 157 command_line->AppendSwitchASCII( | 180 command_line->AppendSwitchASCII( |
| 158 switches::kSpeculativeResourcePrefetching, | 181 switches::kSpeculativeResourcePrefetching, |
| 159 switches::kSpeculativeResourcePrefetchingEnabled); | 182 switches::kSpeculativeResourcePrefetchingEnabled); |
| 160 } | 183 } |
| 161 | 184 |
| 162 void SetUpOnMainThread() override { | 185 void SetUpOnMainThread() override { |
| 163 embedded_test_server()->RegisterRequestHandler( | 186 embedded_test_server()->RegisterRequestHandler( |
| 164 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 187 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
| 165 base::Unretained(this))); | 188 base::Unretained(this))); |
| 166 embedded_test_server()->RegisterRequestHandler( | 189 embedded_test_server()->RegisterRequestHandler( |
| 167 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 190 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
| 168 base::Unretained(this))); | 191 base::Unretained(this))); |
| 169 ASSERT_TRUE(embedded_test_server()->Start()); | 192 ASSERT_TRUE(embedded_test_server()->Start()); |
| 170 predictor_ = | 193 predictor_ = |
| 171 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 194 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
| 172 ASSERT_TRUE(predictor_); | 195 ASSERT_TRUE(predictor_); |
| 173 EnsurePredictorInitialized(); | 196 EnsurePredictorInitialized(); |
| 174 } | 197 } |
| 175 | 198 |
| 176 void NavigateToURLAndCheckSubresources(const GURL& main_frame_url) { | 199 void NavigateToURLAndCheckSubresources( |
| 200 const GURL& main_frame_url, |
| 201 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { |
| 177 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); | 202 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); |
| 178 std::vector<URLRequestSummary> url_request_summaries; | 203 std::vector<URLRequestSummary> url_request_summaries; |
| 179 for (const auto& kv : resources_) { | 204 for (const auto& kv : resources_) { |
| 180 if (kv.second.is_no_store || !kv.second.should_be_recorded) | 205 if (kv.second.is_no_store || !kv.second.should_be_recorded) |
| 181 continue; | 206 continue; |
| 182 url_request_summaries.push_back( | 207 url_request_summaries.push_back( |
| 183 GetURLRequestSummaryForResource(endpoint_url, kv.second)); | 208 GetURLRequestSummaryForResource(endpoint_url, kv.second)); |
| 184 } | 209 } |
| 185 ResourcePrefetchPredictorTestObserver observer( | 210 ResourcePrefetchPredictorTestObserver observer( |
| 186 predictor_, UpdateAndGetVisitCount(main_frame_url), | 211 predictor_, UpdateAndGetVisitCount(main_frame_url), |
| 187 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), | 212 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), |
| 188 url_request_summaries)); | 213 url_request_summaries), |
| 189 ui_test_utils::NavigateToURL(browser(), main_frame_url); | 214 true); // Matching navigation id by default |
| 215 ui_test_utils::NavigateToURLWithDisposition( |
| 216 browser(), main_frame_url, disposition, |
| 217 ui_test_utils::BROWSER_TEST_NONE); |
| 190 observer.Wait(); | 218 observer.Wait(); |
| 191 } | 219 } |
| 192 | 220 |
| 193 ResourceSummary* AddResource(const GURL& resource_url, | 221 ResourceSummary* AddResource(const GURL& resource_url, |
| 194 content::ResourceType resource_type, | 222 content::ResourceType resource_type, |
| 195 net::RequestPriority priority) { | 223 net::RequestPriority priority) { |
| 196 auto pair_and_whether_inserted = | 224 auto pair_and_whether_inserted = |
| 197 resources_.insert(std::make_pair(resource_url, ResourceSummary())); | 225 resources_.insert(std::make_pair(resource_url, ResourceSummary())); |
| 198 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url | 226 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url |
| 199 << " was inserted twice"; | 227 << " was inserted twice"; |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, | 511 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, |
| 484 net::HIGHEST); | 512 net::HIGHEST); |
| 485 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | 513 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); |
| 486 // Included from <iframe src="html_subresources.html"> and not recored. | 514 // Included from <iframe src="html_subresources.html"> and not recored. |
| 487 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), | 515 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), |
| 488 GetURL(kScriptPath), GetURL(kFontPath)}); | 516 GetURL(kScriptPath), GetURL(kFontPath)}); |
| 489 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath)); | 517 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath)); |
| 490 } | 518 } |
| 491 | 519 |
| 492 } // namespace predictors | 520 } // namespace predictors |
| OLD | NEW |