| 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/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
| 14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
| 15 #include "base/string16.h" | 15 #include "base/string16.h" |
| 16 #include "base/string_util.h" | 16 #include "base/string_util.h" |
| 17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "content/browser/download/download_buffer.h" | |
| 20 #include "content/browser/download/download_create_info.h" | 19 #include "content/browser/download/download_create_info.h" |
| 21 #include "content/browser/download/download_file_impl.h" | 20 #include "content/browser/download/download_file_impl.h" |
| 22 #include "content/browser/download/download_file_manager.h" | 21 #include "content/browser/download/download_file_manager.h" |
| 23 #include "content/browser/download/download_manager_impl.h" | 22 #include "content/browser/download/download_manager_impl.h" |
| 24 #include "content/browser/download/download_request_handle.h" | 23 #include "content/browser/download/download_request_handle.h" |
| 25 #include "content/browser/download/mock_download_file.h" | 24 #include "content/browser/download/mock_download_file.h" |
| 26 #include "content/browser/power_save_blocker.h" | 25 #include "content/browser/power_save_blocker.h" |
| 27 #include "content/public/browser/download_interrupt_reasons.h" | 26 #include "content/public/browser/download_interrupt_reasons.h" |
| 28 #include "content/public/browser/download_item.h" | 27 #include "content/public/browser/download_item.h" |
| 29 #include "content/public/browser/download_manager_delegate.h" | 28 #include "content/public/browser/download_manager_delegate.h" |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 212 public: | 211 public: |
| 213 static const char* kTestData; | 212 static const char* kTestData; |
| 214 static const size_t kTestDataLen; | 213 static const size_t kTestDataLen; |
| 215 | 214 |
| 216 DownloadManagerTest() | 215 DownloadManagerTest() |
| 217 : browser_context(new TestBrowserContext()), | 216 : browser_context(new TestBrowserContext()), |
| 218 download_manager_delegate_(new TestDownloadManagerDelegate()), | 217 download_manager_delegate_(new TestDownloadManagerDelegate()), |
| 219 download_manager_(new DownloadManagerImpl( | 218 download_manager_(new DownloadManagerImpl( |
| 220 download_manager_delegate_.get(), NULL)), | 219 download_manager_delegate_.get(), NULL)), |
| 221 ui_thread_(BrowserThread::UI, &message_loop_), | 220 ui_thread_(BrowserThread::UI, &message_loop_), |
| 222 file_thread_(BrowserThread::FILE, &message_loop_), | 221 file_thread_(BrowserThread::FILE, &message_loop_) { |
| 223 download_buffer_(new content::DownloadBuffer) { | |
| 224 download_manager_->Init(browser_context.get()); | 222 download_manager_->Init(browser_context.get()); |
| 225 download_manager_delegate_->set_download_manager(download_manager_); | 223 download_manager_delegate_->set_download_manager(download_manager_); |
| 226 } | 224 } |
| 227 | 225 |
| 228 ~DownloadManagerTest() { | 226 ~DownloadManagerTest() { |
| 229 download_manager_->Shutdown(); | 227 download_manager_->Shutdown(); |
| 230 // browser_context must outlive download_manager_, so we explicitly delete | 228 // browser_context must outlive download_manager_, so we explicitly delete |
| 231 // download_manager_ first. | 229 // download_manager_ first. |
| 232 download_manager_ = NULL; | 230 download_manager_ = NULL; |
| 233 download_manager_delegate_.reset(); | 231 download_manager_delegate_.reset(); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 252 } | 250 } |
| 253 | 251 |
| 254 void FileSelected(const FilePath& path, int32 download_id) { | 252 void FileSelected(const FilePath& path, int32 download_id) { |
| 255 download_manager_->FileSelected(path, download_id); | 253 download_manager_->FileSelected(path, download_id); |
| 256 } | 254 } |
| 257 | 255 |
| 258 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { | 256 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { |
| 259 download_manager_->ContinueDownloadWithPath(download, path); | 257 download_manager_->ContinueDownloadWithPath(download, path); |
| 260 } | 258 } |
| 261 | 259 |
| 262 void UpdateData(int32 id, const char* data, size_t length) { | |
| 263 // We are passing ownership of this buffer to the download file manager. | |
| 264 net::IOBuffer* io_buffer = new net::IOBuffer(length); | |
| 265 // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|. | |
| 266 io_buffer->AddRef(); | |
| 267 memcpy(io_buffer->data(), data, length); | |
| 268 | |
| 269 download_buffer_->AddData(io_buffer, length); | |
| 270 | |
| 271 BrowserThread::PostTask( | |
| 272 BrowserThread::FILE, FROM_HERE, | |
| 273 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), | |
| 274 DownloadId(kValidIdDomain, id), download_buffer_)); | |
| 275 | |
| 276 message_loop_.RunAllPending(); | |
| 277 } | |
| 278 | |
| 279 void OnDownloadInterrupted(int32 download_id, int64 size, | 260 void OnDownloadInterrupted(int32 download_id, int64 size, |
| 280 const std::string& hash_state, | 261 const std::string& hash_state, |
| 281 content::DownloadInterruptReason reason) { | 262 content::DownloadInterruptReason reason) { |
| 282 download_manager_->OnDownloadInterrupted(download_id, size, | 263 download_manager_->OnDownloadInterrupted(download_id, size, |
| 283 hash_state, reason); | 264 hash_state, reason); |
| 284 } | 265 } |
| 285 | 266 |
| 286 // Get the download item with ID |id|. | 267 // Get the download item with ID |id|. |
| 287 DownloadItem* GetActiveDownloadItem(int32 id) { | 268 DownloadItem* GetActiveDownloadItem(int32 id) { |
| 288 return download_manager_->GetActiveDownload(id); | 269 return download_manager_->GetActiveDownload(id); |
| 289 } | 270 } |
| 290 | 271 |
| 291 protected: | 272 protected: |
| 292 scoped_ptr<TestBrowserContext> browser_context; | 273 scoped_ptr<TestBrowserContext> browser_context; |
| 293 scoped_ptr<TestDownloadManagerDelegate> download_manager_delegate_; | 274 scoped_ptr<TestDownloadManagerDelegate> download_manager_delegate_; |
| 294 scoped_refptr<DownloadManagerImpl> download_manager_; | 275 scoped_refptr<DownloadManagerImpl> download_manager_; |
| 295 scoped_refptr<DownloadFileManager> file_manager_; | 276 scoped_refptr<DownloadFileManager> file_manager_; |
| 296 MessageLoopForUI message_loop_; | 277 MessageLoopForUI message_loop_; |
| 297 content::TestBrowserThread ui_thread_; | 278 content::TestBrowserThread ui_thread_; |
| 298 content::TestBrowserThread file_thread_; | 279 content::TestBrowserThread file_thread_; |
| 299 scoped_refptr<content::DownloadBuffer> download_buffer_; | |
| 300 | 280 |
| 301 DownloadFileManager* file_manager() { | 281 DownloadFileManager* file_manager() { |
| 302 if (!file_manager_) { | 282 if (!file_manager_) { |
| 303 file_manager_ = new DownloadFileManager(new MockDownloadFileFactory); | 283 file_manager_ = new DownloadFileManager(new MockDownloadFileFactory); |
| 304 download_manager_->SetFileManagerForTesting(file_manager_); | 284 download_manager_->SetFileManagerForTesting(file_manager_); |
| 305 } | 285 } |
| 306 return file_manager_; | 286 return file_manager_; |
| 307 } | 287 } |
| 308 | 288 |
| 309 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 289 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
| (...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 925 message_loop_.RunAllPending(); | 905 message_loop_.RunAllPending(); |
| 926 OnResponseCompleted(i, 1024, std::string("fake_hash")); | 906 OnResponseCompleted(i, 1024, std::string("fake_hash")); |
| 927 } | 907 } |
| 928 // Validating the download item, so it will complete. | 908 // Validating the download item, so it will complete. |
| 929 if (state.danger == content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE) | 909 if (state.danger == content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE) |
| 930 download->DangerousDownloadValidated(); | 910 download->DangerousDownloadValidated(); |
| 931 message_loop_.RunAllPending(); | 911 message_loop_.RunAllPending(); |
| 932 } | 912 } |
| 933 } | 913 } |
| 934 | 914 |
| 915 void WriteCopyToPipe(scoped_refptr<content::ByteStream> pipe, |
| 916 const char *source, |
| 917 size_t len) { |
| 918 scoped_refptr<net::IOBuffer> copy(new net::IOBuffer(len)); |
| 919 memcpy(copy.get()->data(), source, len); |
| 920 pipe->AddData(copy, len); |
| 921 } |
| 922 |
| 935 TEST_F(DownloadManagerTest, DownloadInterruptTest) { | 923 TEST_F(DownloadManagerTest, DownloadInterruptTest) { |
| 936 using ::testing::_; | 924 using ::testing::_; |
| 937 using ::testing::CreateFunctor; | 925 using ::testing::CreateFunctor; |
| 938 using ::testing::Invoke; | 926 using ::testing::Invoke; |
| 939 using ::testing::Return; | 927 using ::testing::Return; |
| 940 | 928 |
| 941 // Normally, the download system takes ownership of info, and is | 929 // Normally, the download system takes ownership of info, and is |
| 942 // responsible for deleting it. In these unit tests, however, we | 930 // responsible for deleting it. In these unit tests, however, we |
| 943 // don't call the function that deletes it, so we do so ourselves. | 931 // don't call the function that deletes it, so we do so ourselves. |
| 944 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 932 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 945 info->download_id = DownloadId(kValidIdDomain, 0); | 933 info->download_id = DownloadId(kValidIdDomain, 0); |
| 946 info->prompt_user_for_save_location = false; | 934 info->prompt_user_for_save_location = false; |
| 947 info->url_chain.push_back(GURL()); | 935 info->url_chain.push_back(GURL()); |
| 948 info->total_bytes = static_cast<int64>(kTestDataLen); | 936 info->total_bytes = static_cast<int64>(kTestDataLen); |
| 949 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); | 937 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
| 950 const FilePath cr_path(GetTempDownloadPath(new_path)); | 938 const FilePath cr_path(GetTempDownloadPath(new_path)); |
| 951 | 939 |
| 952 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); | 940 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); |
| 953 ON_CALL(*download_file, AppendDataToFile(_, _)) | |
| 954 .WillByDefault(Return(net::OK)); | |
| 955 | 941 |
| 956 // |download_file| is owned by DownloadFileManager. | 942 // |download_file| is owned by DownloadFileManager. |
| 957 AddMockDownloadToFileManager(info->download_id.local(), download_file); | 943 AddMockDownloadToFileManager(info->download_id.local(), download_file); |
| 958 | 944 |
| 959 EXPECT_CALL(*download_file, Rename(cr_path)) | 945 EXPECT_CALL(*download_file, Rename(cr_path)) |
| 960 .Times(1) | 946 .Times(1) |
| 961 .WillOnce(Return(net::OK)); | 947 .WillOnce(Return(net::OK)); |
| 962 | 948 |
| 963 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 949 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 964 | 950 |
| 965 DownloadItem* download = GetActiveDownloadItem(0); | 951 DownloadItem* download = GetActiveDownloadItem(0); |
| 966 ASSERT_TRUE(download != NULL); | 952 ASSERT_TRUE(download != NULL); |
| 967 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 953 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 968 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 954 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 969 | 955 |
| 970 download_file->AppendDataToFile(kTestData, kTestDataLen); | |
| 971 | |
| 972 ContinueDownloadWithPath(download, new_path); | 956 ContinueDownloadWithPath(download, new_path); |
| 973 message_loop_.RunAllPending(); | 957 message_loop_.RunAllPending(); |
| 974 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 958 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 975 | 959 |
| 976 int64 error_size = 3; | 960 int64 error_size = 3; |
| 977 OnDownloadInterrupted(0, error_size, "", | 961 OnDownloadInterrupted(0, error_size, "", |
| 978 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 962 content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 979 message_loop_.RunAllPending(); | 963 message_loop_.RunAllPending(); |
| 980 | 964 |
| 981 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 965 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1021 // responsible for deleting it. In these unit tests, however, we | 1005 // responsible for deleting it. In these unit tests, however, we |
| 1022 // don't call the function that deletes it, so we do so ourselves. | 1006 // don't call the function that deletes it, so we do so ourselves. |
| 1023 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 1007 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 1024 static const int32 local_id = 0; | 1008 static const int32 local_id = 0; |
| 1025 info->download_id = DownloadId(kValidIdDomain, local_id); | 1009 info->download_id = DownloadId(kValidIdDomain, local_id); |
| 1026 info->prompt_user_for_save_location = false; | 1010 info->prompt_user_for_save_location = false; |
| 1027 info->url_chain.push_back(GURL()); | 1011 info->url_chain.push_back(GURL()); |
| 1028 info->total_bytes = static_cast<int64>(kTestDataLen * 3); | 1012 info->total_bytes = static_cast<int64>(kTestDataLen * 3); |
| 1029 info->save_info.file_path = path; | 1013 info->save_info.file_path = path; |
| 1030 info->save_info.file_stream.reset(stream); | 1014 info->save_info.file_stream.reset(stream); |
| 1015 scoped_refptr<content::ByteStream> data_pipe(new content::ByteStream); |
| 1016 info->pipe = data_pipe; |
| 1031 | 1017 |
| 1032 // Create a download file that we can insert errors into. | 1018 // Create a download file that we can insert errors into. |
| 1033 DownloadFileWithErrors* download_file(new DownloadFileWithErrors( | 1019 scoped_ptr<DownloadFileWithErrors> download_file(new DownloadFileWithErrors( |
| 1034 info.get(), download_manager_, false)); | 1020 info.get(), download_manager_, false)); |
| 1035 download_file->Initialize(); | 1021 download_file->Initialize(); |
| 1036 AddDownloadToFileManager(local_id, download_file); | |
| 1037 | 1022 |
| 1038 // |download_file| is owned by DownloadFileManager. | |
| 1039 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 1023 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 1040 | 1024 |
| 1041 DownloadItem* download = GetActiveDownloadItem(0); | 1025 DownloadItem* download = GetActiveDownloadItem(0); |
| 1042 ASSERT_TRUE(download != NULL); | 1026 ASSERT_TRUE(download != NULL); |
| 1043 | 1027 |
| 1044 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 1028 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 1045 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 1029 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 1046 | 1030 |
| 1047 // Add some data before finalizing the file name. | 1031 // Add some data before finalizing the file name. |
| 1048 UpdateData(local_id, kTestData, kTestDataLen); | 1032 WriteCopyToPipe(data_pipe, kTestData, kTestDataLen); |
| 1049 | 1033 |
| 1050 // Finalize the file name. | 1034 // Finalize the file name. |
| 1051 ContinueDownloadWithPath(download, path); | 1035 ContinueDownloadWithPath(download, path); |
| 1052 message_loop_.RunAllPending(); | 1036 message_loop_.RunAllPending(); |
| 1053 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1037 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 1054 | 1038 |
| 1055 // Add more data. | 1039 // Add more data. |
| 1056 UpdateData(local_id, kTestData, kTestDataLen); | 1040 WriteCopyToPipe(data_pipe, kTestData, kTestDataLen); |
| 1057 | 1041 |
| 1058 // Add more data, but an error occurs. | 1042 // Add more data, but an error occurs. |
| 1059 download_file->set_forced_error(net::ERR_FAILED); | 1043 download_file->set_forced_error(net::ERR_FAILED); |
| 1060 UpdateData(local_id, kTestData, kTestDataLen); | 1044 WriteCopyToPipe(data_pipe, kTestData, kTestDataLen); |
| 1061 | 1045 |
| 1062 // Check the state. The download should have been interrupted. | 1046 // Check the state. The download should have been interrupted. |
| 1063 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 1047 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| 1064 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 1048 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 1065 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); | 1049 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 1066 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); | 1050 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
| 1067 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); | 1051 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 1068 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 1052 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 1069 EXPECT_TRUE(observer->was_updated()); | 1053 EXPECT_TRUE(observer->was_updated()); |
| 1070 EXPECT_FALSE(observer->was_opened()); | 1054 EXPECT_FALSE(observer->was_opened()); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 1087 // don't call the function that deletes it, so we do so ourselves. | 1071 // don't call the function that deletes it, so we do so ourselves. |
| 1088 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 1072 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 1089 DownloadId id = DownloadId(kValidIdDomain, 0); | 1073 DownloadId id = DownloadId(kValidIdDomain, 0); |
| 1090 info->download_id = id; | 1074 info->download_id = id; |
| 1091 info->prompt_user_for_save_location = false; | 1075 info->prompt_user_for_save_location = false; |
| 1092 info->url_chain.push_back(GURL()); | 1076 info->url_chain.push_back(GURL()); |
| 1093 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); | 1077 const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
| 1094 const FilePath cr_path(GetTempDownloadPath(new_path)); | 1078 const FilePath cr_path(GetTempDownloadPath(new_path)); |
| 1095 | 1079 |
| 1096 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); | 1080 MockDownloadFile* download_file(new NiceMock<MockDownloadFile>()); |
| 1097 ON_CALL(*download_file, AppendDataToFile(_, _)) | |
| 1098 .WillByDefault(Return(net::OK)); | |
| 1099 AddMockDownloadToFileManager(info->download_id.local(), download_file); | 1081 AddMockDownloadToFileManager(info->download_id.local(), download_file); |
| 1100 | 1082 |
| 1101 // |download_file| is owned by DownloadFileManager. | 1083 // |download_file| is owned by DownloadFileManager. |
| 1102 EXPECT_CALL(*download_file, Rename(cr_path)) | 1084 EXPECT_CALL(*download_file, Rename(cr_path)) |
| 1103 .Times(1) | 1085 .Times(1) |
| 1104 .WillOnce(Return(net::OK)); | 1086 .WillOnce(Return(net::OK)); |
| 1105 | 1087 |
| 1106 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 1088 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 1107 | 1089 |
| 1108 DownloadItem* download = GetActiveDownloadItem(0); | 1090 DownloadItem* download = GetActiveDownloadItem(0); |
| 1109 ASSERT_TRUE(download != NULL); | 1091 ASSERT_TRUE(download != NULL); |
| 1110 | 1092 |
| 1111 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 1093 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 1112 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 1094 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 1113 | 1095 |
| 1114 ContinueDownloadWithPath(download, new_path); | 1096 ContinueDownloadWithPath(download, new_path); |
| 1115 message_loop_.RunAllPending(); | 1097 message_loop_.RunAllPending(); |
| 1116 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1098 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 1117 | 1099 |
| 1118 download_file->AppendDataToFile(kTestData, kTestDataLen); | |
| 1119 | |
| 1120 download->Cancel(false); | 1100 download->Cancel(false); |
| 1121 message_loop_.RunAllPending(); | 1101 message_loop_.RunAllPending(); |
| 1122 | 1102 |
| 1123 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1103 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 1124 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 1104 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 1125 EXPECT_TRUE(observer->hit_state(DownloadItem::CANCELLED)); | 1105 EXPECT_TRUE(observer->hit_state(DownloadItem::CANCELLED)); |
| 1126 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); | 1106 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 1127 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); | 1107 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
| 1128 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 1108 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 1129 EXPECT_TRUE(observer->was_updated()); | 1109 EXPECT_TRUE(observer->was_updated()); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1167 unique_new_path = unique_new_path.InsertBeforeExtensionASCII( | 1147 unique_new_path = unique_new_path.InsertBeforeExtensionASCII( |
| 1168 StringPrintf(" (%d)", uniquifier)); | 1148 StringPrintf(" (%d)", uniquifier)); |
| 1169 | 1149 |
| 1170 // Normally, the download system takes ownership of info, and is | 1150 // Normally, the download system takes ownership of info, and is |
| 1171 // responsible for deleting it. In these unit tests, however, we | 1151 // responsible for deleting it. In these unit tests, however, we |
| 1172 // don't call the function that deletes it, so we do so ourselves. | 1152 // don't call the function that deletes it, so we do so ourselves. |
| 1173 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 1153 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 1174 info->download_id = DownloadId(kValidIdDomain, 0); | 1154 info->download_id = DownloadId(kValidIdDomain, 0); |
| 1175 info->prompt_user_for_save_location = true; | 1155 info->prompt_user_for_save_location = true; |
| 1176 info->url_chain.push_back(GURL()); | 1156 info->url_chain.push_back(GURL()); |
| 1157 scoped_refptr<content::ByteStream> data_pipe(new content::ByteStream); |
| 1158 info->pipe = data_pipe; |
| 1177 | 1159 |
| 1178 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 1160 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 1179 | 1161 |
| 1180 DownloadItem* download = GetActiveDownloadItem(0); | 1162 DownloadItem* download = GetActiveDownloadItem(0); |
| 1181 ASSERT_TRUE(download != NULL); | 1163 ASSERT_TRUE(download != NULL); |
| 1182 | 1164 |
| 1183 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 1165 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 1184 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 1166 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 1185 | 1167 |
| 1186 // Create and initialize the download file. We're bypassing the first part | 1168 // Create and initialize the download file. We're bypassing the first part |
| 1187 // of the download process and skipping to the part after the final file | 1169 // of the download process and skipping to the part after the final file |
| 1188 // name has been chosen, so we need to initialize the download file | 1170 // name has been chosen, so we need to initialize the download file |
| 1189 // properly. | 1171 // properly. |
| 1190 DownloadFile* download_file( | 1172 DownloadFile* download_file( |
| 1191 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), | 1173 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 1192 download_manager_, false, | 1174 download_manager_, false, |
| 1193 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), | 1175 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), |
| 1194 net::BoundNetLog())); | 1176 net::BoundNetLog())); |
| 1195 download_file->Rename(cr_path); | 1177 download_file->Rename(cr_path); |
| 1196 // This creates the .temp version of the file. | 1178 // This creates the .temp version of the file. |
| 1197 download_file->Initialize(); | 1179 download_file->Initialize(); |
| 1198 // |download_file| is owned by DownloadFileManager. | 1180 // |download_file| is owned by DownloadFileManager. |
| 1199 AddDownloadToFileManager(info->download_id.local(), download_file); | 1181 AddDownloadToFileManager(info->download_id.local(), download_file); |
| 1200 | 1182 |
| 1201 ContinueDownloadWithPath(download, new_path); | 1183 ContinueDownloadWithPath(download, new_path); |
| 1202 message_loop_.RunAllPending(); | 1184 message_loop_.RunAllPending(); |
| 1203 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1185 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 1204 | 1186 |
| 1205 download_file->AppendDataToFile(kTestData, kTestDataLen); | 1187 WriteCopyToPipe(data_pipe, kTestData, kTestDataLen); |
| 1206 | 1188 |
| 1207 // Finish the download. | 1189 // Finish the download. |
| 1208 OnResponseCompleted(0, kTestDataLen, ""); | 1190 OnResponseCompleted(0, kTestDataLen, ""); |
| 1209 message_loop_.RunAllPending(); | 1191 message_loop_.RunAllPending(); |
| 1210 | 1192 |
| 1211 // Download is complete. | 1193 // Download is complete. |
| 1212 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 1194 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| 1213 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 1195 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 1214 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); | 1196 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 1215 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); | 1197 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1243 const FilePath cr_path(GetTempDownloadPath(new_path)); | 1225 const FilePath cr_path(GetTempDownloadPath(new_path)); |
| 1244 EXPECT_FALSE(file_util::PathExists(new_path)); | 1226 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 1245 | 1227 |
| 1246 // Normally, the download system takes ownership of info, and is | 1228 // Normally, the download system takes ownership of info, and is |
| 1247 // responsible for deleting it. In these unit tests, however, we | 1229 // responsible for deleting it. In these unit tests, however, we |
| 1248 // don't call the function that deletes it, so we do so ourselves. | 1230 // don't call the function that deletes it, so we do so ourselves. |
| 1249 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 1231 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 1250 info->download_id = DownloadId(kValidIdDomain, 0); | 1232 info->download_id = DownloadId(kValidIdDomain, 0); |
| 1251 info->prompt_user_for_save_location = true; | 1233 info->prompt_user_for_save_location = true; |
| 1252 info->url_chain.push_back(GURL()); | 1234 info->url_chain.push_back(GURL()); |
| 1235 scoped_refptr<content::ByteStream> data_pipe(new content::ByteStream); |
| 1236 info->pipe = data_pipe; |
| 1253 | 1237 |
| 1254 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 1238 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 1255 | 1239 |
| 1256 DownloadItem* download = GetActiveDownloadItem(0); | 1240 DownloadItem* download = GetActiveDownloadItem(0); |
| 1257 ASSERT_TRUE(download != NULL); | 1241 ASSERT_TRUE(download != NULL); |
| 1258 | 1242 |
| 1259 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 1243 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 1260 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 1244 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 1261 | 1245 |
| 1262 // Create and initialize the download file. We're bypassing the first part | 1246 // Create and initialize the download file. We're bypassing the first part |
| 1263 // of the download process and skipping to the part after the final file | 1247 // of the download process and skipping to the part after the final file |
| 1264 // name has been chosen, so we need to initialize the download file | 1248 // name has been chosen, so we need to initialize the download file |
| 1265 // properly. | 1249 // properly. |
| 1266 DownloadFile* download_file( | 1250 DownloadFile* download_file( |
| 1267 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), | 1251 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 1268 download_manager_, false, | 1252 download_manager_, false, |
| 1269 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), | 1253 scoped_ptr<PowerSaveBlocker>(NULL).Pass(), |
| 1270 net::BoundNetLog())); | 1254 net::BoundNetLog())); |
| 1271 download_file->Rename(cr_path); | 1255 download_file->Rename(cr_path); |
| 1272 // This creates the .temp version of the file. | 1256 // This creates the .temp version of the file. |
| 1273 download_file->Initialize(); | 1257 download_file->Initialize(); |
| 1274 // |download_file| is owned by DownloadFileManager. | 1258 // |download_file| is owned by DownloadFileManager. |
| 1275 AddDownloadToFileManager(info->download_id.local(), download_file); | 1259 AddDownloadToFileManager(info->download_id.local(), download_file); |
| 1276 | 1260 |
| 1277 ContinueDownloadWithPath(download, new_path); | 1261 ContinueDownloadWithPath(download, new_path); |
| 1278 message_loop_.RunAllPending(); | 1262 message_loop_.RunAllPending(); |
| 1279 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1263 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 1280 | 1264 |
| 1281 download_file->AppendDataToFile(kTestData, kTestDataLen); | 1265 WriteCopyToPipe(data_pipe, kTestData, kTestDataLen); |
| 1282 | 1266 |
| 1283 // Finish the download. | 1267 // Finish the download. |
| 1284 OnResponseCompleted(0, kTestDataLen, ""); | 1268 OnResponseCompleted(0, kTestDataLen, ""); |
| 1285 message_loop_.RunAllPending(); | 1269 message_loop_.RunAllPending(); |
| 1286 | 1270 |
| 1287 // Download is complete. | 1271 // Download is complete. |
| 1288 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 1272 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| 1289 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 1273 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 1290 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); | 1274 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 1291 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); | 1275 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 1310 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); | 1294 EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 1311 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); | 1295 EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); |
| 1312 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 1296 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 1313 EXPECT_TRUE(observer->was_updated()); | 1297 EXPECT_TRUE(observer->was_updated()); |
| 1314 EXPECT_FALSE(observer->was_opened()); | 1298 EXPECT_FALSE(observer->was_opened()); |
| 1315 EXPECT_TRUE(download->GetFileExternallyRemoved()); | 1299 EXPECT_TRUE(download->GetFileExternallyRemoved()); |
| 1316 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); | 1300 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); |
| 1317 | 1301 |
| 1318 EXPECT_FALSE(file_util::PathExists(new_path)); | 1302 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 1319 } | 1303 } |
| OLD | NEW |