| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/i18n/number_formatting.h" | 10 #include "base/i18n/number_formatting.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 | 63 |
| 64 namespace { | 64 namespace { |
| 65 | 65 |
| 66 class MockDownloadFileFactory | 66 class MockDownloadFileFactory |
| 67 : public DownloadFileManager::DownloadFileFactory { | 67 : public DownloadFileManager::DownloadFileFactory { |
| 68 public: | 68 public: |
| 69 MockDownloadFileFactory() {} | 69 MockDownloadFileFactory() {} |
| 70 | 70 |
| 71 virtual DownloadFile* CreateFile(DownloadCreateInfo* info, | 71 virtual DownloadFile* CreateFile(DownloadCreateInfo* info, |
| 72 const DownloadRequestHandle& request_handle, | 72 const DownloadRequestHandle& request_handle, |
| 73 DownloadManager* download_manager) OVERRIDE; | 73 DownloadManager* download_manager, |
| 74 bool calculate_hash) OVERRIDE; |
| 74 }; | 75 }; |
| 75 | 76 |
| 76 DownloadFile* MockDownloadFileFactory::CreateFile( | 77 DownloadFile* MockDownloadFileFactory::CreateFile( |
| 77 DownloadCreateInfo* info, | 78 DownloadCreateInfo* info, |
| 78 const DownloadRequestHandle& request_handle, | 79 const DownloadRequestHandle& request_handle, |
| 79 DownloadManager* download_manager) { | 80 DownloadManager* download_manager, |
| 81 bool calculate_hash) { |
| 80 NOTREACHED(); | 82 NOTREACHED(); |
| 81 return NULL; | 83 return NULL; |
| 82 } | 84 } |
| 83 | 85 |
| 84 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; | 86 DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; |
| 85 | 87 |
| 86 class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate { | 88 class TestDownloadManagerDelegate : public ChromeDownloadManagerDelegate { |
| 87 public: | 89 public: |
| 88 explicit TestDownloadManagerDelegate(Profile* profile) | 90 explicit TestDownloadManagerDelegate(Profile* profile) |
| 89 : ChromeDownloadManagerDelegate(profile), | 91 : ChromeDownloadManagerDelegate(profile), |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 216 |
| 215 BrowserThread::PostTask( | 217 BrowserThread::PostTask( |
| 216 BrowserThread::FILE, FROM_HERE, | 218 BrowserThread::FILE, FROM_HERE, |
| 217 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), | 219 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), |
| 218 DownloadId(kValidIdDomain, id), download_buffer_)); | 220 DownloadId(kValidIdDomain, id), download_buffer_)); |
| 219 | 221 |
| 220 message_loop_.RunAllPending(); | 222 message_loop_.RunAllPending(); |
| 221 } | 223 } |
| 222 | 224 |
| 223 void OnDownloadInterrupted(int32 download_id, int64 size, | 225 void OnDownloadInterrupted(int32 download_id, int64 size, |
| 226 const std::string& hash_state, |
| 224 InterruptReason reason) { | 227 InterruptReason reason) { |
| 225 download_manager_->OnDownloadInterrupted(download_id, size, reason); | 228 download_manager_->OnDownloadInterrupted(download_id, size, |
| 229 hash_state, reason); |
| 226 } | 230 } |
| 227 | 231 |
| 228 // Get the download item with ID |id|. | 232 // Get the download item with ID |id|. |
| 229 DownloadItem* GetActiveDownloadItem(int32 id) { | 233 DownloadItem* GetActiveDownloadItem(int32 id) { |
| 230 return download_manager_->GetActiveDownload(id); | 234 return download_manager_->GetActiveDownload(id); |
| 231 } | 235 } |
| 232 | 236 |
| 233 protected: | 237 protected: |
| 234 DownloadStatusUpdater download_status_updater_; | 238 DownloadStatusUpdater download_status_updater_; |
| 235 scoped_ptr<TestingProfile> profile_; | 239 scoped_ptr<TestingProfile> profile_; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 254 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 258 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
| 255 }; | 259 }; |
| 256 | 260 |
| 257 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; | 261 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; |
| 258 const size_t DownloadManagerTest::kTestDataLen = | 262 const size_t DownloadManagerTest::kTestDataLen = |
| 259 strlen(DownloadManagerTest::kTestData); | 263 strlen(DownloadManagerTest::kTestData); |
| 260 | 264 |
| 261 // A DownloadFile that we can inject errors into. | 265 // A DownloadFile that we can inject errors into. |
| 262 class DownloadFileWithErrors : public DownloadFileImpl { | 266 class DownloadFileWithErrors : public DownloadFileImpl { |
| 263 public: | 267 public: |
| 264 DownloadFileWithErrors(DownloadCreateInfo* info, DownloadManager* manager); | 268 DownloadFileWithErrors(DownloadCreateInfo* info, |
| 269 DownloadManager* manager, |
| 270 bool calculate_hash); |
| 265 virtual ~DownloadFileWithErrors() {} | 271 virtual ~DownloadFileWithErrors() {} |
| 266 | 272 |
| 267 // BaseFile delegated functions. | 273 // BaseFile delegated functions. |
| 268 virtual net::Error Initialize(bool calculate_hash); | 274 virtual net::Error Initialize(); |
| 269 virtual net::Error AppendDataToFile(const char* data, size_t data_len); | 275 virtual net::Error AppendDataToFile(const char* data, size_t data_len); |
| 270 virtual net::Error Rename(const FilePath& full_path); | 276 virtual net::Error Rename(const FilePath& full_path); |
| 271 | 277 |
| 272 void set_forced_error(net::Error error) { forced_error_ = error; } | 278 void set_forced_error(net::Error error) { forced_error_ = error; } |
| 273 void clear_forced_error() { forced_error_ = net::OK; } | 279 void clear_forced_error() { forced_error_ = net::OK; } |
| 274 net::Error forced_error() const { return forced_error_; } | 280 net::Error forced_error() const { return forced_error_; } |
| 275 | 281 |
| 276 private: | 282 private: |
| 277 net::Error ReturnError(net::Error function_error) { | 283 net::Error ReturnError(net::Error function_error) { |
| 278 if (forced_error_ != net::OK) { | 284 if (forced_error_ != net::OK) { |
| 279 net::Error ret = forced_error_; | 285 net::Error ret = forced_error_; |
| 280 clear_forced_error(); | 286 clear_forced_error(); |
| 281 return ret; | 287 return ret; |
| 282 } | 288 } |
| 283 | 289 |
| 284 return function_error; | 290 return function_error; |
| 285 } | 291 } |
| 286 | 292 |
| 287 net::Error forced_error_; | 293 net::Error forced_error_; |
| 288 }; | 294 }; |
| 289 | 295 |
| 290 DownloadFileWithErrors::DownloadFileWithErrors(DownloadCreateInfo* info, | 296 DownloadFileWithErrors::DownloadFileWithErrors(DownloadCreateInfo* info, |
| 291 DownloadManager* manager) | 297 DownloadManager* manager, |
| 292 : DownloadFileImpl(info, new DownloadRequestHandle(), manager), | 298 bool calculate_hash) |
| 299 : DownloadFileImpl(info, |
| 300 new DownloadRequestHandle(), |
| 301 manager, |
| 302 calculate_hash), |
| 293 forced_error_(net::OK) { | 303 forced_error_(net::OK) { |
| 294 } | 304 } |
| 295 | 305 |
| 296 net::Error DownloadFileWithErrors::Initialize(bool calculate_hash) { | 306 net::Error DownloadFileWithErrors::Initialize() { |
| 297 return ReturnError(DownloadFileImpl::Initialize(calculate_hash)); | 307 return ReturnError(DownloadFileImpl::Initialize()); |
| 298 } | 308 } |
| 299 | 309 |
| 300 net::Error DownloadFileWithErrors::AppendDataToFile(const char* data, | 310 net::Error DownloadFileWithErrors::AppendDataToFile(const char* data, |
| 301 size_t data_len) { | 311 size_t data_len) { |
| 302 return ReturnError(DownloadFileImpl::AppendDataToFile(data, data_len)); | 312 return ReturnError(DownloadFileImpl::AppendDataToFile(data, data_len)); |
| 303 } | 313 } |
| 304 | 314 |
| 305 net::Error DownloadFileWithErrors::Rename(const FilePath& full_path) { | 315 net::Error DownloadFileWithErrors::Rename(const FilePath& full_path) { |
| 306 return ReturnError(DownloadFileImpl::Rename(full_path)); | 316 return ReturnError(DownloadFileImpl::Rename(full_path)); |
| 307 } | 317 } |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 475 // don't call the function that deletes it, so we do so ourselves. | 485 // don't call the function that deletes it, so we do so ourselves. |
| 476 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); | 486 scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
| 477 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); | 487 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); |
| 478 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; | 488 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; |
| 479 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); | 489 info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); |
| 480 info->mime_type = kStartDownloadCases[i].mime_type; | 490 info->mime_type = kStartDownloadCases[i].mime_type; |
| 481 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 491 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 482 | 492 |
| 483 DownloadFile* download_file( | 493 DownloadFile* download_file( |
| 484 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), | 494 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 485 download_manager_)); | 495 download_manager_, false)); |
| 486 AddDownloadToFileManager(info->download_id.local(), download_file); | 496 AddDownloadToFileManager(info->download_id.local(), download_file); |
| 487 download_file->Initialize(false); | 497 download_file->Initialize(); |
| 488 download_manager_->StartDownload(info->download_id.local()); | 498 download_manager_->StartDownload(info->download_id.local()); |
| 489 message_loop_.RunAllPending(); | 499 message_loop_.RunAllPending(); |
| 490 | 500 |
| 491 // SelectFileObserver will have recorded any attempt to open the | 501 // SelectFileObserver will have recorded any attempt to open the |
| 492 // select file dialog. | 502 // select file dialog. |
| 493 // Note that DownloadManager::FileSelectionCanceled() is never called. | 503 // Note that DownloadManager::FileSelectionCanceled() is never called. |
| 494 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, | 504 EXPECT_EQ(kStartDownloadCases[i].expected_save_as, |
| 495 observer.ShowedFileDialogForId(i)); | 505 observer.ShowedFileDialogForId(i)); |
| 496 } | 506 } |
| 497 } | 507 } |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 | 923 |
| 914 download_file->AppendDataToFile(kTestData, kTestDataLen); | 924 download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 915 | 925 |
| 916 ContinueDownloadWithPath(download, new_path); | 926 ContinueDownloadWithPath(download, new_path); |
| 917 message_loop_.RunAllPending(); | 927 message_loop_.RunAllPending(); |
| 918 EXPECT_EQ(1, | 928 EXPECT_EQ(1, |
| 919 recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); | 929 recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); |
| 920 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 930 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 921 | 931 |
| 922 int64 error_size = 3; | 932 int64 error_size = 3; |
| 923 OnDownloadInterrupted(0, error_size, | 933 OnDownloadInterrupted(0, error_size, "", |
| 924 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); | 934 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
| 925 message_loop_.RunAllPending(); | 935 message_loop_.RunAllPending(); |
| 926 | 936 |
| 927 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); | 937 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| 928 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); | 938 EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 929 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); | 939 EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 930 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); | 940 EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
| 931 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); | 941 EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 932 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 942 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 933 EXPECT_TRUE(observer->was_updated()); | 943 EXPECT_TRUE(observer->was_updated()); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 979 static const int32 local_id = 0; | 989 static const int32 local_id = 0; |
| 980 info->download_id = DownloadId(kValidIdDomain, local_id); | 990 info->download_id = DownloadId(kValidIdDomain, local_id); |
| 981 info->prompt_user_for_save_location = false; | 991 info->prompt_user_for_save_location = false; |
| 982 info->url_chain.push_back(GURL()); | 992 info->url_chain.push_back(GURL()); |
| 983 info->total_bytes = static_cast<int64>(kTestDataLen * 3); | 993 info->total_bytes = static_cast<int64>(kTestDataLen * 3); |
| 984 info->save_info.file_path = path; | 994 info->save_info.file_path = path; |
| 985 info->save_info.file_stream.reset(stream); | 995 info->save_info.file_stream.reset(stream); |
| 986 | 996 |
| 987 // Create a download file that we can insert errors into. | 997 // Create a download file that we can insert errors into. |
| 988 DownloadFileWithErrors* download_file(new DownloadFileWithErrors( | 998 DownloadFileWithErrors* download_file(new DownloadFileWithErrors( |
| 989 info.get(), download_manager_)); | 999 info.get(), download_manager_, false)); |
| 1000 download_file->Initialize(); |
| 990 AddDownloadToFileManager(local_id, download_file); | 1001 AddDownloadToFileManager(local_id, download_file); |
| 991 | 1002 |
| 992 // |download_file| is owned by DownloadFileManager. | 1003 // |download_file| is owned by DownloadFileManager. |
| 993 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); | 1004 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
| 994 | 1005 |
| 995 DownloadItem* download = GetActiveDownloadItem(0); | 1006 DownloadItem* download = GetActiveDownloadItem(0); |
| 996 ASSERT_TRUE(download != NULL); | 1007 ASSERT_TRUE(download != NULL); |
| 997 // This will keep track of what should be displayed on the shelf. | 1008 // This will keep track of what should be displayed on the shelf. |
| 998 scoped_ptr<DownloadItemModel> download_item_model( | 1009 scoped_ptr<DownloadItemModel> download_item_model( |
| 999 new DownloadItemModel(download)); | 1010 new DownloadItemModel(download)); |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1153 | 1164 |
| 1154 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 1165 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 1155 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 1166 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 1156 | 1167 |
| 1157 // 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 |
| 1158 // 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 |
| 1159 // 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 |
| 1160 // properly. | 1171 // properly. |
| 1161 DownloadFile* download_file( | 1172 DownloadFile* download_file( |
| 1162 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), | 1173 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 1163 download_manager_)); | 1174 download_manager_, false)); |
| 1164 download_file->Rename(cr_path); | 1175 download_file->Rename(cr_path); |
| 1165 // This creates the .crdownload version of the file. | 1176 // This creates the .crdownload version of the file. |
| 1166 download_file->Initialize(false); | 1177 download_file->Initialize(); |
| 1167 // |download_file| is owned by DownloadFileManager. | 1178 // |download_file| is owned by DownloadFileManager. |
| 1168 AddDownloadToFileManager(info->download_id.local(), download_file); | 1179 AddDownloadToFileManager(info->download_id.local(), download_file); |
| 1169 | 1180 |
| 1170 ContinueDownloadWithPath(download, new_path); | 1181 ContinueDownloadWithPath(download, new_path); |
| 1171 message_loop_.RunAllPending(); | 1182 message_loop_.RunAllPending(); |
| 1172 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1183 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 1173 | 1184 |
| 1174 download_file->AppendDataToFile(kTestData, kTestDataLen); | 1185 download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 1175 | 1186 |
| 1176 // Finish the download. | 1187 // Finish the download. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1230 | 1241 |
| 1231 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 1242 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 1232 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); | 1243 scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 1233 | 1244 |
| 1234 // Create and initialize the download file. We're bypassing the first part | 1245 // Create and initialize the download file. We're bypassing the first part |
| 1235 // of the download process and skipping to the part after the final file | 1246 // of the download process and skipping to the part after the final file |
| 1236 // name has been chosen, so we need to initialize the download file | 1247 // name has been chosen, so we need to initialize the download file |
| 1237 // properly. | 1248 // properly. |
| 1238 DownloadFile* download_file( | 1249 DownloadFile* download_file( |
| 1239 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), | 1250 new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 1240 download_manager_)); | 1251 download_manager_, false)); |
| 1241 download_file->Rename(cr_path); | 1252 download_file->Rename(cr_path); |
| 1242 // This creates the .crdownload version of the file. | 1253 // This creates the .crdownload version of the file. |
| 1243 download_file->Initialize(false); | 1254 download_file->Initialize(); |
| 1244 // |download_file| is owned by DownloadFileManager. | 1255 // |download_file| is owned by DownloadFileManager. |
| 1245 AddDownloadToFileManager(info->download_id.local(), download_file); | 1256 AddDownloadToFileManager(info->download_id.local(), download_file); |
| 1246 | 1257 |
| 1247 ContinueDownloadWithPath(download, new_path); | 1258 ContinueDownloadWithPath(download, new_path); |
| 1248 message_loop_.RunAllPending(); | 1259 message_loop_.RunAllPending(); |
| 1249 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); | 1260 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 1250 | 1261 |
| 1251 download_file->AppendDataToFile(kTestData, kTestDataLen); | 1262 download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 1252 | 1263 |
| 1253 // Finish the download. | 1264 // Finish the download. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 1283 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 1294 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 1284 EXPECT_TRUE(observer->was_updated()); | 1295 EXPECT_TRUE(observer->was_updated()); |
| 1285 EXPECT_FALSE(observer->was_opened()); | 1296 EXPECT_FALSE(observer->was_opened()); |
| 1286 EXPECT_TRUE(download->GetFileExternallyRemoved()); | 1297 EXPECT_TRUE(download->GetFileExternallyRemoved()); |
| 1287 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); | 1298 EXPECT_EQ(DownloadItem::COMPLETE, download->GetState()); |
| 1288 EXPECT_EQ(download_item_model->GetStatusText(), | 1299 EXPECT_EQ(download_item_model->GetStatusText(), |
| 1289 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); | 1300 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); |
| 1290 | 1301 |
| 1291 EXPECT_FALSE(file_util::PathExists(new_path)); | 1302 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 1292 } | 1303 } |
| OLD | NEW |