Chromium Code Reviews| 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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 }); | 100 }); |
| 101 subresources.erase( | 101 subresources.erase( |
| 102 std::unique(subresources.begin(), subresources.end(), | 102 std::unique(subresources.begin(), subresources.end(), |
| 103 [](const URLRequestSummary& x, const URLRequestSummary& y) { | 103 [](const URLRequestSummary& x, const URLRequestSummary& y) { |
| 104 return x.resource_url == y.resource_url; | 104 return x.resource_url == y.resource_url; |
| 105 }), | 105 }), |
| 106 subresources.end()); | 106 subresources.end()); |
| 107 return subresources; | 107 return subresources; |
| 108 } | 108 } |
| 109 | 109 |
| 110 // Fill a NavigationID with "empty" data that does not trigger | |
| 111 // the is_valid DCHECK(). Allows comparing. | |
| 112 void EmptyNavigationId(NavigationID* navigation_id) { | |
| 113 navigation_id->render_process_id = 0; | |
| 114 navigation_id->render_frame_id = 0; | |
| 115 navigation_id->main_frame_url = GURL("http://127.0.0.1"); | |
| 116 } | |
| 117 | |
| 110 } // namespace | 118 } // namespace |
| 111 | 119 |
| 112 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. | 120 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. |
| 113 // These events are also used to verify that ResourcePrefetchPredictor works as | 121 // These events are also used to verify that ResourcePrefetchPredictor works as |
| 114 // expected. | 122 // expected. |
| 115 class ResourcePrefetchPredictorTestObserver : public TestObserver { | 123 class ResourcePrefetchPredictorTestObserver : public TestObserver { |
| 116 public: | 124 public: |
| 117 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | 125 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; |
| 118 | 126 |
| 119 explicit ResourcePrefetchPredictorTestObserver( | 127 explicit ResourcePrefetchPredictorTestObserver( |
| 120 ResourcePrefetchPredictor* predictor, | 128 ResourcePrefetchPredictor* predictor, |
| 121 const size_t expected_url_visit_count, | 129 const size_t expected_url_visit_count, |
| 122 const PageRequestSummary& expected_summary) | 130 const PageRequestSummary& expected_summary, |
| 131 bool match_navigation_id) | |
| 123 : TestObserver(predictor), | 132 : TestObserver(predictor), |
| 124 url_visit_count_(expected_url_visit_count), | 133 url_visit_count_(expected_url_visit_count), |
| 125 summary_(expected_summary) {} | 134 summary_(expected_summary), |
| 135 match_navigation_id_(match_navigation_id) {} | |
| 126 | 136 |
| 127 // TestObserver: | 137 // TestObserver: |
| 128 void OnNavigationLearned(size_t url_visit_count, | 138 void OnNavigationLearned(size_t url_visit_count, |
| 129 const PageRequestSummary& summary) override { | 139 const PageRequestSummary& summary) override { |
| 130 EXPECT_EQ(url_visit_count, url_visit_count_); | 140 EXPECT_EQ(url_visit_count, url_visit_count_); |
| 131 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | 141 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); |
| 132 EXPECT_EQ(summary.initial_url, summary_.initial_url); | 142 EXPECT_EQ(summary.initial_url, summary_.initial_url); |
| 133 // Duplicate resources can be observed in a single navigation but | 143 // Duplicate resources can be observed in a single navigation but |
| 134 // ResourcePrefetchPredictor only cares about the first occurrence of each. | 144 // ResourcePrefetchPredictor only cares about the first occurrence of each. |
| 135 std::vector<ResourcePrefetchPredictor::URLRequestSummary> subresources = | 145 std::vector<ResourcePrefetchPredictor::URLRequestSummary> |
|
alexilin
2016/12/07 16:57:12
Probably, it could be better to extract the code r
ahemery
2016/12/08 11:08:45
Extracted in CompareSubresources(...)
| |
| 136 GetUniqueSubresources(summary); | 146 subresources_results = GetUniqueSubresources(summary); |
|
alexilin
2016/12/07 16:57:11
nit: I'd prefer different naming. For example, act
ahemery
2016/12/08 11:08:44
Renamed
| |
| 137 EXPECT_THAT(subresources, testing::UnorderedElementsAreArray( | 147 std::vector<ResourcePrefetchPredictor::URLRequestSummary> subresources_ref( |
| 138 summary_.subresource_requests)); | 148 summary_.subresource_requests); |
| 149 if (!match_navigation_id_) { | |
| 150 for (auto& subresource : subresources_ref) { | |
|
alexilin
2016/12/07 16:57:11
nit: Remove curly brace.
ahemery
2016/12/08 11:08:44
Done
| |
| 151 EmptyNavigationId(&subresource.navigation_id); | |
| 152 } | |
| 153 for (auto& subresource : subresources_results) { | |
|
alexilin
2016/12/07 16:57:12
ditto
| |
| 154 EmptyNavigationId(&subresource.navigation_id); | |
| 155 } | |
| 156 } | |
| 157 EXPECT_THAT(subresources_results, | |
| 158 testing::UnorderedElementsAreArray(subresources_ref)); | |
| 139 run_loop_.Quit(); | 159 run_loop_.Quit(); |
| 140 } | 160 } |
| 141 | 161 |
| 142 void Wait() { run_loop_.Run(); } | 162 void Wait() { run_loop_.Run(); } |
| 143 | 163 |
| 144 private: | 164 private: |
| 145 base::RunLoop run_loop_; | 165 base::RunLoop run_loop_; |
| 146 size_t url_visit_count_; | 166 size_t url_visit_count_; |
| 147 PageRequestSummary summary_; | 167 PageRequestSummary summary_; |
| 168 bool match_navigation_id_; | |
| 148 | 169 |
| 149 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); | 170 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); |
| 150 }; | 171 }; |
| 151 | 172 |
| 152 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { | 173 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { |
| 153 protected: | 174 protected: |
| 154 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | 175 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; |
| 155 | 176 |
| 156 void SetUpCommandLine(base::CommandLine* command_line) override { | 177 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 157 command_line->AppendSwitchASCII( | 178 command_line->AppendSwitchASCII( |
| 158 switches::kSpeculativeResourcePrefetching, | 179 switches::kSpeculativeResourcePrefetching, |
| 159 switches::kSpeculativeResourcePrefetchingEnabled); | 180 switches::kSpeculativeResourcePrefetchingEnabled); |
| 160 } | 181 } |
| 161 | 182 |
| 162 void SetUpOnMainThread() override { | 183 void SetUpOnMainThread() override { |
| 163 embedded_test_server()->RegisterRequestHandler( | 184 embedded_test_server()->RegisterRequestHandler( |
| 164 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, | 185 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, |
| 165 base::Unretained(this))); | 186 base::Unretained(this))); |
| 166 embedded_test_server()->RegisterRequestHandler( | 187 embedded_test_server()->RegisterRequestHandler( |
| 167 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, | 188 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, |
| 168 base::Unretained(this))); | 189 base::Unretained(this))); |
| 169 ASSERT_TRUE(embedded_test_server()->Start()); | 190 ASSERT_TRUE(embedded_test_server()->Start()); |
| 170 predictor_ = | 191 predictor_ = |
| 171 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | 192 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); |
| 172 ASSERT_TRUE(predictor_); | 193 ASSERT_TRUE(predictor_); |
| 173 EnsurePredictorInitialized(); | 194 EnsurePredictorInitialized(); |
| 174 } | 195 } |
| 175 | 196 |
| 176 void NavigateToURLAndCheckSubresources(const GURL& main_frame_url) { | 197 void NavigateToURLAndCheckSubresources( |
| 198 const GURL& main_frame_url, | |
| 199 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) { | |
| 177 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); | 200 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); |
| 178 std::vector<URLRequestSummary> url_request_summaries; | 201 std::vector<URLRequestSummary> url_request_summaries; |
| 179 for (const auto& kv : resources_) { | 202 for (const auto& kv : resources_) { |
| 180 if (kv.second.is_no_store || !kv.second.should_be_recorded) | 203 if (kv.second.is_no_store || !kv.second.should_be_recorded) |
| 181 continue; | 204 continue; |
| 182 url_request_summaries.push_back( | 205 url_request_summaries.push_back( |
| 183 GetURLRequestSummaryForResource(endpoint_url, kv.second)); | 206 GetURLRequestSummaryForResource(endpoint_url, kv.second)); |
| 184 } | 207 } |
| 208 bool match_navigation_id = | |
| 209 (disposition == WindowOpenDisposition::CURRENT_TAB); | |
| 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 match_navigation_id); |
| 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 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 482 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | 510 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); |
| 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 |
| 520 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningInPopup) { | |
| 521 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 522 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 523 net::HIGHEST); | |
| 524 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 525 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
| 526 net::HIGHEST); | |
| 527 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
| 528 WindowOpenDisposition::NEW_POPUP); | |
| 529 } | |
| 530 | |
| 531 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningInNewTab) { | |
| 532 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE, | |
|
alexilin
2016/12/07 16:57:11
Why font resource became first in the list?
ahemery
2016/12/08 11:08:44
Corrected, see main comment
alexilin
2016/12/08 12:57:25
There wasn't any logical mistake it was just a lit
| |
| 533 net::HIGHEST); | |
| 534 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 535 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET, | |
| 536 net::HIGHEST); | |
| 537 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 538 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath), | |
| 539 WindowOpenDisposition::NEW_FOREGROUND_TAB); | |
| 540 } | |
| 541 | |
| 492 } // namespace predictors | 542 } // namespace predictors |
| OLD | NEW |