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

Side by Side Diff: chrome/browser/net/predictor_browsertest.cc

Issue 922533003: Eliminated the logic that accumulated multiple preconnect requests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added browser test Created 5 years, 10 months 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 | components/network_hints/renderer/renderer_preconnect.h » ('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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/base64.h"
6 #include "base/command_line.h"
5 #include "base/json/json_string_value_serializer.h" 7 #include "base/json/json_string_value_serializer.h"
6 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/net/chrome_net_log.h"
7 #include "chrome/browser/net/predictor.h" 11 #include "chrome/browser/net/predictor.h"
8 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
10 #include "chrome/common/pref_names.h" 14 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/in_process_browser_test.h" 15 #include "chrome/test/base/in_process_browser_test.h"
12 #include "chrome/test/base/ui_test_utils.h" 16 #include "chrome/test/base/ui_test_utils.h"
13 #include "content/public/test/test_utils.h" 17 #include "content/public/test/test_utils.h"
18 #include "net/base/capturing_net_log_observer.h"
14 #include "net/base/net_errors.h" 19 #include "net/base/net_errors.h"
20 #include "net/base/net_log.h"
15 #include "net/dns/host_resolver_proc.h" 21 #include "net/dns/host_resolver_proc.h"
16 #include "net/dns/mock_host_resolver.h" 22 #include "net/dns/mock_host_resolver.h"
17 #include "testing/gmock/include/gmock/gmock.h" 23 #include "testing/gmock/include/gmock/gmock.h"
18 24
19 using content::BrowserThread; 25 using content::BrowserThread;
20 using testing::HasSubstr; 26 using testing::HasSubstr;
21 27
22 namespace { 28 namespace {
23 29
30 const char kEnablePreconnect[] = "--enable-blink-features=LinkPreconnect";
31 const char kEnableExperimentalWebPlatformFeatures[] =
32 "--enable-experimental-web-platform-features";
24 const char kChromiumHostname[] = "chromium.org"; 33 const char kChromiumHostname[] = "chromium.org";
25 34
26 // Records a history of all hostnames for which resolving has been requested, 35 // Records a history of all hostnames for which resolving has been requested,
27 // and immediately fails the resolution requests themselves. 36 // and immediately fails the resolution requests themselves.
28 class HostResolutionRequestRecorder : public net::HostResolverProc { 37 class HostResolutionRequestRecorder : public net::HostResolverProc {
29 public: 38 public:
30 HostResolutionRequestRecorder() 39 HostResolutionRequestRecorder()
31 : HostResolverProc(NULL), 40 : HostResolverProc(NULL),
32 is_waiting_for_hostname_(false) { 41 is_waiting_for_hostname_(false) {
33 } 42 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 host_resolution_request_recorder_(new HostResolutionRequestRecorder) { 113 host_resolution_request_recorder_(new HostResolutionRequestRecorder) {
105 } 114 }
106 115
107 protected: 116 protected:
108 void SetUpInProcessBrowserTestFixture() override { 117 void SetUpInProcessBrowserTestFixture() override {
109 scoped_host_resolver_proc_.reset(new net::ScopedDefaultHostResolverProc( 118 scoped_host_resolver_proc_.reset(new net::ScopedDefaultHostResolverProc(
110 host_resolution_request_recorder_.get())); 119 host_resolution_request_recorder_.get()));
111 InProcessBrowserTest::SetUpInProcessBrowserTestFixture(); 120 InProcessBrowserTest::SetUpInProcessBrowserTestFixture();
112 } 121 }
113 122
123 void SetUpCommandLine(base::CommandLine* command_line) override {
124 command_line->AppendSwitch(kEnablePreconnect);
125 command_line->AppendSwitch(kEnableExperimentalWebPlatformFeatures);
126 }
127
114 void TearDownInProcessBrowserTestFixture() override { 128 void TearDownInProcessBrowserTestFixture() override {
115 InProcessBrowserTest::TearDownInProcessBrowserTestFixture(); 129 InProcessBrowserTest::TearDownInProcessBrowserTestFixture();
116 scoped_host_resolver_proc_.reset(); 130 scoped_host_resolver_proc_.reset();
117 } 131 }
118 132
119 void LearnAboutInitialNavigation(const GURL& url) { 133 void LearnAboutInitialNavigation(const GURL& url) {
120 Predictor* predictor = browser()->profile()->GetNetworkPredictor(); 134 Predictor* predictor = browser()->profile()->GetNetworkPredictor();
121 BrowserThread::PostTask(BrowserThread::IO, 135 BrowserThread::PostTask(BrowserThread::IO,
122 FROM_HERE, 136 FROM_HERE,
123 base::Bind(&Predictor::LearnAboutInitialNavigation, 137 base::Bind(&Predictor::LearnAboutInitialNavigation,
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
193 } 207 }
194 208
195 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) { 209 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, DnsPrefetch) {
196 ASSERT_TRUE(test_server()->Start()); 210 ASSERT_TRUE(test_server()->Start());
197 ui_test_utils::NavigateToURL( 211 ui_test_utils::NavigateToURL(
198 browser(), 212 browser(),
199 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html"))); 213 GURL(test_server()->GetURL("files/predictor/dns_prefetch.html")));
200 WaitUntilHostHasBeenRequested(kChromiumHostname); 214 WaitUntilHostHasBeenRequested(kChromiumHostname);
201 } 215 }
202 216
217 IN_PROC_BROWSER_TEST_F(PredictorBrowserTest, Preconnect) {
218 ASSERT_TRUE(test_server()->Start());
219
220 net::CapturingNetLogObserver net_log;
mmenke 2015/02/13 16:42:17 Calling a NetLogObserver net_log is a little confu
Pat Meenan 2015/02/13 20:45:26 Done.
221 g_browser_process->net_log()->AddThreadSafeObserver(&net_log,
222 net::NetLog::LOG_ALL_BUT_BYTES);
mmenke 2015/02/13 16:42:16 nit: Should indent 4, and move &net_log on to the
Pat Meenan 2015/02/13 20:45:26 Done.
223
224 // Create a HTML preconnect reference to the local server in the form
225 // <link rel="preconnect" href="http://test-server/">
226 // and navigate to it as a data URI. The only netlog activity to the test
227 // server will be as a result of parsing and making the preconnect request.
mmenke 2015/02/13 16:42:17 Claiming "The only netlog activity" is anything is
Pat Meenan 2015/02/13 20:45:26 Done.
228 std::string preconnect_url = test_server()->GetURL("").spec();
229 std::string preconnect_content = "<link rel=\"preconnect\" href=\"";
230 preconnect_content += preconnect_url + "\">";
231 std::string data_uri = "data:text/html;base64,";
232 std::string encoded;
233 base::Base64Encode(preconnect_content, &encoded);
234 data_uri += encoded;
mmenke 2015/02/13 16:42:17 nit: Suggest merging this line with the one where
mmenke 2015/02/13 16:42:17 optional: Do we have to base64 encode it? "data:
Pat Meenan 2015/02/13 20:45:26 We either need to base64 encode or URL encode it.
235
236 ui_test_utils::NavigateToURL(browser(), GURL(data_uri));
mmenke 2015/02/13 16:42:16 For this to be non-racy, we're depending on the pr
Pat Meenan 2015/02/13 20:45:26 My original implementation had a custom observer w
mmenke 2015/02/13 20:55:05 I must be missing something...Why doesn't this wor
237
238 // Look through the recorded netlog events for the URL that we specified
mmenke 2015/02/13 16:42:17 nit: netlog -> NetLog
Pat Meenan 2015/02/13 20:45:26 Done.
239 // in the preconnect request.
240 bool saw_preconnect_request = false;
241 g_browser_process->net_log()->RemoveThreadSafeObserver(&net_log);
mmenke 2015/02/13 16:42:17 nit: This should probably go above this code bloc
Pat Meenan 2015/02/13 20:45:26 Done.
242 net::CapturedNetLogEntry::List list;
243 net_log.GetEntries(&list);
244 for (const auto& entry : list) {
245 if (entry.GetParamsJson().find(preconnect_url)) {
246 saw_preconnect_request = true;
247 break;
248 }
249 }
250
251 ASSERT_TRUE(saw_preconnect_request);
mmenke 2015/02/13 16:42:16 nit: EXPECT_TRUE?
Pat Meenan 2015/02/13 20:45:26 Done.
252 }
253
203 } // namespace chrome_browser_net 254 } // namespace chrome_browser_net
204 255
OLDNEW
« no previous file with comments | « no previous file | components/network_hints/renderer/renderer_preconnect.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698