| 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" | 5 #include "config.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 | 8 |
| 9 MSVC_PUSH_WARNING_LEVEL(0); | 9 MSVC_PUSH_WARNING_LEVEL(0); |
| 10 #include "ResourceResponse.h" | 10 #include "ResourceResponse.h" |
| 11 MSVC_POP_WARNING(); | 11 MSVC_POP_WARNING(); |
| 12 #undef LOG | 12 #undef LOG |
| 13 | 13 |
| 14 #if defined(OS_LINUX) |
| 15 #include <gtk/gtk.h> |
| 16 #endif |
| 17 |
| 14 #include "webkit/glue/unittest_test_server.h" | 18 #include "webkit/glue/unittest_test_server.h" |
| 15 #include "webkit/glue/webview.h" | 19 #include "webkit/glue/webview.h" |
| 16 #include "webkit/glue/webframe_impl.h" | 20 #include "webkit/glue/webframe_impl.h" |
| 17 #include "webkit/glue/resource_fetcher.h" | 21 #include "webkit/glue/resource_fetcher.h" |
| 18 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" | 22 #include "webkit/tools/test_shell/simple_resource_loader_bridge.h" |
| 19 #include "webkit/tools/test_shell/test_shell_test.h" | 23 #include "webkit/tools/test_shell/test_shell_test.h" |
| 20 | 24 |
| 21 using WebCore::ResourceResponse; | 25 using WebCore::ResourceResponse; |
| 22 | 26 |
| 23 namespace { | 27 namespace { |
| 24 | 28 |
| 25 | |
| 26 class ResourceFetcherTests : public TestShellTest { | 29 class ResourceFetcherTests : public TestShellTest { |
| 27 public: | 30 public: |
| 28 void SetUp() { | 31 void SetUp() { |
| 29 TestShellTest::SetUp(); | 32 TestShellTest::SetUp(); |
| 30 } | 33 } |
| 31 void TearDown() { | 34 void TearDown() { |
| 32 TestShellTest::TearDown(); | 35 TestShellTest::TearDown(); |
| 33 } | 36 } |
| 34 }; | 37 }; |
| 35 | 38 |
| 36 static const int kMaxWaitTimeMs = 5000; | 39 static const int kMaxWaitTimeMs = 5000; |
| 37 static const int kWaitIntervalMs = 100; | 40 static const int kWaitIntervalMs = 100; |
| 38 | 41 |
| 39 class FetcherDelegate : public ResourceFetcher::Delegate { | 42 class FetcherDelegate : public ResourceFetcher::Delegate { |
| 40 public: | 43 public: |
| 41 FetcherDelegate() | 44 FetcherDelegate() |
| 42 : timer_id_(0), completed_(false), time_elapsed_ms_(0) { | 45 : timer_id_(0), completed_(false), time_elapsed_ms_(0) { |
| 43 // Start a repeating timer waiting for the download to complete. The | 46 // Start a repeating timer waiting for the download to complete. The |
| 44 // callback has to be a static function, so we hold on to our instance. | 47 // callback has to be a static function, so we hold on to our instance. |
| 45 FetcherDelegate::instance_ = this; | 48 FetcherDelegate::instance_ = this; |
| 46 timer_id_ = SetTimer(NULL, NULL, kWaitIntervalMs, | 49 CreateTimer(kWaitIntervalMs); |
| 47 &FetcherDelegate::TimerCallback); | |
| 48 } | 50 } |
| 49 | 51 |
| 50 virtual void OnURLFetchComplete(const ResourceResponse& response, | 52 virtual void OnURLFetchComplete(const ResourceResponse& response, |
| 51 const std::string& data) { | 53 const std::string& data) { |
| 52 response_ = response; | 54 response_ = response; |
| 53 data_ = data; | 55 data_ = data; |
| 54 completed_ = true; | 56 completed_ = true; |
| 55 KillTimer(NULL, timer_id_); | 57 DestroyTimer(); |
| 56 MessageLoop::current()->Quit(); | 58 MessageLoop::current()->Quit(); |
| 57 } | 59 } |
| 58 | 60 |
| 59 bool completed() const { return completed_; } | 61 bool completed() const { return completed_; } |
| 60 bool timed_out() const { return time_elapsed_ms_ > kMaxWaitTimeMs; } | 62 bool timed_out() const { return time_elapsed_ms_ > kMaxWaitTimeMs; } |
| 61 | 63 |
| 62 int time_elapsed_ms() const { return time_elapsed_ms_; } | 64 int time_elapsed_ms() const { return time_elapsed_ms_; } |
| 63 std::string data() const { return data_; } | 65 std::string data() const { return data_; } |
| 64 ResourceResponse response() const { return response_; } | 66 ResourceResponse response() const { return response_; } |
| 65 | 67 |
| 66 // Wait for the request to complete or timeout. We use a loop here b/c the | 68 // Wait for the request to complete or timeout. We use a loop here b/c the |
| 67 // testing infrastructure (test_shell) can generate spurious calls to the | 69 // testing infrastructure (test_shell) can generate spurious calls to the |
| 68 // MessageLoop's Quit method. | 70 // MessageLoop's Quit method. |
| 69 void WaitForResponse() { | 71 void WaitForResponse() { |
| 70 while (!completed() && !timed_out()) | 72 while (!completed() && !timed_out()) |
| 71 MessageLoop::current()->Run(); | 73 MessageLoop::current()->Run(); |
| 72 } | 74 } |
| 73 | 75 |
| 76 void CreateTimer(int interval) { |
| 77 #if defined(OS_WIN) |
| 78 timer_id_ = ::SetTimer(NULL, NULL, interval, |
| 79 &FetcherDelegate::TimerCallback); |
| 80 #elif defined(OS_LINUX) |
| 81 timer_id_ = g_timeout_add(interval, &FetcherDelegate::TimerCallback, NULL); |
| 82 #endif |
| 83 } |
| 84 |
| 85 void DestroyTimer() { |
| 86 #if defined(OS_WIN) |
| 87 ::KillTimer(NULL, timer_id_); |
| 88 #elif defined(OS_LINUX) |
| 89 g_source_remove(timer_id_); |
| 90 #endif |
| 91 } |
| 92 |
| 93 #if defined(OS_WIN) |
| 74 // Static timer callback, just passes through to instance version. | 94 // Static timer callback, just passes through to instance version. |
| 75 static VOID CALLBACK TimerCallback(HWND hwnd, UINT msg, UINT_PTR timer_id, | 95 static VOID CALLBACK TimerCallback(HWND hwnd, UINT msg, UINT_PTR timer_id, |
| 76 DWORD ms) { | 96 DWORD ms) { |
| 77 instance_->TimerFired(hwnd, timer_id); | 97 instance_->TimerFired(); |
| 78 } | 98 } |
| 79 | 99 #elif defined(OS_LINUX) |
| 80 void TimerFired(HWND hwnd, UINT_PTR timer_id) { | 100 static gboolean TimerCallback(gpointer data) { |
| 101 instance_->TimerFired(); |
| 102 return true; |
| 103 } |
| 104 #endif |
| 105 |
| 106 void TimerFired() { |
| 81 ASSERT_FALSE(completed_); | 107 ASSERT_FALSE(completed_); |
| 82 | 108 |
| 83 if (timed_out()) { | 109 if (timed_out()) { |
| 84 printf("timer fired\n"); | 110 DestroyTimer(); |
| 85 KillTimer(hwnd, timer_id); | |
| 86 MessageLoop::current()->Quit(); | 111 MessageLoop::current()->Quit(); |
| 87 FAIL() << "fetch timed out"; | 112 FAIL() << "fetch timed out"; |
| 88 return; | 113 return; |
| 89 } | 114 } |
| 90 | 115 |
| 91 time_elapsed_ms_ += kWaitIntervalMs; | 116 time_elapsed_ms_ += kWaitIntervalMs; |
| 92 } | 117 } |
| 93 | 118 |
| 94 static FetcherDelegate* instance_; | 119 static FetcherDelegate* instance_; |
| 95 | 120 |
| 96 private: | 121 private: |
| 122 #if defined(OS_WIN) |
| 97 UINT_PTR timer_id_; | 123 UINT_PTR timer_id_; |
| 124 #elif defined(OS_LINUX) |
| 125 guint timer_id_; |
| 126 #endif |
| 98 bool completed_; | 127 bool completed_; |
| 99 int time_elapsed_ms_; | 128 int time_elapsed_ms_; |
| 100 ResourceResponse response_; | 129 ResourceResponse response_; |
| 101 std::string data_; | 130 std::string data_; |
| 102 }; | 131 }; |
| 103 | 132 |
| 104 FetcherDelegate* FetcherDelegate::instance_ = NULL; | 133 FetcherDelegate* FetcherDelegate::instance_ = NULL; |
| 105 | 134 |
| 106 } // namespace | 135 } // namespace |
| 107 | 136 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 delegate->WaitForResponse(); | 208 delegate->WaitForResponse(); |
| 180 | 209 |
| 181 // When we timeout, we still call the Delegate callback but we pass in empty | 210 // When we timeout, we still call the Delegate callback but we pass in empty |
| 182 // values. | 211 // values. |
| 183 EXPECT_TRUE(delegate->completed()); | 212 EXPECT_TRUE(delegate->completed()); |
| 184 EXPECT_TRUE(delegate->response().isNull()); | 213 EXPECT_TRUE(delegate->response().isNull()); |
| 185 EXPECT_EQ(delegate->data(), std::string()); | 214 EXPECT_EQ(delegate->data(), std::string()); |
| 186 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); | 215 EXPECT_TRUE(delegate->time_elapsed_ms() < kMaxWaitTimeMs); |
| 187 } | 216 } |
| 188 | 217 |
| OLD | NEW |