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

Unified Diff: chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc

Issue 2540803002: predictors: Add browsertests that check fetching through javascript. (Closed)
Patch Set: Address pasko 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/test/data/predictors/append_child.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc
diff --git a/chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc b/chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc
index ded071a052341777e9943a51a92fa64a142da4f1..8c0bc6f207bd77ebd1b9047df5b6708e34c26351 100644
--- a/chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc
+++ b/chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc
@@ -23,23 +23,45 @@ namespace predictors {
namespace {
-static const char kImagePath[] = "/predictors/image.png";
-static const char kStylePath[] = "/predictors/style.css";
-static const char kScriptPath[] = "/predictors/script.js";
-static const char kFontPath[] = "/predictors/font.ttf";
+// Paths to resources handled by a custom request handler. They return empty
+// responses with controllable response headers.
+static const char kImagePath[] = "/handled-by-test/image.png";
+static const char kImagePath2[] = "/handled-by-test/image2.png";
+static const char kStylePath[] = "/handled-by-test/style.css";
+static const char kStylePath2[] = "/handled-by-test/style2.css";
+static const char kScriptPath[] = "/handled-by-test/script.js";
+static const char kScriptPath2[] = "/handled-by-test/script2.js";
+static const char kFontPath[] = "/handled-by-test/font.ttf";
+static const char kRedirectPath[] = "/handled-by-test/redirect.html";
+static const char kRedirectPath2[] = "/handled-by-test/redirect2.html";
+static const char kRedirectPath3[] = "/handled-by-test/redirect3.html";
+
+// These are loaded from a file by the test server.
static const char kHtmlSubresourcesPath[] =
"/predictors/html_subresources.html";
-static const char kRedirectPath[] = "/predictors/redirect.html";
-static const char kRedirectPath2[] = "/predictors/redirect2.html";
-static const char kRedirectPath3[] = "/predictors/redirect3.html";
+static const char kHtmlDocumentWritePath[] = "/predictors/document_write.html";
+static const char kScriptDocumentWritePath[] = "/predictors/document_write.js";
+static const char kHtmlAppendChildPath[] = "/predictors/append_child.html";
+static const char kScriptAppendChildPath[] = "/predictors/append_child.js";
+static const char kHtmlInnerHtmlPath[] = "/predictors/inner_html.html";
+static const char kScriptInnerHtmlPath[] = "/predictors/inner_html.js";
+static const char kHtmlFetchPath[] = "/predictors/fetch.html";
+static const char kScriptFetchPath[] = "/predictors/fetch.js";
+static const char kHtmlIframePath[] = "/predictors/html_iframe.html";
struct ResourceSummary {
- ResourceSummary() : is_no_store(false), version(0) {}
+ ResourceSummary()
+ : is_no_store(false),
+ version(0),
+ is_external(false),
+ should_be_recorded(true) {}
ResourcePrefetchPredictor::URLRequestSummary request;
std::string content;
bool is_no_store;
size_t version;
+ bool is_external;
+ bool should_be_recorded;
};
struct RedirectEdge {
@@ -152,7 +174,7 @@ class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest {
GURL endpoint_url = GetRedirectEndpoint(main_frame_url);
std::vector<URLRequestSummary> url_request_summaries;
for (const auto& kv : resources_) {
- if (kv.second.is_no_store)
+ if (kv.second.is_no_store || !kv.second.should_be_recorded)
continue;
url_request_summaries.push_back(
GetURLRequestSummaryForResource(endpoint_url, kv.second));
@@ -180,6 +202,23 @@ class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest {
return resource;
}
+ ResourceSummary* AddExternalResource(const GURL& resource_url,
+ content::ResourceType resource_type,
+ net::RequestPriority priority) {
+ auto resource = AddResource(resource_url, resource_type, priority);
+ resource->is_external = true;
+ return resource;
+ }
+
+ void AddUnrecordedResources(const std::vector<GURL>& resource_urls) {
+ for (const GURL& resource_url : resource_urls) {
+ auto resource =
+ AddResource(resource_url, content::RESOURCE_TYPE_SUB_RESOURCE,
+ net::DEFAULT_PRIORITY);
+ resource->should_be_recorded = false;
+ }
+ }
+
void AddRedirectChain(const GURL& initial_url,
const std::vector<RedirectEdge>& redirect_chain) {
ASSERT_FALSE(redirect_chain.empty());
@@ -267,6 +306,9 @@ class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest {
return nullptr;
const ResourceSummary& summary = resource_it->second;
+ if (summary.is_external)
+ return nullptr;
+
auto http_response =
base::MakeUnique<net::test_server::BasicHttpResponse>();
http_response->set_code(net::HTTP_OK);
@@ -370,4 +412,70 @@ IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
NavigateToURLAndCheckSubresources(GetURL(kRedirectPath));
}
+IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
+ LearningJavascriptDocumentWrite) {
+ auto externalScript =
+ AddExternalResource(GetURL(kScriptDocumentWritePath),
+ content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
+ externalScript->request.mime_type = "application/javascript";
+ AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
+ AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
+ net::HIGHEST);
+ AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
+ NavigateToURLAndCheckSubresources(GetURL(kHtmlDocumentWritePath));
+}
+
+IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
+ LearningJavascriptAppendChild) {
+ auto externalScript =
+ AddExternalResource(GetURL(kScriptAppendChildPath),
+ content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
+ externalScript->request.mime_type = "application/javascript";
+ AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
+ AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
+ net::HIGHEST);
+ // This script has net::LOWEST priority because it's executed asynchronously.
+ AddResource(GetURL(kScriptPath), content::RESOURCE_TYPE_SCRIPT, net::LOWEST);
+ NavigateToURLAndCheckSubresources(GetURL(kHtmlAppendChildPath));
+}
+
+IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
+ LearningJavascriptInnerHtml) {
+ auto externalScript = AddExternalResource(
+ GetURL(kScriptInnerHtmlPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
+ externalScript->request.mime_type = "application/javascript";
+ AddResource(GetURL(kImagePath), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
+ AddResource(GetURL(kStylePath), content::RESOURCE_TYPE_STYLESHEET,
+ net::HIGHEST);
+ // The script isn't requested. Need furhter investigation.
Benoit L 2016/12/05 14:03:30 nit: further.
alexilin 2016/12/06 09:10:44 Done.
+ AddUnrecordedResources({GetURL(kScriptPath)});
+ NavigateToURLAndCheckSubresources(GetURL(kHtmlInnerHtmlPath));
+}
+
+// Javascript fetch requests are ignored by the ResourcePrefetchPredictor
+// because they have unsupported resource type content::RESOURCE_TYPE_XHR.
Benoit L 2016/12/05 14:03:30 Oh, that's a good find! I wonder what we should do
alexilin 2016/12/06 09:10:44 Already done: https://codereview.chromium.org/2539
+IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
+ LearningJavascriptFetchIgnored) {
+ auto externalScript = AddExternalResource(
+ GetURL(kScriptFetchPath), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
+ externalScript->request.mime_type = "application/javascript";
+ AddUnrecordedResources(
+ {GetURL(kImagePath), GetURL(kStylePath), GetURL(kScriptPath)});
+ NavigateToURLAndCheckSubresources(GetURL(kHtmlFetchPath));
+}
+
+// ResourcePrefetchPredictor ignores all resources requested from subframes.
+IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest,
+ LearningWithIframe) {
+ // Included from html_iframe.html.
+ AddResource(GetURL(kImagePath2), content::RESOURCE_TYPE_IMAGE, net::LOWEST);
+ AddResource(GetURL(kStylePath2), content::RESOURCE_TYPE_STYLESHEET,
+ net::HIGHEST);
+ AddResource(GetURL(kScriptPath2), content::RESOURCE_TYPE_SCRIPT, net::MEDIUM);
+ // Included from <iframe src="html_subresources.html"> and not recored.
+ AddUnrecordedResources({GetURL(kImagePath), GetURL(kStylePath),
+ GetURL(kScriptPath), GetURL(kFontPath)});
+ NavigateToURLAndCheckSubresources(GetURL(kHtmlIframePath));
+}
+
} // namespace predictors
« 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