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

Side by Side Diff: content/renderer/resource_fetcher_browsertest.cc

Issue 14304004: Convert a bunch of test_shell_tests to content_browsertests. These are tests that depend on loading… (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: sync Created 7 years, 8 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
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 "webkit/glue/resource_fetcher.h" 5 #include "webkit/glue/resource_fetcher.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
9 #include "base/message_loop.h" 10 #include "base/message_loop.h"
10 #include "base/timer.h" 11 #include "base/timer.h"
12 #include "content/public/common/content_switches.h"
13 #include "content/public/renderer/render_view.h"
14 #include "content/public/test/test_utils.h"
15 #include "content/shell/shell.h"
16 #include "content/test/content_browser_test.h"
17 #include "content/test/content_browser_test_utils.h"
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h" 18 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
14 #include "webkit/glue/unittest_test_server.h"
15 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h"
16 #include "webkit/tools/test_shell/test_shell_test.h"
17 21
18 using WebKit::WebFrame; 22 using WebKit::WebFrame;
19 using WebKit::WebURLRequest; 23 using WebKit::WebURLRequest;
20 using WebKit::WebURLResponse; 24 using WebKit::WebURLResponse;
21 using webkit_glue::ResourceFetcher; 25 using webkit_glue::ResourceFetcher;
22 using webkit_glue::ResourceFetcherWithTimeout; 26 using webkit_glue::ResourceFetcherWithTimeout;
23 27
24 namespace { 28 namespace content {
25
26 class ResourceFetcherTests : public TestShellTest {
27 protected:
28 UnittestTestServer test_server_;
29 };
30 29
31 static const int kMaxWaitTimeMs = 5000; 30 static const int kMaxWaitTimeMs = 5000;
32 31
33 class FetcherDelegate { 32 class FetcherDelegate {
34 public: 33 public:
35 FetcherDelegate() 34 FetcherDelegate()
36 : completed_(false), 35 : completed_(false),
37 timed_out_(false) { 36 timed_out_(false) {
38 // Start a repeating timer waiting for the download to complete. The 37 // Start a repeating timer waiting for the download to complete. The
39 // callback has to be a static function, so we hold on to our instance. 38 // callback has to be a static function, so we hold on to our instance.
40 FetcherDelegate::instance_ = this; 39 FetcherDelegate::instance_ = this;
41 StartTimer(); 40 StartTimer();
42 } 41 }
43 42
44 virtual ~FetcherDelegate() {} 43 virtual ~FetcherDelegate() {}
45 44
46 ResourceFetcher::Callback NewCallback() { 45 ResourceFetcher::Callback NewCallback() {
47 return base::Bind(&FetcherDelegate::OnURLFetchComplete, 46 return base::Bind(&FetcherDelegate::OnURLFetchComplete,
48 base::Unretained(this)); 47 base::Unretained(this));
49 } 48 }
50 49
51 virtual void OnURLFetchComplete(const WebURLResponse& response, 50 virtual void OnURLFetchComplete(const WebURLResponse& response,
52 const std::string& data) { 51 const std::string& data) {
53 response_ = response; 52 response_ = response;
54 data_ = data; 53 data_ = data;
55 completed_ = true; 54 completed_ = true;
56 timer_.Stop(); 55 timer_.Stop();
57 MessageLoop::current()->Quit(); 56 if (!timed_out_)
57 quit_task_.Run();
58 } 58 }
59 59
60 bool completed() const { return completed_; } 60 bool completed() const { return completed_; }
61 bool timed_out() const { return timed_out_; } 61 bool timed_out() const { return timed_out_; }
62 62
63 std::string data() const { return data_; } 63 std::string data() const { return data_; }
64 const WebURLResponse& response() const { return response_; } 64 const WebURLResponse& response() const { return response_; }
65 65
66 // Wait for the request to complete or timeout. We use a loop here b/c the 66 // Wait for the request to complete or timeout.
67 // testing infrastructure (test_shell) can generate spurious calls to the
68 // MessageLoop's Quit method.
69 void WaitForResponse() { 67 void WaitForResponse() {
70 while (!completed() && !timed_out()) 68 scoped_refptr<MessageLoopRunner> runner = new MessageLoopRunner;
71 MessageLoop::current()->Run(); 69 quit_task_ = runner->QuitClosure();
70 runner->Run();
72 } 71 }
73 72
74 void StartTimer() { 73 void StartTimer() {
75 timer_.Start(FROM_HERE, 74 timer_.Start(FROM_HERE,
76 base::TimeDelta::FromMilliseconds(kMaxWaitTimeMs), 75 base::TimeDelta::FromMilliseconds(kMaxWaitTimeMs),
77 this, 76 this,
78 &FetcherDelegate::TimerFired); 77 &FetcherDelegate::TimerFired);
79 } 78 }
80 79
81 void TimerFired() { 80 void TimerFired() {
82 ASSERT_FALSE(completed_); 81 ASSERT_FALSE(completed_);
83 82
84 timed_out_ = true; 83 timed_out_ = true;
85 MessageLoop::current()->Quit(); 84 if (!completed_)
85 quit_task_.Run();
86 FAIL() << "fetch timed out"; 86 FAIL() << "fetch timed out";
87 } 87 }
88 88
89 static FetcherDelegate* instance_; 89 static FetcherDelegate* instance_;
90 90
91 private: 91 private:
92 base::OneShotTimer<FetcherDelegate> timer_; 92 base::OneShotTimer<FetcherDelegate> timer_;
93 bool completed_; 93 bool completed_;
94 bool timed_out_; 94 bool timed_out_;
95 WebURLResponse response_; 95 WebURLResponse response_;
96 std::string data_; 96 std::string data_;
97 base::Closure quit_task_;
97 }; 98 };
98 99
99 FetcherDelegate* FetcherDelegate::instance_ = NULL; 100 FetcherDelegate* FetcherDelegate::instance_ = NULL;
100 101
101 // Test a fetch from the test server.
102 // Flaky, http://crbug.com/51622.
103 TEST_F(ResourceFetcherTests, DISABLED_ResourceFetcherDownload) {
104 ASSERT_TRUE(test_server_.Start());
105
106 WebFrame* frame = test_shell_->webView()->mainFrame();
107
108 GURL url(test_server_.GetURL("files/test_shell/index.html"));
109 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
110 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher(
111 url, frame, WebURLRequest::TargetIsMainFrame, delegate->NewCallback()));
112
113 delegate->WaitForResponse();
114
115 ASSERT_TRUE(delegate->completed());
116 EXPECT_EQ(delegate->response().httpStatusCode(), 200);
117 std::string text = delegate->data();
118 EXPECT_TRUE(text.find("What is this page?") != std::string::npos);
119
120 // Test 404 response.
121 url = test_server_.GetURL("files/thisfiledoesntexist.html");
122 delegate.reset(new FetcherDelegate);
123 fetcher.reset(new ResourceFetcher(url, frame,
124 WebURLRequest::TargetIsMainFrame,
125 delegate->NewCallback()));
126
127 delegate->WaitForResponse();
128
129 ASSERT_TRUE(delegate->completed());
130 EXPECT_EQ(delegate->response().httpStatusCode(), 404);
131 EXPECT_TRUE(delegate->data().find("Not Found.") != std::string::npos);
132 }
133
134 // Flaky, http://crbug.com/51622.
135 TEST_F(ResourceFetcherTests, DISABLED_ResourceFetcherDidFail) {
136 ASSERT_TRUE(test_server_.Start());
137
138 WebFrame* frame = test_shell_->webView()->mainFrame();
139
140 // Try to fetch a page on a site that doesn't exist.
141 GURL url("http://localhost:1339/doesnotexist");
142 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
143 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher(
144 url, frame, WebURLRequest::TargetIsMainFrame, delegate->NewCallback()));
145
146 delegate->WaitForResponse();
147
148 // When we fail, we still call the Delegate callback but we pass in empty
149 // values.
150 EXPECT_TRUE(delegate->completed());
151 EXPECT_TRUE(delegate->response().isNull());
152 EXPECT_EQ(delegate->data(), std::string());
153 EXPECT_FALSE(delegate->timed_out());
154 }
155
156 TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) {
157 ASSERT_TRUE(test_server_.Start());
158
159 WebFrame* frame = test_shell_->webView()->mainFrame();
160
161 // Grab a page that takes at least 1 sec to respond, but set the fetcher to
162 // timeout in 0 sec.
163 GURL url(test_server_.GetURL("slow?1"));
164 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
165 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout(
166 url, frame, WebURLRequest::TargetIsMainFrame,
167 0, delegate->NewCallback()));
168
169 delegate->WaitForResponse();
170
171 // When we timeout, we still call the Delegate callback but we pass in empty
172 // values.
173 EXPECT_TRUE(delegate->completed());
174 EXPECT_TRUE(delegate->response().isNull());
175 EXPECT_EQ(delegate->data(), std::string());
176 EXPECT_FALSE(delegate->timed_out());
177 }
178
179 class EvilFetcherDelegate : public FetcherDelegate { 102 class EvilFetcherDelegate : public FetcherDelegate {
180 public: 103 public:
181 virtual ~EvilFetcherDelegate() {} 104 virtual ~EvilFetcherDelegate() {}
182 105
183 void SetFetcher(ResourceFetcher* fetcher) { 106 void SetFetcher(ResourceFetcher* fetcher) {
184 fetcher_.reset(fetcher); 107 fetcher_.reset(fetcher);
185 } 108 }
186 109
187 virtual void OnURLFetchComplete(const WebURLResponse& response, 110 virtual void OnURLFetchComplete(const WebURLResponse& response,
188 const std::string& data) OVERRIDE { 111 const std::string& data) OVERRIDE {
189 // Destroy the ResourceFetcher here. We are testing that upon returning 112 // Destroy the ResourceFetcher here. We are testing that upon returning
190 // to the ResourceFetcher that it does not crash. 113 // to the ResourceFetcher that it does not crash.
191 fetcher_.reset(); 114 fetcher_.reset();
192 FetcherDelegate::OnURLFetchComplete(response, data); 115 FetcherDelegate::OnURLFetchComplete(response, data);
193 } 116 }
194 117
195 private: 118 private:
196 scoped_ptr<ResourceFetcher> fetcher_; 119 scoped_ptr<ResourceFetcher> fetcher_;
197 }; 120 };
198 121
199 TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) { 122 class ResourceFetcherTests : public ContentBrowserTest {
200 ASSERT_TRUE(test_server_.Start()); 123 public:
124 virtual void SetUpCommandLine(CommandLine* command_line) {
125 command_line->AppendSwitch(switches::kSingleProcess);
126 }
201 127
202 WebFrame* frame = test_shell_->webView()->mainFrame(); 128 RenderView* GetRenderView() {
129 // We could have the test on the UI thread get the WebContent's routing ID,
130 // but we know this will be the first RV so skip that and just hardcode it.
131 return RenderView::FromRoutingID(1);
132 }
133
134 void ResourceFetcherDownloadOnRenderer(const GURL& url) {
135 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
136
137 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
138 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher(
139 url, frame, WebURLRequest::TargetIsMainFrame, delegate->NewCallback()));
140
141 delegate->WaitForResponse();
142
143 ASSERT_TRUE(delegate->completed());
144 EXPECT_EQ(delegate->response().httpStatusCode(), 200);
145 std::string text = delegate->data();
146 EXPECT_TRUE(text.find("Basic html test.") != std::string::npos);
147 }
148
149 void ResourceFetcher404OnRenderer(const GURL& url) {
150 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
151
152 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
153 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher(
154 url, frame, WebURLRequest::TargetIsMainFrame, delegate->NewCallback()));
155
156 delegate->WaitForResponse();
157
158 ASSERT_TRUE(delegate->completed());
159 EXPECT_EQ(delegate->response().httpStatusCode(), 404);
160 EXPECT_TRUE(delegate->data().find("Not Found.") != std::string::npos);
161 }
162
163 void ResourceFetcherDidFailOnRenderer() {
164 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
165
166 // Try to fetch a page on a site that doesn't exist.
167 GURL url("http://localhost:1339/doesnotexist");
168 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
169 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher(
170 url, frame, WebURLRequest::TargetIsMainFrame, delegate->NewCallback()));
171
172 delegate->WaitForResponse();
173
174 // When we fail, we still call the Delegate callback but we pass in empty
175 // values.
176 EXPECT_TRUE(delegate->completed());
177 EXPECT_TRUE(delegate->response().isNull());
178 EXPECT_EQ(delegate->data(), std::string());
179 EXPECT_FALSE(delegate->timed_out());
180 }
181
182 void ResourceFetcherTimeoutOnRenderer(const GURL& url) {
183 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
184
185 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate);
186 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout(
187 url, frame, WebURLRequest::TargetIsMainFrame,
188 0, delegate->NewCallback()));
189
190 delegate->WaitForResponse();
191
192 // When we timeout, we still call the Delegate callback but we pass in empty
193 // values.
194 EXPECT_TRUE(delegate->completed());
195 EXPECT_TRUE(delegate->response().isNull());
196 EXPECT_EQ(delegate->data(), std::string());
197 EXPECT_FALSE(delegate->timed_out());
198 }
199
200 void ResourceFetcherDeletedInCallbackOnRenderer(const GURL& url) {
201 WebFrame* frame = GetRenderView()->GetWebView()->mainFrame();
202
203 scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate);
204 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout(
205 url, frame, WebURLRequest::TargetIsMainFrame,
206 0, delegate->NewCallback()));
207 delegate->SetFetcher(fetcher.release());
208
209 delegate->WaitForResponse();
210 EXPECT_FALSE(delegate->timed_out());
211 }
212 };
213
214 // These tests randomly crash on the Mac trybots with no callstacks. They never
215 // failed locally across hundreds of runs in both debug and release.
216 #if !defined(OS_MACOSX)
217
218 // Test a fetch from the test server.
219 // If this flakes, use http://crbug.com/51622.
220 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDownload) {
221 // Need to spin up the renderer.
222 NavigateToURL(shell(), GURL("about:blank"));
223
224 ASSERT_TRUE(test_server()->Start());
225 GURL url(test_server()->GetURL("files/simple_page.html"));
226
227 PostTaskToInProcessRendererAndWait(
228 base::Bind(&ResourceFetcherTests::ResourceFetcherDownloadOnRenderer,
229 base::Unretained(this), url));
230 }
231
232 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcher404) {
233 // Need to spin up the renderer.
234 NavigateToURL(shell(), GURL("about:blank"));
235
236 // Test 404 response.
237 ASSERT_TRUE(test_server()->Start());
238 GURL url = test_server()->GetURL("files/thisfiledoesntexist.html");
239
240 PostTaskToInProcessRendererAndWait(
241 base::Bind(&ResourceFetcherTests::ResourceFetcher404OnRenderer,
242 base::Unretained(this), url));
243 }
244
245 // If this flakes, use http://crbug.com/51622.
246 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDidFail) {
247 // Need to spin up the renderer.
248 NavigateToURL(shell(), GURL("about:blank"));
249
250 PostTaskToInProcessRendererAndWait(
251 base::Bind(&ResourceFetcherTests::ResourceFetcherDidFailOnRenderer,
252 base::Unretained(this)));
253 }
254
255 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) {
256 // Need to spin up the renderer.
257 NavigateToURL(shell(), GURL("about:blank"));
203 258
204 // Grab a page that takes at least 1 sec to respond, but set the fetcher to 259 // Grab a page that takes at least 1 sec to respond, but set the fetcher to
205 // timeout in 0 sec. 260 // timeout in 0 sec.
206 GURL url(test_server_.GetURL("slow?1")); 261 ASSERT_TRUE(test_server()->Start());
207 scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); 262 GURL url(test_server()->GetURL("slow?1"));
208 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout(
209 url, frame, WebURLRequest::TargetIsMainFrame,
210 0, delegate->NewCallback()));
211 delegate->SetFetcher(fetcher.release());
212 263
213 delegate->WaitForResponse(); 264 PostTaskToInProcessRendererAndWait(
214 EXPECT_FALSE(delegate->timed_out()); 265 base::Bind(&ResourceFetcherTests::ResourceFetcherTimeoutOnRenderer,
266 base::Unretained(this), url));
215 } 267 }
216 268
217 } // namespace 269 IN_PROC_BROWSER_TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) {
270 // Need to spin up the renderer.
271 NavigateToURL(shell(), GURL("about:blank"));
272
273 // Grab a page that takes at least 1 sec to respond, but set the fetcher to
274 // timeout in 0 sec.
275 ASSERT_TRUE(test_server()->Start());
276 GURL url(test_server()->GetURL("slow?1"));
277
278 PostTaskToInProcessRendererAndWait(
279 base::Bind(
280 &ResourceFetcherTests::ResourceFetcherDeletedInCallbackOnRenderer,
281 base::Unretained(this), url));
282 }
283
284 #endif // OS_MACOSX
285
286 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/dom_serializer_browsertest.cc ('k') | webkit/chromeos/fileapi/file_access_permissions.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698