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

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

Issue 2561493003: Rework of ResourcePrefetchPredictor browser tests infrastructure. (Closed)
Patch Set: 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 | chrome/test/base/ui_test_utils.cc » ('j') | chrome/test/base/ui_test_utils.cc » ('J')
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) {
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)
ahemery 2016/12/07 14:13:02 New test observer parameter. If true, when compari
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>
136 GetUniqueSubresources(summary); 146 subresources_results = GetUniqueSubresources(summary);
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_) {
ahemery 2016/12/07 14:13:02 If we are not matching the navigation_id we empty
150 for (auto& subresource : subresources_ref) {
151 EmptyNavigationId(&subresource.navigation_id);
152 }
153 for (auto& subresource : subresources_results) {
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
209 bool match_navigation_id =
210 (disposition == WindowOpenDisposition::CURRENT_TAB);
211 int wait_flags = ui_test_utils::BROWSER_TEST_NONE;
212 if (disposition == WindowOpenDisposition::NEW_FOREGROUND_TAB)
213 wait_flags = ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB;
214 if (disposition == WindowOpenDisposition::NEW_POPUP)
215 wait_flags = ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER;
216
185 ResourcePrefetchPredictorTestObserver observer( 217 ResourcePrefetchPredictorTestObserver observer(
186 predictor_, UpdateAndGetVisitCount(main_frame_url), 218 predictor_, UpdateAndGetVisitCount(main_frame_url),
187 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), 219 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(),
188 url_request_summaries)); 220 url_request_summaries),
189 ui_test_utils::NavigateToURL(browser(), main_frame_url); 221 match_navigation_id);
222 ui_test_utils::NavigateToURLWithDisposition(
223 browser(), main_frame_url, disposition,
224 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION | wait_flags);
alexilin 2016/12/07 16:57:11 From offline discussion: we don't have to wait for
190 observer.Wait(); 225 observer.Wait();
191 } 226 }
192 227
193 ResourceSummary* AddResource(const GURL& resource_url, 228 ResourceSummary* AddResource(const GURL& resource_url,
194 content::ResourceType resource_type, 229 content::ResourceType resource_type,
195 net::RequestPriority priority) { 230 net::RequestPriority priority) {
196 auto pair_and_whether_inserted = 231 auto pair_and_whether_inserted =
197 resources_.insert(std::make_pair(resource_url, ResourceSummary())); 232 resources_.insert(std::make_pair(resource_url, ResourceSummary()));
198 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url 233 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url
199 << " was inserted twice"; 234 << " 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); 517 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
483 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET, 518 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET,
484 net::HIGHEST); 519 net::HIGHEST);
485 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); 520 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
486 // Included from <iframe src="html_subresources.html"> and not recored. 521 // Included from <iframe src="html_subresources.html"> and not recored.
487 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath), 522 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath),
488 GetURL(kScriptPath), GetURL(kFontPath)}); 523 GetURL(kScriptPath), GetURL(kFontPath)});
489 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath)); 524 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath));
490 } 525 }
491 526
527 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningInPopup) {
ahemery 2016/12/07 14:13:02 New tests. Do the same as simple learning but in a
528 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
529 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
530 net::HIGHEST);
531 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
532 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
533 net::HIGHEST);
534 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath),
535 WindowOpenDisposition::NEW_POPUP);
536 }
537
538 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningInNewTab) {
539 AddResource(GetURL(kFontPath), content::RESOURCE_TYPE_FONT_RESOURCE,
540 net::HIGHEST);
541 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
542 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
543 net::HIGHEST);
544 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
545 NavigateToURLAndCheckSubresources(GetURL(kHtmlSubresourcesPath),
546 WindowOpenDisposition::NEW_FOREGROUND_TAB);
547 }
548
492 } // namespace predictors 549 } // namespace predictors
OLDNEW
« no previous file with comments | « no previous file | chrome/test/base/ui_test_utils.cc » ('j') | chrome/test/base/ui_test_utils.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698