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