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

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

Issue 2540803002: predictors: Add browsertests that check fetching through javascript. (Closed)
Patch Set: Address lizeb comments. 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/data/predictors/append_child.html » ('j') | 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"
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 { 24 namespace {
25 25
26 static const char kImagePath[] = "/predictors/image.png"; 26 const char kImageMime[] = "image/png";
27 static const char kStylePath[] = "/predictors/style.css"; 27 const char kStyleMime[] = "text/css";
28 static const char kScriptPath[] = "/predictors/script.js"; 28 const char kJavascriptMime[] = "application/javascript";
29 static const char kFontPath[] = "/predictors/font.ttf"; 29
30 static const char kHtmlSubresourcesPath[] = 30 // Paths to resources handled by a custom request handler. They return empty
31 "/predictors/html_subresources.html"; 31 // responses with controllable response headers.
32 static const char kRedirectPath[] = "/predictors/redirect.html"; 32 const char kImagePath[] = "/handled-by-test/image.png";
33 static const char kRedirectPath2[] = "/predictors/redirect2.html"; 33 const char kImagePath2[] = "/handled-by-test/image2.png";
34 static const char kRedirectPath3[] = "/predictors/redirect3.html"; 34 const char kStylePath[] = "/handled-by-test/style.css";
35 const char kStylePath2[] = "/handled-by-test/style2.css";
36 const char kScriptPath[] = "/handled-by-test/script.js";
37 const char kScriptPath2[] = "/handled-by-test/script2.js";
38 const char kFontPath[] = "/handled-by-test/font.ttf";
39 const char kRedirectPath[] = "/handled-by-test/redirect.html";
40 const char kRedirectPath2[] = "/handled-by-test/redirect2.html";
41 const char kRedirectPath3[] = "/handled-by-test/redirect3.html";
42
43 // These are loaded from a file by the test server.
44 const char kHtmlSubresourcesPath[] = "/predictors/html_subresources.html";
45 const char kHtmlDocumentWritePath[] = "/predictors/document_write.html";
46 const char kScriptDocumentWritePath[] = "/predictors/document_write.js";
47 const char kHtmlAppendChildPath[] = "/predictors/append_child.html";
48 const char kScriptAppendChildPath[] = "/predictors/append_child.js";
49 const char kHtmlInnerHtmlPath[] = "/predictors/inner_html.html";
50 const char kScriptInnerHtmlPath[] = "/predictors/inner_html.js";
51 const char kHtmlXHRPath[] = "/predictors/xhr.html";
52 const char kScriptXHRPath[] = "/predictors/xhr.js";
53 const char kHtmlIframePath[] = "/predictors/html_iframe.html";
35 54
36 struct ResourceSummary { 55 struct ResourceSummary {
37 ResourceSummary() : is_no_store(false), version(0) {} 56 ResourceSummary()
57 : is_no_store(false),
58 version(0),
59 is_external(false),
60 should_be_recorded(true) {}
38 61
39 ResourcePrefetchPredictor::URLRequestSummary request; 62 ResourcePrefetchPredictor::URLRequestSummary request;
40 std::string content; 63 std::string content;
41 bool is_no_store; 64 bool is_no_store;
42 size_t version; 65 size_t version;
66 bool is_external;
67 bool should_be_recorded;
43 }; 68 };
44 69
45 struct RedirectEdge { 70 struct RedirectEdge {
46 // This response code should be returned by previous url in the chain. 71 // This response code should be returned by previous url in the chain.
47 net::HttpStatusCode code; 72 net::HttpStatusCode code;
48 GURL url; 73 GURL url;
49 }; 74 };
50 75
51 class InitializationObserver : public TestObserver { 76 class InitializationObserver : public TestObserver {
52 public: 77 public:
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 predictor_ = 170 predictor_ =
146 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); 171 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile());
147 ASSERT_TRUE(predictor_); 172 ASSERT_TRUE(predictor_);
148 EnsurePredictorInitialized(); 173 EnsurePredictorInitialized();
149 } 174 }
150 175
151 void NavigateToURLAndCheckSubresources(const GURL& main_frame_url) { 176 void NavigateToURLAndCheckSubresources(const GURL& main_frame_url) {
152 GURL endpoint_url = GetRedirectEndpoint(main_frame_url); 177 GURL endpoint_url = GetRedirectEndpoint(main_frame_url);
153 std::vector<URLRequestSummary> url_request_summaries; 178 std::vector<URLRequestSummary> url_request_summaries;
154 for (const auto& kv : resources_) { 179 for (const auto& kv : resources_) {
155 if (kv.second.is_no_store) 180 if (kv.second.is_no_store || !kv.second.should_be_recorded)
156 continue; 181 continue;
157 url_request_summaries.push_back( 182 url_request_summaries.push_back(
158 GetURLRequestSummaryForResource(endpoint_url, kv.second)); 183 GetURLRequestSummaryForResource(endpoint_url, kv.second));
159 } 184 }
160 ResourcePrefetchPredictorTestObserver observer( 185 ResourcePrefetchPredictorTestObserver observer(
161 predictor_, UpdateAndGetVisitCount(main_frame_url), 186 predictor_, UpdateAndGetVisitCount(main_frame_url),
162 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(), 187 CreatePageRequestSummary(endpoint_url.spec(), main_frame_url.spec(),
163 url_request_summaries)); 188 url_request_summaries));
164 ui_test_utils::NavigateToURL(browser(), main_frame_url); 189 ui_test_utils::NavigateToURL(browser(), main_frame_url);
165 observer.Wait(); 190 observer.Wait();
166 } 191 }
167 192
168 ResourceSummary* AddResource(const GURL& resource_url, 193 ResourceSummary* AddResource(const GURL& resource_url,
169 content::ResourceType resource_type, 194 content::ResourceType resource_type,
170 net::RequestPriority priority) { 195 net::RequestPriority priority) {
171 auto pair_and_whether_inserted = 196 auto pair_and_whether_inserted =
172 resources_.insert(std::make_pair(resource_url, ResourceSummary())); 197 resources_.insert(std::make_pair(resource_url, ResourceSummary()));
173 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url 198 EXPECT_TRUE(pair_and_whether_inserted.second) << resource_url
174 << " was inserted twice"; 199 << " was inserted twice";
175 ResourceSummary* resource = &pair_and_whether_inserted.first->second; 200 ResourceSummary* resource = &pair_and_whether_inserted.first->second;
176 resource->request.resource_url = resource_url; 201 resource->request.resource_url = resource_url;
177 resource->request.resource_type = resource_type; 202 resource->request.resource_type = resource_type;
178 resource->request.priority = priority; 203 resource->request.priority = priority;
179 resource->request.has_validators = true; 204 resource->request.has_validators = true;
180 return resource; 205 return resource;
181 } 206 }
182 207
208 ResourceSummary* AddExternalResource(const GURL& resource_url,
209 content::ResourceType resource_type,
210 net::RequestPriority priority) {
211 auto resource = AddResource(resource_url, resource_type, priority);
212 resource->is_external = true;
213 return resource;
214 }
215
216 void AddUnrecordedResources(const std::vector<GURL>& resource_urls) {
217 for (const GURL& resource_url : resource_urls) {
218 auto resource =
219 AddResource(resource_url, content::RESOURCE_TYPE_SUB_RESOURCE,
220 net::DEFAULT_PRIORITY);
221 resource->should_be_recorded = false;
222 }
223 }
224
183 void AddRedirectChain(const GURL& initial_url, 225 void AddRedirectChain(const GURL& initial_url,
184 const std::vector<RedirectEdge>& redirect_chain) { 226 const std::vector<RedirectEdge>& redirect_chain) {
185 ASSERT_FALSE(redirect_chain.empty()); 227 ASSERT_FALSE(redirect_chain.empty());
186 GURL current = initial_url; 228 GURL current = initial_url;
187 for (const auto& edge : redirect_chain) { 229 for (const auto& edge : redirect_chain) {
188 auto result = redirects_.insert(std::make_pair(current, edge)); 230 auto result = redirects_.insert(std::make_pair(current, edge));
189 EXPECT_TRUE(result.second) << current << " already has a redirect."; 231 EXPECT_TRUE(result.second) << current << " already has a redirect.";
190 current = edge.url; 232 current = edge.url;
191 } 233 }
192 } 234 }
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 return current; 302 return current;
261 } 303 }
262 304
263 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest( 305 std::unique_ptr<net::test_server::HttpResponse> HandleResourceRequest(
264 const net::test_server::HttpRequest& request) const { 306 const net::test_server::HttpRequest& request) const {
265 auto resource_it = resources_.find(request.GetURL()); 307 auto resource_it = resources_.find(request.GetURL());
266 if (resource_it == resources_.end()) 308 if (resource_it == resources_.end())
267 return nullptr; 309 return nullptr;
268 310
269 const ResourceSummary& summary = resource_it->second; 311 const ResourceSummary& summary = resource_it->second;
312 if (summary.is_external)
313 return nullptr;
314
270 auto http_response = 315 auto http_response =
271 base::MakeUnique<net::test_server::BasicHttpResponse>(); 316 base::MakeUnique<net::test_server::BasicHttpResponse>();
272 http_response->set_code(net::HTTP_OK); 317 http_response->set_code(net::HTTP_OK);
273 if (!summary.request.mime_type.empty()) 318 if (!summary.request.mime_type.empty())
274 http_response->set_content_type(summary.request.mime_type); 319 http_response->set_content_type(summary.request.mime_type);
275 if (!summary.content.empty()) 320 if (!summary.content.empty())
276 http_response->set_content(summary.content); 321 http_response->set_content(summary.content);
277 if (summary.is_no_store) 322 if (summary.is_no_store)
278 http_response->AddCustomHeader("Cache-Control", "no-store"); 323 http_response->AddCustomHeader("Cache-Control", "no-store");
279 if (summary.request.has_validators) { 324 if (summary.request.has_validators) {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
363 net::LOWEST); 408 net::LOWEST);
364 AddResource(https_server()->GetURL(kStylePath), 409 AddResource(https_server()->GetURL(kStylePath),
365 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); 410 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST);
366 AddResource(https_server()->GetURL(kScriptPath), 411 AddResource(https_server()->GetURL(kScriptPath),
367 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); 412 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
368 AddResource(https_server()->GetURL(kFontPath), 413 AddResource(https_server()->GetURL(kFontPath),
369 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); 414 content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST);
370 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath)); 415 NavigateToURLAndCheckSubresources(GetURL(kRedirectPath));
371 } 416 }
372 417
418 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
419 LearningJavascriptDocumentWrite) {
420 auto externalScript =
421 AddExternalResource(GetURL(kScriptDocumentWritePath),
422 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
423 externalScript->request.mime_type = kJavascriptMime;
424 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
425 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
426 net::HIGHEST);
427 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
428 NavigateToURLAndCheckSubresources(GetURL(kHtmlDocumentWritePath));
429 }
430
431 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
432 LearningJavascriptAppendChild) {
433 auto externalScript =
434 AddExternalResource(GetURL(kScriptAppendChildPath),
435 content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
436 externalScript->request.mime_type = kJavascriptMime;
437 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
438 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
439 net::HIGHEST);
440 // This script has net::LOWEST priority because it's executed asynchronously.
441 AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::LOWEST);
442 NavigateToURLAndCheckSubresources(GetURL(kHtmlAppendChildPath));
443 }
444
445 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
446 LearningJavascriptInnerHtml) {
447 auto externalScript = AddExternalResource(
448 GetURL(kScriptInnerHtmlPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
449 externalScript->request.mime_type = kJavascriptMime;
450 AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
451 AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
452 net::HIGHEST);
453 // https://www.w3.org/TR/2014/REC-html5-20141028/scripting-1.html#the-script-e lement
454 // Script elements don't execute when inserted using innerHTML attribute.
455 AddUnrecordedResources({GetURL(kScriptPath)});
456 NavigateToURLAndCheckSubresources(GetURL(kHtmlInnerHtmlPath));
457 }
458
459 // Requests originated by XMLHttpRequest have content::RESOURCE_TYPE_XHR.
460 // Actual resource type is inferred from the mime-type.
461 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
462 LearningJavascriptXHR) {
463 auto externalScript = AddExternalResource(
464 GetURL(kScriptXHRPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
465 externalScript->request.mime_type = kJavascriptMime;
466 auto image = AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE,
467 net::HIGHEST);
468 image->request.mime_type = kImageMime;
469 auto style = AddResource(GetURL(kStylePath),
470 content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST);
471 style->request.mime_type = kStyleMime;
472 auto script = AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT,
473 net::HIGHEST);
474 script->request.mime_type = kJavascriptMime;
475 NavigateToURLAndCheckSubresources(GetURL(kHtmlXHRPath));
476 }
477
478 // ResourcePrefetchPredictor ignores all resources requested from subframes.
479 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
480 LearningWithIframe) {
481 // Included from html_iframe.html.
482 AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
483 AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET,
484 net::HIGHEST);
485 AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
486 // Included from <iframe src="html_subresources.html"> and not recored.
487 AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath),
488 GetURL(kScriptPath), GetURL(kFontPath)});
489 NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath));
490 }
491
373 } // namespace predictors 492 } // namespace predictors
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/predictors/append_child.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698