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

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

Issue 2449323005: predictors: Basic browsertest checks predictor learning. (Closed)
Patch Set: Revert changes about defaults. Created 4 years, 1 month 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
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
new file mode 100644
index 0000000000000000000000000000000000000000..8a88a2b9638dcc55f17a5edbdb5a8ff1584894f0
--- /dev/null
+++ b/chrome/browser/predictors/resource_prefetch_predictor_browsertest.cc
@@ -0,0 +1,200 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/command_line.h"
+#include "chrome/browser/predictors/resource_prefetch_predictor.h"
+#include "chrome/browser/predictors/resource_prefetch_predictor_factory.h"
+#include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/browser/ui/browser.h"
+#include "chrome/browser/ui/tabs/tab_strip_model.h"
+#include "chrome/common/chrome_switches.h"
+#include "chrome/test/base/in_process_browser_test.h"
+#include "chrome/test/base/ui_test_utils.h"
+#include "content/public/browser/render_frame_host.h"
+#include "content/public/browser/render_process_host.h"
+#include "net/test/embedded_test_server/http_request.h"
+#include "net/test/embedded_test_server/http_response.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using testing::UnorderedElementsAreArray;
+
+namespace predictors {
+
+static const char kImageUrl[] = "/predictors/image.png";
+static const char kStyleUrl[] = "/predictors/style.css";
+static const char kScriptUrl[] = "/predictors/script.js";
+static const char kFontUrl[] = "/predictors/font.ttf";
+static const char kHtmlSubresourcesUrl[] = "/predictors/html_subresources.html";
+
+using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary;
+using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary;
+using net::test_server::HttpRequest;
+using net::test_server::HttpResponse;
+using net::test_server::BasicHttpResponse;
pasko 2016/11/03 16:23:13 the walls of 'using' statements are not adding too
alexilin 2016/11/03 18:42:12 Done.
+
+struct ResourceSummary {
+ ResourceSummary() : is_no_store(false), version(0) {}
+
+ URLRequestSummary request;
+ std::string content;
+ bool is_no_store;
+ size_t version;
+};
+
+// Helper class to track and allow waiting for ResourcePrefetchPredictor events.
pasko 2016/11/02 19:37:02 not only waiting, but also verifying that a PageRe
alexilin 2016/11/03 13:49:08 That's why I don't like comments: they become stal
+class ResourcePrefetchPredictorTestObserver : public TestObserver {
+ public:
+ explicit ResourcePrefetchPredictorTestObserver(
+ ResourcePrefetchPredictor* predictor,
+ const size_t expected_url_visit_count,
+ const PageRequestSummary& expected_summary)
+ : TestObserver(predictor),
+ message_loop_runner_(new content::MessageLoopRunner()),
pasko 2016/11/02 19:37:01 using content::MessageLoopRunner without calling Q
alexilin 2016/11/03 13:49:09 MessageLoopRunner does additional work internally
pasko 2016/11/03 16:23:12 Frankly, this code keeps changing and I lost track
alexilin 2016/11/03 18:42:12 Acknowledged.
+ url_visit_count_(expected_url_visit_count),
+ summary_(expected_summary) {}
+
+ ~ResourcePrefetchPredictorTestObserver() override {}
pasko 2016/11/02 19:37:02 is this necessary? does not add to readability
alexilin 2016/11/03 13:49:09 Done.
+
+ // ResourcePrefetchPredictor::Observer
pasko 2016/11/02 19:37:02 // TestObserver implementation.
alexilin 2016/11/03 13:49:08 Done. Btw, I've seen alike comments with the word
pasko 2016/11/03 16:23:13 me too, also 'blah methods.'. What we do in cases
alexilin 2016/11/03 18:42:12 Actually, your suggestion wasn't completely mislea
+ void OnNavigationLearned(size_t url_visit_count,
+ const PageRequestSummary& summary) override {
+ EXPECT_EQ(url_visit_count, url_visit_count_);
+ EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url);
+ EXPECT_EQ(summary.initial_url, summary_.initial_url);
+ EXPECT_THAT(summary.subresource_requests,
+ UnorderedElementsAreArray(summary_.subresource_requests));
+ message_loop_runner_->Quit();
+ }
+
+ void Wait() { message_loop_runner_->Run(); }
+
+ private:
+ scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
+ size_t url_visit_count_;
+ PageRequestSummary summary_;
+
+ DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver);
+};
+
+class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest {
+ public:
+ void SetUpCommandLine(base::CommandLine* command_line) override {
+ command_line->AppendSwitchASCII(
+ switches::kSpeculativeResourcePrefetching,
+ switches::kSpeculativeResourcePrefetchingEnabled);
+ }
+
+ void SetUpOnMainThread() override {
+ embedded_test_server()->RegisterRequestHandler(
+ base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRequest,
+ base::Unretained(this)));
+ ASSERT_TRUE(embedded_test_server()->Start());
+ predictor_ =
+ ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile());
+ ASSERT_TRUE(predictor_);
+ }
+
+ void NavigateToURLAndCheckSubresources(
+ const std::string& main_frame_relative) {
+ GURL main_frame_absolute =
+ embedded_test_server()->GetURL(main_frame_relative);
+ std::vector<URLRequestSummary> url_request_summaries;
+ for (const auto& kv : resources_) {
+ if (kv.second.is_no_store)
+ continue;
+ url_request_summaries.push_back(
+ GetURLRequestSummaryForResource(main_frame_absolute, kv.second));
+ }
+ ResourcePrefetchPredictorTestObserver observer(
+ predictor_, UpdateAndGetVisitCount(main_frame_relative),
+ CreatePageRequestSummary(main_frame_absolute.spec(),
+ main_frame_absolute.spec(),
+ url_request_summaries));
+ ui_test_utils::NavigateToURL(browser(), main_frame_absolute);
+ observer.Wait();
+ }
+
+ void AddResource(const std::string& relative_url,
+ content::ResourceType resource_type,
+ net::RequestPriority priority,
+ const std::string& mime_type,
+ bool has_validators = true,
pasko 2016/11/02 19:37:01 the style guide "somewhat discourages" function ov
alexilin 2016/11/03 13:49:09 Many of the tests for ResourcePrefetchPredictors c
pasko 2016/11/03 16:23:12 Yes. I did not have a chance to object at the time
alexilin 2016/11/03 18:42:12 Acknowledged.
+ bool always_revalidate = false,
+ const std::string& content = std::string(),
+ bool is_no_store = false) {
+ ResourceSummary resource;
+ GURL resource_url = embedded_test_server()->GetURL(relative_url);
+ resource.request = CreateURLRequestSummary(
+ -1, -1, std::string(), resource_url.spec(), resource_type, priority,
+ mime_type, false, std::string(), has_validators, always_revalidate);
+ resource.content = content;
+ resource.is_no_store = is_no_store;
+ resources_[relative_url] = resource;
+ }
+
+ private:
+ URLRequestSummary GetURLRequestSummaryForResource(
+ const GURL& main_frame_url,
+ const ResourceSummary& resource_summary) {
+ URLRequestSummary summary(resource_summary.request);
+ content::WebContents* web_contents =
+ browser()->tab_strip_model()->GetActiveWebContents();
+ int process_id = web_contents->GetRenderProcessHost()->GetID();
+ int frame_id = web_contents->GetMainFrame()->GetRoutingID();
+ summary.navigation_id =
+ CreateNavigationID(process_id, frame_id, main_frame_url.spec());
+ return summary;
+ }
+
+ std::unique_ptr<HttpResponse> HandleRequest(const HttpRequest& request) {
pasko 2016/11/02 19:37:02 A few concerns with manually handling requests in
alexilin 2016/11/03 13:49:08 I don't see anything special or wrong in manual ha
pasko 2016/11/03 16:23:12 What do you mean by 'nice' in this case?
alexilin 2016/11/03 18:42:12 Pardon, I had in mind "expected_body" parameter bu
pasko 2016/11/04 15:05:34 What makes you say that? How about: chrome/test/d
alexilin 2016/11/04 16:07:52 Oh, I didn't know that HTTP response code is a par
+ auto resource_it = resources_.find(request.relative_url);
+ if (resource_it == resources_.end())
+ return nullptr;
+
+ const ResourceSummary& summary = resource_it->second;
+ std::unique_ptr<BasicHttpResponse> http_response =
pasko 2016/11/03 16:23:12 nit: since the type should be obvious: auto http_r
alexilin 2016/11/03 18:42:12 Done.
+ base::MakeUnique<BasicHttpResponse>();
+ http_response->set_code(net::HTTP_OK);
+ http_response->set_content_type(summary.request.mime_type);
+ http_response->set_content(summary.content);
+ if (summary.is_no_store)
+ http_response->AddCustomHeader("Cache-Control", "no-store");
+ if (summary.request.has_validators) {
+ http_response->AddCustomHeader(
+ "ETag", base::StringPrintf("'%zu%s'", summary.version,
+ request.relative_url.c_str()));
+ }
+ if (summary.request.always_revalidate)
+ http_response->AddCustomHeader("Cache-Control", "no-cache");
+ else
+ http_response->AddCustomHeader("Cache-Control", "max-age=2147483648");
+ return std::move(http_response);
+ }
+
+ size_t UpdateAndGetVisitCount(const std::string& main_frame_relative) {
+ return ++visit_count_[main_frame_relative];
+ }
+
+ ResourcePrefetchPredictor* predictor_;
+ std::map<std::string, ResourceSummary> resources_;
+ std::map<std::string, size_t> visit_count_;
pasko 2016/11/02 19:37:01 This value gets updated, but the test only observe
alexilin 2016/11/03 13:49:09 In future tests we definitely will navigate to the
pasko 2016/11/03 16:23:13 I see, makes sense, thanks.
alexilin 2016/11/03 18:42:12 Acknowledged.
+};
+
+IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, ) {
pasko 2016/11/02 19:37:01 name of the test missing, suggest: ResourcePrefetc
alexilin 2016/11/03 13:49:09 Wow, how does it even work? Yeah, I see GTest uses
+ // These resources have default priorities that correspond to
+ // blink::typeToPriority function.
+ AddResource(kImageUrl, content::RESOURCE_TYPE_IMAGE, net::LOWEST,
+ "image/png");
pasko 2016/11/04 15:05:34 let's try to remove setting the mime types returne
alexilin 2016/11/04 16:07:52 Done.
+ AddResource(kStyleUrl, content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST,
+ "text/css");
+ AddResource(kScriptUrl, content::RESOURCE_TYPE_SCRIPT, net::MEDIUM,
+ "application/javascript");
+ AddResource(kFontUrl, content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST,
+ "application/x-font-truetype");
+ NavigateToURLAndCheckSubresources(kHtmlSubresourcesUrl);
+}
+
+} // namespace predictors

Powered by Google App Engine
This is Rietveld 408576698