OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "webkit/glue/resource_fetcher.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/timer.h" | |
11 #include "third_party/WebKit/Source/Platform/chromium/public/WebURLResponse.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
13 #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 | |
18 using WebKit::WebFrame; | |
19 using WebKit::WebURLRequest; | |
20 using WebKit::WebURLResponse; | |
21 using webkit_glue::ResourceFetcher; | |
22 using webkit_glue::ResourceFetcherWithTimeout; | |
23 | |
24 namespace { | |
25 | |
26 class ResourceFetcherTests : public TestShellTest { | |
27 protected: | |
28 UnittestTestServer test_server_; | |
29 }; | |
30 | |
31 static const int kMaxWaitTimeMs = 5000; | |
32 | |
33 class FetcherDelegate { | |
34 public: | |
35 FetcherDelegate() | |
36 : completed_(false), | |
37 timed_out_(false) { | |
38 // 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. | |
40 FetcherDelegate::instance_ = this; | |
41 StartTimer(); | |
42 } | |
43 | |
44 virtual ~FetcherDelegate() {} | |
45 | |
46 ResourceFetcher::Callback NewCallback() { | |
47 return base::Bind(&FetcherDelegate::OnURLFetchComplete, | |
48 base::Unretained(this)); | |
49 } | |
50 | |
51 virtual void OnURLFetchComplete(const WebURLResponse& response, | |
52 const std::string& data) { | |
53 response_ = response; | |
54 data_ = data; | |
55 completed_ = true; | |
56 timer_.Stop(); | |
57 MessageLoop::current()->Quit(); | |
58 } | |
59 | |
60 bool completed() const { return completed_; } | |
61 bool timed_out() const { return timed_out_; } | |
62 | |
63 std::string data() const { return data_; } | |
64 const WebURLResponse& response() const { return response_; } | |
65 | |
66 // 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 | |
68 // MessageLoop's Quit method. | |
69 void WaitForResponse() { | |
70 while (!completed() && !timed_out()) | |
71 MessageLoop::current()->Run(); | |
72 } | |
73 | |
74 void StartTimer() { | |
75 timer_.Start(FROM_HERE, | |
76 base::TimeDelta::FromMilliseconds(kMaxWaitTimeMs), | |
77 this, | |
78 &FetcherDelegate::TimerFired); | |
79 } | |
80 | |
81 void TimerFired() { | |
82 ASSERT_FALSE(completed_); | |
83 | |
84 timed_out_ = true; | |
85 MessageLoop::current()->Quit(); | |
86 FAIL() << "fetch timed out"; | |
87 } | |
88 | |
89 static FetcherDelegate* instance_; | |
90 | |
91 private: | |
92 base::OneShotTimer<FetcherDelegate> timer_; | |
93 bool completed_; | |
94 bool timed_out_; | |
95 WebURLResponse response_; | |
96 std::string data_; | |
97 }; | |
98 | |
99 FetcherDelegate* FetcherDelegate::instance_ = NULL; | |
100 | |
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 { | |
180 public: | |
181 virtual ~EvilFetcherDelegate() {} | |
182 | |
183 void SetFetcher(ResourceFetcher* fetcher) { | |
184 fetcher_.reset(fetcher); | |
185 } | |
186 | |
187 virtual void OnURLFetchComplete(const WebURLResponse& response, | |
188 const std::string& data) OVERRIDE { | |
189 // Destroy the ResourceFetcher here. We are testing that upon returning | |
190 // to the ResourceFetcher that it does not crash. | |
191 fetcher_.reset(); | |
192 FetcherDelegate::OnURLFetchComplete(response, data); | |
193 } | |
194 | |
195 private: | |
196 scoped_ptr<ResourceFetcher> fetcher_; | |
197 }; | |
198 | |
199 TEST_F(ResourceFetcherTests, ResourceFetcherDeletedInCallback) { | |
200 ASSERT_TRUE(test_server_.Start()); | |
201 | |
202 WebFrame* frame = test_shell_->webView()->mainFrame(); | |
203 | |
204 // Grab a page that takes at least 1 sec to respond, but set the fetcher to | |
205 // timeout in 0 sec. | |
206 GURL url(test_server_.GetURL("slow?1")); | |
207 scoped_ptr<EvilFetcherDelegate> delegate(new EvilFetcherDelegate); | |
208 scoped_ptr<ResourceFetcher> fetcher(new ResourceFetcherWithTimeout( | |
209 url, frame, WebURLRequest::TargetIsMainFrame, | |
210 0, delegate->NewCallback())); | |
211 delegate->SetFetcher(fetcher.release()); | |
212 | |
213 delegate->WaitForResponse(); | |
214 EXPECT_FALSE(delegate->timed_out()); | |
215 } | |
216 | |
217 } // namespace | |
OLD | NEW |