OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 <stack> | 5 #include <stack> |
6 #include <utility> | 6 #include <utility> |
7 | 7 |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/scoped_temp_dir.h" | 10 #include "base/scoped_temp_dir.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 static const char kTestFileData2[] = "This is sample file."; | 34 static const char kTestFileData2[] = "This is sample file."; |
35 static const char kTestContentType[] = "foo/bar"; | 35 static const char kTestContentType[] = "foo/bar"; |
36 static const char kTestContentDisposition[] = "attachment; filename=foo.txt"; | 36 static const char kTestContentDisposition[] = "attachment; filename=foo.txt"; |
37 | 37 |
38 class BlobURLRequestJobTest : public testing::Test { | 38 class BlobURLRequestJobTest : public testing::Test { |
39 public: | 39 public: |
40 | 40 |
41 // Test Harness ------------------------------------------------------------- | 41 // Test Harness ------------------------------------------------------------- |
42 // TODO(jianli): share this test harness with AppCacheURLRequestJobTest | 42 // TODO(jianli): share this test harness with AppCacheURLRequestJobTest |
43 | 43 |
44 class MockURLRequestDelegate : public URLRequest::Delegate { | 44 class MockURLRequestDelegate : public net::URLRequest::Delegate { |
45 public: | 45 public: |
46 explicit MockURLRequestDelegate(BlobURLRequestJobTest* test) | 46 explicit MockURLRequestDelegate(BlobURLRequestJobTest* test) |
47 : test_(test), | 47 : test_(test), |
48 received_data_(new net::IOBuffer(kBufferSize)) { | 48 received_data_(new net::IOBuffer(kBufferSize)) { |
49 } | 49 } |
50 | 50 |
51 virtual void OnResponseStarted(URLRequest* request) { | 51 virtual void OnResponseStarted(net::URLRequest* request) { |
52 if (request->status().is_success()) { | 52 if (request->status().is_success()) { |
53 EXPECT_TRUE(request->response_headers()); | 53 EXPECT_TRUE(request->response_headers()); |
54 ReadSome(request); | 54 ReadSome(request); |
55 } else { | 55 } else { |
56 RequestComplete(); | 56 RequestComplete(); |
57 } | 57 } |
58 } | 58 } |
59 | 59 |
60 virtual void OnReadCompleted(URLRequest* request, int bytes_read) { | 60 virtual void OnReadCompleted(net::URLRequest* request, int bytes_read) { |
61 if (bytes_read > 0) | 61 if (bytes_read > 0) |
62 ReceiveData(request, bytes_read); | 62 ReceiveData(request, bytes_read); |
63 else | 63 else |
64 RequestComplete(); | 64 RequestComplete(); |
65 } | 65 } |
66 | 66 |
67 const std::string& response_data() const { return response_data_; } | 67 const std::string& response_data() const { return response_data_; } |
68 | 68 |
69 private: | 69 private: |
70 void ReadSome(URLRequest* request) { | 70 void ReadSome(net::URLRequest* request) { |
71 if (request->job()->is_done()) { | 71 if (request->job()->is_done()) { |
72 RequestComplete(); | 72 RequestComplete(); |
73 return; | 73 return; |
74 } | 74 } |
75 | 75 |
76 int bytes_read = 0; | 76 int bytes_read = 0; |
77 if (!request->Read(received_data_.get(), kBufferSize, &bytes_read)) { | 77 if (!request->Read(received_data_.get(), kBufferSize, &bytes_read)) { |
78 if (!request->status().is_io_pending()) { | 78 if (!request->status().is_io_pending()) { |
79 RequestComplete(); | 79 RequestComplete(); |
80 } | 80 } |
81 return; | 81 return; |
82 } | 82 } |
83 | 83 |
84 ReceiveData(request, bytes_read); | 84 ReceiveData(request, bytes_read); |
85 } | 85 } |
86 | 86 |
87 void ReceiveData(URLRequest* request, int bytes_read) { | 87 void ReceiveData(net::URLRequest* request, int bytes_read) { |
88 if (bytes_read) { | 88 if (bytes_read) { |
89 response_data_.append(received_data_->data(), | 89 response_data_.append(received_data_->data(), |
90 static_cast<size_t>(bytes_read)); | 90 static_cast<size_t>(bytes_read)); |
91 ReadSome(request); | 91 ReadSome(request); |
92 } else { | 92 } else { |
93 RequestComplete(); | 93 RequestComplete(); |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 void RequestComplete() { | 97 void RequestComplete() { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 | 143 |
144 io_thread_.reset(new base::Thread("BlobRLRequestJobTest Thread")); | 144 io_thread_.reset(new base::Thread("BlobRLRequestJobTest Thread")); |
145 base::Thread::Options options(MessageLoop::TYPE_IO, 0); | 145 base::Thread::Options options(MessageLoop::TYPE_IO, 0); |
146 io_thread_->StartWithOptions(options); | 146 io_thread_->StartWithOptions(options); |
147 } | 147 } |
148 | 148 |
149 void TearDown() { | 149 void TearDown() { |
150 io_thread_.reset(NULL); | 150 io_thread_.reset(NULL); |
151 } | 151 } |
152 | 152 |
153 static URLRequestJob* BlobURLRequestJobFactory(URLRequest* request, | 153 static URLRequestJob* BlobURLRequestJobFactory(net::URLRequest* request, |
154 const std::string& scheme) { | 154 const std::string& scheme) { |
155 BlobURLRequestJob* temp = blob_url_request_job_; | 155 BlobURLRequestJob* temp = blob_url_request_job_; |
156 blob_url_request_job_ = NULL; | 156 blob_url_request_job_ = NULL; |
157 return temp; | 157 return temp; |
158 } | 158 } |
159 | 159 |
160 BlobURLRequestJobTest() | 160 BlobURLRequestJobTest() |
161 : expected_status_code_(0) { | 161 : expected_status_code_(0) { |
162 } | 162 } |
163 | 163 |
164 template <class Method> | 164 template <class Method> |
165 void RunTestOnIOThread(Method method) { | 165 void RunTestOnIOThread(Method method) { |
166 test_finished_event_ .reset(new base::WaitableEvent(false, false)); | 166 test_finished_event_ .reset(new base::WaitableEvent(false, false)); |
167 io_thread_->message_loop()->PostTask( | 167 io_thread_->message_loop()->PostTask( |
168 FROM_HERE, new WrapperTask<Method>(this, method)); | 168 FROM_HERE, new WrapperTask<Method>(this, method)); |
169 test_finished_event_->Wait(); | 169 test_finished_event_->Wait(); |
170 } | 170 } |
171 | 171 |
172 void SetUpTest() { | 172 void SetUpTest() { |
173 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 173 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
174 | 174 |
175 URLRequest::RegisterProtocolFactory("blob", &BlobURLRequestJobFactory); | 175 net::URLRequest::RegisterProtocolFactory("blob", &BlobURLRequestJobFactory); |
176 url_request_delegate_.reset(new MockURLRequestDelegate(this)); | 176 url_request_delegate_.reset(new MockURLRequestDelegate(this)); |
177 } | 177 } |
178 | 178 |
179 void TearDownTest() { | 179 void TearDownTest() { |
180 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 180 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
181 | 181 |
182 request_.reset(); | 182 request_.reset(); |
183 url_request_delegate_.reset(); | 183 url_request_delegate_.reset(); |
184 | 184 |
185 DCHECK(!blob_url_request_job_); | 185 DCHECK(!blob_url_request_job_); |
186 URLRequest::RegisterProtocolFactory("blob", NULL); | 186 net::URLRequest::RegisterProtocolFactory("blob", NULL); |
187 } | 187 } |
188 | 188 |
189 void TestFinished() { | 189 void TestFinished() { |
190 // We unwind the stack prior to finishing up to let stack | 190 // We unwind the stack prior to finishing up to let stack |
191 // based objects get deleted. | 191 // based objects get deleted. |
192 DCHECK(MessageLoop::current() == io_thread_->message_loop()); | 192 DCHECK(MessageLoop::current() == io_thread_->message_loop()); |
193 MessageLoop::current()->PostTask(FROM_HERE, | 193 MessageLoop::current()->PostTask(FROM_HERE, |
194 NewRunnableMethod( | 194 NewRunnableMethod( |
195 this, &BlobURLRequestJobTest::TestFinishedUnwound)); | 195 this, &BlobURLRequestJobTest::TestFinishedUnwound)); |
196 } | 196 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 this, &BlobURLRequestJobTest::VerifyResponse)); | 238 this, &BlobURLRequestJobTest::VerifyResponse)); |
239 expected_status_code_ = expected_status_code; | 239 expected_status_code_ = expected_status_code; |
240 expected_response_ = ""; | 240 expected_response_ = ""; |
241 return TestRequest("GET", net::HttpRequestHeaders(), blob_data); | 241 return TestRequest("GET", net::HttpRequestHeaders(), blob_data); |
242 } | 242 } |
243 | 243 |
244 void TestRequest(const std::string& method, | 244 void TestRequest(const std::string& method, |
245 const net::HttpRequestHeaders& extra_headers, | 245 const net::HttpRequestHeaders& extra_headers, |
246 BlobData* blob_data) { | 246 BlobData* blob_data) { |
247 // This test has async steps. | 247 // This test has async steps. |
248 request_.reset(new URLRequest(GURL("blob:blah"), | 248 request_.reset(new net::URLRequest(GURL("blob:blah"), |
249 url_request_delegate_.get())); | 249 url_request_delegate_.get())); |
250 request_->set_method(method); | 250 request_->set_method(method); |
251 blob_url_request_job_ = new BlobURLRequestJob(request_.get(), | 251 blob_url_request_job_ = new BlobURLRequestJob(request_.get(), |
252 blob_data, NULL); | 252 blob_data, NULL); |
253 | 253 |
254 // Start the request. | 254 // Start the request. |
255 if (!extra_headers.IsEmpty()) | 255 if (!extra_headers.IsEmpty()) |
256 request_->SetExtraRequestHeaders(extra_headers); | 256 request_->SetExtraRequestHeaders(extra_headers); |
257 request_->Start(); | 257 request_->Start(); |
258 | 258 |
259 // Completion is async. | 259 // Completion is async. |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
384 ScopedTempDir temp_dir_; | 384 ScopedTempDir temp_dir_; |
385 FilePath temp_file1_; | 385 FilePath temp_file1_; |
386 FilePath temp_file2_; | 386 FilePath temp_file2_; |
387 base::Time temp_file_modification_time1_; | 387 base::Time temp_file_modification_time1_; |
388 base::Time temp_file_modification_time2_; | 388 base::Time temp_file_modification_time2_; |
389 scoped_ptr<base::Thread> io_thread_; | 389 scoped_ptr<base::Thread> io_thread_; |
390 static BlobURLRequestJob* blob_url_request_job_; | 390 static BlobURLRequestJob* blob_url_request_job_; |
391 | 391 |
392 scoped_ptr<base::WaitableEvent> test_finished_event_; | 392 scoped_ptr<base::WaitableEvent> test_finished_event_; |
393 std::stack<std::pair<Task*, bool> > task_stack_; | 393 std::stack<std::pair<Task*, bool> > task_stack_; |
394 scoped_ptr<URLRequest> request_; | 394 scoped_ptr<net::URLRequest> request_; |
395 scoped_ptr<MockURLRequestDelegate> url_request_delegate_; | 395 scoped_ptr<MockURLRequestDelegate> url_request_delegate_; |
396 int expected_status_code_; | 396 int expected_status_code_; |
397 std::string expected_response_; | 397 std::string expected_response_; |
398 }; | 398 }; |
399 | 399 |
400 // static | 400 // static |
401 BlobURLRequestJob* BlobURLRequestJobTest::blob_url_request_job_ = NULL; | 401 BlobURLRequestJob* BlobURLRequestJobTest::blob_url_request_job_ = NULL; |
402 | 402 |
403 TEST_F(BlobURLRequestJobTest, TestGetSimpleDataRequest) { | 403 TEST_F(BlobURLRequestJobTest, TestGetSimpleDataRequest) { |
404 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetSimpleDataRequest); | 404 RunTestOnIOThread(&BlobURLRequestJobTest::TestGetSimpleDataRequest); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 | 439 |
440 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { | 440 TEST_F(BlobURLRequestJobTest, TestExtraHeaders) { |
441 RunTestOnIOThread(&BlobURLRequestJobTest::TestExtraHeaders); | 441 RunTestOnIOThread(&BlobURLRequestJobTest::TestExtraHeaders); |
442 } | 442 } |
443 | 443 |
444 } // namespace webkit_blob | 444 } // namespace webkit_blob |
445 | 445 |
446 // BlobURLRequestJobTest is expected to always live longer than the | 446 // BlobURLRequestJobTest is expected to always live longer than the |
447 // runnable methods. This lets us call NewRunnableMethod on its instances. | 447 // runnable methods. This lets us call NewRunnableMethod on its instances. |
448 DISABLE_RUNNABLE_METHOD_REFCOUNT(webkit_blob::BlobURLRequestJobTest); | 448 DISABLE_RUNNABLE_METHOD_REFCOUNT(webkit_blob::BlobURLRequestJobTest); |
OLD | NEW |