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

Side by Side Diff: chrome/browser/prefetch/prefetch_browsertest.cc

Issue 334483004: Enable resource prefetch from the browser process. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: sync Created 6 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/chrome_renderer.gypi » ('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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/prefs/pref_service.h" 6 #include "base/prefs/pref_service.h"
7 #include "base/run_loop.h"
7 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/tabs/tab_strip_model.h" 11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
11 #include "chrome/common/chrome_switches.h" 12 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/pref_names.h" 13 #include "chrome/common/pref_names.h"
14 #include "chrome/common/prefetch_messages.h"
13 #include "chrome/test/base/in_process_browser_test.h" 15 #include "chrome/test/base/in_process_browser_test.h"
14 #include "chrome/test/base/ui_test_utils.h" 16 #include "chrome/test/base/ui_test_utils.h"
17 #include "content/public/browser/render_frame_host.h"
18 #include "content/public/browser/web_contents.h"
15 #include "content/public/test/browser_test_utils.h" 19 #include "content/public/test/browser_test_utils.h"
20 #include "net/url_request/url_request_filter.h"
21 #include "net/url_request/url_request_job.h"
22
23 using content::BrowserThread;
16 24
17 namespace { 25 namespace {
18 26
19 const char kPrefetchPage[] = "files/prerender/simple_prefetch.html"; 27 const char kPrefetchPage[] = "files/prerender/simple_prefetch.html";
20 28
21 class PrefetchBrowserTestBase : public InProcessBrowserTest { 29 class PrefetchBrowserTestBase : public InProcessBrowserTest {
22 public: 30 public:
23 explicit PrefetchBrowserTestBase(bool do_predictive_networking, 31 explicit PrefetchBrowserTestBase(bool do_predictive_networking,
24 bool do_prefetch_field_trial) 32 bool do_prefetch_field_trial)
25 : do_predictive_networking_(do_predictive_networking), 33 : do_predictive_networking_(do_predictive_networking),
26 do_prefetch_field_trial_(do_prefetch_field_trial) {} 34 do_prefetch_field_trial_(do_prefetch_field_trial) {}
27 35
28 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 36 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
29 if (do_prefetch_field_trial_) { 37 if (do_prefetch_field_trial_) {
30 command_line->AppendSwitchASCII( 38 command_line->AppendSwitchASCII(switches::kForceFieldTrials,
31 switches::kForceFieldTrials, 39 "Prefetch/ExperimentYes/");
32 "Prefetch/ExperimentYes/");
33 } else { 40 } else {
34 command_line->AppendSwitchASCII( 41 command_line->AppendSwitchASCII(switches::kForceFieldTrials,
35 switches::kForceFieldTrials, 42 "Prefetch/ExperimentNo/");
36 "Prefetch/ExperimentNo/");
37 } 43 }
38 } 44 }
39 45
40 virtual void SetUpOnMainThread() OVERRIDE { 46 virtual void SetUpOnMainThread() OVERRIDE {
41 browser()->profile()->GetPrefs()->SetBoolean( 47 browser()->profile()->GetPrefs()->SetBoolean(
42 prefs::kNetworkPredictionEnabled, do_predictive_networking_); 48 prefs::kNetworkPredictionEnabled, do_predictive_networking_);
43 } 49 }
44 50
45 bool RunPrefetchExperiment(bool expect_success, Browser* browser) { 51 bool RunPrefetchExperiment(bool expect_success, Browser* browser) {
46 CHECK(test_server()->Start()); 52 CHECK(test_server()->Start());
(...skipping 30 matching lines...) Expand all
77 PrefetchBrowserTestPredictionOffExpOn() 83 PrefetchBrowserTestPredictionOffExpOn()
78 : PrefetchBrowserTestBase(false, true) {} 84 : PrefetchBrowserTestBase(false, true) {}
79 }; 85 };
80 86
81 class PrefetchBrowserTestPredictionOffExpOff : public PrefetchBrowserTestBase { 87 class PrefetchBrowserTestPredictionOffExpOff : public PrefetchBrowserTestBase {
82 public: 88 public:
83 PrefetchBrowserTestPredictionOffExpOff() 89 PrefetchBrowserTestPredictionOffExpOff()
84 : PrefetchBrowserTestBase(false, false) {} 90 : PrefetchBrowserTestBase(false, false) {}
85 }; 91 };
86 92
93 // URLRequestJob (and associated handler) which hangs.
94 class HangingURLRequestJob : public net::URLRequestJob {
95 public:
96 HangingURLRequestJob(net::URLRequest* request,
97 net::NetworkDelegate* network_delegate)
98 : net::URLRequestJob(request, network_delegate) {}
99
100 // net::URLRequestJob implementation
101 virtual void Start() OVERRIDE {}
102
103 private:
104 virtual ~HangingURLRequestJob() {}
105
106 DISALLOW_COPY_AND_ASSIGN(HangingURLRequestJob);
107 };
108
109 class HangingRequestInterceptor : public net::URLRequestInterceptor {
110 public:
111 explicit HangingRequestInterceptor(const base::Closure& callback)
112 : callback_(callback) {}
113
114 virtual ~HangingRequestInterceptor() {}
115
116 virtual net::URLRequestJob* MaybeInterceptRequest(
117 net::URLRequest* request,
118 net::NetworkDelegate* network_delegate) const OVERRIDE {
119 if (!callback_.is_null())
120 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback_);
121 return new HangingURLRequestJob(request, network_delegate);
122 }
123
124 private:
125 base::Closure callback_;
126 };
127
128 void CreateHangingRequestInterceptorOnIO(const GURL& url,
129 base::Closure callback) {
130 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
131 scoped_ptr<net::URLRequestInterceptor> never_respond_handler(
132 new HangingRequestInterceptor(callback));
133 net::URLRequestFilter::GetInstance()->AddUrlInterceptor(
134 url, never_respond_handler.Pass());
135 }
136
87 // Privacy option is on, experiment is on. Prefetch should succeed. 137 // Privacy option is on, experiment is on. Prefetch should succeed.
88 IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOn, PredOnExpOn) { 138 IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOn, PredOnExpOn) {
89 EXPECT_TRUE(RunPrefetchExperiment(true, browser())); 139 EXPECT_TRUE(RunPrefetchExperiment(true, browser()));
90 } 140 }
91 141
92 // Privacy option is on, experiment is off. Prefetch should be dropped. 142 // Privacy option is on, experiment is off. Prefetch should be dropped.
93 IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOff, PredOnExpOff) { 143 IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOff, PredOnExpOff) {
94 EXPECT_TRUE(RunPrefetchExperiment(false, browser())); 144 EXPECT_TRUE(RunPrefetchExperiment(false, browser()));
95 } 145 }
96 146
(...skipping 14 matching lines...) Expand all
111 Browser* incognito_browser = new Browser( 161 Browser* incognito_browser = new Browser(
112 Browser::CreateParams(incognito_profile, browser()->host_desktop_type())); 162 Browser::CreateParams(incognito_profile, browser()->host_desktop_type()));
113 163
114 // Navigate just to have a tab in this window, otherwise there is no 164 // Navigate just to have a tab in this window, otherwise there is no
115 // WebContents for the incognito browser. 165 // WebContents for the incognito browser.
116 ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank")); 166 ui_test_utils::OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
117 167
118 EXPECT_TRUE(RunPrefetchExperiment(true, incognito_browser)); 168 EXPECT_TRUE(RunPrefetchExperiment(true, incognito_browser));
119 } 169 }
120 170
171 // This test will verify the following:
172 // - that prefetches from the browser are actually launched
173 // - if a prefetch is in progress, but the originating renderer is destroyed,
174 // that the pending prefetch request is cleaned up cleanly and does not
175 // result in a crash.
176 IN_PROC_BROWSER_TEST_F(PrefetchBrowserTestPredictionOnExpOn,
177 PrefetchFromBrowser) {
178 const GURL kHangingUrl("http://hanging-url.com");
179 base::RunLoop loop_;
180 BrowserThread::PostTask(BrowserThread::IO,
181 FROM_HERE,
182 base::Bind(&CreateHangingRequestInterceptorOnIO,
183 kHangingUrl,
184 loop_.QuitClosure()));
185 ui_test_utils::NavigateToURL(browser(), GURL("about:blank"));
186 content::RenderFrameHost* rfh =
187 browser()->tab_strip_model()->GetActiveWebContents()->GetMainFrame();
188 rfh->Send(new PrefetchMsg_Prefetch(rfh->GetRoutingID(), kHangingUrl));
189 loop_.Run();
190 }
191
121 } // namespace 192 } // namespace
122
OLDNEW
« no previous file with comments | « no previous file | chrome/chrome_renderer.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698