| 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 class DownloadManagerTest : public testing::Test { | 48 class DownloadManagerTest : public testing::Test { |
| 49 public: | 49 public: |
| 50 static const char* kTestData; | 50 static const char* kTestData; |
| 51 static const size_t kTestDataLen; | 51 static const size_t kTestDataLen; |
| 52 | 52 |
| 53 DownloadManagerTest() | 53 DownloadManagerTest() |
| 54 : profile_(new TestingProfile()), | 54 : profile_(new TestingProfile()), |
| 55 download_manager_delegate_(new ChromeDownloadManagerDelegate( | 55 download_manager_delegate_(new ChromeDownloadManagerDelegate( |
| 56 profile_.get())), | 56 profile_.get())), |
| 57 id_factory_(new DownloadIdFactory(kValidIdDomain)), | 57 id_factory_(new DownloadIdFactory(kValidIdDomain)), |
| 58 download_manager_(new MockDownloadManager( | 58 download_manager_(new DownloadManager( |
| 59 download_manager_delegate_, | 59 download_manager_delegate_, |
| 60 id_factory_, | 60 id_factory_, |
| 61 &download_status_updater_)), | 61 &download_status_updater_)), |
| 62 ui_thread_(BrowserThread::UI, &message_loop_), | 62 ui_thread_(BrowserThread::UI, &message_loop_), |
| 63 file_thread_(BrowserThread::FILE, &message_loop_), | 63 file_thread_(BrowserThread::FILE, &message_loop_), |
| 64 download_buffer_(new content::DownloadBuffer) { | 64 download_buffer_(new content::DownloadBuffer) { |
| 65 download_manager_->Init(profile_.get()); | 65 download_manager_->Init(profile_.get()); |
| 66 download_manager_delegate_->SetDownloadManager(download_manager_); | 66 download_manager_delegate_->SetDownloadManager(download_manager_); |
| 67 } | 67 } |
| 68 | 68 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 message_loop_.RunAllPending(); | 111 message_loop_.RunAllPending(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void OnDownloadInterrupted(int32 download_id, int64 size, | 114 void OnDownloadInterrupted(int32 download_id, int64 size, |
| 115 InterruptReason reason) { | 115 InterruptReason reason) { |
| 116 download_manager_->OnDownloadInterrupted(download_id, size, reason); | 116 download_manager_->OnDownloadInterrupted(download_id, size, reason); |
| 117 } | 117 } |
| 118 | 118 |
| 119 // Get the download item with ID |id|. | 119 // Get the download item with ID |id|. |
| 120 DownloadItem* GetActiveDownloadItem(int32 id) { | 120 DownloadItem* GetActiveDownloadItem(int32 id) { |
| 121 if (ContainsKey(download_manager_->active_downloads_, id)) | 121 return download_manager_->GetActiveDownload(id); |
| 122 return download_manager_->active_downloads_[id]; | |
| 123 return NULL; | |
| 124 } | 122 } |
| 125 | 123 |
| 126 protected: | 124 protected: |
| 127 DownloadStatusUpdater download_status_updater_; | 125 DownloadStatusUpdater download_status_updater_; |
| 128 scoped_ptr<TestingProfile> profile_; | 126 scoped_ptr<TestingProfile> profile_; |
| 129 scoped_refptr<ChromeDownloadManagerDelegate> download_manager_delegate_; | 127 scoped_refptr<ChromeDownloadManagerDelegate> download_manager_delegate_; |
| 130 scoped_refptr<DownloadIdFactory> id_factory_; | 128 scoped_refptr<DownloadIdFactory> id_factory_; |
| 131 scoped_refptr<DownloadManager> download_manager_; | 129 scoped_refptr<DownloadManagerInterface> download_manager_; |
| 132 scoped_refptr<DownloadFileManager> file_manager_; | 130 scoped_refptr<DownloadFileManager> file_manager_; |
| 133 MessageLoopForUI message_loop_; | 131 MessageLoopForUI message_loop_; |
| 134 content::TestBrowserThread ui_thread_; | 132 content::TestBrowserThread ui_thread_; |
| 135 content::TestBrowserThread file_thread_; | 133 content::TestBrowserThread file_thread_; |
| 136 scoped_refptr<content::DownloadBuffer> download_buffer_; | 134 scoped_refptr<content::DownloadBuffer> download_buffer_; |
| 137 | 135 |
| 138 DownloadFileManager* file_manager() { | 136 DownloadFileManager* file_manager() { |
| 139 if (!file_manager_) { | 137 if (!file_manager_) { |
| 140 file_manager_ = new DownloadFileManager(NULL); | 138 file_manager_ = new DownloadFileManager(NULL); |
| 141 download_manager_->file_manager_ = file_manager_; | 139 download_manager_->SetFileManager(file_manager_); |
| 142 } | 140 } |
| 143 return file_manager_; | 141 return file_manager_; |
| 144 } | 142 } |
| 145 | 143 |
| 146 // Make sure download item |id| was set with correct safety state for | 144 // Make sure download item |id| was set with correct safety state for |
| 147 // given |is_dangerous_file| and |is_dangerous_url|. | 145 // given |is_dangerous_file| and |is_dangerous_url|. |
| 148 bool VerifySafetyState(bool is_dangerous_file, | 146 bool VerifySafetyState(bool is_dangerous_file, |
| 149 bool is_dangerous_url, | 147 bool is_dangerous_url, |
| 150 int id) { | 148 int id) { |
| 151 DownloadItem::SafetyState safety_state = | 149 DownloadItem::SafetyState safety_state = |
| 152 download_manager_->GetDownloadItem(id)->safety_state(); | 150 download_manager_->GetDownloadItem(id)->safety_state(); |
| 153 return (is_dangerous_file || is_dangerous_url) ? | 151 return (is_dangerous_file || is_dangerous_url) ? |
| 154 safety_state != DownloadItem::SAFE : safety_state == DownloadItem::SAFE; | 152 safety_state != DownloadItem::SAFE : safety_state == DownloadItem::SAFE; |
| 155 } | 153 } |
| 156 | 154 |
| 157 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); | 155 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
| 158 }; | 156 }; |
| 159 | 157 |
| 160 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; | 158 const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; |
| 161 const size_t DownloadManagerTest::kTestDataLen = | 159 const size_t DownloadManagerTest::kTestDataLen = |
| 162 strlen(DownloadManagerTest::kTestData); | 160 strlen(DownloadManagerTest::kTestData); |
| 163 | 161 |
| 164 // A DownloadFile that we can inject errors into. Uses MockFileStream. | 162 // A DownloadFile that we can inject errors into. Uses MockFileStream. |
| 165 // Note: This can't be in an anonymous namespace because it must be declared | 163 // Note: This can't be in an anonymous namespace because it must be declared |
| 166 // as a friend of |DownloadFile| in order to access its private members. | 164 // as a friend of |DownloadFile| in order to access its private members. |
| 167 class DownloadFileWithMockStream : public DownloadFile { | 165 class DownloadFileWithMockStream : public DownloadFile { |
| 168 public: | 166 public: |
| 169 DownloadFileWithMockStream(DownloadCreateInfo* info, | 167 DownloadFileWithMockStream(DownloadCreateInfo* info, |
| 170 DownloadManager* manager, | 168 DownloadManagerInterface* manager, |
| 171 net::testing::MockFileStream* stream); | 169 net::testing::MockFileStream* stream); |
| 172 | 170 |
| 173 virtual ~DownloadFileWithMockStream() {} | 171 virtual ~DownloadFileWithMockStream() {} |
| 174 | 172 |
| 175 void SetForcedError(int error); | 173 void SetForcedError(int error); |
| 176 | 174 |
| 177 protected: | 175 protected: |
| 178 // This version creates a |MockFileStream| instead of a |FileStream|. | 176 // This version creates a |MockFileStream| instead of a |FileStream|. |
| 179 virtual void CreateFileStream() OVERRIDE; | 177 virtual void CreateFileStream() OVERRIDE; |
| 180 }; | 178 }; |
| 181 | 179 |
| 182 DownloadFileWithMockStream::DownloadFileWithMockStream( | 180 DownloadFileWithMockStream::DownloadFileWithMockStream( |
| 183 DownloadCreateInfo* info, | 181 DownloadCreateInfo* info, |
| 184 DownloadManager* manager, | 182 DownloadManagerInterface* manager, |
| 185 net::testing::MockFileStream* stream) | 183 net::testing::MockFileStream* stream) |
| 186 : DownloadFile(info, DownloadRequestHandle(), manager) { | 184 : DownloadFile(info, DownloadRequestHandle(), manager) { |
| 187 DCHECK(file_stream_ == NULL); | 185 DCHECK(file_stream_ == NULL); |
| 188 file_stream_.reset(stream); | 186 file_stream_.reset(stream); |
| 189 } | 187 } |
| 190 | 188 |
| 191 void DownloadFileWithMockStream::SetForcedError(int error) | 189 void DownloadFileWithMockStream::SetForcedError(int error) |
| 192 { | 190 { |
| 193 // |file_stream_| can only be set in the constructor and in | 191 // |file_stream_| can only be set in the constructor and in |
| 194 // CreateFileStream(), both of which insure that it is a |MockFileStream|. | 192 // CreateFileStream(), both of which insure that it is a |MockFileStream|. |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 273 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, }, | 271 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, }, |
| 274 // Dangerous download, download finishes AFTER file name determined. | 272 // Dangerous download, download finishes AFTER file name determined. |
| 275 // Needs to be renamed only once. | 273 // Needs to be renamed only once. |
| 276 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, | 274 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, |
| 277 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, | 275 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, |
| 278 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, | 276 { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, |
| 279 }; | 277 }; |
| 280 | 278 |
| 281 class MockDownloadFile : public DownloadFile { | 279 class MockDownloadFile : public DownloadFile { |
| 282 public: | 280 public: |
| 283 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager) | 281 MockDownloadFile(DownloadCreateInfo* info, DownloadManagerInterface* manager) |
| 284 : DownloadFile(info, DownloadRequestHandle(), manager), | 282 : DownloadFile(info, DownloadRequestHandle(), manager), |
| 285 renamed_count_(0) { } | 283 renamed_count_(0) { } |
| 286 virtual ~MockDownloadFile() { Destructed(); } | 284 virtual ~MockDownloadFile() { Destructed(); } |
| 287 MOCK_METHOD1(Rename, net::Error(const FilePath&)); | 285 MOCK_METHOD1(Rename, net::Error(const FilePath&)); |
| 288 MOCK_METHOD0(Destructed, void()); | 286 MOCK_METHOD0(Destructed, void()); |
| 289 | 287 |
| 290 net::Error TestMultipleRename( | 288 net::Error TestMultipleRename( |
| 291 int expected_count, const FilePath& expected, | 289 int expected_count, const FilePath& expected, |
| 292 const FilePath& path) { | 290 const FilePath& path) { |
| 293 ++renamed_count_; | 291 ++renamed_count_; |
| 294 EXPECT_EQ(expected_count, renamed_count_); | 292 EXPECT_EQ(expected_count, renamed_count_); |
| 295 EXPECT_EQ(expected.value(), path.value()); | 293 EXPECT_EQ(expected.value(), path.value()); |
| 296 return net::OK; | 294 return net::OK; |
| 297 } | 295 } |
| 298 | 296 |
| 299 private: | 297 private: |
| 300 int renamed_count_; | 298 int renamed_count_; |
| 301 }; | 299 }; |
| 302 | 300 |
| 303 // This is an observer that records what download IDs have opened a select | 301 // This is an observer that records what download IDs have opened a select |
| 304 // file dialog. | 302 // file dialog. |
| 305 class SelectFileObserver : public DownloadManager::Observer { | 303 class SelectFileObserver : public DownloadManager::Observer { |
| 306 public: | 304 public: |
| 307 explicit SelectFileObserver(DownloadManager* download_manager) | 305 explicit SelectFileObserver(DownloadManagerInterface* download_manager) |
| 308 : download_manager_(download_manager) { | 306 : download_manager_(download_manager) { |
| 309 DCHECK(download_manager_.get()); | 307 DCHECK(download_manager_.get()); |
| 310 download_manager_->AddObserver(this); | 308 download_manager_->AddObserver(this); |
| 311 } | 309 } |
| 312 | 310 |
| 313 ~SelectFileObserver() { | 311 ~SelectFileObserver() { |
| 314 download_manager_->RemoveObserver(this); | 312 download_manager_->RemoveObserver(this); |
| 315 } | 313 } |
| 316 | 314 |
| 317 // Downloadmanager::Observer functions. | 315 // Downloadmanager::Observer functions. |
| 318 virtual void ModelChanged() {} | 316 virtual void ModelChanged() {} |
| 319 virtual void ManagerGoingDown() {} | 317 virtual void ManagerGoingDown() {} |
| 320 virtual void SelectFileDialogDisplayed(int32 id) { | 318 virtual void SelectFileDialogDisplayed(int32 id) { |
| 321 file_dialog_ids_.insert(id); | 319 file_dialog_ids_.insert(id); |
| 322 } | 320 } |
| 323 | 321 |
| 324 bool ShowedFileDialogForId(int32 id) { | 322 bool ShowedFileDialogForId(int32 id) { |
| 325 return file_dialog_ids_.find(id) != file_dialog_ids_.end(); | 323 return file_dialog_ids_.find(id) != file_dialog_ids_.end(); |
| 326 } | 324 } |
| 327 | 325 |
| 328 private: | 326 private: |
| 329 std::set<int32> file_dialog_ids_; | 327 std::set<int32> file_dialog_ids_; |
| 330 scoped_refptr<DownloadManager> download_manager_; | 328 scoped_refptr<DownloadManagerInterface> download_manager_; |
| 331 }; | 329 }; |
| 332 | 330 |
| 333 // This observer tracks the progress of |DownloadItem|s. | 331 // This observer tracks the progress of |DownloadItem|s. |
| 334 class ItemObserver : public DownloadItem::Observer { | 332 class ItemObserver : public DownloadItem::Observer { |
| 335 public: | 333 public: |
| 336 explicit ItemObserver(DownloadItem* tracked) | 334 explicit ItemObserver(DownloadItem* tracked) |
| 337 : tracked_(tracked), states_hit_(0), | 335 : tracked_(tracked), states_hit_(0), |
| 338 was_updated_(false), was_opened_(false) { | 336 was_updated_(false), was_opened_(false) { |
| 339 DCHECK(tracked_); | 337 DCHECK(tracked_); |
| 340 tracked_->AddObserver(this); | 338 tracked_->AddObserver(this); |
| (...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 880 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); | 878 EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 881 EXPECT_TRUE(observer->was_updated()); | 879 EXPECT_TRUE(observer->was_updated()); |
| 882 EXPECT_FALSE(observer->was_opened()); | 880 EXPECT_FALSE(observer->was_opened()); |
| 883 EXPECT_TRUE(download->file_externally_removed()); | 881 EXPECT_TRUE(download->file_externally_removed()); |
| 884 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); | 882 EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
| 885 EXPECT_EQ(download_item_model->GetStatusText(), | 883 EXPECT_EQ(download_item_model->GetStatusText(), |
| 886 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); | 884 l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); |
| 887 | 885 |
| 888 EXPECT_FALSE(file_util::PathExists(new_path)); | 886 EXPECT_FALSE(file_util::PathExists(new_path)); |
| 889 } | 887 } |
| OLD | NEW |