[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 5 | #include <set> |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 6 | #include <string> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | |
[email protected] | a309407 | 2011-10-10 17:14:05 | [diff] [blame] | 8 | #include "base/bind.h" |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 9 | #include "base/file_util.h" |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 10 | #include "base/i18n/number_formatting.h" |
| 11 | #include "base/i18n/rtl.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 13 | #include "base/stl_util.h" |
[email protected] | c2475df | 2011-09-07 23:52:22 | [diff] [blame] | 14 | #include "base/string16.h" |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 15 | #include "base/string_util.h" |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 16 | #include "base/utf_string_conversions.h" |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 17 | #include "build/build_config.h" |
[email protected] | aa9881c | 2011-08-15 18:01:12 | [diff] [blame] | 18 | #include "chrome/browser/download/chrome_download_manager_delegate.h" |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 19 | #include "chrome/browser/download/download_item_model.h" |
[email protected] | e5dc422 | 2010-08-30 22:16:32 | [diff] [blame] | 20 | #include "chrome/browser/download/download_prefs.h" |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 21 | #include "chrome/browser/download/download_util.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 22 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 23 | #include "chrome/common/pref_names.h" |
[email protected] | a4ff9eae | 2011-08-01 19:58:16 | [diff] [blame] | 24 | #include "chrome/test/base/testing_profile.h" |
[email protected] | 2606e5a9 | 2011-10-21 00:09:52 | [diff] [blame] | 25 | #include "content/browser/download/download_buffer.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 26 | #include "content/browser/download/download_create_info.h" |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 27 | #include "content/browser/download/download_file_impl.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 28 | #include "content/browser/download/download_file_manager.h" |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 29 | #include "content/browser/download/download_id_factory.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 30 | #include "content/browser/download/download_item.h" |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame^] | 31 | #include "content/browser/download/download_manager_impl.h" |
[email protected] | 594e66fe | 2011-10-25 22:49:41 | [diff] [blame] | 32 | #include "content/browser/download/download_request_handle.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 33 | #include "content/browser/download/download_status_updater.h" |
[email protected] | be76b7e | 2011-10-13 12:57:57 | [diff] [blame] | 34 | #include "content/browser/download/interrupt_reasons.h" |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 35 | #include "content/browser/download/mock_download_file.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 36 | #include "content/browser/download/mock_download_manager.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 37 | #include "content/test/test_browser_thread.h" |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 38 | #include "grit/generated_resources.h" |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 39 | #include "net/base/io_buffer.h" |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 40 | #include "testing/gmock/include/gmock/gmock.h" |
| 41 | #include "testing/gmock_mutant.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 43 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 7a3b263 | 2011-06-22 20:40:22 | [diff] [blame] | 44 | #include "ui/base/text/bytes_formatting.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 46 | using content::BrowserThread; |
| 47 | |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 48 | DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain"; |
| 49 | |
[email protected] | 583844c | 2011-08-27 00:38:35 | [diff] [blame] | 50 | class DownloadManagerTest : public testing::Test { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | public: |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 52 | static const char* kTestData; |
| 53 | static const size_t kTestDataLen; |
| 54 | |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 55 | DownloadManagerTest() |
| 56 | : profile_(new TestingProfile()), |
[email protected] | e7557f17 | 2011-08-19 23:42:01 | [diff] [blame] | 57 | download_manager_delegate_(new ChromeDownloadManagerDelegate( |
| 58 | profile_.get())), |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 59 | id_factory_(new DownloadIdFactory(kValidIdDomain)), |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame^] | 60 | download_manager_(new DownloadManagerImpl( |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 61 | download_manager_delegate_, |
| 62 | id_factory_, |
| 63 | &download_status_updater_)), |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 64 | ui_thread_(BrowserThread::UI, &message_loop_), |
[email protected] | 2606e5a9 | 2011-10-21 00:09:52 | [diff] [blame] | 65 | file_thread_(BrowserThread::FILE, &message_loop_), |
| 66 | download_buffer_(new content::DownloadBuffer) { |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 67 | download_manager_->Init(profile_.get()); |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 68 | download_manager_delegate_->SetDownloadManager(download_manager_); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | ~DownloadManagerTest() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 72 | download_manager_->Shutdown(); |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 73 | // profile_ must outlive download_manager_, so we explicitly delete |
| 74 | // download_manager_ first. |
[email protected] | 1b2f8da | 2010-07-22 08:29:29 | [diff] [blame] | 75 | download_manager_ = NULL; |
[email protected] | e7557f17 | 2011-08-19 23:42:01 | [diff] [blame] | 76 | download_manager_delegate_ = NULL; |
[email protected] | 1b2f8da | 2010-07-22 08:29:29 | [diff] [blame] | 77 | profile_.reset(NULL); |
| 78 | message_loop_.RunAllPending(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | } |
| 80 | |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 81 | void AddDownloadToFileManager(int id, DownloadFile* download_file) { |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 82 | file_manager()->downloads_[DownloadId(kValidIdDomain, id)] = |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 83 | download_file; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 86 | void OnResponseCompleted(int32 download_id, int64 size, |
| 87 | const std::string& hash) { |
| 88 | download_manager_->OnResponseCompleted(download_id, size, hash); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 89 | } |
| 90 | |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 91 | void FileSelected(const FilePath& path, void* params) { |
| 92 | download_manager_->FileSelected(path, params); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 95 | void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) { |
| 96 | download_manager_->ContinueDownloadWithPath(download, path); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 99 | void UpdateData(int32 id, const char* data, size_t length) { |
| 100 | // We are passing ownership of this buffer to the download file manager. |
| 101 | net::IOBuffer* io_buffer = new net::IOBuffer(length); |
| 102 | // We need |AddRef()| because we do a |Release()| in |UpdateDownload()|. |
| 103 | io_buffer->AddRef(); |
| 104 | memcpy(io_buffer->data(), data, length); |
| 105 | |
[email protected] | 2606e5a9 | 2011-10-21 00:09:52 | [diff] [blame] | 106 | download_buffer_->AddData(io_buffer, length); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 107 | |
| 108 | BrowserThread::PostTask( |
| 109 | BrowserThread::FILE, FROM_HERE, |
[email protected] | a309407 | 2011-10-10 17:14:05 | [diff] [blame] | 110 | base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(), |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 111 | DownloadId(kValidIdDomain, id), download_buffer_)); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 112 | |
| 113 | message_loop_.RunAllPending(); |
| 114 | } |
| 115 | |
[email protected] | be76b7e | 2011-10-13 12:57:57 | [diff] [blame] | 116 | void OnDownloadInterrupted(int32 download_id, int64 size, |
| 117 | InterruptReason reason) { |
| 118 | download_manager_->OnDownloadInterrupted(download_id, size, reason); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Get the download item with ID |id|. |
| 122 | DownloadItem* GetActiveDownloadItem(int32 id) { |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame^] | 123 | return download_manager_->GetActiveDownload(id); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 124 | } |
| 125 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | protected: |
[email protected] | a3d2bc4 | 2010-10-06 14:08:49 | [diff] [blame] | 127 | DownloadStatusUpdater download_status_updater_; |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 128 | scoped_ptr<TestingProfile> profile_; |
[email protected] | aa9881c | 2011-08-15 18:01:12 | [diff] [blame] | 129 | scoped_refptr<ChromeDownloadManagerDelegate> download_manager_delegate_; |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 130 | scoped_refptr<DownloadIdFactory> id_factory_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | scoped_refptr<DownloadManager> download_manager_; |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 132 | scoped_refptr<DownloadFileManager> file_manager_; |
[email protected] | dd3eac2 | 2008-08-26 07:28:34 | [diff] [blame] | 133 | MessageLoopForUI message_loop_; |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 134 | content::TestBrowserThread ui_thread_; |
| 135 | content::TestBrowserThread file_thread_; |
[email protected] | 2606e5a9 | 2011-10-21 00:09:52 | [diff] [blame] | 136 | scoped_refptr<content::DownloadBuffer> download_buffer_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 138 | DownloadFileManager* file_manager() { |
| 139 | if (!file_manager_) { |
| 140 | file_manager_ = new DownloadFileManager(NULL); |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame^] | 141 | download_manager_->SetFileManager(file_manager_); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 142 | } |
| 143 | return file_manager_; |
| 144 | } |
| 145 | |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 146 | // Make sure download item |id| was set with correct safety state for |
| 147 | // given |is_dangerous_file| and |is_dangerous_url|. |
| 148 | bool VerifySafetyState(bool is_dangerous_file, |
| 149 | bool is_dangerous_url, |
| 150 | int id) { |
| 151 | DownloadItem::SafetyState safety_state = |
| 152 | download_manager_->GetDownloadItem(id)->safety_state(); |
| 153 | return (is_dangerous_file || is_dangerous_url) ? |
| 154 | safety_state != DownloadItem::SAFE : safety_state == DownloadItem::SAFE; |
| 155 | } |
| 156 | |
[email protected] | 4d818fee | 2010-06-06 13:32:27 | [diff] [blame] | 157 | DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | }; |
| 159 | |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 160 | const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad"; |
| 161 | const size_t DownloadManagerTest::kTestDataLen = |
| 162 | strlen(DownloadManagerTest::kTestData); |
| 163 | |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 164 | // A DownloadFile that we can inject errors into. |
| 165 | class DownloadFileWithErrors : public DownloadFileImpl { |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 166 | public: |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 167 | DownloadFileWithErrors(DownloadCreateInfo* info, DownloadManager* manager); |
| 168 | virtual ~DownloadFileWithErrors() {} |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 169 | |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 170 | // BaseFile delegated functions. |
| 171 | virtual net::Error Initialize(bool calculate_hash); |
| 172 | virtual net::Error AppendDataToFile(const char* data, size_t data_len); |
| 173 | virtual net::Error Rename(const FilePath& full_path); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 174 | |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 175 | void set_forced_error(net::Error error) { forced_error_ = error; } |
| 176 | void clear_forced_error() { forced_error_ = net::OK; } |
| 177 | net::Error forced_error() const { return forced_error_; } |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 178 | |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 179 | private: |
| 180 | net::Error ReturnError(net::Error function_error) { |
| 181 | if (forced_error_ != net::OK) { |
| 182 | net::Error ret = forced_error_; |
| 183 | clear_forced_error(); |
| 184 | return ret; |
| 185 | } |
| 186 | |
| 187 | return function_error; |
| 188 | } |
| 189 | |
| 190 | net::Error forced_error_; |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 191 | }; |
| 192 | |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 193 | DownloadFileWithErrors::DownloadFileWithErrors(DownloadCreateInfo* info, |
| 194 | DownloadManager* manager) |
| 195 | : DownloadFileImpl(info, new DownloadRequestHandle(), manager), |
| 196 | forced_error_(net::OK) { |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 197 | } |
| 198 | |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 199 | net::Error DownloadFileWithErrors::Initialize(bool calculate_hash) { |
| 200 | return ReturnError(DownloadFileImpl::Initialize(calculate_hash)); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 201 | } |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 202 | |
| 203 | net::Error DownloadFileWithErrors::AppendDataToFile(const char* data, |
| 204 | size_t data_len) { |
| 205 | return ReturnError(DownloadFileImpl::AppendDataToFile(data, data_len)); |
| 206 | } |
| 207 | |
| 208 | net::Error DownloadFileWithErrors::Rename(const FilePath& full_path) { |
| 209 | return ReturnError(DownloadFileImpl::Rename(full_path)); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 210 | } |
| 211 | |
[email protected] | 763f946a | 2009-01-06 19:04:39 | [diff] [blame] | 212 | namespace { |
| 213 | |
| 214 | const struct { |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 215 | const char* url; |
| 216 | const char* mime_type; |
| 217 | bool save_as; |
| 218 | bool prompt_for_download; |
| 219 | bool expected_save_as; |
| 220 | } kStartDownloadCases[] = { |
| 221 | { "http://www.foo.com/dont-open.html", |
| 222 | "text/html", |
| 223 | false, |
| 224 | false, |
| 225 | false, }, |
| 226 | { "http://www.foo.com/save-as.html", |
| 227 | "text/html", |
| 228 | true, |
| 229 | false, |
| 230 | true, }, |
| 231 | { "http://www.foo.com/always-prompt.html", |
| 232 | "text/html", |
| 233 | false, |
| 234 | true, |
| 235 | true, }, |
[email protected] | bac4f4b | 2011-03-05 02:01:40 | [diff] [blame] | 236 | { "http://www.foo.com/user-script-text-html-mimetype.user.js", |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 237 | "text/html", |
| 238 | false, |
[email protected] | bac4f4b | 2011-03-05 02:01:40 | [diff] [blame] | 239 | false, |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 240 | false, }, |
| 241 | { "http://www.foo.com/extensionless-extension", |
| 242 | "application/x-chrome-extension", |
| 243 | true, |
| 244 | false, |
| 245 | true, }, |
| 246 | { "http://www.foo.com/save-as.pdf", |
| 247 | "application/pdf", |
| 248 | true, |
| 249 | false, |
| 250 | true, }, |
[email protected] | 2fc9385 | 2011-01-08 22:13:56 | [diff] [blame] | 251 | { "http://www.foo.com/sometimes_prompt.pdf", |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 252 | "application/pdf", |
| 253 | false, |
| 254 | true, |
[email protected] | 2fc9385 | 2011-01-08 22:13:56 | [diff] [blame] | 255 | false, }, |
| 256 | { "http://www.foo.com/always_prompt.jar", |
| 257 | "application/jar", |
| 258 | false, |
| 259 | true, |
[email protected] | 3366aca6 | 2010-10-20 01:18:24 | [diff] [blame] | 260 | true, }, |
[email protected] | 2941c239 | 2010-07-15 22:54:30 | [diff] [blame] | 261 | }; |
| 262 | |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 263 | const struct { |
| 264 | FilePath::StringType suggested_path; |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 265 | bool is_dangerous_file; |
| 266 | bool is_dangerous_url; |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 267 | bool finish_before_rename; |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 268 | int expected_rename_count; |
| 269 | } kDownloadRenameCases[] = { |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 270 | // Safe download, download finishes BEFORE file name determined. |
| 271 | // Renamed twice (linear path through UI). Crdownload file does not need |
| 272 | // to be deleted. |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 273 | { FILE_PATH_LITERAL("foo.zip"), false, false, true, 2, }, |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 274 | // Dangerous download (file is dangerous or download URL is not safe or both), |
| 275 | // download finishes BEFORE file name determined. Needs to be renamed only |
| 276 | // once. |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 277 | { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, true, 1, }, |
| 278 | { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, true, 1, }, |
| 279 | { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, true, 1, }, |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 280 | // Safe download, download finishes AFTER file name determined. |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 281 | // Needs to be renamed twice. |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 282 | { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, }, |
[email protected] | adb2f3d1 | 2011-01-23 16:24:54 | [diff] [blame] | 283 | // Dangerous download, download finishes AFTER file name determined. |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 284 | // Needs to be renamed only once. |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 285 | { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, false, false, 1, }, |
| 286 | { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), false, true, false, 1, }, |
| 287 | { FILE_PATH_LITERAL("Unconfirmed xxx.crdownload"), true, true, false, 1, }, |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 288 | }; |
| 289 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 290 | // This is an observer that records what download IDs have opened a select |
| 291 | // file dialog. |
| 292 | class SelectFileObserver : public DownloadManager::Observer { |
| 293 | public: |
| 294 | explicit SelectFileObserver(DownloadManager* download_manager) |
| 295 | : download_manager_(download_manager) { |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 296 | DCHECK(download_manager_.get()); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 297 | download_manager_->AddObserver(this); |
| 298 | } |
| 299 | |
| 300 | ~SelectFileObserver() { |
| 301 | download_manager_->RemoveObserver(this); |
| 302 | } |
| 303 | |
| 304 | // Downloadmanager::Observer functions. |
| 305 | virtual void ModelChanged() {} |
| 306 | virtual void ManagerGoingDown() {} |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 307 | virtual void SelectFileDialogDisplayed(int32 id) { |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 308 | file_dialog_ids_.insert(id); |
| 309 | } |
| 310 | |
| 311 | bool ShowedFileDialogForId(int32 id) { |
| 312 | return file_dialog_ids_.find(id) != file_dialog_ids_.end(); |
| 313 | } |
| 314 | |
| 315 | private: |
| 316 | std::set<int32> file_dialog_ids_; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 317 | scoped_refptr<DownloadManager> download_manager_; |
| 318 | }; |
| 319 | |
| 320 | // This observer tracks the progress of |DownloadItem|s. |
| 321 | class ItemObserver : public DownloadItem::Observer { |
| 322 | public: |
| 323 | explicit ItemObserver(DownloadItem* tracked) |
| 324 | : tracked_(tracked), states_hit_(0), |
[email protected] | 4883796 | 2011-04-19 17:03:29 | [diff] [blame] | 325 | was_updated_(false), was_opened_(false) { |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 326 | DCHECK(tracked_); |
| 327 | tracked_->AddObserver(this); |
| 328 | // Record the initial state. |
| 329 | OnDownloadUpdated(tracked_); |
| 330 | } |
| 331 | ~ItemObserver() { |
| 332 | tracked_->RemoveObserver(this); |
| 333 | } |
| 334 | |
| 335 | bool hit_state(int state) const { |
| 336 | return (1 << state) & states_hit_; |
| 337 | } |
| 338 | bool was_updated() const { return was_updated_; } |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 339 | bool was_opened() const { return was_opened_; } |
| 340 | |
| 341 | private: |
| 342 | // DownloadItem::Observer methods |
| 343 | virtual void OnDownloadUpdated(DownloadItem* download) { |
| 344 | DCHECK_EQ(tracked_, download); |
| 345 | states_hit_ |= (1 << download->state()); |
| 346 | was_updated_ = true; |
| 347 | } |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 348 | virtual void OnDownloadOpened(DownloadItem* download) { |
| 349 | DCHECK_EQ(tracked_, download); |
| 350 | states_hit_ |= (1 << download->state()); |
| 351 | was_opened_ = true; |
| 352 | } |
| 353 | |
| 354 | DownloadItem* tracked_; |
| 355 | int states_hit_; |
| 356 | bool was_updated_; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 357 | bool was_opened_; |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 358 | }; |
| 359 | |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 360 | } // namespace |
| 361 | |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 362 | TEST_F(DownloadManagerTest, StartDownload) { |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 363 | content::TestBrowserThread io_thread(BrowserThread::IO, &message_loop_); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 364 | PrefService* prefs = profile_->GetPrefs(); |
[email protected] | fed3825 | 2011-07-08 17:26:50 | [diff] [blame] | 365 | prefs->SetFilePath(prefs::kDownloadDefaultDirectory, FilePath()); |
[email protected] | e7557f17 | 2011-08-19 23:42:01 | [diff] [blame] | 366 | DownloadPrefs* download_prefs = |
| 367 | DownloadPrefs::FromDownloadManager(download_manager_); |
| 368 | download_prefs->EnableAutoOpenBasedOnExtension( |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 369 | FilePath(FILE_PATH_LITERAL("example.pdf"))); |
| 370 | |
| 371 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kStartDownloadCases); ++i) { |
| 372 | prefs->SetBoolean(prefs::kPromptForDownload, |
| 373 | kStartDownloadCases[i].prompt_for_download); |
| 374 | |
| 375 | SelectFileObserver observer(download_manager_); |
[email protected] | c9994e0 | 2011-05-24 20:52:19 | [diff] [blame] | 376 | // Normally, the download system takes ownership of info, and is |
| 377 | // responsible for deleting it. In these unit tests, however, we |
| 378 | // don't call the function that deletes it, so we do so ourselves. |
| 379 | scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 380 | info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 381 | info->prompt_user_for_save_location = kStartDownloadCases[i].save_as; |
[email protected] | 8799e54 | 2011-04-20 03:47:34 | [diff] [blame] | 382 | info->url_chain.push_back(GURL(kStartDownloadCases[i].url)); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 383 | info->mime_type = kStartDownloadCases[i].mime_type; |
[email protected] | 594e66fe | 2011-10-25 22:49:41 | [diff] [blame] | 384 | download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 385 | |
[email protected] | c9994e0 | 2011-05-24 20:52:19 | [diff] [blame] | 386 | DownloadFile* download_file( |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 387 | new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 388 | download_manager_)); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 389 | AddDownloadToFileManager(info->download_id.local(), download_file); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 390 | download_file->Initialize(false); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 391 | download_manager_->StartDownload(info->download_id.local()); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 392 | message_loop_.RunAllPending(); |
| 393 | |
[email protected] | c9994e0 | 2011-05-24 20:52:19 | [diff] [blame] | 394 | // SelectFileObserver will have recorded any attempt to open the |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 395 | // select file dialog. |
[email protected] | c9994e0 | 2011-05-24 20:52:19 | [diff] [blame] | 396 | // Note that DownloadManager::FileSelectionCanceled() is never called. |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 397 | EXPECT_EQ(kStartDownloadCases[i].expected_save_as, |
| 398 | observer.ShowedFileDialogForId(i)); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 402 | TEST_F(DownloadManagerTest, DownloadRenameTest) { |
| 403 | using ::testing::_; |
| 404 | using ::testing::CreateFunctor; |
| 405 | using ::testing::Invoke; |
| 406 | using ::testing::Return; |
| 407 | |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 408 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) { |
[email protected] | c9994e0 | 2011-05-24 20:52:19 | [diff] [blame] | 409 | // Normally, the download system takes ownership of info, and is |
| 410 | // responsible for deleting it. In these unit tests, however, we |
| 411 | // don't call the function that deletes it, so we do so ourselves. |
| 412 | scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 413 | info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i)); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 414 | info->prompt_user_for_save_location = false; |
[email protected] | 30917cb | 2011-05-10 21:40:47 | [diff] [blame] | 415 | info->url_chain.push_back(GURL()); |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 416 | const FilePath new_path(kDownloadRenameCases[i].suggested_path); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 417 | |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 418 | MockDownloadFile::StatisticsRecorder recorder; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 419 | MockDownloadFile* download_file( |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 420 | new MockDownloadFile(info.get(), |
| 421 | DownloadRequestHandle(), |
| 422 | download_manager_, |
| 423 | &recorder)); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 424 | AddDownloadToFileManager(info->download_id.local(), download_file); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 425 | |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 426 | // |download_file| is owned by DownloadFileManager. |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 427 | if (kDownloadRenameCases[i].expected_rename_count == 1) { |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 428 | download_file->SetExpectedPath(0, new_path); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 429 | } else { |
| 430 | ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count); |
| 431 | FilePath crdownload(download_util::GetCrDownloadPath(new_path)); |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 432 | download_file->SetExpectedPath(0, crdownload); |
| 433 | download_file->SetExpectedPath(1, new_path); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 434 | } |
[email protected] | 594e66fe | 2011-10-25 22:49:41 | [diff] [blame] | 435 | download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
[email protected] | 8800800 | 2011-05-24 23:14:15 | [diff] [blame] | 436 | DownloadItem* download = GetActiveDownloadItem(i); |
| 437 | ASSERT_TRUE(download != NULL); |
| 438 | if (kDownloadRenameCases[i].is_dangerous_file) |
| 439 | download->MarkFileDangerous(); |
| 440 | if (kDownloadRenameCases[i].is_dangerous_url) |
| 441 | download->MarkUrlDangerous(); |
[email protected] | c2e7601 | 2010-12-23 21:10:29 | [diff] [blame] | 442 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 443 | int32* id_ptr = new int32; |
| 444 | *id_ptr = i; // Deleted in FileSelected(). |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 445 | if (kDownloadRenameCases[i].finish_before_rename) { |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 446 | OnResponseCompleted(i, 1024, std::string("fake_hash")); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 447 | message_loop_.RunAllPending(); |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 448 | FileSelected(new_path, id_ptr); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 449 | } else { |
[email protected] | 99cb7f8 | 2011-07-28 17:27:26 | [diff] [blame] | 450 | FileSelected(new_path, id_ptr); |
[email protected] | f592032 | 2011-03-24 20:34:16 | [diff] [blame] | 451 | message_loop_.RunAllPending(); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 452 | OnResponseCompleted(i, 1024, std::string("fake_hash")); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | message_loop_.RunAllPending(); |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 456 | EXPECT_EQ( |
| 457 | kDownloadRenameCases[i].expected_rename_count, |
| 458 | recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 459 | EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file, |
| 460 | kDownloadRenameCases[i].is_dangerous_url, |
| 461 | i)); |
[email protected] | 594cd7d | 2010-07-21 03:23:56 | [diff] [blame] | 462 | } |
| 463 | } |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 464 | |
| 465 | TEST_F(DownloadManagerTest, DownloadInterruptTest) { |
| 466 | using ::testing::_; |
| 467 | using ::testing::CreateFunctor; |
| 468 | using ::testing::Invoke; |
| 469 | using ::testing::Return; |
| 470 | |
[email protected] | c9994e0 | 2011-05-24 20:52:19 | [diff] [blame] | 471 | // Normally, the download system takes ownership of info, and is |
| 472 | // responsible for deleting it. In these unit tests, however, we |
| 473 | // don't call the function that deletes it, so we do so ourselves. |
| 474 | scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 475 | info->download_id = DownloadId(kValidIdDomain, 0); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 476 | info->prompt_user_for_save_location = false; |
[email protected] | 30917cb | 2011-05-10 21:40:47 | [diff] [blame] | 477 | info->url_chain.push_back(GURL()); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 478 | info->total_bytes = static_cast<int64>(kTestDataLen); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 479 | const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
| 480 | const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
| 481 | |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 482 | MockDownloadFile::StatisticsRecorder recorder; |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 483 | MockDownloadFile* download_file( |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 484 | new MockDownloadFile(info.get(), |
| 485 | DownloadRequestHandle(), |
| 486 | download_manager_, |
| 487 | &recorder)); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 488 | AddDownloadToFileManager(info->download_id.local(), download_file); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 489 | |
| 490 | // |download_file| is owned by DownloadFileManager. |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 491 | download_file->SetExpectedPath(0, cr_path); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 492 | |
[email protected] | 594e66fe | 2011-10-25 22:49:41 | [diff] [blame] | 493 | download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 494 | |
| 495 | DownloadItem* download = GetActiveDownloadItem(0); |
| 496 | ASSERT_TRUE(download != NULL); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 497 | scoped_ptr<DownloadItemModel> download_item_model( |
| 498 | new DownloadItemModel(download)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 499 | |
| 500 | EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
[email protected] | d8ca4d1 | 2011-04-07 22:05:13 | [diff] [blame] | 501 | scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 502 | |
| 503 | download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 504 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 505 | ContinueDownloadWithPath(download, new_path); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 506 | message_loop_.RunAllPending(); |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 507 | EXPECT_EQ(1, |
| 508 | recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 509 | EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 510 | |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 511 | int64 error_size = 3; |
[email protected] | be76b7e | 2011-10-13 12:57:57 | [diff] [blame] | 512 | OnDownloadInterrupted(0, error_size, |
| 513 | DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 514 | message_loop_.RunAllPending(); |
| 515 | |
| 516 | EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 517 | EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 518 | EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 519 | EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
| 520 | EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 521 | EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 522 | EXPECT_TRUE(observer->was_updated()); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 523 | EXPECT_FALSE(observer->was_opened()); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 524 | EXPECT_FALSE(download->file_externally_removed()); |
| 525 | EXPECT_EQ(DownloadItem::INTERRUPTED, download->state()); |
[email protected] | 7a3b263 | 2011-06-22 20:40:22 | [diff] [blame] | 526 | ui::DataUnits amount_units = ui::GetByteDisplayUnits(kTestDataLen); |
| 527 | string16 simple_size = |
| 528 | ui::FormatBytesWithUnits(error_size, amount_units, false); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 529 | string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality( |
[email protected] | 7a3b263 | 2011-06-22 20:40:22 | [diff] [blame] | 530 | ui::FormatBytesWithUnits(kTestDataLen, amount_units, true)); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 531 | EXPECT_EQ(download_item_model->GetStatusText(), |
| 532 | l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED, |
| 533 | simple_size, |
| 534 | simple_total)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 535 | |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 536 | download->Cancel(true); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 537 | |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 538 | EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 539 | EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 540 | EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
| 541 | EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 542 | EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 543 | EXPECT_TRUE(observer->was_updated()); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 544 | EXPECT_FALSE(observer->was_opened()); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 545 | EXPECT_FALSE(download->file_externally_removed()); |
| 546 | EXPECT_EQ(DownloadItem::INTERRUPTED, download->state()); |
| 547 | EXPECT_EQ(download->received_bytes(), error_size); |
| 548 | EXPECT_EQ(download->total_bytes(), static_cast<int64>(kTestDataLen)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 549 | } |
| 550 | |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 551 | // Test the behavior of DownloadFileManager and DownloadManager in the event |
| 552 | // of a file error while writing the download to disk. |
| 553 | TEST_F(DownloadManagerTest, DownloadFileErrorTest) { |
| 554 | // Create a temporary file and a mock stream. |
| 555 | FilePath path; |
| 556 | ASSERT_TRUE(file_util::CreateTemporaryFile(&path)); |
| 557 | |
| 558 | // This file stream will be used, until the first rename occurs. |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 559 | net::FileStream* stream = new net::FileStream; |
| 560 | ASSERT_EQ(0, stream->Open( |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 561 | path, |
| 562 | base::PLATFORM_FILE_OPEN_ALWAYS | base::PLATFORM_FILE_WRITE)); |
| 563 | |
| 564 | // Normally, the download system takes ownership of info, and is |
| 565 | // responsible for deleting it. In these unit tests, however, we |
| 566 | // don't call the function that deletes it, so we do so ourselves. |
| 567 | scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 568 | static const int32 local_id = 0; |
| 569 | info->download_id = DownloadId(kValidIdDomain, local_id); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 570 | info->prompt_user_for_save_location = false; |
| 571 | info->url_chain.push_back(GURL()); |
| 572 | info->total_bytes = static_cast<int64>(kTestDataLen * 3); |
| 573 | info->save_info.file_path = path; |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 574 | info->save_info.file_stream.reset(stream); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 575 | |
| 576 | // Create a download file that we can insert errors into. |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 577 | DownloadFileWithErrors* download_file(new DownloadFileWithErrors( |
| 578 | info.get(), download_manager_)); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 579 | AddDownloadToFileManager(local_id, download_file); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 580 | |
| 581 | // |download_file| is owned by DownloadFileManager. |
[email protected] | 594e66fe | 2011-10-25 22:49:41 | [diff] [blame] | 582 | download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 583 | |
| 584 | DownloadItem* download = GetActiveDownloadItem(0); |
| 585 | ASSERT_TRUE(download != NULL); |
| 586 | // This will keep track of what should be displayed on the shelf. |
| 587 | scoped_ptr<DownloadItemModel> download_item_model( |
| 588 | new DownloadItemModel(download)); |
| 589 | |
| 590 | EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
| 591 | scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 592 | |
| 593 | // Add some data before finalizing the file name. |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 594 | UpdateData(local_id, kTestData, kTestDataLen); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 595 | |
| 596 | // Finalize the file name. |
| 597 | ContinueDownloadWithPath(download, path); |
| 598 | message_loop_.RunAllPending(); |
| 599 | EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 600 | |
| 601 | // Add more data. |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 602 | UpdateData(local_id, kTestData, kTestDataLen); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 603 | |
| 604 | // Add more data, but an error occurs. |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 605 | download_file->set_forced_error(net::ERR_FAILED); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 606 | UpdateData(local_id, kTestData, kTestDataLen); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 607 | |
| 608 | // Check the state. The download should have been interrupted. |
| 609 | EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| 610 | EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 611 | EXPECT_TRUE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 612 | EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
| 613 | EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 614 | EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 615 | EXPECT_TRUE(observer->was_updated()); |
| 616 | EXPECT_FALSE(observer->was_opened()); |
| 617 | EXPECT_FALSE(download->file_externally_removed()); |
| 618 | EXPECT_EQ(DownloadItem::INTERRUPTED, download->state()); |
| 619 | |
| 620 | // Check the download shelf's information. |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 621 | size_t error_size = kTestDataLen * 3; |
[email protected] | e5e120f | 2011-09-23 23:08:15 | [diff] [blame] | 622 | size_t total_size = kTestDataLen * 3; |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 623 | ui::DataUnits amount_units = ui::GetByteDisplayUnits(kTestDataLen); |
| 624 | string16 simple_size = |
| 625 | ui::FormatBytesWithUnits(error_size, amount_units, false); |
| 626 | string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality( |
[email protected] | e5e120f | 2011-09-23 23:08:15 | [diff] [blame] | 627 | ui::FormatBytesWithUnits(total_size, amount_units, true)); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 628 | EXPECT_EQ(l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED, |
| 629 | simple_size, |
| 630 | simple_total), |
| 631 | download_item_model->GetStatusText()); |
| 632 | |
| 633 | // Clean up. |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 634 | download->Cancel(true); |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 635 | message_loop_.RunAllPending(); |
| 636 | } |
| 637 | |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 638 | TEST_F(DownloadManagerTest, DownloadCancelTest) { |
| 639 | using ::testing::_; |
| 640 | using ::testing::CreateFunctor; |
| 641 | using ::testing::Invoke; |
| 642 | using ::testing::Return; |
| 643 | |
[email protected] | c9994e0 | 2011-05-24 20:52:19 | [diff] [blame] | 644 | // Normally, the download system takes ownership of info, and is |
| 645 | // responsible for deleting it. In these unit tests, however, we |
| 646 | // don't call the function that deletes it, so we do so ourselves. |
| 647 | scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 648 | info->download_id = DownloadId(kValidIdDomain, 0); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 649 | info->prompt_user_for_save_location = false; |
[email protected] | 30917cb | 2011-05-10 21:40:47 | [diff] [blame] | 650 | info->url_chain.push_back(GURL()); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 651 | const FilePath new_path(FILE_PATH_LITERAL("foo.zip")); |
| 652 | const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
| 653 | |
| 654 | MockDownloadFile* download_file( |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 655 | new MockDownloadFile(info.get(), |
| 656 | DownloadRequestHandle(), |
| 657 | download_manager_, |
| 658 | NULL)); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 659 | AddDownloadToFileManager(info->download_id.local(), download_file); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 660 | |
| 661 | // |download_file| is owned by DownloadFileManager. |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 662 | download_file->SetExpectedPath(0, cr_path); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 663 | |
[email protected] | 594e66fe | 2011-10-25 22:49:41 | [diff] [blame] | 664 | download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 665 | |
| 666 | DownloadItem* download = GetActiveDownloadItem(0); |
| 667 | ASSERT_TRUE(download != NULL); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 668 | scoped_ptr<DownloadItemModel> download_item_model( |
| 669 | new DownloadItemModel(download)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 670 | |
| 671 | EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
[email protected] | d8ca4d1 | 2011-04-07 22:05:13 | [diff] [blame] | 672 | scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 673 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 674 | ContinueDownloadWithPath(download, new_path); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 675 | message_loop_.RunAllPending(); |
| 676 | EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 677 | |
| 678 | download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 679 | |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 680 | download->Cancel(false); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 681 | message_loop_.RunAllPending(); |
| 682 | |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 683 | EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 684 | EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 685 | EXPECT_TRUE(observer->hit_state(DownloadItem::CANCELLED)); |
| 686 | EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 687 | EXPECT_FALSE(observer->hit_state(DownloadItem::COMPLETE)); |
| 688 | EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 689 | EXPECT_TRUE(observer->was_updated()); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 690 | EXPECT_FALSE(observer->was_opened()); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 691 | EXPECT_FALSE(download->file_externally_removed()); |
| 692 | EXPECT_EQ(DownloadItem::CANCELLED, download->state()); |
| 693 | EXPECT_EQ(download_item_model->GetStatusText(), |
| 694 | l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELED)); |
[email protected] | bf68a00b | 2011-04-07 17:28:26 | [diff] [blame] | 695 | |
| 696 | EXPECT_FALSE(file_util::PathExists(new_path)); |
| 697 | EXPECT_FALSE(file_util::PathExists(cr_path)); |
| 698 | } |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 699 | |
| 700 | TEST_F(DownloadManagerTest, DownloadOverwriteTest) { |
| 701 | using ::testing::_; |
| 702 | using ::testing::CreateFunctor; |
| 703 | using ::testing::Invoke; |
| 704 | using ::testing::Return; |
| 705 | |
| 706 | // Create a temporary directory. |
| 707 | ScopedTempDir temp_dir_; |
| 708 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 709 | |
| 710 | // File names we're using. |
| 711 | const FilePath new_path(temp_dir_.path().AppendASCII("foo.txt")); |
| 712 | const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
| 713 | EXPECT_FALSE(file_util::PathExists(new_path)); |
| 714 | |
| 715 | // Create the file that we will overwrite. Will be automatically cleaned |
| 716 | // up when temp_dir_ is destroyed. |
| 717 | FILE* fp = file_util::OpenFile(new_path, "w"); |
| 718 | file_util::CloseFile(fp); |
| 719 | EXPECT_TRUE(file_util::PathExists(new_path)); |
| 720 | |
| 721 | // Construct the unique file name that normally would be created, but |
| 722 | // which we will override. |
[email protected] | ec86526 | 2011-08-23 20:01:48 | [diff] [blame] | 723 | int uniquifier = DownloadFile::GetUniquePathNumber(new_path); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 724 | FilePath unique_new_path = new_path; |
| 725 | EXPECT_NE(0, uniquifier); |
[email protected] | ec86526 | 2011-08-23 20:01:48 | [diff] [blame] | 726 | DownloadFile::AppendNumberToPath(&unique_new_path, uniquifier); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 727 | |
[email protected] | c9994e0 | 2011-05-24 20:52:19 | [diff] [blame] | 728 | // Normally, the download system takes ownership of info, and is |
| 729 | // responsible for deleting it. In these unit tests, however, we |
| 730 | // don't call the function that deletes it, so we do so ourselves. |
| 731 | scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 732 | info->download_id = DownloadId(kValidIdDomain, 0); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 733 | info->prompt_user_for_save_location = true; |
[email protected] | 30917cb | 2011-05-10 21:40:47 | [diff] [blame] | 734 | info->url_chain.push_back(GURL()); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 735 | |
[email protected] | 594e66fe | 2011-10-25 22:49:41 | [diff] [blame] | 736 | download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 737 | |
| 738 | DownloadItem* download = GetActiveDownloadItem(0); |
| 739 | ASSERT_TRUE(download != NULL); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 740 | scoped_ptr<DownloadItemModel> download_item_model( |
| 741 | new DownloadItemModel(download)); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 742 | |
| 743 | EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
| 744 | scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 745 | |
| 746 | // Create and initialize the download file. We're bypassing the first part |
| 747 | // of the download process and skipping to the part after the final file |
| 748 | // name has been chosen, so we need to initialize the download file |
| 749 | // properly. |
| 750 | DownloadFile* download_file( |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 751 | new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 752 | download_manager_)); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 753 | download_file->Rename(cr_path); |
| 754 | // This creates the .crdownload version of the file. |
| 755 | download_file->Initialize(false); |
| 756 | // |download_file| is owned by DownloadFileManager. |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 757 | AddDownloadToFileManager(info->download_id.local(), download_file); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 758 | |
[email protected] | 4cd82f7 | 2011-05-23 19:15:01 | [diff] [blame] | 759 | ContinueDownloadWithPath(download, new_path); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 760 | message_loop_.RunAllPending(); |
| 761 | EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 762 | |
| 763 | download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 764 | |
| 765 | // Finish the download. |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 766 | OnResponseCompleted(0, kTestDataLen, ""); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 767 | message_loop_.RunAllPending(); |
| 768 | |
| 769 | // Download is complete. |
| 770 | EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| 771 | EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 772 | EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 773 | EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 774 | EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); |
| 775 | EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 776 | EXPECT_TRUE(observer->was_updated()); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 777 | EXPECT_FALSE(observer->was_opened()); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 778 | EXPECT_FALSE(download->file_externally_removed()); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 779 | EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 780 | EXPECT_EQ(download_item_model->GetStatusText(), ASCIIToUTF16("")); |
[email protected] | 8fa1eeb5 | 2011-04-13 14:18:02 | [diff] [blame] | 781 | |
| 782 | EXPECT_TRUE(file_util::PathExists(new_path)); |
| 783 | EXPECT_FALSE(file_util::PathExists(cr_path)); |
| 784 | EXPECT_FALSE(file_util::PathExists(unique_new_path)); |
| 785 | std::string file_contents; |
| 786 | EXPECT_TRUE(file_util::ReadFileToString(new_path, &file_contents)); |
| 787 | EXPECT_EQ(std::string(kTestData), file_contents); |
| 788 | } |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 789 | |
| 790 | TEST_F(DownloadManagerTest, DownloadRemoveTest) { |
| 791 | using ::testing::_; |
| 792 | using ::testing::CreateFunctor; |
| 793 | using ::testing::Invoke; |
| 794 | using ::testing::Return; |
| 795 | |
| 796 | // Create a temporary directory. |
| 797 | ScopedTempDir temp_dir_; |
| 798 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 799 | |
| 800 | // File names we're using. |
| 801 | const FilePath new_path(temp_dir_.path().AppendASCII("foo.txt")); |
| 802 | const FilePath cr_path(download_util::GetCrDownloadPath(new_path)); |
| 803 | EXPECT_FALSE(file_util::PathExists(new_path)); |
| 804 | |
| 805 | // Normally, the download system takes ownership of info, and is |
| 806 | // responsible for deleting it. In these unit tests, however, we |
| 807 | // don't call the function that deletes it, so we do so ourselves. |
| 808 | scoped_ptr<DownloadCreateInfo> info(new DownloadCreateInfo); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 809 | info->download_id = DownloadId(kValidIdDomain, 0); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 810 | info->prompt_user_for_save_location = true; |
| 811 | info->url_chain.push_back(GURL()); |
| 812 | |
[email protected] | 594e66fe | 2011-10-25 22:49:41 | [diff] [blame] | 813 | download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle()); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 814 | |
| 815 | DownloadItem* download = GetActiveDownloadItem(0); |
| 816 | ASSERT_TRUE(download != NULL); |
| 817 | scoped_ptr<DownloadItemModel> download_item_model( |
| 818 | new DownloadItemModel(download)); |
| 819 | |
| 820 | EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state()); |
| 821 | scoped_ptr<ItemObserver> observer(new ItemObserver(download)); |
| 822 | |
| 823 | // Create and initialize the download file. We're bypassing the first part |
| 824 | // of the download process and skipping to the part after the final file |
| 825 | // name has been chosen, so we need to initialize the download file |
| 826 | // properly. |
| 827 | DownloadFile* download_file( |
[email protected] | f6c2b531 | 2011-11-16 22:31:39 | [diff] [blame] | 828 | new DownloadFileImpl(info.get(), new DownloadRequestHandle(), |
| 829 | download_manager_)); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 830 | download_file->Rename(cr_path); |
| 831 | // This creates the .crdownload version of the file. |
| 832 | download_file->Initialize(false); |
| 833 | // |download_file| is owned by DownloadFileManager. |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 834 | AddDownloadToFileManager(info->download_id.local(), download_file); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 835 | |
| 836 | ContinueDownloadWithPath(download, new_path); |
| 837 | message_loop_.RunAllPending(); |
| 838 | EXPECT_TRUE(GetActiveDownloadItem(0) != NULL); |
| 839 | |
| 840 | download_file->AppendDataToFile(kTestData, kTestDataLen); |
| 841 | |
| 842 | // Finish the download. |
[email protected] | 47a881b | 2011-08-29 22:59:21 | [diff] [blame] | 843 | OnResponseCompleted(0, kTestDataLen, ""); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 844 | message_loop_.RunAllPending(); |
| 845 | |
| 846 | // Download is complete. |
| 847 | EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| 848 | EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 849 | EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 850 | EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 851 | EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); |
| 852 | EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 853 | EXPECT_TRUE(observer->was_updated()); |
| 854 | EXPECT_FALSE(observer->was_opened()); |
| 855 | EXPECT_FALSE(download->file_externally_removed()); |
| 856 | EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
| 857 | EXPECT_EQ(download_item_model->GetStatusText(), ASCIIToUTF16("")); |
| 858 | |
| 859 | EXPECT_TRUE(file_util::PathExists(new_path)); |
| 860 | EXPECT_FALSE(file_util::PathExists(cr_path)); |
| 861 | |
| 862 | // Remove the downloaded file. |
| 863 | ASSERT_TRUE(file_util::Delete(new_path, false)); |
| 864 | download->OnDownloadedFileRemoved(); |
| 865 | message_loop_.RunAllPending(); |
| 866 | |
| 867 | EXPECT_TRUE(GetActiveDownloadItem(0) == NULL); |
| 868 | EXPECT_TRUE(observer->hit_state(DownloadItem::IN_PROGRESS)); |
| 869 | EXPECT_FALSE(observer->hit_state(DownloadItem::CANCELLED)); |
| 870 | EXPECT_FALSE(observer->hit_state(DownloadItem::INTERRUPTED)); |
| 871 | EXPECT_TRUE(observer->hit_state(DownloadItem::COMPLETE)); |
| 872 | EXPECT_FALSE(observer->hit_state(DownloadItem::REMOVING)); |
| 873 | EXPECT_TRUE(observer->was_updated()); |
| 874 | EXPECT_FALSE(observer->was_opened()); |
| 875 | EXPECT_TRUE(download->file_externally_removed()); |
| 876 | EXPECT_EQ(DownloadItem::COMPLETE, download->state()); |
| 877 | EXPECT_EQ(download_item_model->GetStatusText(), |
| 878 | l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED)); |
| 879 | |
| 880 | EXPECT_FALSE(file_util::PathExists(new_path)); |
| 881 | } |