Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc

Issue 2561493003: Rework of ResourcePrefetchPredictor browser tests infrastructure. (Closed)
Patch Set: Post-Review Modifications Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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) {
Benoit L 2016/12/08 15:07:44 nit: I think it would be clearer to change this to
ahemery 2016/12/09 14:19:16 Changed
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
118 // Does a custom comparison of subresources of URLRequestSummary
119 // and fail the test if the expectation is not met.
120 void CompareSubresources(const PageRequestSummary& actual_summary,
121 const PageRequestSummary& expected_summary,
122 bool match_navigation_id) {
123 // Duplicate resources can be observed in a single navigation but
124 // ResourcePrefetchPredictor only cares about the first occurrence of each.
125 std::vector<ResourcePrefetchPredictor::URLRequestSummary>
126 actual_subresources = GetUniqueSubresources(actual_summary);
127 std::vector<ResourcePrefetchPredictor::URLRequestSummary>
128 expected_subresources(expected_summary.subresource_requests);
129 if (!match_navigation_id) {
130 for (auto& subresource : actual_subresources)
131 EmptyNavigationId(&subresource.navigation_id);
132 for (auto& subresource : expected_subresources)
133 EmptyNavigationId(&subresource.navigation_id);
134 }
135 EXPECT_THAT(actual_subresources,
136 testing::UnorderedElementsAreArray(expected_subresources));
137 }
138
110 } // namespace 139 } // namespace
111 140
112 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. 141 // Helper class to track and allow waiting for ResourcePrefetchPredictor events.
113 // These events are also used to verify that ResourcePrefetchPredictor works as 142 // These events are also used to verify that ResourcePrefetchPredictor works as
114 // expected. 143 // expected.
115 class ResourcePrefetchPredictorTestObserver : public TestObserver { 144 class ResourcePrefetchPredictorTestObserver : public TestObserver {
116 public: 145 public:
117 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; 146 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary;
118 147
119 explicit ResourcePrefetchPredictorTestObserver( 148 explicit ResourcePrefetchPredictorTestObserver(
120 ResourcePrefetchPredictor* predictor, 149 ResourcePrefetchPredictor* predictor,
121 const size_t expected_url_visit_count, 150 const size_t expected_url_visit_count,
122 const PageRequestSummary& expected_summary) 151 const PageRequestSummary& expected_summary,
152 bool match_navigation_id)
123 : TestObserver(predictor), 153 : TestObserver(predictor),
124 url_visit_count_(expected_url_visit_count), 154 url_visit_count_(expected_url_visit_count),
125 summary_(expected_summary) {} 155 summary_(expected_summary),
156 match_navigation_id_(match_navigation_id) {}
126 157
127 // TestObserver: 158 // TestObserver:
128 void OnNavigationLearned(size_t url_visit_count, 159 void OnNavigationLearned(size_t url_visit_count,
129 const PageRequestSummary& summary) override { 160 const PageRequestSummary& summary) override {
130 EXPECT_EQ(url_visit_count, url_visit_count_); 161 EXPECT_EQ(url_visit_count, url_visit_count_);
131 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); 162 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url);
132 EXPECT_EQ(summary.initial_url, summary_.initial_url); 163 EXPECT_EQ(summary.initial_url, summary_.initial_url);
133 // Duplicate resources can be observed in a single navigation but 164 CompareSubresources(summary, summary_, match_navigation_id_);
alexilin 2016/12/08 12:57:25 nit: I found a little be confusing that the functi
ahemery 2016/12/09 14:19:16 Modified. The copy is now done by parameters and d
134 // ResourcePrefetchPredictor only cares about the first occurrence of each.
135 std::vector<ResourcePrefetchPredictor::URLRequestSummary> subresources =
136 GetUniqueSubresources(summary);
137 EXPECT_THAT(subresources, testing::UnorderedElementsAreArray(
138 summary_.subresource_requests));
139 run_loop_.Quit(); 165 run_loop_.Quit();
140 } 166 }
141 167
142 void Wait() { run_loop_.Run(); } 168 void Wait() { run_loop_.Run(); }
143 169
144 private: 170 private:
145 base::RunLoop run_loop_; 171 base::RunLoop run_loop_;
146 size_t url_visit_count_; 172 size_t url_visit_count_;
147 PageRequestSummary summary_; 173 PageRequestSummary summary_;
174 bool match_navigation_id_;
148 175
149 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); 176 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver);
150 }; 177 };
151 178
152 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { 179 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest {
153 protected: 180 protected:
154 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; 181 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
155 182
156 void SetUpCommandLine(base::CommandLine* command_line) override { 183 void SetUpCommandLine(base::CommandLine* command_line) override {
157 command_line->AppendSwitchASCII( 184 command_line->AppendSwitchASCII(
158 switches::kSpeculativeResourcePrefetching, 185 switches::kSpeculativeResourcePrefetching,
159 switches::kSpeculativeResourcePrefetchingEnabled); 186 switches::kSpeculativeResourcePrefetchingEnabled);
160 } 187 }
161 188
162 void SetUpOnMainThread() override { 189 void SetUpOnMainThread() override {
163 embedded_test_server()->RegisterRequestHandler( 190 embedded_test_server()->RegisterRequestHandler(
164 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest, 191 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRedirectRequest,
165 base::Unretained(this))); 192 base::Unretained(this)));
166 embedded_test_server()->RegisterRequestHandler( 193 embedded_test_server()->RegisterRequestHandler(
167 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest, 194 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleResourceRequest,
168 base::Unretained(this))); 195 base::Unretained(this)));
169 ASSERT_TRUE(embedded_test_server()->Start()); 196 ASSERT_TRUE(embedded_test_server()->Start());
170 predictor_ = 197 predictor_ =
171 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); 198 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile());
172 ASSERT_TRUE(predictor_); 199 ASSERT_TRUE(predictor_);
173 EnsurePredictorInitialized(); 200 EnsurePredictorInitialized();
174 } 201 }
175 202
176 void NavigateToURLAndCheckSubresources(const GURL& main_frame_url) { 203 void NavigateToURLAndCheckSubresources(
204 const GURL& main_frame_url,
205 WindowOpenDisposition disposition = WindowOpenDisposition::CURRENT_TAB) {
177 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); 206 GURL endpoint_url = GetRedirectEndpoint(main_frame_url);
178 std::vector<URLRequestSummary> url_request_summaries; 207 std::vector<URLRequestSummary> url_request_summaries;
179 for (const auto& kv : resources_) { 208 for (const auto& kv : resources_) {
180 if (kv.second.is_no_store || !kv.second.should_be_recorded) 209 if (kv.second.is_no_store || !kv.second.should_be_recorded)
181 continue; 210 continue;
182 url_request_summaries.push_back( 211 url_request_summaries.push_back(
183 GetURLRequestSummaryForResource(endpoint_url, kv.second)); 212 GetURLRequestSummaryForResource(endpoint_url, kv.second));
184 } 213 }
214 bool match_navigation_id =
215 (disposition == WindowOpenDisposition::CURRENT_TAB);
185 ResourcePrefetchPredictorTestObserver observer( 216 ResourcePrefetchPredictorTestObserver observer(
186 predictor_, UpdateAndGetVisitCount(main_frame_url), 217 predictor_, UpdateAndGetVisitCount(main_frame_url),
187 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), 218 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(),
188 url_request_summaries)); 219 url_request_summaries),
189 ui_test_utils::NavigateToURL(browser(), main_frame_url); 220 match_navigation_id);
221 ui_test_utils::NavigateToURLWithDisposition(
222 browser(), main_frame_url, disposition,
223 ui_test_utils::BROWSER_TEST_NONE);
190 observer.Wait(); 224 observer.Wait();
191 } 225 }
192 226
193 ResourceSummary* AddResource(const GURL& resource_url, 227 ResourceSummary* AddResource(const GURL& resource_url,
194 content::ResourceType resource_type, 228 content::ResourceType resource_type,
195 net::RequestPriority priority) { 229 net::RequestPriority priority) {
196 auto pair_and_whether_inserted = 230 auto pair_and_whether_inserted =
197 resources_.insert(std::make_pair(resource_url, ResourceSummary())); 231 resources_.insert(std::make_pair(resource_url, ResourceSummary()));
198 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url 232 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url
199 << " was inserted twice"; 233 << " was inserted twice";
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
482 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST); 516 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
483 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, 517 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET,
484 net::HIGHEST); 518 net::HIGHEST);
485 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); 519 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
486 // Included from <iframe src="html_subresources.html"> and not recored. 520 // Included from <iframe src="html_subresources.html"> and not recored.
487 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), 521 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath),
488 GetURL(kScriptPath), GetURL(kFontPath)}); 522 GetURL(kScriptPath), GetURL(kFontPath)});
489 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath)); 523 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath));
490 } 524 }
491 525
526 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningInNewTab) {
Benoit L 2016/12/08 15:07:44 It's not entirely clear to me that these tests do
ahemery 2016/12/09 14:19:16 It does not test any specific behavior but is more
Benoit L 2016/12/09 14:44:01 Ok, reading through the code in ui_test_utils seem
527 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
528 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
529 net::HIGHEST);
530 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
531 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
532 net::HIGHEST);
533 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath),
534 WindowOpenDisposition::NEW_FOREGROUND_TAB);
535 }
536
537 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningInPopup) {
538 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
539 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
540 net::HIGHEST);
541 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
542 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
543 net::HIGHEST);
544 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath),
545 WindowOpenDisposition::NEW_POPUP);
546 }
547
492 } // namespace predictors 548 } // namespace predictors
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698