OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <set> | 5 #include <set> |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/scoped_temp_dir.h" | 12 #include "base/scoped_temp_dir.h" |
13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
14 #include "base/string16.h" | 14 #include "base/string16.h" |
15 #include "base/string_util.h" | 15 #include "base/string_util.h" |
16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "content/browser/download/download_buffer.h" | |
19 #include "content/browser/download/download_create_info.h" | 18 #include "content/browser/download/download_create_info.h" |
20 #include "content/browser/download/download_file_impl.h" | 19 #include "content/browser/download/download_file_impl.h" |
21 #include "content/browser/download/download_file_manager.h" | 20 #include "content/browser/download/download_file_manager.h" |
22 #include "content/browser/download/download_manager_impl.h" | 21 #include "content/browser/download/download_manager_impl.h" |
23 #include "content/browser/download/download_request_handle.h" | 22 #include "content/browser/download/download_request_handle.h" |
24 #include "content/browser/download/mock_download_file.h" | 23 #include "content/browser/download/mock_download_file.h" |
25 #include "content/public/browser/download_interrupt_reasons.h" | 24 #include "content/public/browser/download_interrupt_reasons.h" |
26 #include "content/public/browser/download_item.h" | 25 #include "content/public/browser/download_item.h" |
27 #include "content/public/browser/download_manager_delegate.h" | 26 #include "content/public/browser/download_manager_delegate.h" |
28 #include "content/test/mock_download_manager.h" | 27 #include "content/test/mock_download_manager.h" |
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 public: | 209 public: |
211 static const char* kTestData; | 210 static const char* kTestData; |
212 static const size_t kTestDataLen; | 211 static const size_t kTestDataLen; |
213 | 212 |
214 DownloadManagerTest() | 213 DownloadManagerTest() |
215 : browser_context(new TestBrowserContext()), | 214 : browser_context(new TestBrowserContext()), |
216 download_manager_delegate_(new TestDownloadManagerDelegate()), | 215 download_manager_delegate_(new TestDownloadManagerDelegate()), |
217 download_manager_(new DownloadManagerImpl( | 216 download_manager_(new DownloadManagerImpl( |
218 download_manager_delegate_.get(), NULL)), | 217 download_manager_delegate_.get(), NULL)), |
219 ui_thread_(BrowserThread::UI, &message_loop_), | 218 ui_thread_(BrowserThread::UI, &message_loop_), |
220 file_thread_(BrowserThread::FILE, &message_loop_), | 219 file_thread_(BrowserThread::FILE, &message_loop_) { |
221 download_buffer_(new content::DownloadBuffer) { | |
222 download_manager_->Init(browser_context.get()); | 220 download_manager_->Init(browser_context.get()); |
223 download_manager_delegate_->set_download_manager(download_manager_); | 221 download_manager_delegate_->set_download_manager(download_manager_); |
224 } | 222 } |
225 | 223 |
226 ~DownloadManagerTest() { | 224 ~DownloadManagerTest() { |
227 download_manager_->Shutdown(); | 225 download_manager_->Shutdown(); |
228 // browser_context must outlive download_manager_, so we explicitly delete | 226 // browser_context must outlive download_manager_, so we explicitly delete |
229 // download_manager_ first. | 227 // download_manager_ first. |
230 download_manager_ = NULL; | 228 download_manager_ = NULL; |
231 download_manager_delegate_.reset(); | 229 download_manager_delegate_.reset(); |
(...skipping 18 matching lines...) Expand all Loading... |
250 } | 248 } |
251 | 249 |
252 void FileSelected(const FilePath& path, int32 download_id) { | 250 void FileSelected(const FilePath& path, int32 download_id) { |
253 download_manager_->FileSelected(path, download_id); | 251 download_manager_->FileSelected(path, download_id); |
254 } | 252 } |
255 | 253 |
256 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { | 254 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { |
257 download_manager_->ContinueDownloadWithPath(download, path); | 255 download_manager_->ContinueDownloadWithPath(download, path); |
258 } | 256 } |
259 | 257 |
260 void UpdateData(int32 id, const char* data, size_t length) { | |
261 // We are passing ownership of this buffer to the download file manager. | |
262 net::IOBuffer* io_buffer = new net::IOBuffer(length); | |
263 // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|. | |
264 io_buffer->AddRef(); | |
265 memcpy(io_buffer->data(), data, length); | |
266 | |
267 download_buffer_->AddData(io_buffer, length); | |
268 | |
269 BrowserThread::PostTask( | |
270 BrowserThread::FILE, FROM_HERE, | |
271 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), | |
272 DownloadId(kValidIdDomain, id), download_buffer_)); | |
273 | |
274 message_loop_.RunAllPending(); | |
275 } | |
276 | |
277 void OnDownloadInterrupted(int32 download_id, int64 size, | 258 void OnDownloadInterrupted(int32 download_id, int64 size, |
278 const std::string& hash_state, | 259 const std::string& hash_state, |
279 content::DownloadInterruptReason reason) { | 260 content::DownloadInterruptReason reason) { |
280 download_manager_->OnDownloadInterrupted(download_id, size, | 261 download_manager_->OnDownloadInterrupted(download_id, size, |
281 hash_state, reason); | 262 hash_state, reason); |
282 } | 263 } |
283 | 264 |
284 // Get the download item with ID |id|. | 265 // Get the download item with ID |id|. |
285 DownloadItem* GetActiveDownloadItem(int32 id) { | 266 DownloadItem* GetActiveDownloadItem(int32 id) { |
286 return download_manager_->GetActiveDownload(id); | 267 return download_manager_->GetActiveDownload(id); |
287 } | 268 } |
288 | 269 |
289 protected: | 270 protected: |
290 scoped_ptr<TestBrowserContext> browser_context; | 271 scoped_ptr<TestBrowserContext> browser_context; |
291 scoped_ptr<TestDownloadManagerDelegate> download_manager_delegate_; | 272 scoped_ptr<TestDownloadManagerDelegate> download_manager_delegate_; |
292 scoped_refptr<DownloadManagerImpl> download_manager_; | 273 scoped_refptr<DownloadManagerImpl> download_manager_; |
293 scoped_refptr<DownloadFileManager> file_manager_; | 274 scoped_refptr<DownloadFileManager> file_manager_; |
294 MessageLoopForUI message_loop_; | 275 MessageLoopForUI message_loop_; |
295 content::TestBrowserThread ui_thread_; | 276 content::TestBrowserThread ui_thread_; |
296 content::TestBrowserThread file_thread_; | 277 content::TestBrowserThread file_thread_; |
297 scoped_refptr<content::DownloadBuffer> download_buffer_; | |
298 | 278 |
299 DownloadFileManager* file_manager() { | 279 DownloadFileManager* file_manager() { |
300 if (!file_manager_) { | 280 if (!file_manager_) { |
301 file_manager_ = new DownloadFileManager(new MockDownloadFileFactory); | 281 file_manager_ = new DownloadFileManager(new MockDownloadFileFactory); |
302 download_manager_->SetFileManagerForTesting(file_manager_); | 282 download_manager_->SetFileManagerForTesting(file_manager_); |
303 } | 283 } |
304 return file_manager_; | 284 return file_manager_; |
305 } | 285 } |
306 | 286 |
307 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 287 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
(...skipping 684 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
992 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); | 972 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
993 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 973 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
994 EXPECT_TRUE(observer->was_updated()); | 974 EXPECT_TRUE(observer->was_updated()); |
995 EXPECT_FALSE(observer->was_opened()); | 975 EXPECT_FALSE(observer->was_opened()); |
996 EXPECT_FALSE(download->GetFileExternallyRemoved()); | 976 EXPECT_FALSE(download->GetFileExternallyRemoved()); |
997 EXPECT_EQ(DownloadItem::INTERRUPTED, download->GetState()); | 977 EXPECT_EQ(DownloadItem::INTERRUPTED, download->GetState()); |
998 EXPECT_EQ(download->GetReceivedBytes(), error_size); | 978 EXPECT_EQ(download->GetReceivedBytes(), error_size); |
999 EXPECT_EQ(download->GetTotalBytes(), static_cast<int64>(kTestDataLen)); | 979 EXPECT_EQ(download->GetTotalBytes(), static_cast<int64>(kTestDataLen)); |
1000 } | 980 } |
1001 | 981 |
| 982 void WriteCopyToPipe(scoped_refptr<content::ByteStream> pipe, |
| 983 const char *source, |
| 984 size_t len) { |
| 985 scoped_refptr<net::IOBuffer> copy(new net::IOBuffer(len)); |
| 986 memcpy(copy.get()->data(), source, len); |
| 987 pipe->AddData(copy, len); |
| 988 } |
| 989 |
1002 // Test the behavior of DownloadFileManager and DownloadManager in the event | 990 // Test the behavior of DownloadFileManager and DownloadManager in the event |
1003 // of a file error while writing the download to disk. | 991 // of a file error while writing the download to disk. |
1004 TEST_F(DownloadManagerTest, MAYBE_DownloadFileErrorTest) { | 992 TEST_F(DownloadManagerTest, MAYBE_DownloadFileErrorTest) { |
1005 // Create a temporary file and a mock stream. | 993 // Create a temporary file and a mock stream. |
1006 FilePath path; | 994 FilePath path; |
1007 ASSERT_TRUE(file_util::CreateTemporaryFile(&path)); | 995 ASSERT_TRUE(file_util::CreateTemporaryFile(&path)); |
1008 | 996 |
1009 // This file stream will be used, until the first rename occurs. | 997 // This file stream will be used, until the first rename occurs. |
1010 net::FileStream* stream = new net::FileStream(NULL); | 998 net::FileStream* stream = new net::FileStream(NULL); |
1011 ASSERT_EQ(0, stream->OpenSync( | 999 ASSERT_EQ(0, stream->OpenSync( |
1012 path, | 1000 path, |
1013 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE)); | 1001 base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE)); |
1014 | 1002 |
1015 // Normally, the download system takes ownership of info, and is | 1003 // Normally, the download system takes ownership of info, and is |
1016 // responsible for deleting it. In these unit tests, however, we | 1004 // responsible for deleting it. In these unit tests, however, we |
1017 // don't call the function that deletes it, so we do so ourselves. | 1005 // don't call the function that deletes it, so we do so ourselves. |
1018 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 1006 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
1019 static const int32 local_id = 0; | 1007 static const int32 local_id = 0; |
1020 info->download_id = DownloadId(kValidIdDomain, local_id); | 1008 info->download_id = DownloadId(kValidIdDomain, local_id); |
1021 info->prompt_user_for_save_location = false; | 1009 info->prompt_user_for_save_location = false; |
1022 info->url_chain.push_back(GURL()); | 1010 info->url_chain.push_back(GURL()); |
1023 info->total_bytes = static_cast<int64>(kTestDataLen * 3); | 1011 info->total_bytes = static_cast<int64>(kTestDataLen * 3); |
1024 info->save_info.file_path = path; | 1012 info->save_info.file_path = path; |
1025 info->save_info.file_stream.reset(stream); | 1013 info->save_info.file_stream.reset(stream); |
| 1014 scoped_refptr<content::ByteStream> data_pipe(new content::ByteStream); |
| 1015 info->pipe = data_pipe; |
1026 | 1016 |
1027 // Create a download file that we can insert errors into. | 1017 // Create a download file that we can insert errors into. |
1028 DownloadFileWithErrors* download_file(new DownloadFileWithErrors( | 1018 scoped_ptr<DownloadFileWithErrors> download_file(new DownloadFileWithErrors( |
1029 info.get(), download_manager_, false)); | 1019 info.get(), download_manager_, false)); |
1030 download_file->Initialize(); | 1020 download_file->Initialize(); |
1031 AddDownloadToFileManager(local_id, download_file); | |
1032 | 1021 |
1033 // |download_file| is owned by DownloadFileManager. | |
1034 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 1022 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
1035 | 1023 |
1036 DownloadItem* download = GetActiveDownloadItem(0); | 1024 DownloadItem* download = GetActiveDownloadItem(0); |
1037 ASSERT_TRUE(download != NULL); | 1025 ASSERT_TRUE(download != NULL); |
1038 | 1026 |
1039 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 1027 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
1040 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 1028 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
1041 | 1029 |
1042 // Add some data before finalizing the file name. | 1030 // Add some data before finalizing the file name. |
1043 UpdateData(local_id, kTestData, kTestDataLen); | 1031 WriteCopyToPipe(data_pipe, kTestData, kTestDataLen); |
1044 | 1032 |
1045 // Finalize the file name. | 1033 // Finalize the file name. |
1046 ContinueDownloadWithPath(download, path); | 1034 ContinueDownloadWithPath(download, path); |
1047 message_loop_.RunAllPending(); | 1035 message_loop_.RunAllPending(); |
1048 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1036 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
1049 | 1037 |
1050 // Add more data. | 1038 // Add more data. |
1051 UpdateData(local_id, kTestData, kTestDataLen); | 1039 WriteCopyToPipe(data_pipe, kTestData, kTestDataLen); |
1052 | 1040 |
1053 // Add more data, but an error occurs. | 1041 // Add more data, but an error occurs. |
1054 download_file->set_forced_error(net::ERR_FAILED); | 1042 download_file->set_forced_error(net::ERR_FAILED); |
1055 UpdateData(local_id, kTestData, kTestDataLen); | 1043 WriteCopyToPipe(data_pipe, kTestData, kTestDataLen); |
1056 | 1044 |
1057 // Check the state. The download should have been interrupted. | 1045 // Check the state. The download should have been interrupted. |
1058 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 1046 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
1059 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 1047 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
1060 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); | 1048 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); |
1061 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); | 1049 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
1062 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); | 1050 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
1063 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 1051 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
1064 EXPECT_TRUE(observer->was_updated()); | 1052 EXPECT_TRUE(observer->was_updated()); |
1065 EXPECT_FALSE(observer->was_opened()); | 1053 EXPECT_FALSE(observer->was_opened()); |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1301 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); | 1289 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
1302 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); | 1290 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); |
1303 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 1291 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
1304 EXPECT_TRUE(observer->was_updated()); | 1292 EXPECT_TRUE(observer->was_updated()); |
1305 EXPECT_FALSE(observer->was_opened()); | 1293 EXPECT_FALSE(observer->was_opened()); |
1306 EXPECT_TRUE(download->GetFileExternallyRemoved()); | 1294 EXPECT_TRUE(download->GetFileExternallyRemoved()); |
1307 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); | 1295 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); |
1308 | 1296 |
1309 EXPECT_FALSE(file_util::PathExists(new_path)); | 1297 EXPECT_FALSE(file_util::PathExists(new_path)); |
1310 } | 1298 } |
OLD | NEW |