| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/command_line.h" | |
| 6 #include "chrome/browser/predictors/resource_prefetch_predictor.h" | |
| 7 #include "chrome/browser/predictors/resource_prefetch_predictor_factory.h" | |
| 8 #include "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/ui/browser.h" | |
| 11 #include "chrome/browser/ui/tabs/tab_strip_model.h" | |
| 12 #include "chrome/common/chrome_switches.h" | |
| 13 #include "chrome/test/base/in_process_browser_test.h" | |
| 14 #include "chrome/test/base/ui_test_utils.h" | |
| 15 #include "content/public/browser/render_frame_host.h" | |
| 16 #include "content/public/browser/render_process_host.h" | |
| 17 #include "net/test/embedded_test_server/http_request.h" | |
| 18 #include "net/test/embedded_test_server/http_response.h" | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 namespace predictors { | |
| 23 | |
| 24 static const char kImageUrl[] = "/predictors/image.png"; | |
| 25 static const char kStyleUrl[] = "/predictors/style.css"; | |
| 26 static const char kScriptUrl[] = "/predictors/script.js"; | |
| 27 static const char kFontUrl[] = "/predictors/font.ttf"; | |
| 28 static const char kHtmlSubresourcesUrl[] = "/predictors/html_subresources.html"; | |
| 29 | |
| 30 struct ResourceSummary { | |
| 31 ResourceSummary() : is_no_store(false), version(0) {} | |
| 32 | |
| 33 ResourcePrefetchPredictor::URLRequestSummary request; | |
| 34 std::string content; | |
| 35 bool is_no_store; | |
| 36 size_t version; | |
| 37 }; | |
| 38 | |
| 39 // Helper class to track and allow waiting for ResourcePrefetchPredictor events. | |
| 40 // These events are also used to verify that ResourcePrefetchPredictor works as | |
| 41 // expected. | |
| 42 class ResourcePrefetchPredictorTestObserver : public TestObserver { | |
| 43 public: | |
| 44 using PageRequestSummary = ResourcePrefetchPredictor::PageRequestSummary; | |
| 45 | |
| 46 explicit ResourcePrefetchPredictorTestObserver( | |
| 47 ResourcePrefetchPredictor* predictor, | |
| 48 const size_t expected_url_visit_count, | |
| 49 const PageRequestSummary& expected_summary) | |
| 50 : TestObserver(predictor), | |
| 51 url_visit_count_(expected_url_visit_count), | |
| 52 summary_(expected_summary) {} | |
| 53 | |
| 54 // TestObserver: | |
| 55 void OnNavigationLearned(size_t url_visit_count, | |
| 56 const PageRequestSummary& summary) override { | |
| 57 EXPECT_EQ(url_visit_count, url_visit_count_); | |
| 58 EXPECT_EQ(summary.main_frame_url, summary_.main_frame_url); | |
| 59 EXPECT_EQ(summary.initial_url, summary_.initial_url); | |
| 60 EXPECT_THAT( | |
| 61 summary.subresource_requests, | |
| 62 testing::UnorderedElementsAreArray(summary_.subresource_requests)); | |
| 63 run_loop_.Quit(); | |
| 64 } | |
| 65 | |
| 66 void Wait() { run_loop_.Run(); } | |
| 67 | |
| 68 private: | |
| 69 base::RunLoop run_loop_; | |
| 70 size_t url_visit_count_; | |
| 71 PageRequestSummary summary_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(ResourcePrefetchPredictorTestObserver); | |
| 74 }; | |
| 75 | |
| 76 class ResourcePrefetchPredictorBrowserTest : public InProcessBrowserTest { | |
| 77 public: | |
| 78 using URLRequestSummary = ResourcePrefetchPredictor::URLRequestSummary; | |
| 79 | |
| 80 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 81 command_line->AppendSwitchASCII( | |
| 82 switches::kSpeculativeResourcePrefetching, | |
| 83 switches::kSpeculativeResourcePrefetchingEnabled); | |
| 84 } | |
| 85 | |
| 86 void SetUpOnMainThread() override { | |
| 87 embedded_test_server()->RegisterRequestHandler( | |
| 88 base::Bind(&ResourcePrefetchPredictorBrowserTest::HandleRequest, | |
| 89 base::Unretained(this))); | |
| 90 ASSERT_TRUE(embedded_test_server()->Start()); | |
| 91 predictor_ = | |
| 92 ResourcePrefetchPredictorFactory::GetForProfile(browser()->profile()); | |
| 93 ASSERT_TRUE(predictor_); | |
| 94 } | |
| 95 | |
| 96 void NavigateToURLAndCheckSubresources( | |
| 97 const std::string& main_frame_relative) { | |
| 98 GURL main_frame_absolute = | |
| 99 embedded_test_server()->GetURL(main_frame_relative); | |
| 100 std::vector<URLRequestSummary> url_request_summaries; | |
| 101 for (const auto& kv : resources_) { | |
| 102 if (kv.second.is_no_store) | |
| 103 continue; | |
| 104 url_request_summaries.push_back( | |
| 105 GetURLRequestSummaryForResource(main_frame_absolute, kv.second)); | |
| 106 } | |
| 107 ResourcePrefetchPredictorTestObserver observer( | |
| 108 predictor_, UpdateAndGetVisitCount(main_frame_relative), | |
| 109 CreatePageRequestSummary(main_frame_absolute.spec(), | |
| 110 main_frame_absolute.spec(), | |
| 111 url_request_summaries)); | |
| 112 ui_test_utils::NavigateToURL(browser(), main_frame_absolute); | |
| 113 observer.Wait(); | |
| 114 } | |
| 115 | |
| 116 ResourceSummary* AddResource(const std::string& relative_url, | |
| 117 content::ResourceType resource_type, | |
| 118 net::RequestPriority priority) { | |
| 119 ResourceSummary resource; | |
| 120 resource.request.resource_url = | |
| 121 embedded_test_server()->GetURL(relative_url); | |
| 122 resource.request.resource_type = resource_type; | |
| 123 resource.request.priority = priority; | |
| 124 auto result = resources_.insert(std::make_pair(relative_url, resource)); | |
| 125 return &(result.first->second); | |
| 126 } | |
| 127 | |
| 128 private: | |
| 129 URLRequestSummary GetURLRequestSummaryForResource( | |
| 130 const GURL& main_frame_url, | |
| 131 const ResourceSummary& resource_summary) { | |
| 132 URLRequestSummary summary(resource_summary.request); | |
| 133 content::WebContents* web_contents = | |
| 134 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 135 int process_id = web_contents->GetRenderProcessHost()->GetID(); | |
| 136 int frame_id = web_contents->GetMainFrame()->GetRoutingID(); | |
| 137 summary.navigation_id = | |
| 138 CreateNavigationID(process_id, frame_id, main_frame_url.spec()); | |
| 139 return summary; | |
| 140 } | |
| 141 | |
| 142 std::unique_ptr<net::test_server::HttpResponse> HandleRequest( | |
| 143 const net::test_server::HttpRequest& request) { | |
| 144 auto resource_it = resources_.find(request.relative_url); | |
| 145 if (resource_it == resources_.end()) | |
| 146 return nullptr; | |
| 147 | |
| 148 const ResourceSummary& summary = resource_it->second; | |
| 149 auto http_response = | |
| 150 base::MakeUnique<net::test_server::BasicHttpResponse>(); | |
| 151 http_response->set_code(net::HTTP_OK); | |
| 152 if (!summary.request.mime_type.empty()) | |
| 153 http_response->set_content_type(summary.request.mime_type); | |
| 154 http_response->set_content(summary.content); | |
| 155 if (summary.is_no_store) | |
| 156 http_response->AddCustomHeader("Cache-Control", "no-store"); | |
| 157 if (summary.request.has_validators) { | |
| 158 http_response->AddCustomHeader( | |
| 159 "ETag", base::StringPrintf("'%zu%s'", summary.version, | |
| 160 request.relative_url.c_str())); | |
| 161 } | |
| 162 if (summary.request.always_revalidate) | |
| 163 http_response->AddCustomHeader("Cache-Control", "no-cache"); | |
| 164 else | |
| 165 http_response->AddCustomHeader("Cache-Control", "max-age=2147483648"); | |
| 166 return std::move(http_response); | |
| 167 } | |
| 168 | |
| 169 size_t UpdateAndGetVisitCount(const std::string& main_frame_relative) { | |
| 170 return ++visit_count_[main_frame_relative]; | |
| 171 } | |
| 172 | |
| 173 ResourcePrefetchPredictor* predictor_; | |
| 174 std::map<std::string, ResourceSummary> resources_; | |
| 175 std::map<std::string, size_t> visit_count_; | |
| 176 }; | |
| 177 | |
| 178 IN_PROC_BROWSER_TEST_F(ResourcePrefetchPredictorBrowserTest, LearningSimple) { | |
| 179 // These resources have default priorities that correspond to | |
| 180 // blink::typeToPriority function. | |
| 181 AddResource(kImageUrl, content::RESOURCE_TYPE_IMAGE, net::LOWEST); | |
| 182 AddResource(kStyleUrl, content::RESOURCE_TYPE_STYLESHEET, net::HIGHEST); | |
| 183 AddResource(kScriptUrl, content::RESOURCE_TYPE_SCRIPT, net::MEDIUM); | |
| 184 AddResource(kFontUrl, content::RESOURCE_TYPE_FONT_RESOURCE, net::HIGHEST); | |
| 185 NavigateToURLAndCheckSubresources(kHtmlSubresourcesUrl); | |
| 186 } | |
| 187 | |
| 188 } // namespace predictors | |
| OLD | NEW |