| 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 "webkit/glue/resource_fetcher.h" | 5 #include "webkit/glue/resource_fetcher.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/timer.h" |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLResponse.h" |
| 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 12 #include "webkit/glue/unittest_test_server.h" | 13 #include "webkit/glue/unittest_test_server.h" |
| 13 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | 14 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
| 14 #include "webkit/tools/test_shell/test_shell_test.h" | 15 #include "webkit/tools/test_shell/test_shell_test.h" |
| 15 | 16 |
| 16 #if defined(TOOLKIT_USES_GTK) | |
| 17 #include <gtk/gtk.h> | |
| 18 #endif | |
| 19 | |
| 20 using WebKit::WebFrame; | 17 using WebKit::WebFrame; |
| 21 using WebKit::WebURLResponse; | 18 using WebKit::WebURLResponse; |
| 22 using webkit_glue::ResourceFetcher; | 19 using webkit_glue::ResourceFetcher; |
| 23 using webkit_glue::ResourceFetcherWithTimeout; | 20 using webkit_glue::ResourceFetcherWithTimeout; |
| 24 | 21 |
| 25 namespace { | 22 namespace { |
| 26 | 23 |
| 27 class ResourceFetcherTests : public TestShellTest { | 24 class ResourceFetcherTests : public TestShellTest { |
| 28 protected: | 25 protected: |
| 29 UnittestTestServer test_server_; | 26 UnittestTestServer test_server_; |
| 30 }; | 27 }; |
| 31 | 28 |
| 32 static const int kMaxWaitTimeMs = 5000; | 29 static const int kMaxWaitTimeMs = 5000; |
| 33 static const int kWaitIntervalMs = 100; | |
| 34 | 30 |
| 35 class FetcherDelegate { | 31 class FetcherDelegate { |
| 36 public: | 32 public: |
| 37 FetcherDelegate() | 33 FetcherDelegate() |
| 38 : timer_id_(0), completed_(false), time_elapsed_ms_(0) { | 34 : completed_(false), |
| 35 timed_out_(false) { |
| 39 // Start a repeating timer waiting for the download to complete. The | 36 // Start a repeating timer waiting for the download to complete. The |
| 40 // callback has to be a static function, so we hold on to our instance. | 37 // callback has to be a static function, so we hold on to our instance. |
| 41 FetcherDelegate::instance_ = this; | 38 FetcherDelegate::instance_ = this; |
| 42 CreateTimer(kWaitIntervalMs); | 39 StartTimer(); |
| 43 } | 40 } |
| 44 | 41 |
| 45 ResourceFetcher::Callback* NewCallback() { | 42 ResourceFetcher::Callback* NewCallback() { |
| 46 return ::NewCallback(this, &FetcherDelegate::OnURLFetchComplete); | 43 return ::NewCallback(this, &FetcherDelegate::OnURLFetchComplete); |
| 47 } | 44 } |
| 48 | 45 |
| 49 virtual void OnURLFetchComplete(const WebURLResponse& response, | 46 virtual void OnURLFetchComplete(const WebURLResponse& response, |
| 50 const std::string& data) { | 47 const std::string& data) { |
| 51 response_ = response; | 48 response_ = response; |
| 52 data_ = data; | 49 data_ = data; |
| 53 completed_ = true; | 50 completed_ = true; |
| 54 DestroyTimer(); | 51 timer_.Stop(); |
| 55 MessageLoop::current()->Quit(); | 52 MessageLoop::current()->Quit(); |
| 56 } | 53 } |
| 57 | 54 |
| 58 bool completed() const { return completed_; } | 55 bool completed() const { return completed_; } |
| 59 bool timed_out() const { return time_elapsed_ms_ > kMaxWaitTimeMs; } | 56 bool timed_out() const { return timed_out_; } |
| 60 | 57 |
| 61 int time_elapsed_ms() const { return time_elapsed_ms_; } | |
| 62 std::string data() const { return data_; } | 58 std::string data() const { return data_; } |
| 63 const WebURLResponse& response() const { return response_; } | 59 const WebURLResponse& response() const { return response_; } |
| 64 | 60 |
| 65 // Wait for the request to complete or timeout. We use a loop here b/c the | 61 // Wait for the request to complete or timeout. We use a loop here b/c the |
| 66 // testing infrastructure (test_shell) can generate spurious calls to the | 62 // testing infrastructure (test_shell) can generate spurious calls to the |
| 67 // MessageLoop's Quit method. | 63 // MessageLoop's Quit method. |
| 68 void WaitForResponse() { | 64 void WaitForResponse() { |
| 69 while (!completed() && !timed_out()) | 65 while (!completed() && !timed_out()) |
| 70 MessageLoop::current()->Run(); | 66 MessageLoop::current()->Run(); |
| 71 } | 67 } |
| 72 | 68 |
| 73 void CreateTimer(int interval) { | 69 void StartTimer() { |
| 74 #if defined(OS_WIN) | 70 timer_.Start(base::TimeDelta::FromMilliseconds(kMaxWaitTimeMs), |
| 75 timer_id_ = ::SetTimer(NULL, NULL, interval, | 71 this, |
| 76 &FetcherDelegate::TimerCallback); | 72 &FetcherDelegate::TimerFired); |
| 77 #elif defined(TOOLKIT_USES_GTK) | |
| 78 timer_id_ = g_timeout_add(interval, &FetcherDelegate::TimerCallback, NULL); | |
| 79 #elif defined(OS_MACOSX) | |
| 80 // CFAbsoluteTime is in seconds and |interval| is in ms, so make sure we | |
| 81 // keep the units correct. | |
| 82 CFTimeInterval interval_in_seconds = static_cast<double>(interval) / 1000.0; | |
| 83 CFAbsoluteTime fire_date = | |
| 84 CFAbsoluteTimeGetCurrent() + interval_in_seconds; | |
| 85 timer_id_ = CFRunLoopTimerCreate(NULL, fire_date, interval_in_seconds, 0, | |
| 86 0, FetcherDelegate::TimerCallback, NULL); | |
| 87 CFRunLoopAddTimer(CFRunLoopGetCurrent(), timer_id_, kCFRunLoopCommonModes); | |
| 88 #endif | |
| 89 } | 73 } |
| 90 | 74 |
| 91 void DestroyTimer() { | |
| 92 #if defined(OS_WIN) | |
| 93 ::KillTimer(NULL, timer_id_); | |
| 94 #elif defined(TOOLKIT_USES_GTK) | |
| 95 g_source_remove(timer_id_); | |
| 96 #elif defined(OS_MACOSX) | |
| 97 CFRunLoopRemoveTimer(CFRunLoopGetCurrent(), timer_id_, | |
| 98 kCFRunLoopCommonModes); | |
| 99 CFRelease(timer_id_); | |
| 100 #endif | |
| 101 } | |
| 102 | |
| 103 #if defined(OS_WIN) | |
| 104 // Static timer callback, just passes through to instance version. | |
| 105 static VOID CALLBACK TimerCallback(HWND hwnd, UINT msg, UINT_PTR timer_id, | |
| 106 DWORD ms) { | |
| 107 instance_->TimerFired(); | |
| 108 } | |
| 109 #elif defined(TOOLKIT_USES_GTK) | |
| 110 static gboolean TimerCallback(gpointer data) { | |
| 111 instance_->TimerFired(); | |
| 112 return true; | |
| 113 } | |
| 114 #elif defined(OS_MACOSX) | |
| 115 static void TimerCallback(CFRunLoopTimerRef timer, void* info) { | |
| 116 instance_->TimerFired(); | |
| 117 } | |
| 118 #endif | |
| 119 | |
| 120 void TimerFired() { | 75 void TimerFired() { |
| 121 ASSERT_FALSE(completed_); | 76 ASSERT_FALSE(completed_); |
| 122 | 77 |
| 123 if (timed_out()) { | 78 timed_out_ = true; |
| 124 DestroyTimer(); | 79 MessageLoop::current()->Quit(); |
| 125 MessageLoop::current()->Quit(); | 80 FAIL() << "fetch timed out"; |
| 126 FAIL() << "fetch timed out"; | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 time_elapsed_ms_ += kWaitIntervalMs; | |
| 131 } | 81 } |
| 132 | 82 |
| 133 static FetcherDelegate* instance_; | 83 static FetcherDelegate* instance_; |
| 134 | 84 |
| 135 private: | 85 private: |
| 136 #if defined(OS_WIN) | 86 base::OneShotTimer<FetcherDelegate> timer_; |
| 137 UINT_PTR timer_id_; | |
| 138 #elif defined(TOOLKIT_USES_GTK) | |
| 139 guint timer_id_; | |
| 140 #elif defined(OS_MACOSX) | |
| 141 CFRunLoopTimerRef timer_id_; | |
| 142 #endif | |
| 143 bool completed_; | 87 bool completed_; |
| 144 int time_elapsed_ms_; | 88 bool timed_out_; |
| 145 WebURLResponse response_; | 89 WebURLResponse response_; |
| 146 std::string data_; | 90 std::string data_; |
| 147 }; | 91 }; |
| 148 | 92 |
| 149 FetcherDelegate* FetcherDelegate::instance_ = NULL; | 93 FetcherDelegate* FetcherDelegate::instance_ = NULL; |
| 150 | 94 |
| 151 // Test a fetch from the test server. | 95 // Test a fetch from the test server. |
| 152 // Flaky, http://crbug.com/51622. | 96 // Flaky, http://crbug.com/51622. |
| 153 TEST_F(ResourceFetcherTests, FLAKY_ResourceFetcherDownload) { | 97 TEST_F(ResourceFetcherTests, FLAKY_ResourceFetcherDownload) { |
| 154 ASSERT_TRUE(test_server_.Start()); | 98 ASSERT_TRUE(test_server_.Start()); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher( | 135 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcher( |
| 192 url, frame, delegate->NewCallback())); | 136 url, frame, delegate->NewCallback())); |
| 193 | 137 |
| 194 delegate->WaitForResponse(); | 138 delegate->WaitForResponse(); |
| 195 | 139 |
| 196 // When we fail, we still call the Delegate callback but we pass in empty | 140 // When we fail, we still call the Delegate callback but we pass in empty |
| 197 // values. | 141 // values. |
| 198 EXPECT_TRUE(delegate->completed()); | 142 EXPECT_TRUE(delegate->completed()); |
| 199 EXPECT_TRUE(delegate->response().isNull()); | 143 EXPECT_TRUE(delegate->response().isNull()); |
| 200 EXPECT_EQ(delegate->data(), std::string()); | 144 EXPECT_EQ(delegate->data(), std::string()); |
| 201 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); | 145 EXPECT_FALSE(delegate->timed_out()); |
| 202 } | 146 } |
| 203 | 147 |
| 204 TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) { | 148 TEST_F(ResourceFetcherTests, ResourceFetcherTimeout) { |
| 205 ASSERT_TRUE(test_server_.Start()); | 149 ASSERT_TRUE(test_server_.Start()); |
| 206 | 150 |
| 207 WebFrame* frame = test_shell_->webView()->mainFrame(); | 151 WebFrame* frame = test_shell_->webView()->mainFrame(); |
| 208 | 152 |
| 209 // Grab a page that takes at least 1 sec to respond, but set the fetcher to | 153 // Grab a page that takes at least 1 sec to respond, but set the fetcher to |
| 210 // timeout in 0 sec. | 154 // timeout in 0 sec. |
| 211 GURL url(test_server_.GetURL("slow?1")); | 155 GURL url(test_server_.GetURL("slow?1")); |
| 212 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); | 156 scoped_ptr<FetcherDelegate> delegate(new FetcherDelegate); |
| 213 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( | 157 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( |
| 214 url, frame, 0, delegate->NewCallback())); | 158 url, frame, 0, delegate->NewCallback())); |
| 215 | 159 |
| 216 delegate->WaitForResponse(); | 160 delegate->WaitForResponse(); |
| 217 | 161 |
| 218 // When we timeout, we still call the Delegate callback but we pass in empty | 162 // When we timeout, we still call the Delegate callback but we pass in empty |
| 219 // values. | 163 // values. |
| 220 EXPECT_TRUE(delegate->completed()); | 164 EXPECT_TRUE(delegate->completed()); |
| 221 EXPECT_TRUE(delegate->response().isNull()); | 165 EXPECT_TRUE(delegate->response().isNull()); |
| 222 EXPECT_EQ(delegate->data(), std::string()); | 166 EXPECT_EQ(delegate->data(), std::string()); |
| 223 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); | 167 EXPECT_FALSE(delegate->timed_out()); |
| 224 } | 168 } |
| 225 | 169 |
| 226 class EvilFetcherDelegate : public FetcherDelegate { | 170 class EvilFetcherDelegate : public FetcherDelegate { |
| 227 public: | 171 public: |
| 228 void SetFetcher(ResourceFetcher* fetcher) { | 172 void SetFetcher(ResourceFetcher* fetcher) { |
| 229 fetcher_.reset(fetcher); | 173 fetcher_.reset(fetcher); |
| 230 } | 174 } |
| 231 | 175 |
| 232 void OnURLFetchComplete(const WebURLResponse& response, | 176 void OnURLFetchComplete(const WebURLResponse& response, |
| 233 const std::string& data) { | 177 const std::string& data) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 248 | 192 |
| 249 // Grab a page that takes at least 1 sec to respond, but set the fetcher to | 193 // Grab a page that takes at least 1 sec to respond, but set the fetcher to |
| 250 // timeout in 0 sec. | 194 // timeout in 0 sec. |
| 251 GURL url(test_server_.GetURL("slow?1")); | 195 GURL url(test_server_.GetURL("slow?1")); |
| 252 scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); | 196 scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); |
| 253 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( | 197 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( |
| 254 url, frame, 0, delegate->NewCallback())); | 198 url, frame, 0, delegate->NewCallback())); |
| 255 delegate->SetFetcher(fetcher.release()); | 199 delegate->SetFetcher(fetcher.release()); |
| 256 | 200 |
| 257 delegate->WaitForResponse(); | 201 delegate->WaitForResponse(); |
| 202 EXPECT_FALSE(delegate->timed_out()); |
| 258 } | 203 } |
| 259 | 204 |
| 260 } // namespace | 205 } // namespace |
| OLD | NEW |