OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "config.h" | |
6 | |
7 #include "base/compiler_specific.h" | |
8 | |
9 MSVC_PUSH_WARNING_LEVEL(0); | |
10 #include "ResourceResponse.h" | |
11 MSVC_POP_WARNING(); | |
12 #undef LOG | |
13 | |
14 #if defined(OS_LINUX) | 5 #if defined(OS_LINUX) |
15 #include <gtk/gtk.h> | 6 #include <gtk/gtk.h> |
16 #endif | 7 #endif |
17 | 8 |
| 9 #include "webkit/api/public/WebURLResponse.h" |
18 #include "webkit/glue/unittest_test_server.h" | 10 #include "webkit/glue/unittest_test_server.h" |
19 #include "webkit/glue/webview.h" | 11 #include "webkit/glue/webview.h" |
20 #include "webkit/glue/webframe_impl.h" | 12 #include "webkit/glue/webframe.h" |
21 #include "webkit/glue/resource_fetcher.h" | 13 #include "webkit/glue/resource_fetcher.h" |
22 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | 14 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
23 #include "webkit/tools/test_shell/test_shell_test.h" | 15 #include "webkit/tools/test_shell/test_shell_test.h" |
24 | 16 |
25 using WebCore::ResourceResponse; | 17 using WebKit::WebURLResponse; |
| 18 using webkit_glue::ResourceFetcher; |
| 19 using webkit_glue::ResourceFetcherWithTimeout; |
26 | 20 |
27 namespace { | 21 namespace { |
28 | 22 |
29 class ResourceFetcherTests : public TestShellTest { | 23 class ResourceFetcherTests : public TestShellTest { |
30 public: | 24 public: |
31 void SetUp() { | 25 void SetUp() { |
32 TestShellTest::SetUp(); | 26 TestShellTest::SetUp(); |
33 } | 27 } |
34 void TearDown() { | 28 void TearDown() { |
35 TestShellTest::TearDown(); | 29 TestShellTest::TearDown(); |
36 } | 30 } |
37 }; | 31 }; |
38 | 32 |
39 static const int kMaxWaitTimeMs = 5000; | 33 static const int kMaxWaitTimeMs = 5000; |
40 static const int kWaitIntervalMs = 100; | 34 static const int kWaitIntervalMs = 100; |
41 | 35 |
42 class FetcherDelegate : public ResourceFetcher::Delegate { | 36 class FetcherDelegate { |
43 public: | 37 public: |
44 FetcherDelegate() | 38 FetcherDelegate() |
45 : timer_id_(0), completed_(false), time_elapsed_ms_(0) { | 39 : timer_id_(0), completed_(false), time_elapsed_ms_(0) { |
46 // Start a repeating timer waiting for the download to complete. The | 40 // Start a repeating timer waiting for the download to complete. The |
47 // callback has to be a static function, so we hold on to our instance. | 41 // callback has to be a static function, so we hold on to our instance. |
48 FetcherDelegate::instance_ = this; | 42 FetcherDelegate::instance_ = this; |
49 CreateTimer(kWaitIntervalMs); | 43 CreateTimer(kWaitIntervalMs); |
50 } | 44 } |
51 | 45 |
52 virtual void OnURLFetchComplete(const ResourceResponse& response, | 46 ResourceFetcher::Callback* NewCallback() { |
53 const std::string& data) { | 47 return ::NewCallback(this, &FetcherDelegate::OnURLFetchComplete); |
| 48 } |
| 49 |
| 50 void OnURLFetchComplete(const WebURLResponse& response, |
| 51 const std::string& data) { |
54 response_ = response; | 52 response_ = response; |
55 data_ = data; | 53 data_ = data; |
56 completed_ = true; | 54 completed_ = true; |
57 DestroyTimer(); | 55 DestroyTimer(); |
58 MessageLoop::current()->Quit(); | 56 MessageLoop::current()->Quit(); |
59 } | 57 } |
60 | 58 |
61 bool completed() const { return completed_; } | 59 bool completed() const { return completed_; } |
62 bool timed_out() const { return time_elapsed_ms_ > kMaxWaitTimeMs; } | 60 bool timed_out() const { return time_elapsed_ms_ > kMaxWaitTimeMs; } |
63 | 61 |
64 int time_elapsed_ms() const { return time_elapsed_ms_; } | 62 int time_elapsed_ms() const { return time_elapsed_ms_; } |
65 std::string data() const { return data_; } | 63 std::string data() const { return data_; } |
66 ResourceResponse response() const { return response_; } | 64 const WebURLResponse& response() const { return response_; } |
67 | 65 |
68 // 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. We use a loop here b/c the |
69 // testing infrastructure (test_shell) can generate spurious calls to the | 67 // testing infrastructure (test_shell) can generate spurious calls to the |
70 // MessageLoop's Quit method. | 68 // MessageLoop's Quit method. |
71 void WaitForResponse() { | 69 void WaitForResponse() { |
72 while (!completed() && !timed_out()) | 70 while (!completed() && !timed_out()) |
73 MessageLoop::current()->Run(); | 71 MessageLoop::current()->Run(); |
74 } | 72 } |
75 | 73 |
76 void CreateTimer(int interval) { | 74 void CreateTimer(int interval) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 private: | 136 private: |
139 #if defined(OS_WIN) | 137 #if defined(OS_WIN) |
140 UINT_PTR timer_id_; | 138 UINT_PTR timer_id_; |
141 #elif defined(OS_LINUX) | 139 #elif defined(OS_LINUX) |
142 guint timer_id_; | 140 guint timer_id_; |
143 #elif defined(OS_MACOSX) | 141 #elif defined(OS_MACOSX) |
144 CFRunLoopTimerRef timer_id_; | 142 CFRunLoopTimerRef timer_id_; |
145 #endif | 143 #endif |
146 bool completed_; | 144 bool completed_; |
147 int time_elapsed_ms_; | 145 int time_elapsed_ms_; |
148 ResourceResponse response_; | 146 WebURLResponse response_; |
149 std::string data_; | 147 std::string data_; |
150 }; | 148 }; |
151 | 149 |
152 FetcherDelegate* FetcherDelegate::instance_ = NULL; | 150 FetcherDelegate* FetcherDelegate::instance_ = NULL; |
153 | 151 |
154 // Test a fetch from the test server. | 152 // Test a fetch from the test server. |
155 TEST_F(ResourceFetcherTests, ResourceFetcherDownload) { | 153 TEST_F(ResourceFetcherTests, ResourceFetcherDownload) { |
156 scoped_refptr<UnittestTestServer> server = | 154 scoped_refptr<UnittestTestServer> server = |
157 UnittestTestServer::CreateServer(); | 155 UnittestTestServer::CreateServer(); |
158 ASSERT_TRUE(NULL != server.get()); | 156 ASSERT_TRUE(NULL != server.get()); |
159 | 157 |
160 WebFrame* web_frame = test_shell_->webView()->GetMainFrame(); | 158 WebFrame* frame = test_shell_->webView()->GetMainFrame(); |
161 // Not safe, but this is a unittest, so whatever. | |
162 WebFrameImpl* web_frame_impl = reinterpret_cast<WebFrameImpl*>(web_frame); | |
163 WebCore::Frame* frame = web_frame_impl->frame(); | |
164 | 159 |
165 GURL url = server->TestServerPage("files/test_shell/index.html"); | 160 GURL url = server->TestServerPage("files/test_shell/index.html"); |
166 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 161 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
167 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher( | 162 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher( |
168 url, frame, delegate.get())); | 163 url, frame, delegate->NewCallback())); |
169 | 164 |
170 delegate->WaitForResponse(); | 165 delegate->WaitForResponse(); |
171 | 166 |
172 ASSERT_TRUE(delegate->completed()); | 167 ASSERT_TRUE(delegate->completed()); |
173 EXPECT_EQ(delegate->response().httpStatusCode(), 200); | 168 EXPECT_EQ(delegate->response().httpStatusCode(), 200); |
174 std::string text = delegate->data(); | 169 std::string text = delegate->data(); |
175 EXPECT_TRUE(text.find("What is this page?") != std::string::npos); | 170 EXPECT_TRUE(text.find("What is this page?") != std::string::npos); |
176 | 171 |
177 // Test 404 response. | 172 // Test 404 response. |
178 url = server->TestServerPage("files/thisfiledoesntexist.html"); | 173 url = server->TestServerPage("files/thisfiledoesntexist.html"); |
179 delegate.reset(new FetcherDelegate); | 174 delegate.reset(new FetcherDelegate); |
180 fetcher.reset(new ResourceFetcher(url, frame, delegate.get())); | 175 fetcher.reset(new ResourceFetcher(url, frame, delegate->NewCallback())); |
181 | 176 |
182 delegate->WaitForResponse(); | 177 delegate->WaitForResponse(); |
183 | 178 |
184 ASSERT_TRUE(delegate->completed()); | 179 ASSERT_TRUE(delegate->completed()); |
185 EXPECT_EQ(delegate->response().httpStatusCode(), 404); | 180 EXPECT_EQ(delegate->response().httpStatusCode(), 404); |
186 EXPECT_TRUE(delegate->data().find("Not Found.") != std::string::npos); | 181 EXPECT_TRUE(delegate->data().find("Not Found.") != std::string::npos); |
187 } | 182 } |
188 | 183 |
189 TEST_F(ResourceFetcherTests, ResourceFetcherDidFail) { | 184 TEST_F(ResourceFetcherTests, ResourceFetcherDidFail) { |
190 scoped_refptr<UnittestTestServer> server = | 185 scoped_refptr<UnittestTestServer> server = |
191 UnittestTestServer::CreateServer(); | 186 UnittestTestServer::CreateServer(); |
192 ASSERT_TRUE(NULL != server.get()); | 187 ASSERT_TRUE(NULL != server.get()); |
193 | 188 |
194 WebFrame* web_frame = test_shell_->webView()->GetMainFrame(); | 189 WebFrame* frame = test_shell_->webView()->GetMainFrame(); |
195 // Not safe, but this is a unittest, so whatever. | |
196 WebFrameImpl* web_frame_impl = reinterpret_cast<WebFrameImpl*>(web_frame); | |
197 WebCore::Frame* frame = web_frame_impl->frame(); | |
198 | 190 |
199 // Try to fetch a page on a site that doesn't exist. | 191 // Try to fetch a page on a site that doesn't exist. |
200 GURL url("http://localhost:1339/doesnotexist"); | 192 GURL url("http://localhost:1339/doesnotexist"); |
201 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 193 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
202 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher( | 194 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher( |
203 url, frame, delegate.get())); | 195 url, frame, delegate->NewCallback())); |
204 | 196 |
205 delegate->WaitForResponse(); | 197 delegate->WaitForResponse(); |
206 | 198 |
207 // When we fail, we still call the Delegate callback but we pass in empty | 199 // When we fail, we still call the Delegate callback but we pass in empty |
208 // values. | 200 // values. |
209 EXPECT_TRUE(delegate->completed()); | 201 EXPECT_TRUE(delegate->completed()); |
210 EXPECT_TRUE(delegate->response().isNull()); | 202 EXPECT_TRUE(delegate->response().isNull()); |
211 EXPECT_EQ(delegate->data(), std::string()); | 203 EXPECT_EQ(delegate->data(), std::string()); |
212 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); | 204 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); |
213 } | 205 } |
214 | 206 |
215 TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) { | 207 TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) { |
216 scoped_refptr<UnittestTestServer> server = | 208 scoped_refptr<UnittestTestServer> server = |
217 UnittestTestServer::CreateServer(); | 209 UnittestTestServer::CreateServer(); |
218 ASSERT_TRUE(NULL != server.get()); | 210 ASSERT_TRUE(NULL != server.get()); |
219 | 211 |
220 WebFrame* web_frame = test_shell_->webView()->GetMainFrame(); | 212 WebFrame* frame = test_shell_->webView()->GetMainFrame(); |
221 // Not safe, but this is a unittest, so whatever. | |
222 WebFrameImpl* web_frame_impl = reinterpret_cast<WebFrameImpl*>(web_frame); | |
223 WebCore::Frame* frame = web_frame_impl->frame(); | |
224 | 213 |
225 // Grab a page that takes at least 1 sec to respond, but set the fetcher to | 214 // Grab a page that takes at least 1 sec to respond, but set the fetcher to |
226 // timeout in 0 sec. | 215 // timeout in 0 sec. |
227 GURL url = server->TestServerPage("slow?1"); | 216 GURL url = server->TestServerPage("slow?1"); |
228 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 217 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
229 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( | 218 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( |
230 url, frame, 0, delegate.get())); | 219 url, frame, 0, delegate->NewCallback())); |
231 | 220 |
232 delegate->WaitForResponse(); | 221 delegate->WaitForResponse(); |
233 | 222 |
234 // When we timeout, we still call the Delegate callback but we pass in empty | 223 // When we timeout, we still call the Delegate callback but we pass in empty |
235 // values. | 224 // values. |
236 EXPECT_TRUE(delegate->completed()); | 225 EXPECT_TRUE(delegate->completed()); |
237 EXPECT_TRUE(delegate->response().isNull()); | 226 EXPECT_TRUE(delegate->response().isNull()); |
238 EXPECT_EQ(delegate->data(), std::string()); | 227 EXPECT_EQ(delegate->data(), std::string()); |
239 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); | 228 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); |
240 } | 229 } |
241 | 230 |
242 } // namespace | 231 } // namespace |
OLD | NEW |