IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / renderer / resource_fetcher_browsertest.cc
bloba1f1593b972308478aa67a8cc9890779a8d6cb23
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
3 // found in the LICENSE file.
5 #include "content/public/renderer/resource_fetcher.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/time/time.h"
12 #include "base/timer/timer.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/common/url_constants.h"
15 #include "content/public/renderer/render_view.h"
16 #include "content/public/test/test_utils.h"
17 #include "content/shell/browser/shell.h"
18 #include "content/test/content_browser_test.h"
19 #include "content/test/content_browser_test_utils.h"
20 #include "third_party/WebKit/public/platform/WebURLResponse.h"
21 #include "third_party/WebKit/public/web/WebFrame.h"
22 #include "third_party/WebKit/public/web/WebView.h"
24 using blink::WebFrame;
25 using blink::WebURLRequest;
26 using blink::WebURLResponse;
28 namespace {
30 // The first RenderFrame is routing ID 1, and the first RenderView is 2.
31 const int kRenderViewRoutingId = 2;
35 namespace content {
37 static const int kMaxWaitTimeMs = 5000;
39 class FetcherDelegate {
40 public:
41 FetcherDelegate()
42 : completed_(false),
43 timed_out_(false) {
44 // Start a repeating timer waiting for the download to complete. The
45 // callback has to be a static function, so we hold on to our instance.
46 FetcherDelegate::instance_ = this;
47 StartTimer();
50 virtual ~FetcherDelegate() {}
52 ResourceFetcher::Callback NewCallback() {
53 return base::Bind(&FetcherDelegate::OnURLFetchComplete,
54 base::Unretained(this));
57 virtual void OnURLFetchComplete(const WebURLResponse& response,
58 const std::string& data) {
59 response_ = response;
60 data_ = data;
61 completed_ = true;
62 timer_.Stop();
63 if (!timed_out_)
64 quit_task_.Run();
67 bool completed() const { return completed_; }
68 bool timed_out() const { return timed_out_; }
70 std::string data() const { return data_; }
71 const WebURLResponse& response() const { return response_; }
73 // Wait for the request to complete or timeout.
74 void WaitForResponse() {
75 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
76 quit_task_ = runner->QuitClosure();
77 runner->Run();
80 void StartTimer() {
81 timer_.Start(FROM_HERE,
82 base::TimeDelta::FromMilliseconds(kMaxWaitTimeMs),
83 this,
84 &FetcherDelegate::TimerFired);
87 void TimerFired() {
88 ASSERT_FALSE(completed_);
90 timed_out_ = true;
91 if (!completed_)
92 quit_task_.Run();
93 FAIL() << "fetch timed out";
96 static FetcherDelegate* instance_;
98 private:
99 base::OneShotTimer<FetcherDelegate> timer_;
100 bool completed_;
101 bool timed_out_;
102 WebURLResponse response_;
103 std::string data_;
104 base::Closure quit_task_;
107 FetcherDelegate* FetcherDelegate::instance_ = NULL;
109 class EvilFetcherDelegate : public FetcherDelegate {
110 public:
111 virtual ~EvilFetcherDelegate() {}
113 void SetFetcher(ResourceFetcher* fetcher) {
114 fetcher_.reset(fetcher);
117 virtual void OnURLFetchComplete(const WebURLResponse& response,
118 const std::string& data) OVERRIDE {
119 FetcherDelegate::OnURLFetchComplete(response, data);
121 // Destroy the ResourceFetcher here. We are testing that upon returning
122 // to the ResourceFetcher that it does not crash. This must be done after
123 // calling FetcherDelegate::OnURLFetchComplete, since deleting the fetcher
124 // invalidates |response| and |data|.
125 fetcher_.reset();
128 private:
129 scoped_ptr<ResourceFetcher> fetcher_;
132 class ResourceFetcherTests : public ContentBrowserTest {
133 public:
134 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
135 command_line->AppendSwitch(switches::kSingleProcess);
136 #if defined(OS_WIN) && defined(USE_AURA)
137 // Don't want to try to create a GPU process.
138 command_line->AppendSwitch(switches::kDisableAcceleratedCompositing);
139 #endif
142 RenderView* GetRenderView() {
143 // We could have the test on the UI thread get the WebContent's routing ID,
144 // but we know this will be the first RV so skip that and just hardcode it.
145 return RenderView::FromRoutingID(kRenderViewRoutingId);
148 void ResourceFetcherDownloadOnRenderer(const GURL& url) {
149 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
151 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
152 scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(
153 url, frame, WebURLRequest::TargetIsMainFrame, delegate->NewCallback()));
155 delegate->WaitForResponse();
157 ASSERT_TRUE(delegate->completed());
158 EXPECT_EQ(delegate->response().httpStatusCode(), 200);
159 std::string text = delegate->data();
160 EXPECT_TRUE(text.find("Basic html test.") != std::string::npos);
163 void ResourceFetcher404OnRenderer(const GURL& url) {
164 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
166 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
167 scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(
168 url, frame, WebURLRequest::TargetIsMainFrame, delegate->NewCallback()));
170 delegate->WaitForResponse();
172 ASSERT_TRUE(delegate->completed());
173 EXPECT_EQ(delegate->response().httpStatusCode(), 404);
174 EXPECT_TRUE(delegate->data().find("Not Found.") != std::string::npos);
177 void ResourceFetcherDidFailOnRenderer() {
178 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
180 // Try to fetch a page on a site that doesn't exist.
181 GURL url("http://localhost:1339/doesnotexist");
182 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
183 scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(
184 url, frame, WebURLRequest::TargetIsMainFrame, delegate->NewCallback()));
186 delegate->WaitForResponse();
188 // When we fail, we still call the Delegate callback but we pass in empty
189 // values.
190 EXPECT_TRUE(delegate->completed());
191 EXPECT_TRUE(delegate->response().isNull());
192 EXPECT_EQ(delegate->data(), std::string());
193 EXPECT_FALSE(delegate->timed_out());
196 void ResourceFetcherTimeoutOnRenderer(const GURL& url) {
197 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
199 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
200 scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(
201 url, frame, WebURLRequest::TargetIsMainFrame,
202 delegate->NewCallback()));
203 fetcher->SetTimeout(base::TimeDelta());
205 delegate->WaitForResponse();
207 // When we timeout, we still call the Delegate callback but we pass in empty
208 // values.
209 EXPECT_TRUE(delegate->completed());
210 EXPECT_TRUE(delegate->response().isNull());
211 EXPECT_EQ(delegate->data(), std::string());
212 EXPECT_FALSE(delegate->timed_out());
215 void ResourceFetcherDeletedInCallbackOnRenderer(const GURL& url) {
216 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
218 scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate);
219 scoped_ptr<ResourceFetcher> fetcher(ResourceFetcher::Create(
220 url, frame, WebURLRequest::TargetIsMainFrame,
221 delegate->NewCallback()));
222 fetcher->SetTimeout(base::TimeDelta());
223 delegate->SetFetcher(fetcher.release());
225 delegate->WaitForResponse();
226 EXPECT_FALSE(delegate->timed_out());
230 // Test a fetch from the test server.
231 // If this flakes, use http://crbug.com/51622.
232 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDownload) {
233 // Need to spin up the renderer.
234 NavigateToURL(shell(), GURL(kAboutBlankURL));
236 ASSERT_TRUE(test_server()->Start());
237 GURL url(test_server()->GetURL("files/simple_page.html"));
239 PostTaskToInProcessRendererAndWait(
240 base::Bind(&ResourceFetcherTests::ResourceFetcherDownloadOnRenderer,
241 base::Unretained(this), url));
244 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcher404) {
245 // Need to spin up the renderer.
246 NavigateToURL(shell(), GURL(kAboutBlankURL));
248 // Test 404 response.
249 ASSERT_TRUE(test_server()->Start());
250 GURL url = test_server()->GetURL("files/thisfiledoesntexist.html");
252 PostTaskToInProcessRendererAndWait(
253 base::Bind(&ResourceFetcherTests::ResourceFetcher404OnRenderer,
254 base::Unretained(this), url));
257 // If this flakes, use http://crbug.com/51622.
258 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDidFail) {
259 // Need to spin up the renderer.
260 NavigateToURL(shell(), GURL(kAboutBlankURL));
262 PostTaskToInProcessRendererAndWait(
263 base::Bind(&ResourceFetcherTests::ResourceFetcherDidFailOnRenderer,
264 base::Unretained(this)));
267 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) {
268 // Need to spin up the renderer.
269 NavigateToURL(shell(), GURL(kAboutBlankURL));
271 // Grab a page that takes at least 1 sec to respond, but set the fetcher to
272 // timeout in 0 sec.
273 ASSERT_TRUE(test_server()->Start());
274 GURL url(test_server()->GetURL("slow?1"));
276 PostTaskToInProcessRendererAndWait(
277 base::Bind(&ResourceFetcherTests::ResourceFetcherTimeoutOnRenderer,
278 base::Unretained(this), url));
281 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) {
282 // Need to spin up the renderer.
283 NavigateToURL(shell(), GURL(kAboutBlankURL));
285 // Grab a page that takes at least 1 sec to respond, but set the fetcher to
286 // timeout in 0 sec.
287 ASSERT_TRUE(test_server()->Start());
288 GURL url(test_server()->GetURL("slow?1"));
290 PostTaskToInProcessRendererAndWait(
291 base::Bind(
292 &ResourceFetcherTests::ResourceFetcherDeletedInCallbackOnRenderer,
293 base::Unretained(this), url));
296 } // namespace content