blob: b99a466a64b4357ba8086d13007c007a57a675fb [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]f5920322011-03-24 20:34:165#include <set>
[email protected]c38831a12011-10-28 12:44:496#include <string>
initial.commit09911bf2008-07-26 23:55:297
[email protected]a3094072011-10-10 17:14:058#include "base/bind.h"
[email protected]bf68a00b2011-04-07 17:28:269#include "base/file_util.h"
[email protected]9fc114672011-06-15 08:17:4810#include "base/i18n/number_formatting.h"
11#include "base/i18n/rtl.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/scoped_ptr.h"
[email protected]7286e3fc2011-07-19 22:13:2413#include "base/stl_util.h"
[email protected]c2475df2011-09-07 23:52:2214#include "base/string16.h"
[email protected]eda58402011-09-21 19:32:0215#include "base/string_util.h"
[email protected]9fc114672011-06-15 08:17:4816#include "base/utf_string_conversions.h"
[email protected]047a03f2009-10-07 02:10:2017#include "build/build_config.h"
[email protected]aa9881c2011-08-15 18:01:1218#include "chrome/browser/download/chrome_download_manager_delegate.h"
[email protected]9fc114672011-06-15 08:17:4819#include "chrome/browser/download/download_item_model.h"
[email protected]e5dc4222010-08-30 22:16:3220#include "chrome/browser/download/download_prefs.h"
[email protected]594cd7d2010-07-21 03:23:5621#include "chrome/browser/download/download_util.h"
[email protected]37858e52010-08-26 00:22:0222#include "chrome/browser/prefs/pref_service.h"
[email protected]2941c2392010-07-15 22:54:3023#include "chrome/common/pref_names.h"
[email protected]a4ff9eae2011-08-01 19:58:1624#include "chrome/test/base/testing_profile.h"
[email protected]2606e5a92011-10-21 00:09:5225#include "content/browser/download/download_buffer.h"
[email protected]71bf3f5e2011-08-15 21:05:2226#include "content/browser/download/download_create_info.h"
[email protected]f6c2b5312011-11-16 22:31:3927#include "content/browser/download/download_file_impl.h"
[email protected]71bf3f5e2011-08-15 21:05:2228#include "content/browser/download/download_file_manager.h"
[email protected]2909e342011-10-29 00:46:5329#include "content/browser/download/download_id_factory.h"
[email protected]71bf3f5e2011-08-15 21:05:2230#include "content/browser/download/download_item.h"
[email protected]5656f8a2011-11-17 16:12:5831#include "content/browser/download/download_manager_impl.h"
[email protected]594e66fe2011-10-25 22:49:4132#include "content/browser/download/download_request_handle.h"
[email protected]71bf3f5e2011-08-15 21:05:2233#include "content/browser/download/download_status_updater.h"
[email protected]be76b7e2011-10-13 12:57:5734#include "content/browser/download/interrupt_reasons.h"
[email protected]f6c2b5312011-11-16 22:31:3935#include "content/browser/download/mock_download_file.h"
[email protected]71bf3f5e2011-08-15 21:05:2236#include "content/browser/download/mock_download_manager.h"
[email protected]c38831a12011-10-28 12:44:4937#include "content/test/test_browser_thread.h"
[email protected]9fc114672011-06-15 08:17:4838#include "grit/generated_resources.h"
[email protected]47a881b2011-08-29 22:59:2139#include "net/base/io_buffer.h"
[email protected]594cd7d2010-07-21 03:23:5640#include "testing/gmock/include/gmock/gmock.h"
41#include "testing/gmock_mutant.h"
initial.commit09911bf2008-07-26 23:55:2942#include "testing/gtest/include/gtest/gtest.h"
[email protected]9fc114672011-06-15 08:17:4843#include "ui/base/l10n/l10n_util.h"
[email protected]7a3b2632011-06-22 20:40:2244#include "ui/base/text/bytes_formatting.h"
initial.commit09911bf2008-07-26 23:55:2945
[email protected]631bb742011-11-02 11:29:3946using content::BrowserThread;
47
[email protected]2909e342011-10-29 00:46:5348DownloadId::Domain kValidIdDomain = "valid DownloadId::Domain";
49
[email protected]583844c2011-08-27 00:38:3550class DownloadManagerTest : public testing::Test {
initial.commit09911bf2008-07-26 23:55:2951 public:
[email protected]bf68a00b2011-04-07 17:28:2652 static const char* kTestData;
53 static const size_t kTestDataLen;
54
[email protected]2941c2392010-07-15 22:54:3055 DownloadManagerTest()
56 : profile_(new TestingProfile()),
[email protected]e7557f172011-08-19 23:42:0157 download_manager_delegate_(new ChromeDownloadManagerDelegate(
58 profile_.get())),
[email protected]2909e342011-10-29 00:46:5359 id_factory_(new DownloadIdFactory(kValidIdDomain)),
[email protected]5656f8a2011-11-17 16:12:5860 download_manager_(new DownloadManagerImpl(
[email protected]2909e342011-10-29 00:46:5361 download_manager_delegate_,
62 id_factory_,
63 &download_status_updater_)),
[email protected]f5920322011-03-24 20:34:1664 ui_thread_(BrowserThread::UI, &message_loop_),
[email protected]2606e5a92011-10-21 00:09:5265 file_thread_(BrowserThread::FILE, &message_loop_),
66 download_buffer_(new content::DownloadBuffer) {
[email protected]2941c2392010-07-15 22:54:3067 download_manager_->Init(profile_.get());
[email protected]2588ea9d2011-08-22 20:59:5368 download_manager_delegate_->SetDownloadManager(download_manager_);
[email protected]2941c2392010-07-15 22:54:3069 }
70
71 ~DownloadManagerTest() {
[email protected]326a6a92010-09-10 20:21:1372 download_manager_->Shutdown();
[email protected]2941c2392010-07-15 22:54:3073 // profile_ must outlive download_manager_, so we explicitly delete
74 // download_manager_ first.
[email protected]1b2f8da2010-07-22 08:29:2975 download_manager_ = NULL;
[email protected]e7557f172011-08-19 23:42:0176 download_manager_delegate_ = NULL;
[email protected]1b2f8da2010-07-22 08:29:2977 profile_.reset(NULL);
78 message_loop_.RunAllPending();
initial.commit09911bf2008-07-26 23:55:2979 }
80
[email protected]bf68a00b2011-04-07 17:28:2681 void AddDownloadToFileManager(int id, DownloadFile* download_file) {
[email protected]2909e342011-10-29 00:46:5382 file_manager()->downloads_[DownloadId(kValidIdDomain, id)] =
[email protected]eda58402011-09-21 19:32:0283 download_file;
[email protected]bf68a00b2011-04-07 17:28:2684 }
85
[email protected]47a881b2011-08-29 22:59:2186 void OnResponseCompleted(int32 download_id, int64 size,
87 const std::string& hash) {
88 download_manager_->OnResponseCompleted(download_id, size, hash);
[email protected]bf68a00b2011-04-07 17:28:2689 }
90
[email protected]99cb7f82011-07-28 17:27:2691 void FileSelected(const FilePath& path, void* params) {
92 download_manager_->FileSelected(path, params);
[email protected]bf68a00b2011-04-07 17:28:2693 }
94
[email protected]4cd82f72011-05-23 19:15:0195 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) {
96 download_manager_->ContinueDownloadWithPath(download, path);
[email protected]bf68a00b2011-04-07 17:28:2697 }
98
[email protected]47a881b2011-08-29 22:59:2199 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]2606e5a92011-10-21 00:09:52106 download_buffer_->AddData(io_buffer, length);
[email protected]47a881b2011-08-29 22:59:21107
108 BrowserThread::PostTask(
109 BrowserThread::FILE, FROM_HERE,
[email protected]a3094072011-10-10 17:14:05110 base::Bind(&DownloadFileManager::UpdateDownload, file_manager_.get(),
[email protected]2909e342011-10-29 00:46:53111 DownloadId(kValidIdDomain, id), download_buffer_));
[email protected]47a881b2011-08-29 22:59:21112
113 message_loop_.RunAllPending();
114 }
115
[email protected]be76b7e2011-10-13 12:57:57116 void OnDownloadInterrupted(int32 download_id, int64 size,
117 InterruptReason reason) {
118 download_manager_->OnDownloadInterrupted(download_id, size, reason);
[email protected]bf68a00b2011-04-07 17:28:26119 }
120
121 // Get the download item with ID |id|.
122 DownloadItem* GetActiveDownloadItem(int32 id) {
[email protected]5656f8a2011-11-17 16:12:58123 return download_manager_->GetActiveDownload(id);
[email protected]594cd7d2010-07-21 03:23:56124 }
125
initial.commit09911bf2008-07-26 23:55:29126 protected:
[email protected]a3d2bc42010-10-06 14:08:49127 DownloadStatusUpdater download_status_updater_;
[email protected]2941c2392010-07-15 22:54:30128 scoped_ptr<TestingProfile> profile_;
[email protected]aa9881c2011-08-15 18:01:12129 scoped_refptr<ChromeDownloadManagerDelegate> download_manager_delegate_;
[email protected]2909e342011-10-29 00:46:53130 scoped_refptr<DownloadIdFactory> id_factory_;
initial.commit09911bf2008-07-26 23:55:29131 scoped_refptr<DownloadManager> download_manager_;
[email protected]594cd7d2010-07-21 03:23:56132 scoped_refptr<DownloadFileManager> file_manager_;
[email protected]dd3eac22008-08-26 07:28:34133 MessageLoopForUI message_loop_;
[email protected]c38831a12011-10-28 12:44:49134 content::TestBrowserThread ui_thread_;
135 content::TestBrowserThread file_thread_;
[email protected]2606e5a92011-10-21 00:09:52136 scoped_refptr<content::DownloadBuffer> download_buffer_;
initial.commit09911bf2008-07-26 23:55:29137
[email protected]594cd7d2010-07-21 03:23:56138 DownloadFileManager* file_manager() {
139 if (!file_manager_) {
140 file_manager_ = new DownloadFileManager(NULL);
[email protected]5656f8a2011-11-17 16:12:58141 download_manager_->SetFileManager(file_manager_);
[email protected]594cd7d2010-07-21 03:23:56142 }
143 return file_manager_;
144 }
145
[email protected]287b86b2011-02-26 00:11:35146 // 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]4d818fee2010-06-06 13:32:27157 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
initial.commit09911bf2008-07-26 23:55:29158};
159
[email protected]bf68a00b2011-04-07 17:28:26160const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad";
161const size_t DownloadManagerTest::kTestDataLen =
162 strlen(DownloadManagerTest::kTestData);
163
[email protected]f6c2b5312011-11-16 22:31:39164// A DownloadFile that we can inject errors into.
165class DownloadFileWithErrors : public DownloadFileImpl {
[email protected]47a881b2011-08-29 22:59:21166 public:
[email protected]f6c2b5312011-11-16 22:31:39167 DownloadFileWithErrors(DownloadCreateInfo* info, DownloadManager* manager);
168 virtual ~DownloadFileWithErrors() {}
[email protected]47a881b2011-08-29 22:59:21169
[email protected]f6c2b5312011-11-16 22:31:39170 // 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]47a881b2011-08-29 22:59:21174
[email protected]f6c2b5312011-11-16 22:31:39175 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]47a881b2011-08-29 22:59:21178
[email protected]f6c2b5312011-11-16 22:31:39179 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]47a881b2011-08-29 22:59:21191};
192
[email protected]f6c2b5312011-11-16 22:31:39193DownloadFileWithErrors::DownloadFileWithErrors(DownloadCreateInfo* info,
194 DownloadManager* manager)
195 : DownloadFileImpl(info, new DownloadRequestHandle(), manager),
196 forced_error_(net::OK) {
[email protected]47a881b2011-08-29 22:59:21197}
198
[email protected]f6c2b5312011-11-16 22:31:39199net::Error DownloadFileWithErrors::Initialize(bool calculate_hash) {
200 return ReturnError(DownloadFileImpl::Initialize(calculate_hash));
[email protected]47a881b2011-08-29 22:59:21201}
[email protected]f6c2b5312011-11-16 22:31:39202
203net::Error DownloadFileWithErrors::AppendDataToFile(const char* data,
204 size_t data_len) {
205 return ReturnError(DownloadFileImpl::AppendDataToFile(data, data_len));
206}
207
208net::Error DownloadFileWithErrors::Rename(const FilePath& full_path) {
209 return ReturnError(DownloadFileImpl::Rename(full_path));
[email protected]47a881b2011-08-29 22:59:21210}
211
[email protected]763f946a2009-01-06 19:04:39212namespace {
213
214const struct {
[email protected]2941c2392010-07-15 22:54:30215 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]bac4f4b2011-03-05 02:01:40236 { "http://www.foo.com/user-script-text-html-mimetype.user.js",
[email protected]2941c2392010-07-15 22:54:30237 "text/html",
238 false,
[email protected]bac4f4b2011-03-05 02:01:40239 false,
[email protected]2941c2392010-07-15 22:54:30240 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]2fc93852011-01-08 22:13:56251 { "http://www.foo.com/sometimes_prompt.pdf",
[email protected]2941c2392010-07-15 22:54:30252 "application/pdf",
253 false,
254 true,
[email protected]2fc93852011-01-08 22:13:56255 false, },
256 { "http://www.foo.com/always_prompt.jar",
257 "application/jar",
258 false,
259 true,
[email protected]3366aca62010-10-20 01:18:24260 true, },
[email protected]2941c2392010-07-15 22:54:30261};
262
[email protected]594cd7d2010-07-21 03:23:56263const struct {
264 FilePath::StringType suggested_path;
[email protected]287b86b2011-02-26 00:11:35265 bool is_dangerous_file;
266 bool is_dangerous_url;
[email protected]594cd7d2010-07-21 03:23:56267 bool finish_before_rename;
[email protected]594cd7d2010-07-21 03:23:56268 int expected_rename_count;
269} kDownloadRenameCases[] = {
[email protected]adb2f3d12011-01-23 16:24:54270 // 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]88008002011-05-24 23:14:15273 { FILE_PATH_LITERAL("foo.zip"), false, false, true, 2, },
[email protected]287b86b2011-02-26 00:11:35274 // 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]88008002011-05-24 23:14:15277 { 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]adb2f3d12011-01-23 16:24:54280 // Safe download, download finishes AFTER file name determined.
[email protected]594cd7d2010-07-21 03:23:56281 // Needs to be renamed twice.
[email protected]88008002011-05-24 23:14:15282 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, },
[email protected]adb2f3d12011-01-23 16:24:54283 // Dangerous download, download finishes AFTER file name determined.
[email protected]594cd7d2010-07-21 03:23:56284 // Needs to be renamed only once.
[email protected]88008002011-05-24 23:14:15285 { 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]594cd7d2010-07-21 03:23:56288};
289
[email protected]f5920322011-03-24 20:34:16290// This is an observer that records what download IDs have opened a select
291// file dialog.
292class SelectFileObserver : public DownloadManager::Observer {
293 public:
294 explicit SelectFileObserver(DownloadManager* download_manager)
295 : download_manager_(download_manager) {
[email protected]bf68a00b2011-04-07 17:28:26296 DCHECK(download_manager_.get());
[email protected]f5920322011-03-24 20:34:16297 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]fed38252011-07-08 17:26:50307 virtual void SelectFileDialogDisplayed(int32 id) {
[email protected]f5920322011-03-24 20:34:16308 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]bf68a00b2011-04-07 17:28:26317 scoped_refptr<DownloadManager> download_manager_;
318};
319
320// This observer tracks the progress of |DownloadItem|s.
321class ItemObserver : public DownloadItem::Observer {
322 public:
323 explicit ItemObserver(DownloadItem* tracked)
324 : tracked_(tracked), states_hit_(0),
[email protected]48837962011-04-19 17:03:29325 was_updated_(false), was_opened_(false) {
[email protected]bf68a00b2011-04-07 17:28:26326 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]bf68a00b2011-04-07 17:28:26339 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]bf68a00b2011-04-07 17:28:26348 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]bf68a00b2011-04-07 17:28:26357 bool was_opened_;
[email protected]f5920322011-03-24 20:34:16358};
359
[email protected]594cd7d2010-07-21 03:23:56360} // namespace
361
[email protected]f5920322011-03-24 20:34:16362TEST_F(DownloadManagerTest, StartDownload) {
[email protected]c38831a12011-10-28 12:44:49363 content::TestBrowserThread io_thread(BrowserThread::IO, &message_loop_);
[email protected]f5920322011-03-24 20:34:16364 PrefService* prefs = profile_->GetPrefs();
[email protected]fed38252011-07-08 17:26:50365 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, FilePath());
[email protected]e7557f172011-08-19 23:42:01366 DownloadPrefs* download_prefs =
367 DownloadPrefs::FromDownloadManager(download_manager_);
368 download_prefs->EnableAutoOpenBasedOnExtension(
[email protected]f5920322011-03-24 20:34:16369 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]c9994e02011-05-24 20:52:19376 // 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]2909e342011-10-29 00:46:53380 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i));
[email protected]f5920322011-03-24 20:34:16381 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as;
[email protected]8799e542011-04-20 03:47:34382 info->url_chain.push_back(GURL(kStartDownloadCases[i].url));
[email protected]f5920322011-03-24 20:34:16383 info->mime_type = kStartDownloadCases[i].mime_type;
[email protected]594e66fe2011-10-25 22:49:41384 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
[email protected]f5920322011-03-24 20:34:16385
[email protected]c9994e02011-05-24 20:52:19386 DownloadFile* download_file(
[email protected]f6c2b5312011-11-16 22:31:39387 new DownloadFileImpl(info.get(), new DownloadRequestHandle(),
388 download_manager_));
[email protected]2909e342011-10-29 00:46:53389 AddDownloadToFileManager(info->download_id.local(), download_file);
[email protected]bf68a00b2011-04-07 17:28:26390 download_file->Initialize(false);
[email protected]2909e342011-10-29 00:46:53391 download_manager_->StartDownload(info->download_id.local());
[email protected]f5920322011-03-24 20:34:16392 message_loop_.RunAllPending();
393
[email protected]c9994e02011-05-24 20:52:19394 // SelectFileObserver will have recorded any attempt to open the
[email protected]f5920322011-03-24 20:34:16395 // select file dialog.
[email protected]c9994e02011-05-24 20:52:19396 // Note that DownloadManager::FileSelectionCanceled() is never called.
[email protected]f5920322011-03-24 20:34:16397 EXPECT_EQ(kStartDownloadCases[i].expected_save_as,
398 observer.ShowedFileDialogForId(i));
[email protected]f5920322011-03-24 20:34:16399 }
400}
401
[email protected]594cd7d2010-07-21 03:23:56402TEST_F(DownloadManagerTest, DownloadRenameTest) {
403 using ::testing::_;
404 using ::testing::CreateFunctor;
405 using ::testing::Invoke;
406 using ::testing::Return;
407
[email protected]594cd7d2010-07-21 03:23:56408 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) {
[email protected]c9994e02011-05-24 20:52:19409 // 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]2909e342011-10-29 00:46:53413 info->download_id = DownloadId(kValidIdDomain, static_cast<int>(i));
[email protected]594cd7d2010-07-21 03:23:56414 info->prompt_user_for_save_location = false;
[email protected]30917cb2011-05-10 21:40:47415 info->url_chain.push_back(GURL());
[email protected]4cd82f72011-05-23 19:15:01416 const FilePath new_path(kDownloadRenameCases[i].suggested_path);
[email protected]594cd7d2010-07-21 03:23:56417
[email protected]f6c2b5312011-11-16 22:31:39418 MockDownloadFile::StatisticsRecorder recorder;
[email protected]bf68a00b2011-04-07 17:28:26419 MockDownloadFile* download_file(
[email protected]f6c2b5312011-11-16 22:31:39420 new MockDownloadFile(info.get(),
421 DownloadRequestHandle(),
422 download_manager_,
423 &recorder));
[email protected]2909e342011-10-29 00:46:53424 AddDownloadToFileManager(info->download_id.local(), download_file);
[email protected]594cd7d2010-07-21 03:23:56425
[email protected]bf68a00b2011-04-07 17:28:26426 // |download_file| is owned by DownloadFileManager.
[email protected]594cd7d2010-07-21 03:23:56427 if (kDownloadRenameCases[i].expected_rename_count == 1) {
[email protected]f6c2b5312011-11-16 22:31:39428 download_file->SetExpectedPath(0, new_path);
[email protected]594cd7d2010-07-21 03:23:56429 } else {
430 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count);
431 FilePath crdownload(download_util::GetCrDownloadPath(new_path));
[email protected]f6c2b5312011-11-16 22:31:39432 download_file->SetExpectedPath(0, crdownload);
433 download_file->SetExpectedPath(1, new_path);
[email protected]594cd7d2010-07-21 03:23:56434 }
[email protected]594e66fe2011-10-25 22:49:41435 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
[email protected]88008002011-05-24 23:14:15436 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]c2e76012010-12-23 21:10:29442
[email protected]4cd82f72011-05-23 19:15:01443 int32* id_ptr = new int32;
444 *id_ptr = i; // Deleted in FileSelected().
[email protected]594cd7d2010-07-21 03:23:56445 if (kDownloadRenameCases[i].finish_before_rename) {
[email protected]47a881b2011-08-29 22:59:21446 OnResponseCompleted(i, 1024, std::string("fake_hash"));
[email protected]f5920322011-03-24 20:34:16447 message_loop_.RunAllPending();
[email protected]99cb7f82011-07-28 17:27:26448 FileSelected(new_path, id_ptr);
[email protected]594cd7d2010-07-21 03:23:56449 } else {
[email protected]99cb7f82011-07-28 17:27:26450 FileSelected(new_path, id_ptr);
[email protected]f5920322011-03-24 20:34:16451 message_loop_.RunAllPending();
[email protected]47a881b2011-08-29 22:59:21452 OnResponseCompleted(i, 1024, std::string("fake_hash"));
[email protected]594cd7d2010-07-21 03:23:56453 }
454
455 message_loop_.RunAllPending();
[email protected]f6c2b5312011-11-16 22:31:39456 EXPECT_EQ(
457 kDownloadRenameCases[i].expected_rename_count,
458 recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME));
[email protected]287b86b2011-02-26 00:11:35459 EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file,
460 kDownloadRenameCases[i].is_dangerous_url,
461 i));
[email protected]594cd7d2010-07-21 03:23:56462 }
463}
[email protected]bf68a00b2011-04-07 17:28:26464
465TEST_F(DownloadManagerTest, DownloadInterruptTest) {
466 using ::testing::_;
467 using ::testing::CreateFunctor;
468 using ::testing::Invoke;
469 using ::testing::Return;
470
[email protected]c9994e02011-05-24 20:52:19471 // 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]2909e342011-10-29 00:46:53475 info->download_id = DownloadId(kValidIdDomain, 0);
[email protected]bf68a00b2011-04-07 17:28:26476 info->prompt_user_for_save_location = false;
[email protected]30917cb2011-05-10 21:40:47477 info->url_chain.push_back(GURL());
[email protected]9fc114672011-06-15 08:17:48478 info->total_bytes = static_cast<int64>(kTestDataLen);
[email protected]bf68a00b2011-04-07 17:28:26479 const FilePath new_path(FILE_PATH_LITERAL("foo.zip"));
480 const FilePath cr_path(download_util::GetCrDownloadPath(new_path));
481
[email protected]f6c2b5312011-11-16 22:31:39482 MockDownloadFile::StatisticsRecorder recorder;
[email protected]bf68a00b2011-04-07 17:28:26483 MockDownloadFile* download_file(
[email protected]f6c2b5312011-11-16 22:31:39484 new MockDownloadFile(info.get(),
485 DownloadRequestHandle(),
486 download_manager_,
487 &recorder));
[email protected]2909e342011-10-29 00:46:53488 AddDownloadToFileManager(info->download_id.local(), download_file);
[email protected]bf68a00b2011-04-07 17:28:26489
490 // |download_file| is owned by DownloadFileManager.
[email protected]f6c2b5312011-11-16 22:31:39491 download_file->SetExpectedPath(0, cr_path);
[email protected]bf68a00b2011-04-07 17:28:26492
[email protected]594e66fe2011-10-25 22:49:41493 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
[email protected]bf68a00b2011-04-07 17:28:26494
495 DownloadItem* download = GetActiveDownloadItem(0);
496 ASSERT_TRUE(download != NULL);
[email protected]9fc114672011-06-15 08:17:48497 scoped_ptr<DownloadItemModel> download_item_model(
498 new DownloadItemModel(download));
[email protected]bf68a00b2011-04-07 17:28:26499
500 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
[email protected]d8ca4d12011-04-07 22:05:13501 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
[email protected]bf68a00b2011-04-07 17:28:26502
503 download_file->AppendDataToFile(kTestData, kTestDataLen);
504
[email protected]4cd82f72011-05-23 19:15:01505 ContinueDownloadWithPath(download, new_path);
[email protected]bf68a00b2011-04-07 17:28:26506 message_loop_.RunAllPending();
[email protected]f6c2b5312011-11-16 22:31:39507 EXPECT_EQ(1,
508 recorder.Count(MockDownloadFile::StatisticsRecorder::STAT_RENAME));
[email protected]bf68a00b2011-04-07 17:28:26509 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
510
[email protected]9fc114672011-06-15 08:17:48511 int64 error_size = 3;
[email protected]be76b7e2011-10-13 12:57:57512 OnDownloadInterrupted(0, error_size,
513 DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED);
[email protected]bf68a00b2011-04-07 17:28:26514 message_loop_.RunAllPending();
515
516 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
[email protected]bf68a00b2011-04-07 17:28:26517 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]bf68a00b2011-04-07 17:28:26523 EXPECT_FALSE(observer->was_opened());
[email protected]9fc114672011-06-15 08:17:48524 EXPECT_FALSE(download->file_externally_removed());
525 EXPECT_EQ(DownloadItem::INTERRUPTED, download->state());
[email protected]7a3b2632011-06-22 20:40:22526 ui::DataUnits amount_units = ui::GetByteDisplayUnits(kTestDataLen);
527 string16 simple_size =
528 ui::FormatBytesWithUnits(error_size, amount_units, false);
[email protected]9fc114672011-06-15 08:17:48529 string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality(
[email protected]7a3b2632011-06-22 20:40:22530 ui::FormatBytesWithUnits(kTestDataLen, amount_units, true));
[email protected]9fc114672011-06-15 08:17:48531 EXPECT_EQ(download_item_model->GetStatusText(),
532 l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED,
533 simple_size,
534 simple_total));
[email protected]bf68a00b2011-04-07 17:28:26535
[email protected]93af2272011-09-21 18:29:17536 download->Cancel(true);
[email protected]bf68a00b2011-04-07 17:28:26537
[email protected]bf68a00b2011-04-07 17:28:26538 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]bf68a00b2011-04-07 17:28:26544 EXPECT_FALSE(observer->was_opened());
[email protected]9fc114672011-06-15 08:17:48545 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]bf68a00b2011-04-07 17:28:26549}
550
[email protected]47a881b2011-08-29 22:59:21551// Test the behavior of DownloadFileManager and DownloadManager in the event
552// of a file error while writing the download to disk.
553TEST_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]f6c2b5312011-11-16 22:31:39559 net::FileStream* stream = new net::FileStream;
560 ASSERT_EQ(0, stream->Open(
[email protected]47a881b2011-08-29 22:59:21561 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]2909e342011-10-29 00:46:53568 static const int32 local_id = 0;
569 info->download_id = DownloadId(kValidIdDomain, local_id);
[email protected]47a881b2011-08-29 22:59:21570 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]f6c2b5312011-11-16 22:31:39574 info->save_info.file_stream.reset(stream);
[email protected]47a881b2011-08-29 22:59:21575
576 // Create a download file that we can insert errors into.
[email protected]f6c2b5312011-11-16 22:31:39577 DownloadFileWithErrors* download_file(new DownloadFileWithErrors(
578 info.get(), download_manager_));
[email protected]2909e342011-10-29 00:46:53579 AddDownloadToFileManager(local_id, download_file);
[email protected]47a881b2011-08-29 22:59:21580
581 // |download_file| is owned by DownloadFileManager.
[email protected]594e66fe2011-10-25 22:49:41582 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
[email protected]47a881b2011-08-29 22:59:21583
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]2909e342011-10-29 00:46:53594 UpdateData(local_id, kTestData, kTestDataLen);
[email protected]47a881b2011-08-29 22:59:21595
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]2909e342011-10-29 00:46:53602 UpdateData(local_id, kTestData, kTestDataLen);
[email protected]47a881b2011-08-29 22:59:21603
604 // Add more data, but an error occurs.
[email protected]f6c2b5312011-11-16 22:31:39605 download_file->set_forced_error(net::ERR_FAILED);
[email protected]2909e342011-10-29 00:46:53606 UpdateData(local_id, kTestData, kTestDataLen);
[email protected]47a881b2011-08-29 22:59:21607
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]f6c2b5312011-11-16 22:31:39621 size_t error_size = kTestDataLen * 3;
[email protected]e5e120f2011-09-23 23:08:15622 size_t total_size = kTestDataLen * 3;
[email protected]47a881b2011-08-29 22:59:21623 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]e5e120f2011-09-23 23:08:15627 ui::FormatBytesWithUnits(total_size, amount_units, true));
[email protected]47a881b2011-08-29 22:59:21628 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]93af2272011-09-21 18:29:17634 download->Cancel(true);
[email protected]47a881b2011-08-29 22:59:21635 message_loop_.RunAllPending();
636}
637
[email protected]bf68a00b2011-04-07 17:28:26638TEST_F(DownloadManagerTest, DownloadCancelTest) {
639 using ::testing::_;
640 using ::testing::CreateFunctor;
641 using ::testing::Invoke;
642 using ::testing::Return;
643
[email protected]c9994e02011-05-24 20:52:19644 // 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]2909e342011-10-29 00:46:53648 info->download_id = DownloadId(kValidIdDomain, 0);
[email protected]bf68a00b2011-04-07 17:28:26649 info->prompt_user_for_save_location = false;
[email protected]30917cb2011-05-10 21:40:47650 info->url_chain.push_back(GURL());
[email protected]bf68a00b2011-04-07 17:28:26651 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]f6c2b5312011-11-16 22:31:39655 new MockDownloadFile(info.get(),
656 DownloadRequestHandle(),
657 download_manager_,
658 NULL));
[email protected]2909e342011-10-29 00:46:53659 AddDownloadToFileManager(info->download_id.local(), download_file);
[email protected]bf68a00b2011-04-07 17:28:26660
661 // |download_file| is owned by DownloadFileManager.
[email protected]f6c2b5312011-11-16 22:31:39662 download_file->SetExpectedPath(0, cr_path);
[email protected]bf68a00b2011-04-07 17:28:26663
[email protected]594e66fe2011-10-25 22:49:41664 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
[email protected]bf68a00b2011-04-07 17:28:26665
666 DownloadItem* download = GetActiveDownloadItem(0);
667 ASSERT_TRUE(download != NULL);
[email protected]9fc114672011-06-15 08:17:48668 scoped_ptr<DownloadItemModel> download_item_model(
669 new DownloadItemModel(download));
[email protected]bf68a00b2011-04-07 17:28:26670
671 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
[email protected]d8ca4d12011-04-07 22:05:13672 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
[email protected]bf68a00b2011-04-07 17:28:26673
[email protected]4cd82f72011-05-23 19:15:01674 ContinueDownloadWithPath(download, new_path);
[email protected]bf68a00b2011-04-07 17:28:26675 message_loop_.RunAllPending();
676 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
677
678 download_file->AppendDataToFile(kTestData, kTestDataLen);
679
[email protected]93af2272011-09-21 18:29:17680 download->Cancel(false);
[email protected]bf68a00b2011-04-07 17:28:26681 message_loop_.RunAllPending();
682
[email protected]93af2272011-09-21 18:29:17683 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
[email protected]bf68a00b2011-04-07 17:28:26684 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]bf68a00b2011-04-07 17:28:26690 EXPECT_FALSE(observer->was_opened());
[email protected]9fc114672011-06-15 08:17:48691 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]bf68a00b2011-04-07 17:28:26695
696 EXPECT_FALSE(file_util::PathExists(new_path));
697 EXPECT_FALSE(file_util::PathExists(cr_path));
698}
[email protected]8fa1eeb52011-04-13 14:18:02699
700TEST_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]ec865262011-08-23 20:01:48723 int uniquifier = DownloadFile::GetUniquePathNumber(new_path);
[email protected]8fa1eeb52011-04-13 14:18:02724 FilePath unique_new_path = new_path;
725 EXPECT_NE(0, uniquifier);
[email protected]ec865262011-08-23 20:01:48726 DownloadFile::AppendNumberToPath(&unique_new_path, uniquifier);
[email protected]8fa1eeb52011-04-13 14:18:02727
[email protected]c9994e02011-05-24 20:52:19728 // 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]2909e342011-10-29 00:46:53732 info->download_id = DownloadId(kValidIdDomain, 0);
[email protected]8fa1eeb52011-04-13 14:18:02733 info->prompt_user_for_save_location = true;
[email protected]30917cb2011-05-10 21:40:47734 info->url_chain.push_back(GURL());
[email protected]8fa1eeb52011-04-13 14:18:02735
[email protected]594e66fe2011-10-25 22:49:41736 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
[email protected]8fa1eeb52011-04-13 14:18:02737
738 DownloadItem* download = GetActiveDownloadItem(0);
739 ASSERT_TRUE(download != NULL);
[email protected]9fc114672011-06-15 08:17:48740 scoped_ptr<DownloadItemModel> download_item_model(
741 new DownloadItemModel(download));
[email protected]8fa1eeb52011-04-13 14:18:02742
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]f6c2b5312011-11-16 22:31:39751 new DownloadFileImpl(info.get(), new DownloadRequestHandle(),
752 download_manager_));
[email protected]8fa1eeb52011-04-13 14:18:02753 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]2909e342011-10-29 00:46:53757 AddDownloadToFileManager(info->download_id.local(), download_file);
[email protected]8fa1eeb52011-04-13 14:18:02758
[email protected]4cd82f72011-05-23 19:15:01759 ContinueDownloadWithPath(download, new_path);
[email protected]8fa1eeb52011-04-13 14:18:02760 message_loop_.RunAllPending();
761 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
762
763 download_file->AppendDataToFile(kTestData, kTestDataLen);
764
765 // Finish the download.
[email protected]47a881b2011-08-29 22:59:21766 OnResponseCompleted(0, kTestDataLen, "");
[email protected]8fa1eeb52011-04-13 14:18:02767 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]8fa1eeb52011-04-13 14:18:02777 EXPECT_FALSE(observer->was_opened());
[email protected]9fc114672011-06-15 08:17:48778 EXPECT_FALSE(download->file_externally_removed());
[email protected]8fa1eeb52011-04-13 14:18:02779 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
[email protected]9fc114672011-06-15 08:17:48780 EXPECT_EQ(download_item_model->GetStatusText(), ASCIIToUTF16(""));
[email protected]8fa1eeb52011-04-13 14:18:02781
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]9fc114672011-06-15 08:17:48789
790TEST_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]2909e342011-10-29 00:46:53809 info->download_id = DownloadId(kValidIdDomain, 0);
[email protected]9fc114672011-06-15 08:17:48810 info->prompt_user_for_save_location = true;
811 info->url_chain.push_back(GURL());
812
[email protected]594e66fe2011-10-25 22:49:41813 download_manager_->CreateDownloadItem(info.get(), DownloadRequestHandle());
[email protected]9fc114672011-06-15 08:17:48814
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]f6c2b5312011-11-16 22:31:39828 new DownloadFileImpl(info.get(), new DownloadRequestHandle(),
829 download_manager_));
[email protected]9fc114672011-06-15 08:17:48830 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]2909e342011-10-29 00:46:53834 AddDownloadToFileManager(info->download_id.local(), download_file);
[email protected]9fc114672011-06-15 08:17:48835
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]47a881b2011-08-29 22:59:21843 OnResponseCompleted(0, kTestDataLen, "");
[email protected]9fc114672011-06-15 08:17:48844 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}