blob: 02f1ce88ae33411eba6b462ba646c9ac973504ce [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
5#include <string>
[email protected]f5920322011-03-24 20:34:166#include <set>
initial.commit09911bf2008-07-26 23:55:297
[email protected]bf68a00b2011-04-07 17:28:268#include "base/file_util.h"
[email protected]9fc114672011-06-15 08:17:489#include "base/i18n/number_formatting.h"
10#include "base/i18n/rtl.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/scoped_ptr.h"
[email protected]7286e3fc2011-07-19 22:13:2412#include "base/stl_util.h"
[email protected]c2b2fcf2011-08-29 18:03:1713#include "base/string_util.h"
[email protected]0b600642011-08-29 18:43:5014#include "base/string16.h"
[email protected]9fc114672011-06-15 08:17:4815#include "base/utf_string_conversions.h"
[email protected]047a03f2009-10-07 02:10:2016#include "build/build_config.h"
[email protected]aa9881c2011-08-15 18:01:1217#include "chrome/browser/download/chrome_download_manager_delegate.h"
[email protected]9fc114672011-06-15 08:17:4818#include "chrome/browser/download/download_item_model.h"
[email protected]e5dc4222010-08-30 22:16:3219#include "chrome/browser/download/download_prefs.h"
[email protected]594cd7d2010-07-21 03:23:5620#include "chrome/browser/download/download_util.h"
[email protected]37858e52010-08-26 00:22:0221#include "chrome/browser/prefs/pref_service.h"
[email protected]2941c2392010-07-15 22:54:3022#include "chrome/common/pref_names.h"
[email protected]a4ff9eae2011-08-01 19:58:1623#include "chrome/test/base/testing_profile.h"
[email protected]1bda97552011-03-01 20:11:5224#include "content/browser/browser_thread.h"
[email protected]71bf3f5e2011-08-15 21:05:2225#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]9fc114672011-06-15 08:17:4832#include "grit/generated_resources.h"
[email protected]47a881b2011-08-29 22:59:2133#include "net/base/io_buffer.h"
34#include "net/base/mock_file_stream.h"
[email protected]594cd7d2010-07-21 03:23:5635#include "testing/gmock/include/gmock/gmock.h"
36#include "testing/gmock_mutant.h"
initial.commit09911bf2008-07-26 23:55:2937#include "testing/gtest/include/gtest/gtest.h"
[email protected]9fc114672011-06-15 08:17:4838#include "ui/base/l10n/l10n_util.h"
[email protected]7a3b2632011-06-22 20:40:2239#include "ui/base/text/bytes_formatting.h"
initial.commit09911bf2008-07-26 23:55:2940
[email protected]583844c2011-08-27 00:38:3541class DownloadManagerTest : public testing::Test {
initial.commit09911bf2008-07-26 23:55:2942 public:
[email protected]bf68a00b2011-04-07 17:28:2643 static const char* kTestData;
44 static const size_t kTestDataLen;
45
[email protected]2941c2392010-07-15 22:54:3046 DownloadManagerTest()
47 : profile_(new TestingProfile()),
[email protected]e7557f172011-08-19 23:42:0148 download_manager_delegate_(new ChromeDownloadManagerDelegate(
49 profile_.get())),
[email protected]da1a27b2011-07-29 23:16:3350 download_manager_(new MockDownloadManager(
[email protected]aa9881c2011-08-15 18:01:1251 download_manager_delegate_, &download_status_updater_)),
[email protected]f5920322011-03-24 20:34:1652 ui_thread_(BrowserThread::UI, &message_loop_),
53 file_thread_(BrowserThread::FILE, &message_loop_) {
[email protected]2941c2392010-07-15 22:54:3054 download_manager_->Init(profile_.get());
[email protected]2588ea9d2011-08-22 20:59:5355 download_manager_delegate_->SetDownloadManager(download_manager_);
[email protected]2941c2392010-07-15 22:54:3056 }
57
58 ~DownloadManagerTest() {
[email protected]326a6a92010-09-10 20:21:1359 download_manager_->Shutdown();
[email protected]2941c2392010-07-15 22:54:3060 // profile_ must outlive download_manager_, so we explicitly delete
61 // download_manager_ first.
[email protected]1b2f8da2010-07-22 08:29:2962 download_manager_ = NULL;
[email protected]e7557f172011-08-19 23:42:0163 download_manager_delegate_ = NULL;
[email protected]1b2f8da2010-07-22 08:29:2964 profile_.reset(NULL);
65 message_loop_.RunAllPending();
initial.commit09911bf2008-07-26 23:55:2966 }
67
[email protected]bf68a00b2011-04-07 17:28:2668 void AddDownloadToFileManager(int id, DownloadFile* download_file) {
[email protected]0b600642011-08-29 18:43:5069 file_manager()->downloads_[id] = download_file;
[email protected]bf68a00b2011-04-07 17:28:2670 }
71
[email protected]47a881b2011-08-29 22:59:2172 void OnResponseCompleted(int32 download_id, int64 size,
73 const std::string& hash) {
74 download_manager_->OnResponseCompleted(download_id, size, hash);
[email protected]bf68a00b2011-04-07 17:28:2675 }
76
[email protected]99cb7f82011-07-28 17:27:2677 void FileSelected(const FilePath& path, void* params) {
78 download_manager_->FileSelected(path, params);
[email protected]bf68a00b2011-04-07 17:28:2679 }
80
[email protected]4cd82f72011-05-23 19:15:0181 void ContinueDownloadWithPath(DownloadItem* download, const FilePath& path) {
82 download_manager_->ContinueDownloadWithPath(download, path);
[email protected]bf68a00b2011-04-07 17:28:2683 }
84
[email protected]47a881b2011-08-29 22:59:2185 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]bf68a00b2011-04-07 17:28:26109 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]594cd7d2010-07-21 03:23:56118 }
119
initial.commit09911bf2008-07-26 23:55:29120 protected:
[email protected]a3d2bc42010-10-06 14:08:49121 DownloadStatusUpdater download_status_updater_;
[email protected]2941c2392010-07-15 22:54:30122 scoped_ptr<TestingProfile> profile_;
[email protected]aa9881c2011-08-15 18:01:12123 scoped_refptr<ChromeDownloadManagerDelegate> download_manager_delegate_;
initial.commit09911bf2008-07-26 23:55:29124 scoped_refptr<DownloadManager> download_manager_;
[email protected]594cd7d2010-07-21 03:23:56125 scoped_refptr<DownloadFileManager> file_manager_;
[email protected]dd3eac22008-08-26 07:28:34126 MessageLoopForUI message_loop_;
[email protected]ca4b5fa32010-10-09 12:42:18127 BrowserThread ui_thread_;
[email protected]f5920322011-03-24 20:34:16128 BrowserThread file_thread_;
[email protected]47a881b2011-08-29 22:59:21129 DownloadBuffer download_buffer_;
initial.commit09911bf2008-07-26 23:55:29130
[email protected]594cd7d2010-07-21 03:23:56131 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]287b86b2011-02-26 00:11:35139 // 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]4d818fee2010-06-06 13:32:27150 DISALLOW_COPY_AND_ASSIGN(DownloadManagerTest);
initial.commit09911bf2008-07-26 23:55:29151};
152
[email protected]bf68a00b2011-04-07 17:28:26153const char* DownloadManagerTest::kTestData = "a;sdlfalsdfjalsdkfjad";
154const size_t DownloadManagerTest::kTestDataLen =
155 strlen(DownloadManagerTest::kTestData);
156
[email protected]47a881b2011-08-29 22:59:21157// 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.
160class 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
175DownloadFileWithMockStream::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
184void 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}
192void DownloadFileWithMockStream::CreateFileStream() {
193 file_stream_.reset(new net::testing::MockFileStream);
194}
195
[email protected]763f946a2009-01-06 19:04:39196namespace {
197
198const struct {
[email protected]2941c2392010-07-15 22:54:30199 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]bac4f4b2011-03-05 02:01:40220 { "http://www.foo.com/user-script-text-html-mimetype.user.js",
[email protected]2941c2392010-07-15 22:54:30221 "text/html",
222 false,
[email protected]bac4f4b2011-03-05 02:01:40223 false,
[email protected]2941c2392010-07-15 22:54:30224 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]2fc93852011-01-08 22:13:56235 { "http://www.foo.com/sometimes_prompt.pdf",
[email protected]2941c2392010-07-15 22:54:30236 "application/pdf",
237 false,
238 true,
[email protected]2fc93852011-01-08 22:13:56239 false, },
240 { "http://www.foo.com/always_prompt.jar",
241 "application/jar",
242 false,
243 true,
[email protected]3366aca62010-10-20 01:18:24244 true, },
[email protected]2941c2392010-07-15 22:54:30245};
246
[email protected]594cd7d2010-07-21 03:23:56247const struct {
248 FilePath::StringType suggested_path;
[email protected]287b86b2011-02-26 00:11:35249 bool is_dangerous_file;
250 bool is_dangerous_url;
[email protected]594cd7d2010-07-21 03:23:56251 bool finish_before_rename;
[email protected]594cd7d2010-07-21 03:23:56252 int expected_rename_count;
253} kDownloadRenameCases[] = {
[email protected]adb2f3d12011-01-23 16:24:54254 // 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]88008002011-05-24 23:14:15257 { FILE_PATH_LITERAL("foo.zip"), false, false, true, 2, },
[email protected]287b86b2011-02-26 00:11:35258 // 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]88008002011-05-24 23:14:15261 { 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]adb2f3d12011-01-23 16:24:54264 // Safe download, download finishes AFTER file name determined.
[email protected]594cd7d2010-07-21 03:23:56265 // Needs to be renamed twice.
[email protected]88008002011-05-24 23:14:15266 { FILE_PATH_LITERAL("foo.zip"), false, false, false, 2, },
[email protected]adb2f3d12011-01-23 16:24:54267 // Dangerous download, download finishes AFTER file name determined.
[email protected]594cd7d2010-07-21 03:23:56268 // Needs to be renamed only once.
[email protected]88008002011-05-24 23:14:15269 { 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]594cd7d2010-07-21 03:23:56272};
273
274class MockDownloadFile : public DownloadFile {
275 public:
[email protected]f5920322011-03-24 20:34:16276 MockDownloadFile(DownloadCreateInfo* info, DownloadManager* manager)
277 : DownloadFile(info, manager), renamed_count_(0) { }
[email protected]594cd7d2010-07-21 03:23:56278 virtual ~MockDownloadFile() { Destructed(); }
[email protected]d2310142011-02-18 17:02:55279 MOCK_METHOD1(Rename, bool(const FilePath&));
[email protected]594cd7d2010-07-21 03:23:56280 MOCK_METHOD0(Destructed, void());
281
282 bool TestMultipleRename(
[email protected]d2310142011-02-18 17:02:55283 int expected_count, const FilePath& expected,
284 const FilePath& path) {
[email protected]594cd7d2010-07-21 03:23:56285 ++renamed_count_;
286 EXPECT_EQ(expected_count, renamed_count_);
[email protected]594cd7d2010-07-21 03:23:56287 EXPECT_EQ(expected.value(), path.value());
288 return true;
289 }
290
291 private:
292 int renamed_count_;
293};
294
[email protected]f5920322011-03-24 20:34:16295// This is an observer that records what download IDs have opened a select
296// file dialog.
297class SelectFileObserver : public DownloadManager::Observer {
298 public:
299 explicit SelectFileObserver(DownloadManager* download_manager)
300 : download_manager_(download_manager) {
[email protected]bf68a00b2011-04-07 17:28:26301 DCHECK(download_manager_.get());
[email protected]f5920322011-03-24 20:34:16302 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]fed38252011-07-08 17:26:50312 virtual void SelectFileDialogDisplayed(int32 id) {
[email protected]f5920322011-03-24 20:34:16313 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]bf68a00b2011-04-07 17:28:26322 scoped_refptr<DownloadManager> download_manager_;
323};
324
325// This observer tracks the progress of |DownloadItem|s.
326class ItemObserver : public DownloadItem::Observer {
327 public:
328 explicit ItemObserver(DownloadItem* tracked)
329 : tracked_(tracked), states_hit_(0),
[email protected]48837962011-04-19 17:03:29330 was_updated_(false), was_opened_(false) {
[email protected]bf68a00b2011-04-07 17:28:26331 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]bf68a00b2011-04-07 17:28:26344 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]bf68a00b2011-04-07 17:28:26353 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]bf68a00b2011-04-07 17:28:26362 bool was_opened_;
[email protected]f5920322011-03-24 20:34:16363};
364
[email protected]594cd7d2010-07-21 03:23:56365} // namespace
366
[email protected]f5920322011-03-24 20:34:16367TEST_F(DownloadManagerTest, StartDownload) {
368 BrowserThread io_thread(BrowserThread::IO, &message_loop_);
369 PrefService* prefs = profile_->GetPrefs();
[email protected]fed38252011-07-08 17:26:50370 prefs->SetFilePath(prefs::kDownloadDefaultDirectory, FilePath());
[email protected]e7557f172011-08-19 23:42:01371 DownloadPrefs* download_prefs =
372 DownloadPrefs::FromDownloadManager(download_manager_);
373 download_prefs->EnableAutoOpenBasedOnExtension(
[email protected]f5920322011-03-24 20:34:16374 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]c9994e02011-05-24 20:52:19381 // 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]f5920322011-03-24 20:34:16385 info->download_id = static_cast<int>(i);
386 info->prompt_user_for_save_location = kStartDownloadCases[i].save_as;
[email protected]8799e542011-04-20 03:47:34387 info->url_chain.push_back(GURL(kStartDownloadCases[i].url));
[email protected]f5920322011-03-24 20:34:16388 info->mime_type = kStartDownloadCases[i].mime_type;
[email protected]c9994e02011-05-24 20:52:19389 download_manager_->CreateDownloadItem(info.get());
[email protected]f5920322011-03-24 20:34:16390
[email protected]c9994e02011-05-24 20:52:19391 DownloadFile* download_file(
392 new DownloadFile(info.get(), download_manager_));
[email protected]bf68a00b2011-04-07 17:28:26393 AddDownloadToFileManager(info->download_id, download_file);
394 download_file->Initialize(false);
[email protected]4cd82f72011-05-23 19:15:01395 download_manager_->StartDownload(info->download_id);
[email protected]f5920322011-03-24 20:34:16396 message_loop_.RunAllPending();
397
[email protected]c9994e02011-05-24 20:52:19398 // SelectFileObserver will have recorded any attempt to open the
[email protected]f5920322011-03-24 20:34:16399 // select file dialog.
[email protected]c9994e02011-05-24 20:52:19400 // Note that DownloadManager::FileSelectionCanceled() is never called.
[email protected]f5920322011-03-24 20:34:16401 EXPECT_EQ(kStartDownloadCases[i].expected_save_as,
402 observer.ShowedFileDialogForId(i));
[email protected]f5920322011-03-24 20:34:16403 }
404}
405
[email protected]594cd7d2010-07-21 03:23:56406TEST_F(DownloadManagerTest, DownloadRenameTest) {
407 using ::testing::_;
408 using ::testing::CreateFunctor;
409 using ::testing::Invoke;
410 using ::testing::Return;
411
[email protected]594cd7d2010-07-21 03:23:56412 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kDownloadRenameCases); ++i) {
[email protected]c9994e02011-05-24 20:52:19413 // 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]594cd7d2010-07-21 03:23:56417 info->download_id = static_cast<int>(i);
418 info->prompt_user_for_save_location = false;
[email protected]30917cb2011-05-10 21:40:47419 info->url_chain.push_back(GURL());
[email protected]4cd82f72011-05-23 19:15:01420 const FilePath new_path(kDownloadRenameCases[i].suggested_path);
[email protected]594cd7d2010-07-21 03:23:56421
[email protected]bf68a00b2011-04-07 17:28:26422 MockDownloadFile* download_file(
[email protected]c9994e02011-05-24 20:52:19423 new MockDownloadFile(info.get(), download_manager_));
[email protected]bf68a00b2011-04-07 17:28:26424 AddDownloadToFileManager(info->download_id, download_file);
[email protected]594cd7d2010-07-21 03:23:56425
[email protected]bf68a00b2011-04-07 17:28:26426 // |download_file| is owned by DownloadFileManager.
427 ::testing::Mock::AllowLeak(download_file);
428 EXPECT_CALL(*download_file, Destructed()).Times(1);
[email protected]594cd7d2010-07-21 03:23:56429
430 if (kDownloadRenameCases[i].expected_rename_count == 1) {
[email protected]bf68a00b2011-04-07 17:28:26431 EXPECT_CALL(*download_file, Rename(new_path)).WillOnce(Return(true));
[email protected]594cd7d2010-07-21 03:23:56432 } else {
433 ASSERT_EQ(2, kDownloadRenameCases[i].expected_rename_count);
434 FilePath crdownload(download_util::GetCrDownloadPath(new_path));
[email protected]bf68a00b2011-04-07 17:28:26435 EXPECT_CALL(*download_file, Rename(_))
[email protected]d2310142011-02-18 17:02:55436 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor(
[email protected]bf68a00b2011-04-07 17:28:26437 download_file, &MockDownloadFile::TestMultipleRename,
[email protected]d2310142011-02-18 17:02:55438 1, crdownload))))
439 .WillOnce(testing::WithArgs<0>(Invoke(CreateFunctor(
[email protected]bf68a00b2011-04-07 17:28:26440 download_file, &MockDownloadFile::TestMultipleRename,
[email protected]d2310142011-02-18 17:02:55441 2, new_path))));
[email protected]594cd7d2010-07-21 03:23:56442 }
[email protected]c9994e02011-05-24 20:52:19443 download_manager_->CreateDownloadItem(info.get());
[email protected]88008002011-05-24 23:14:15444 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]c2e76012010-12-23 21:10:29450
[email protected]4cd82f72011-05-23 19:15:01451 int32* id_ptr = new int32;
452 *id_ptr = i; // Deleted in FileSelected().
[email protected]594cd7d2010-07-21 03:23:56453 if (kDownloadRenameCases[i].finish_before_rename) {
[email protected]47a881b2011-08-29 22:59:21454 OnResponseCompleted(i, 1024, std::string("fake_hash"));
[email protected]f5920322011-03-24 20:34:16455 message_loop_.RunAllPending();
[email protected]99cb7f82011-07-28 17:27:26456 FileSelected(new_path, id_ptr);
[email protected]594cd7d2010-07-21 03:23:56457 } else {
[email protected]99cb7f82011-07-28 17:27:26458 FileSelected(new_path, id_ptr);
[email protected]f5920322011-03-24 20:34:16459 message_loop_.RunAllPending();
[email protected]47a881b2011-08-29 22:59:21460 OnResponseCompleted(i, 1024, std::string("fake_hash"));
[email protected]594cd7d2010-07-21 03:23:56461 }
462
463 message_loop_.RunAllPending();
[email protected]287b86b2011-02-26 00:11:35464 EXPECT_TRUE(VerifySafetyState(kDownloadRenameCases[i].is_dangerous_file,
465 kDownloadRenameCases[i].is_dangerous_url,
466 i));
[email protected]594cd7d2010-07-21 03:23:56467 }
468}
[email protected]bf68a00b2011-04-07 17:28:26469
470TEST_F(DownloadManagerTest, DownloadInterruptTest) {
471 using ::testing::_;
472 using ::testing::CreateFunctor;
473 using ::testing::Invoke;
474 using ::testing::Return;
475
[email protected]c9994e02011-05-24 20:52:19476 // 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]bf68a00b2011-04-07 17:28:26480 info->download_id = static_cast<int>(0);
481 info->prompt_user_for_save_location = false;
[email protected]30917cb2011-05-10 21:40:47482 info->url_chain.push_back(GURL());
[email protected]9fc114672011-06-15 08:17:48483 info->total_bytes = static_cast<int64>(kTestDataLen);
[email protected]bf68a00b2011-04-07 17:28:26484 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]c9994e02011-05-24 20:52:19488 new MockDownloadFile(info.get(), download_manager_));
[email protected]bf68a00b2011-04-07 17:28:26489 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]c9994e02011-05-24 20:52:19497 download_manager_->CreateDownloadItem(info.get());
[email protected]bf68a00b2011-04-07 17:28:26498
499 DownloadItem* download = GetActiveDownloadItem(0);
500 ASSERT_TRUE(download != NULL);
[email protected]9fc114672011-06-15 08:17:48501 scoped_ptr<DownloadItemModel> download_item_model(
502 new DownloadItemModel(download));
[email protected]bf68a00b2011-04-07 17:28:26503
504 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
[email protected]d8ca4d12011-04-07 22:05:13505 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
[email protected]bf68a00b2011-04-07 17:28:26506
507 download_file->AppendDataToFile(kTestData, kTestDataLen);
508
[email protected]4cd82f72011-05-23 19:15:01509 ContinueDownloadWithPath(download, new_path);
[email protected]bf68a00b2011-04-07 17:28:26510 message_loop_.RunAllPending();
511 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
512
[email protected]9fc114672011-06-15 08:17:48513 int64 error_size = 3;
514 OnDownloadError(0, error_size, -6);
[email protected]bf68a00b2011-04-07 17:28:26515 message_loop_.RunAllPending();
516
517 EXPECT_TRUE(GetActiveDownloadItem(0) == NULL);
[email protected]bf68a00b2011-04-07 17:28:26518 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]bf68a00b2011-04-07 17:28:26524 EXPECT_FALSE(observer->was_opened());
[email protected]9fc114672011-06-15 08:17:48525 EXPECT_FALSE(download->file_externally_removed());
526 EXPECT_EQ(DownloadItem::INTERRUPTED, download->state());
[email protected]7a3b2632011-06-22 20:40:22527 ui::DataUnits amount_units = ui::GetByteDisplayUnits(kTestDataLen);
528 string16 simple_size =
529 ui::FormatBytesWithUnits(error_size, amount_units, false);
[email protected]9fc114672011-06-15 08:17:48530 string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality(
[email protected]7a3b2632011-06-22 20:40:22531 ui::FormatBytesWithUnits(kTestDataLen, amount_units, true));
[email protected]9fc114672011-06-15 08:17:48532 EXPECT_EQ(download_item_model->GetStatusText(),
533 l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_INTERRUPTED,
534 simple_size,
535 simple_total));
[email protected]bf68a00b2011-04-07 17:28:26536
[email protected]54610672011-07-18 18:24:43537 download->Cancel(true);
[email protected]bf68a00b2011-04-07 17:28:26538
[email protected]bf68a00b2011-04-07 17:28:26539 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]bf68a00b2011-04-07 17:28:26545 EXPECT_FALSE(observer->was_opened());
[email protected]9fc114672011-06-15 08:17:48546 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]bf68a00b2011-04-07 17:28:26550}
551
[email protected]47a881b2011-08-29 22:59:21552// Test the behavior of DownloadFileManager and DownloadManager in the event
553// of a file error while writing the download to disk.
554TEST_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]bf68a00b2011-04-07 17:28:26637TEST_F(DownloadManagerTest, DownloadCancelTest) {
638 using ::testing::_;
639 using ::testing::CreateFunctor;
640 using ::testing::Invoke;
641 using ::testing::Return;
642
[email protected]c9994e02011-05-24 20:52:19643 // 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]bf68a00b2011-04-07 17:28:26647 info->download_id = static_cast<int>(0);
648 info->prompt_user_for_save_location = false;
[email protected]30917cb2011-05-10 21:40:47649 info->url_chain.push_back(GURL());
[email protected]bf68a00b2011-04-07 17:28:26650 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]c9994e02011-05-24 20:52:19654 new MockDownloadFile(info.get(), download_manager_));
[email protected]bf68a00b2011-04-07 17:28:26655 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]c9994e02011-05-24 20:52:19663 download_manager_->CreateDownloadItem(info.get());
[email protected]bf68a00b2011-04-07 17:28:26664
665 DownloadItem* download = GetActiveDownloadItem(0);
666 ASSERT_TRUE(download != NULL);
[email protected]9fc114672011-06-15 08:17:48667 scoped_ptr<DownloadItemModel> download_item_model(
668 new DownloadItemModel(download));
[email protected]bf68a00b2011-04-07 17:28:26669
670 EXPECT_EQ(DownloadItem::IN_PROGRESS, download->state());
[email protected]d8ca4d12011-04-07 22:05:13671 scoped_ptr<ItemObserver> observer(new ItemObserver(download));
[email protected]bf68a00b2011-04-07 17:28:26672
[email protected]4cd82f72011-05-23 19:15:01673 ContinueDownloadWithPath(download, new_path);
[email protected]bf68a00b2011-04-07 17:28:26674 message_loop_.RunAllPending();
675 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
676
677 download_file->AppendDataToFile(kTestData, kTestDataLen);
678
[email protected]54610672011-07-18 18:24:43679 download->Cancel(false);
[email protected]bf68a00b2011-04-07 17:28:26680 message_loop_.RunAllPending();
681
[email protected]54610672011-07-18 18:24:43682 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
[email protected]bf68a00b2011-04-07 17:28:26683 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]bf68a00b2011-04-07 17:28:26689 EXPECT_FALSE(observer->was_opened());
[email protected]9fc114672011-06-15 08:17:48690 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]bf68a00b2011-04-07 17:28:26694
695 EXPECT_FALSE(file_util::PathExists(new_path));
696 EXPECT_FALSE(file_util::PathExists(cr_path));
697}
[email protected]8fa1eeb52011-04-13 14:18:02698
699TEST_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]ec865262011-08-23 20:01:48722 int uniquifier = DownloadFile::GetUniquePathNumber(new_path);
[email protected]8fa1eeb52011-04-13 14:18:02723 FilePath unique_new_path = new_path;
724 EXPECT_NE(0, uniquifier);
[email protected]ec865262011-08-23 20:01:48725 DownloadFile::AppendNumberToPath(&unique_new_path, uniquifier);
[email protected]8fa1eeb52011-04-13 14:18:02726
[email protected]c9994e02011-05-24 20:52:19727 // 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]8fa1eeb52011-04-13 14:18:02731 info->download_id = static_cast<int>(0);
732 info->prompt_user_for_save_location = true;
[email protected]30917cb2011-05-10 21:40:47733 info->url_chain.push_back(GURL());
[email protected]8fa1eeb52011-04-13 14:18:02734
[email protected]c9994e02011-05-24 20:52:19735 download_manager_->CreateDownloadItem(info.get());
[email protected]8fa1eeb52011-04-13 14:18:02736
737 DownloadItem* download = GetActiveDownloadItem(0);
738 ASSERT_TRUE(download != NULL);
[email protected]9fc114672011-06-15 08:17:48739 scoped_ptr<DownloadItemModel> download_item_model(
740 new DownloadItemModel(download));
[email protected]8fa1eeb52011-04-13 14:18:02741
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]c9994e02011-05-24 20:52:19750 new DownloadFile(info.get(), download_manager_));
[email protected]8fa1eeb52011-04-13 14:18:02751 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]4cd82f72011-05-23 19:15:01757 ContinueDownloadWithPath(download, new_path);
[email protected]8fa1eeb52011-04-13 14:18:02758 message_loop_.RunAllPending();
759 EXPECT_TRUE(GetActiveDownloadItem(0) != NULL);
760
761 download_file->AppendDataToFile(kTestData, kTestDataLen);
762
763 // Finish the download.
[email protected]47a881b2011-08-29 22:59:21764 OnResponseCompleted(0, kTestDataLen, "");
[email protected]8fa1eeb52011-04-13 14:18:02765 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]8fa1eeb52011-04-13 14:18:02775 EXPECT_FALSE(observer->was_opened());
[email protected]9fc114672011-06-15 08:17:48776 EXPECT_FALSE(download->file_externally_removed());
[email protected]8fa1eeb52011-04-13 14:18:02777 EXPECT_EQ(DownloadItem::COMPLETE, download->state());
[email protected]9fc114672011-06-15 08:17:48778 EXPECT_EQ(download_item_model->GetStatusText(), ASCIIToUTF16(""));
[email protected]8fa1eeb52011-04-13 14:18:02779
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]9fc114672011-06-15 08:17:48787
788TEST_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]47a881b2011-08-29 22:59:21840 OnResponseCompleted(0, kTestDataLen, "");
[email protected]9fc114672011-06-15 08:17:48841 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}