Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: chrome/browser/download/chrome_download_manager_delegate_unittest.cc

Issue 10704052: Download filename determination refactor (3/3) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge with r148594 to and resolve conflicts with r148576 Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_path.h" 5 #include "base/file_path.h"
6 #include "base/file_util.h" 6 #include "base/file_util.h"
7 #include "base/message_loop.h" 7 #include "base/message_loop.h"
8 #include "base/observer_list.h" 8 #include "base/observer_list.h"
9 #include "base/scoped_temp_dir.h" 9 #include "base/scoped_temp_dir.h"
10 #include "base/stl_util.h" 10 #include "base/stl_util.h"
(...skipping 19 matching lines...) Expand all
30 using safe_browsing::DownloadProtectionService; 30 using safe_browsing::DownloadProtectionService;
31 using ::testing::AtMost; 31 using ::testing::AtMost;
32 using ::testing::Invoke; 32 using ::testing::Invoke;
33 using ::testing::Return; 33 using ::testing::Return;
34 using ::testing::ReturnPointee; 34 using ::testing::ReturnPointee;
35 using ::testing::ReturnRef; 35 using ::testing::ReturnRef;
36 using ::testing::ReturnRefOfCopy; 36 using ::testing::ReturnRefOfCopy;
37 using ::testing::WithArg; 37 using ::testing::WithArg;
38 using ::testing::_; 38 using ::testing::_;
39 39
40 namespace {
41
40 // Google Mock action that posts a task to the current message loop that invokes 42 // Google Mock action that posts a task to the current message loop that invokes
41 // the first argument of the mocked method as a callback. Said argument must be 43 // the first argument of the mocked method as a callback. Said argument must be
42 // a base::Callback<void(ParamType)>. |result| must be of |ParamType| and is 44 // a base::Callback<void(ParamType)>. |result| must be of |ParamType| and is
43 // bound as that parameter. 45 // bound as that parameter.
44 // Example: 46 // Example:
45 // class FooClass { 47 // class FooClass {
46 // public: 48 // public:
47 // virtual void Foo(base::Callback<void(bool)> callback); 49 // virtual void Foo(base::Callback<void(bool)> callback);
48 // }; 50 // };
49 // ... 51 // ...
50 // EXPECT_CALL(mock_fooclass_instance, Foo(callback)) 52 // EXPECT_CALL(mock_fooclass_instance, Foo(callback))
51 // .WillOnce(ScheduleCallback(false)); 53 // .WillOnce(ScheduleCallback(false));
52 ACTION_P(ScheduleCallback, result) { 54 ACTION_P(ScheduleCallback, result) {
53 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result)); 55 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result));
54 } 56 }
55 57
56 // Matches a safe_browsing::DownloadProtectionService::DownloadInfo that has 58 // Matches a safe_browsing::DownloadProtectionService::DownloadInfo that has
57 // |url| as the first URL in the |download_url_chain|. 59 // |url| as the first URL in the |download_url_chain|.
58 // Example: 60 // Example:
59 // EXPECT_CALL(Foo(InfoMatchinURL(url))) 61 // EXPECT_CALL(Foo(InfoMatchinURL(url)))
60 MATCHER_P(InfoMatchingURL, url, "DownloadInfo matching URL " + url.spec()) { 62 MATCHER_P(InfoMatchingURL, url, "DownloadInfo matching URL " + url.spec()) {
61 return url == arg.download_url_chain.front(); 63 return url == arg.download_url_chain.front();
62 } 64 }
63 65
64 namespace {
65
66 // Used with DownloadTestCase. Indicates the type of test case. The expectations 66 // Used with DownloadTestCase. Indicates the type of test case. The expectations
67 // for the test is set based on the type. 67 // for the test is set based on the type.
68 enum TestCaseType { 68 enum TestCaseType {
69 SAVE_AS, 69 SAVE_AS,
70 AUTOMATIC, 70 AUTOMATIC,
71 FORCED // Requires that forced_file_path be non-empty. 71 FORCED // Requires that forced_file_path be non-empty.
72 }; 72 };
73 73
74 // Used with DownloadTestCase. Indicates whether the a file should be 74 // Used with DownloadTestCase. Indicates whether the a file should be
75 // overwritten. 75 // overwritten.
76 enum TestCaseExpectOverwrite { 76 enum TestCaseExpectOverwrite {
77 EXPECT_OVERWRITE, 77 EXPECT_OVERWRITE,
78 EXPECT_NO_OVERWRITE 78 EXPECT_NO_OVERWRITE
79 }; 79 };
80 80
81 // Used with DownloadTestCase. Type of intermediate filename to expect. 81 // Used with DownloadTestCase. Type of intermediate filename to expect.
82 enum TestCaseExpectIntermediate { 82 enum TestCaseExpectIntermediate {
83 EXPECT_CRDOWNLOAD, // Expect path/to/target.crdownload 83 EXPECT_CRDOWNLOAD, // Expect path/to/target.crdownload
84 EXPECT_UNCONFIRMED // Expect path/to/Unconfirmed xxx.crdownload 84 EXPECT_UNCONFIRMED // Expect path/to/Unconfirmed xxx.crdownload
85 }; 85 };
86 86
87 // Typical download test case. Used with 87 // Typical download test case. Used with
88 // ChromeDownloadManagerDelegateTest::RunTestCase(). 88 // ChromeDownloadManagerDelegateTest::RunTestCase().
89 struct DownloadTestCase { 89 struct DownloadTestCase {
90 // Type of test. 90 // Type of test.
91 TestCaseType test_type; 91 TestCaseType test_type;
92 92
93 // The |danger_type| value is used to determine the behavior of 93 // The |danger_type| is the expected danger type for the download as
94 // DownloadProtectionService::IsSupportedDownload(), GetUrlCheckResult() and 94 // determined by CDMD. This value is also used to determine the behavior of
95 // well as set expectations for GetDangerType() as necessary for flagging the 95 // DownloadProtectionService::IsSupportedDownload(), CDMD::CheckDownloadUrl()
96 // download with as a dangerous download of type |danger_type|. 96 // as necessary for flagging the download with as a dangerous download of type
97 // |danger_type|.
97 content::DownloadDangerType danger_type; 98 content::DownloadDangerType danger_type;
98 99
99 // Value of GetURL() 100 // Value of DownloadItem::GetURL()
100 const char* url; 101 const char* url;
101 102
102 // Value of GetMimeType() 103 // Value of DownloadItem::GetMimeType()
103 const char* mime_type; 104 const char* mime_type;
104 105
105 // Should be non-empty if |test_type| == FORCED. Value of GetForcedFilePath(). 106 // Should be non-empty if |test_type| == FORCED. Value of GetForcedFilePath().
106 const FilePath::CharType* forced_file_path; 107 const FilePath::CharType* forced_file_path;
107 108
108 // Expected final download path. Specified relative to the test download path. 109 // Expected final download path. Specified relative to the test download path.
110 // If the user is presented with a file chooser, this path will also be the
111 // response sent back from the file chooser.
109 const FilePath::CharType* expected_target_path; 112 const FilePath::CharType* expected_target_path;
110 113
111 // Expected target disposition. 114 // The path to expect as the suggested path if the user will be prompted for a
115 // download path.
116 const FilePath::CharType* expected_prompt_path;
117
118 // Expected target disposition. If this is TARGET_DISPOSITION_PROMPT, then the
119 // test run will expect ChromeDownloadManagerDelegate to prompt the user for a
120 // download location.
112 DownloadItem::TargetDisposition expected_disposition; 121 DownloadItem::TargetDisposition expected_disposition;
113 122
114 // Type of intermediate path to expect. 123 // Type of intermediate path to expect.
115 TestCaseExpectIntermediate expected_intermediate; 124 TestCaseExpectIntermediate expected_intermediate;
116 }; 125 };
117 126
118 #if defined(ENABLE_SAFE_BROWSING) 127 #if defined(ENABLE_SAFE_BROWSING)
119 // DownloadProtectionService with mock methods. Since the SafeBrowsingService is 128 // DownloadProtectionService with mock methods. Since the SafeBrowsingService is
120 // set to NULL, it is not safe to call any non-mocked methods other than 129 // set to NULL, it is not safe to call any non-mocked methods other than
121 // SetEnabled() and enabled(). 130 // SetEnabled() and enabled().
(...skipping 17 matching lines...) Expand all
139 // DownloadProtectionService and IsDangerousFile. 148 // DownloadProtectionService and IsDangerousFile.
140 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate { 149 class TestChromeDownloadManagerDelegate : public ChromeDownloadManagerDelegate {
141 public: 150 public:
142 explicit TestChromeDownloadManagerDelegate(Profile* profile) 151 explicit TestChromeDownloadManagerDelegate(Profile* profile)
143 : ChromeDownloadManagerDelegate(profile) { 152 : ChromeDownloadManagerDelegate(profile) {
144 #if defined(ENABLE_SAFE_BROWSING) 153 #if defined(ENABLE_SAFE_BROWSING)
145 download_protection_service_.reset(new TestDownloadProtectionService()); 154 download_protection_service_.reset(new TestDownloadProtectionService());
146 download_protection_service_->SetEnabled(true); 155 download_protection_service_->SetEnabled(true);
147 #endif 156 #endif
148 } 157 }
158
149 virtual safe_browsing::DownloadProtectionService* 159 virtual safe_browsing::DownloadProtectionService*
150 GetDownloadProtectionService() OVERRIDE { 160 GetDownloadProtectionService() OVERRIDE {
151 #if defined(ENABLE_SAFE_BROWSING) 161 #if defined(ENABLE_SAFE_BROWSING)
152 return download_protection_service_.get(); 162 return download_protection_service_.get();
153 #else 163 #else
154 return NULL; 164 return NULL;
155 #endif 165 #endif
156 } 166 }
167
157 virtual bool IsDangerousFile(const DownloadItem& download, 168 virtual bool IsDangerousFile(const DownloadItem& download,
158 const FilePath& suggested_path, 169 const FilePath& suggested_path,
159 bool visited_referrer_before) OVERRIDE { 170 bool visited_referrer_before) OVERRIDE {
160 // The implementaion of ChromeDownloadManagerDelegate::IsDangerousFile() is 171 // The implementaion of ChromeDownloadManagerDelegate::IsDangerousFile() is
161 // sensitive to a number of external factors (e.g. whether off-store 172 // sensitive to a number of external factors (e.g. whether off-store
162 // extension installs are allowed, whether a given extension download is 173 // extension installs are allowed, whether a given extension download is
163 // approved, whether the user wants files of a given type to be opened 174 // approved, whether the user wants files of a given type to be opened
164 // automatically etc...). We should test these specifically, but for other 175 // automatically etc...). We should test these specifically, but for other
165 // tests, we keep the IsDangerousFile() test simple so as not to make the 176 // tests, we keep the IsDangerousFile() test simple so as not to make the
166 // tests flaky. 177 // tests flaky.
167 return suggested_path.MatchesExtension(FILE_PATH_LITERAL(".jar")) || 178 return suggested_path.MatchesExtension(FILE_PATH_LITERAL(".jar")) ||
168 suggested_path.MatchesExtension(FILE_PATH_LITERAL(".exe")); 179 suggested_path.MatchesExtension(FILE_PATH_LITERAL(".exe"));
169 } 180 }
181
170 virtual void GetReservedPath( 182 virtual void GetReservedPath(
171 content::DownloadItem& download, 183 content::DownloadItem& download,
172 const FilePath& target_path, 184 const FilePath& target_path,
173 const FilePath& default_download_path, 185 const FilePath& default_download_path,
174 bool should_uniquify_path, 186 bool should_uniquify_path,
175 const DownloadPathReservationTracker::ReservedPathCallback callback) { 187 const DownloadPathReservationTracker::ReservedPathCallback& callback)
188 OVERRIDE {
176 // Pretend the path reservation succeeded without any change to 189 // Pretend the path reservation succeeded without any change to
177 // |target_path|. 190 // |target_path|.
178 MessageLoop::current()->PostTask(FROM_HERE, 191 MessageLoop::current()->PostTask(FROM_HERE,
179 base::Bind(callback, target_path, true)); 192 base::Bind(callback, target_path, true));
180 } 193 }
181 194
195 // During tests, we want to mock the behavior of this method.
196 MOCK_METHOD3(ChooseDownloadPath,
197 void(content::DownloadItem*,
198 const FilePath&,
199 const FileSelectedCallback&));
200
182 #if defined(ENABLE_SAFE_BROWSING) 201 #if defined(ENABLE_SAFE_BROWSING)
183 // A TestDownloadProtectionService* is convenient for setting up mocks. 202 // A TestDownloadProtectionService* is convenient for setting up mocks.
184 TestDownloadProtectionService* test_download_protection_service() { 203 TestDownloadProtectionService* test_download_protection_service() {
185 return download_protection_service_.get(); 204 return download_protection_service_.get();
186 } 205 }
187 #endif 206 #endif
188 207
189 private: 208 private:
190 ~TestChromeDownloadManagerDelegate() {} 209 ~TestChromeDownloadManagerDelegate() {}
191 210
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 247
229 // Set the kDownloadDefaultDirectory user preference to |path|. 248 // Set the kDownloadDefaultDirectory user preference to |path|.
230 void SetDefaultDownloadPath(const FilePath& path); 249 void SetDefaultDownloadPath(const FilePath& path);
231 250
232 // Set the kDownloadDefaultDirectory managed preference to |path|. 251 // Set the kDownloadDefaultDirectory managed preference to |path|.
233 void SetManagedDownloadPath(const FilePath& path); 252 void SetManagedDownloadPath(const FilePath& path);
234 253
235 // Set the kPromptForDownload user preference to |prompt|. 254 // Set the kPromptForDownload user preference to |prompt|.
236 void SetPromptForDownload(bool prompt); 255 void SetPromptForDownload(bool prompt);
237 256
238 // Verifies that the intermediate path in |intermediate| is the path that is
239 // expected for |target| given the intermediate path type in |expectation|.
240 void VerifyIntermediatePath(TestCaseExpectIntermediate expectation,
241 const FilePath& target,
242 const FilePath& intermediate);
243
244 const FilePath& default_download_path() const; 257 const FilePath& default_download_path() const;
245 TestChromeDownloadManagerDelegate* delegate(); 258 TestChromeDownloadManagerDelegate* delegate();
246 content::MockDownloadManager* download_manager(); 259 content::MockDownloadManager* download_manager();
247 DownloadPrefs* download_prefs(); 260 DownloadPrefs* download_prefs();
248 261
249 private: 262 private:
263 // Verifies that |target_path|, |disposition|, |danger_type| and
264 // |intermediate_path| matches the expectations of |test_case|.
265 void DownloadTargetVerifier(const DownloadTestCase* test_case,
266 const FilePath& target_path,
267 DownloadItem::TargetDisposition disposition,
268 content::DownloadDangerType danger_type,
269 const FilePath& intermediate_path);
270
250 MessageLoopForUI message_loop_; 271 MessageLoopForUI message_loop_;
251 TestingPrefService* pref_service_; 272 TestingPrefService* pref_service_;
252 ScopedTempDir test_download_dir_; 273 ScopedTempDir test_download_dir_;
253 TestingProfile profile_; 274 TestingProfile profile_;
254 content::TestBrowserThread ui_thread_; 275 content::TestBrowserThread ui_thread_;
255 content::TestBrowserThread file_thread_; 276 content::TestBrowserThread file_thread_;
256 scoped_refptr<content::MockDownloadManager> download_manager_; 277 scoped_refptr<content::MockDownloadManager> download_manager_;
257 scoped_refptr<TestChromeDownloadManagerDelegate> delegate_; 278 scoped_refptr<TestChromeDownloadManagerDelegate> delegate_;
258 }; 279 };
259 280
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 .WillOnce(Return(maybe_dangerous)); 379 .WillOnce(Return(maybe_dangerous));
359 } 380 }
360 #else // ENABLE_SAFE_BROWSING 381 #else // ENABLE_SAFE_BROWSING
361 // If safe browsing is not enabled, then these tests would fail. If such a 382 // If safe browsing is not enabled, then these tests would fail. If such a
362 // test was added, then fail early. 383 // test was added, then fail early.
363 EXPECT_NE(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, test_case.danger_type); 384 EXPECT_NE(content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, test_case.danger_type);
364 EXPECT_NE(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, 385 EXPECT_NE(content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
365 test_case.danger_type); 386 test_case.danger_type);
366 #endif // !ENABLE_SAFE_BROWSING 387 #endif // !ENABLE_SAFE_BROWSING
367 388
368 // Expectations for filename determination results. 389 DownloadItem::TargetDisposition initial_disposition =
369 FilePath expected_target_path( 390 (test_case.test_type == SAVE_AS) ?
370 GetPathInDownloadDir(test_case.expected_target_path)); 391 DownloadItem::TARGET_DISPOSITION_PROMPT :
371 { 392 DownloadItem::TARGET_DISPOSITION_OVERWRITE;
372 ::testing::Sequence s1, s2, s3; 393 EXPECT_CALL(*item, GetTargetFilePath())
373 DownloadItem::TargetDisposition initial_disposition = 394 .WillRepeatedly(ReturnRefOfCopy(FilePath()));
374 (test_case.test_type == SAVE_AS) ? 395 EXPECT_CALL(*item, GetTargetDisposition())
375 DownloadItem::TARGET_DISPOSITION_PROMPT : 396 .WillRepeatedly(Return(initial_disposition));
376 DownloadItem::TARGET_DISPOSITION_OVERWRITE; 397 EXPECT_CALL(*item, GetDangerType())
377 EXPECT_CALL(*item, GetTargetFilePath()) 398 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
378 .InSequence(s1) 399
379 .WillRepeatedly(ReturnRefOfCopy(FilePath())); 400 if (test_case.expected_disposition ==
380 EXPECT_CALL(*item, GetTargetDisposition()) 401 DownloadItem::TARGET_DISPOSITION_PROMPT) {
381 .InSequence(s2) 402 FilePath expected_prompt_path = GetPathInDownloadDir(
382 .WillRepeatedly(Return(initial_disposition)); 403 test_case.expected_prompt_path);
383 EXPECT_CALL(*item, GetDangerType()) 404 FilePath expected_target_path = GetPathInDownloadDir(
384 .InSequence(s3) 405 test_case.expected_target_path);
385 .WillRepeatedly(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS)); 406 EXPECT_CALL(*delegate_,
386 EXPECT_CALL(*item, OnTargetPathDetermined(expected_target_path, 407 ChooseDownloadPath(item, expected_prompt_path, _))
387 test_case.expected_disposition, 408 .WillOnce(WithArg<2>(ScheduleCallback(expected_target_path)));
388 test_case.danger_type))
389 .InSequence(s1, s2, s3);
390 EXPECT_CALL(*item, GetTargetFilePath())
391 .InSequence(s1)
392 .WillRepeatedly(ReturnRef(expected_target_path));
393 EXPECT_CALL(*item, GetTargetDisposition())
394 .InSequence(s2)
395 .WillRepeatedly(Return(test_case.expected_disposition));
396 EXPECT_CALL(*item, GetDangerType())
397 .InSequence(s3)
398 .WillRepeatedly(Return(test_case.danger_type));
399 } 409 }
400 410
401 // RestartDownload() should be called at this point.
402 EXPECT_CALL(*download_manager_, RestartDownload(item->GetId()));
403 EXPECT_CALL(*download_manager_, LastDownloadPath())
404 .WillRepeatedly(Return(FilePath()));
405
406 // Kick off the test. 411 // Kick off the test.
407 EXPECT_FALSE(delegate_->ShouldStartDownload(item->GetId())); 412 base::WeakPtrFactory<ChromeDownloadManagerDelegateTest> factory(this);
413 EXPECT_TRUE(delegate_->DetermineDownloadTarget(
414 item,
415 base::Bind(&ChromeDownloadManagerDelegateTest::DownloadTargetVerifier,
416 factory.GetWeakPtr(), base::Unretained(&test_case))));
408 message_loop_.RunAllPending(); 417 message_loop_.RunAllPending();
409 418 VerifyAndClearExpectations();
410 // Now query the intermediate path.
411 EXPECT_CALL(*item, GetDangerType())
412 .WillOnce(Return(test_case.danger_type));
413 bool ok_to_overwrite = false;
414 FilePath intermediate_path = delegate_->GetIntermediatePath(*item);
415 EXPECT_FALSE(ok_to_overwrite);
416 VerifyIntermediatePath(test_case.expected_intermediate,
417 GetPathInDownloadDir(test_case.expected_target_path),
418 intermediate_path);
419 } 419 }
420 420
421 void ChromeDownloadManagerDelegateTest::RunTestCases( 421 void ChromeDownloadManagerDelegateTest::RunTestCases(
422 const DownloadTestCase test_cases[], 422 const DownloadTestCase test_cases[],
423 size_t test_case_count) { 423 size_t test_case_count) {
424 for (size_t i = 0; i < test_case_count; ++i) { 424 for (size_t i = 0; i < test_case_count; ++i) {
425 scoped_ptr<content::MockDownloadItem> item(CreateActiveDownloadItem(i)); 425 scoped_ptr<content::MockDownloadItem> item(CreateActiveDownloadItem(i));
426 SCOPED_TRACE(testing::Message() << "Running test case " << i); 426 SCOPED_TRACE(testing::Message() << "Running test case " << i);
427 RunTestCaseWithDownloadItem(test_cases[i], item.get()); 427 RunTestCaseWithDownloadItem(test_cases[i], item.get());
428 } 428 }
429 } 429 }
430 430
431 void ChromeDownloadManagerDelegateTest::SetDefaultDownloadPath( 431 void ChromeDownloadManagerDelegateTest::SetDefaultDownloadPath(
432 const FilePath& path) { 432 const FilePath& path) {
433 pref_service_->SetFilePath(prefs::kDownloadDefaultDirectory, path); 433 pref_service_->SetFilePath(prefs::kDownloadDefaultDirectory, path);
434 } 434 }
435 435
436 void ChromeDownloadManagerDelegateTest::SetManagedDownloadPath( 436 void ChromeDownloadManagerDelegateTest::SetManagedDownloadPath(
437 const FilePath& path) { 437 const FilePath& path) {
438 pref_service_->SetManagedPref(prefs::kDownloadDefaultDirectory, 438 pref_service_->SetManagedPref(prefs::kDownloadDefaultDirectory,
439 base::CreateFilePathValue(path)); 439 base::CreateFilePathValue(path));
440 } 440 }
441 441
442 void ChromeDownloadManagerDelegateTest::SetPromptForDownload(bool prompt) { 442 void ChromeDownloadManagerDelegateTest::SetPromptForDownload(bool prompt) {
443 pref_service_->SetBoolean(prefs::kPromptForDownload, prompt); 443 pref_service_->SetBoolean(prefs::kPromptForDownload, prompt);
444 } 444 }
445 445
446 void ChromeDownloadManagerDelegateTest::VerifyIntermediatePath( 446 void ChromeDownloadManagerDelegateTest::DownloadTargetVerifier(
447 TestCaseExpectIntermediate expectation, 447 const DownloadTestCase* test_case,
448 const FilePath& target, 448 const FilePath& target_path,
449 const FilePath& intermediate) { 449 DownloadItem::TargetDisposition disposition,
450 if (expectation == EXPECT_CRDOWNLOAD) { 450 content::DownloadDangerType danger_type,
451 EXPECT_EQ(download_util::GetCrDownloadPath(target).value(), 451 const FilePath& intermediate_path) {
452 intermediate.value()); 452 FilePath expected_target_path(
453 GetPathInDownloadDir(test_case->expected_target_path));
454 EXPECT_EQ(expected_target_path.value(), target_path.value());
455 EXPECT_EQ(test_case->expected_disposition, disposition);
456 EXPECT_EQ(test_case->danger_type, danger_type);
457
458 if (test_case->expected_intermediate == EXPECT_CRDOWNLOAD) {
459 EXPECT_EQ(download_util::GetCrDownloadPath(target_path).value(),
460 intermediate_path.value());
453 } else { 461 } else {
454 // The paths (in English) look like: /path/Unconfirmed xxx.crdownload. 462 // The paths (in English) look like: /path/Unconfirmed xxx.crdownload.
455 // Of this, we only check that the path is: 463 // Of this, we only check that the path is:
456 // 1. Not "/path/target.crdownload", 464 // 1. Not "/path/target.crdownload",
457 // 2. Points to the same directory as the target. 465 // 2. Points to the same directory as the target.
458 // 3. Has extension ".crdownload". 466 // 3. Has extension ".crdownload".
459 EXPECT_NE(download_util::GetCrDownloadPath(target).value(), 467 EXPECT_NE(download_util::GetCrDownloadPath(target_path).value(),
460 intermediate.value()); 468 intermediate_path.value());
461 EXPECT_EQ(target.DirName().value(), 469 EXPECT_EQ(target_path.DirName().value(),
462 intermediate.DirName().value()); 470 intermediate_path.DirName().value());
463 EXPECT_TRUE(intermediate.MatchesExtension( 471 EXPECT_TRUE(intermediate_path.MatchesExtension(
464 FILE_PATH_LITERAL(".crdownload"))); 472 FILE_PATH_LITERAL(".crdownload")));
465 } 473 }
466 } 474 }
467 475
468 const FilePath& ChromeDownloadManagerDelegateTest::default_download_path() 476 const FilePath& ChromeDownloadManagerDelegateTest::default_download_path()
469 const { 477 const {
470 return test_download_dir_.path(); 478 return test_download_dir_.path();
471 } 479 }
472 480
473 TestChromeDownloadManagerDelegate* 481 TestChromeDownloadManagerDelegate*
474 ChromeDownloadManagerDelegateTest::delegate() { 482 ChromeDownloadManagerDelegateTest::delegate() {
475 return delegate_.get(); 483 return delegate_.get();
476 } 484 }
477 485
478 content::MockDownloadManager* 486 content::MockDownloadManager*
479 ChromeDownloadManagerDelegateTest::download_manager() { 487 ChromeDownloadManagerDelegateTest::download_manager() {
480 return download_manager_.get(); 488 return download_manager_.get();
481 } 489 }
482 490
483 DownloadPrefs* ChromeDownloadManagerDelegateTest::download_prefs() { 491 DownloadPrefs* ChromeDownloadManagerDelegateTest::download_prefs() {
484 return delegate_->download_prefs(); 492 return delegate_->download_prefs();
485 } 493 }
486 494
487 } // namespace 495 } // namespace
488 496
489 TEST_F(ChromeDownloadManagerDelegateTest, ShouldStartDownload_Invalid) {
490 // Invalid download ID shouldn't do anything.
491 EXPECT_CALL(*download_manager(), GetActiveDownloadItem(-1))
492 .WillOnce(Return(reinterpret_cast<DownloadItem*>(NULL)));
493 EXPECT_FALSE(delegate()->ShouldStartDownload(-1));
494 }
495
496 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_Basic) { 497 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_Basic) {
497 const DownloadTestCase kBasicTestCases[] = { 498 const DownloadTestCase kBasicTestCases[] = {
498 { 499 {
499 // 0: Automatic Safe 500 // 0: Automatic Safe
500 AUTOMATIC, 501 AUTOMATIC,
501 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 502 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
502 "http://example.com/foo.txt", "text/plain", 503 "http://example.com/foo.txt", "text/plain",
503 FILE_PATH_LITERAL(""), 504 FILE_PATH_LITERAL(""),
504 505
505 FILE_PATH_LITERAL("foo.txt"), 506 FILE_PATH_LITERAL("foo.txt"),
507 FILE_PATH_LITERAL(""),
506 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 508 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
507 509
508 EXPECT_CRDOWNLOAD 510 EXPECT_CRDOWNLOAD
509 }, 511 },
510 512
511 { 513 {
512 // 1: Save_As Safe 514 // 1: Save_As Safe
513 SAVE_AS, 515 SAVE_AS,
514 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 516 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
515 "http://example.com/foo.txt", "text/plain", 517 "http://example.com/foo.txt", "text/plain",
516 FILE_PATH_LITERAL(""), 518 FILE_PATH_LITERAL(""),
517 519
518 FILE_PATH_LITERAL("foo.txt"), 520 FILE_PATH_LITERAL("foo.txt"),
521 FILE_PATH_LITERAL("foo.txt"),
519 DownloadItem::TARGET_DISPOSITION_PROMPT, 522 DownloadItem::TARGET_DISPOSITION_PROMPT,
520 523
521 EXPECT_CRDOWNLOAD 524 EXPECT_CRDOWNLOAD
522 }, 525 },
523 526
524 { 527 {
525 // 2: Automatic Dangerous 528 // 2: Automatic Dangerous
526 AUTOMATIC, 529 AUTOMATIC,
527 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE, 530 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
528 "http://example.com/foo.exe", "", 531 "http://example.com/foo.exe", "",
529 FILE_PATH_LITERAL(""), 532 FILE_PATH_LITERAL(""),
530 533
531 FILE_PATH_LITERAL("foo.exe"), 534 FILE_PATH_LITERAL("foo.exe"),
535 FILE_PATH_LITERAL(""),
532 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 536 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
533 537
534 EXPECT_UNCONFIRMED 538 EXPECT_UNCONFIRMED
535 }, 539 },
536 540
537 { 541 {
538 // 3 Forced Safe 542 // 3 Forced Safe
539 FORCED, 543 FORCED,
540 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 544 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
541 "http://example.com/foo.txt", "", 545 "http://example.com/foo.txt", "",
542 FILE_PATH_LITERAL("forced-foo.txt"), 546 FILE_PATH_LITERAL("forced-foo.txt"),
543 547
544 FILE_PATH_LITERAL("forced-foo.txt"), 548 FILE_PATH_LITERAL("forced-foo.txt"),
549 FILE_PATH_LITERAL(""),
545 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 550 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
546 551
547 EXPECT_CRDOWNLOAD 552 EXPECT_CRDOWNLOAD
548 }, 553 },
549 554
550 #if defined(ENABLE_SAFE_BROWSING) 555 #if defined(ENABLE_SAFE_BROWSING)
551 // These test cases are only applicable if safe browsing is enabled. Without 556 // These test cases are only applicable if safe browsing is enabled. Without
552 // it, these are equivalent to FORCED/SAFE and SAFE_AS/SAFE respectively. 557 // it, these are equivalent to FORCED/SAFE and SAFE_AS/SAFE respectively.
553 { 558 {
554 // 4: Forced Dangerous. As above. .jar is considered to be one of the file 559 // 4: Forced Dangerous. As above. .jar is considered to be one of the file
555 // types supportred by safe browsing. 560 // types supportred by safe browsing.
556 FORCED, 561 FORCED,
557 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, 562 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
558 "http://example.com/foo.exe", "", 563 "http://example.com/foo.exe", "",
559 FILE_PATH_LITERAL("forced-foo.exe"), 564 FILE_PATH_LITERAL("forced-foo.exe"),
560 565
561 FILE_PATH_LITERAL("forced-foo.exe"), 566 FILE_PATH_LITERAL("forced-foo.exe"),
567 FILE_PATH_LITERAL(""),
562 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 568 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
563 569
564 EXPECT_UNCONFIRMED 570 EXPECT_UNCONFIRMED
565 }, 571 },
566 572
567 { 573 {
568 // 5: Save_As Dangerous. 574 // 5: Save_As Dangerous.
569 SAVE_AS, 575 SAVE_AS,
570 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, 576 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
571 "http://example.com/foo.exe", "", 577 "http://example.com/foo.exe", "",
572 FILE_PATH_LITERAL(""), 578 FILE_PATH_LITERAL(""),
573 579
574 FILE_PATH_LITERAL("foo.exe"), 580 FILE_PATH_LITERAL("foo.exe"),
581 FILE_PATH_LITERAL("foo.exe"),
575 DownloadItem::TARGET_DISPOSITION_PROMPT, 582 DownloadItem::TARGET_DISPOSITION_PROMPT,
576 583
577 EXPECT_UNCONFIRMED 584 EXPECT_UNCONFIRMED
578 } 585 }
579 #endif 586 #endif
580 }; 587 };
581 588
582 RunTestCases(kBasicTestCases, arraysize(kBasicTestCases)); 589 RunTestCases(kBasicTestCases, arraysize(kBasicTestCases));
583 } 590 }
584 591
585 #if defined(ENABLE_SAFE_BROWSING) 592 #if defined(ENABLE_SAFE_BROWSING)
586 // The SafeBrowsing URL check is performed early. Make sure that a download item 593 // The SafeBrowsing URL check is performed early. Make sure that a download item
587 // that has been marked as DANGEROUS_URL behaves correctly. 594 // that has been marked as DANGEROUS_URL behaves correctly.
588 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_DangerousURL) { 595 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_DangerousURL) {
589 const DownloadTestCase kDangerousURLTestCases[] = { 596 const DownloadTestCase kDangerousURLTestCases[] = {
590 { 597 {
591 // 0: Automatic Dangerous URL 598 // 0: Automatic Dangerous URL
592 AUTOMATIC, 599 AUTOMATIC,
593 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, 600 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
594 "http://phishing.example.com/foo.txt", "", 601 "http://phishing.example.com/foo.txt", "",
595 FILE_PATH_LITERAL(""), 602 FILE_PATH_LITERAL(""),
596 603
597 FILE_PATH_LITERAL("foo.txt"), 604 FILE_PATH_LITERAL("foo.txt"),
605 FILE_PATH_LITERAL(""),
598 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 606 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
599 607
600 EXPECT_UNCONFIRMED 608 EXPECT_UNCONFIRMED
601 }, 609 },
602 610
603 { 611 {
604 // 1: Save As Dangerous URL 612 // 1: Save As Dangerous URL
605 SAVE_AS, 613 SAVE_AS,
606 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, 614 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
607 "http://phishing.example.com/foo.txt", "", 615 "http://phishing.example.com/foo.txt", "",
608 FILE_PATH_LITERAL(""), 616 FILE_PATH_LITERAL(""),
609 617
610 FILE_PATH_LITERAL("foo.txt"), 618 FILE_PATH_LITERAL("foo.txt"),
619 FILE_PATH_LITERAL("foo.txt"),
611 DownloadItem::TARGET_DISPOSITION_PROMPT, 620 DownloadItem::TARGET_DISPOSITION_PROMPT,
612 621
613 EXPECT_UNCONFIRMED 622 EXPECT_UNCONFIRMED
614 }, 623 },
615 624
616 { 625 {
617 // 2: Forced Dangerous URL 626 // 2: Forced Dangerous URL
618 FORCED, 627 FORCED,
619 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, 628 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
620 "http://phishing.example.com/foo.txt", "", 629 "http://phishing.example.com/foo.txt", "",
621 FILE_PATH_LITERAL("forced-foo.txt"), 630 FILE_PATH_LITERAL("forced-foo.txt"),
622 631
623 FILE_PATH_LITERAL("forced-foo.txt"), 632 FILE_PATH_LITERAL("forced-foo.txt"),
633 FILE_PATH_LITERAL(""),
624 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 634 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
625 635
626 EXPECT_UNCONFIRMED 636 EXPECT_UNCONFIRMED
627 }, 637 },
628 638
629 { 639 {
630 // 3: Automatic Dangerous URL + Dangerous file. Dangerous URL takes 640 // 3: Automatic Dangerous URL + Dangerous file. Dangerous URL takes
631 // precendence. 641 // precendence.
632 AUTOMATIC, 642 AUTOMATIC,
633 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, 643 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
634 "http://phishing.example.com/foo.jar", "", 644 "http://phishing.example.com/foo.jar", "",
635 FILE_PATH_LITERAL(""), 645 FILE_PATH_LITERAL(""),
636 646
637 FILE_PATH_LITERAL("foo.jar"), 647 FILE_PATH_LITERAL("foo.jar"),
648 FILE_PATH_LITERAL(""),
638 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 649 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
639 650
640 EXPECT_UNCONFIRMED 651 EXPECT_UNCONFIRMED
641 }, 652 },
642 653
643 { 654 {
644 // 4: Save As Dangerous URL + Dangerous file 655 // 4: Save As Dangerous URL + Dangerous file
645 SAVE_AS, 656 SAVE_AS,
646 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, 657 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
647 "http://phishing.example.com/foo.jar", "", 658 "http://phishing.example.com/foo.jar", "",
648 FILE_PATH_LITERAL(""), 659 FILE_PATH_LITERAL(""),
649 660
650 FILE_PATH_LITERAL("foo.jar"), 661 FILE_PATH_LITERAL("foo.jar"),
662 FILE_PATH_LITERAL("foo.jar"),
651 DownloadItem::TARGET_DISPOSITION_PROMPT, 663 DownloadItem::TARGET_DISPOSITION_PROMPT,
652 664
653 EXPECT_UNCONFIRMED 665 EXPECT_UNCONFIRMED
654 }, 666 },
655 667
656 { 668 {
657 // 5: Forced Dangerous URL + Dangerous file 669 // 5: Forced Dangerous URL + Dangerous file
658 FORCED, 670 FORCED,
659 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL, 671 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
660 "http://phishing.example.com/foo.jar", "", 672 "http://phishing.example.com/foo.jar", "",
661 FILE_PATH_LITERAL("forced-foo.jar"), 673 FILE_PATH_LITERAL("forced-foo.jar"),
662 674
663 FILE_PATH_LITERAL("forced-foo.jar"), 675 FILE_PATH_LITERAL("forced-foo.jar"),
676 FILE_PATH_LITERAL(""),
664 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 677 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
665 678
666 EXPECT_UNCONFIRMED 679 EXPECT_UNCONFIRMED
667 }, 680 },
668 }; 681 };
669 682
670 RunTestCases(kDangerousURLTestCases, arraysize(kDangerousURLTestCases)); 683 RunTestCases(kDangerousURLTestCases, arraysize(kDangerousURLTestCases));
671 } 684 }
672 #endif // ENABLE_SAFE_BROWSING 685 #endif // ENABLE_SAFE_BROWSING
673 686
674 // These test cases are run with "Prompt for download" user preference set to 687 // These test cases are run with "Prompt for download" user preference set to
675 // true. Even with the preference set, some of these downloads should not cause 688 // true. Even with the preference set, some of these downloads should not cause
676 // a prompt to appear. 689 // a prompt to appear.
677 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_PromptAlways) { 690 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_PromptAlways) {
678 const DownloadTestCase kPromptingTestCases[] = { 691 const DownloadTestCase kPromptingTestCases[] = {
679 { 692 {
680 // 0: Safe Automatic - Should prompt because of "Prompt for download" 693 // 0: Safe Automatic - Should prompt because of "Prompt for download"
681 // preference setting. 694 // preference setting.
682 AUTOMATIC, 695 AUTOMATIC,
683 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 696 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
684 "http://example.com/foo.txt", "text/plain", 697 "http://example.com/foo.txt", "text/plain",
685 FILE_PATH_LITERAL(""), 698 FILE_PATH_LITERAL(""),
686 699
687 FILE_PATH_LITERAL("foo.txt"), 700 FILE_PATH_LITERAL("foo.txt"),
701 FILE_PATH_LITERAL("foo.txt"),
688 DownloadItem::TARGET_DISPOSITION_PROMPT, 702 DownloadItem::TARGET_DISPOSITION_PROMPT,
689 703
690 EXPECT_CRDOWNLOAD 704 EXPECT_CRDOWNLOAD
691 }, 705 },
692 706
693 { 707 {
694 // 1: Automatic Browser Extension download. - Shouldn't prompt for browser 708 // 1: Automatic Browser Extension download. - Shouldn't prompt for browser
695 // extension downloads even if "Prompt for download" preference is set. 709 // extension downloads even if "Prompt for download" preference is set.
696 AUTOMATIC, 710 AUTOMATIC,
697 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 711 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
698 "http://example.com/foo.crx", 712 "http://example.com/foo.crx",
699 extensions::Extension::kMimeType, 713 extensions::Extension::kMimeType,
700 FILE_PATH_LITERAL(""), 714 FILE_PATH_LITERAL(""),
701 715
702 FILE_PATH_LITERAL("foo.crx"), 716 FILE_PATH_LITERAL("foo.crx"),
717 FILE_PATH_LITERAL(""),
703 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 718 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
704 719
705 EXPECT_CRDOWNLOAD 720 EXPECT_CRDOWNLOAD
706 }, 721 },
707 722
708 { 723 {
709 // 2: Automatic User Script - Shouldn't prompt for user script downloads 724 // 2: Automatic User Script - Shouldn't prompt for user script downloads
710 // even if "Prompt for download" preference is set. 725 // even if "Prompt for download" preference is set.
711 AUTOMATIC, 726 AUTOMATIC,
712 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 727 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
713 "http://example.com/foo.user.js", "", 728 "http://example.com/foo.user.js", "",
714 FILE_PATH_LITERAL(""), 729 FILE_PATH_LITERAL(""),
715 730
716 FILE_PATH_LITERAL("foo.user.js"), 731 FILE_PATH_LITERAL("foo.user.js"),
732 FILE_PATH_LITERAL(""),
717 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 733 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
718 734
719 EXPECT_CRDOWNLOAD 735 EXPECT_CRDOWNLOAD
720 }, 736 },
721 737
722 { 738 {
723 // 3: Automatic - The filename extension is marked as one that we will 739 // 3: Automatic - The filename extension is marked as one that we will
724 // open automatically. Shouldn't prompt. 740 // open automatically. Shouldn't prompt.
725 AUTOMATIC, 741 AUTOMATIC,
726 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 742 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
727 "http://example.com/foo.dummy", "", 743 "http://example.com/foo.dummy", "",
728 FILE_PATH_LITERAL(""), 744 FILE_PATH_LITERAL(""),
729 745
730 FILE_PATH_LITERAL("foo.dummy"), 746 FILE_PATH_LITERAL("foo.dummy"),
747 FILE_PATH_LITERAL(""),
731 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 748 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
732 749
733 EXPECT_CRDOWNLOAD 750 EXPECT_CRDOWNLOAD
734 }, 751 },
735 752
736 #if defined(ENABLE_SAFE_BROWSING) 753 #if defined(ENABLE_SAFE_BROWSING)
737 // If safe browsing is disabled, this case is equivalent to AUTOMATIC/SAFE 754 // If safe browsing is disabled, this case is equivalent to AUTOMATIC/SAFE
738 // since the download isn't marked as dangerous when we are going to prompt 755 // since the download isn't marked as dangerous when we are going to prompt
739 // the user. 756 // the user.
740 { 757 {
741 // 4: Dangerous Automatic - Should prompt due to "Prompt for download" 758 // 4: Dangerous Automatic - Should prompt due to "Prompt for download"
742 // preference setting. 759 // preference setting.
743 AUTOMATIC, 760 AUTOMATIC,
744 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT, 761 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
745 "http://example.com/foo.exe", "", 762 "http://example.com/foo.exe", "",
746 FILE_PATH_LITERAL(""), 763 FILE_PATH_LITERAL(""),
747 764
748 FILE_PATH_LITERAL("foo.exe"), 765 FILE_PATH_LITERAL("foo.exe"),
766 FILE_PATH_LITERAL("foo.exe"),
749 DownloadItem::TARGET_DISPOSITION_PROMPT, 767 DownloadItem::TARGET_DISPOSITION_PROMPT,
750 768
751 EXPECT_UNCONFIRMED 769 EXPECT_UNCONFIRMED
752 }, 770 },
753 #endif 771 #endif
754 }; 772 };
755 773
756 SetPromptForDownload(true); 774 SetPromptForDownload(true);
757 EnableAutoOpenBasedOnExtension(FilePath(FILE_PATH_LITERAL("dummy.dummy"))); 775 EnableAutoOpenBasedOnExtension(FilePath(FILE_PATH_LITERAL("dummy.dummy")));
758 RunTestCases(kPromptingTestCases, arraysize(kPromptingTestCases)); 776 RunTestCases(kPromptingTestCases, arraysize(kPromptingTestCases));
759 } 777 }
760 778
761 // If the download path is managed, then we don't show any prompts. 779 // If the download path is managed, then we don't show any prompts.
762 // Note that if the download path is managed, then PromptForDownload() is false. 780 // Note that if the download path is managed, then PromptForDownload() is false.
763 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_ManagedPath) { 781 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_ManagedPath) {
764 const DownloadTestCase kManagedPathTestCases[] = { 782 const DownloadTestCase kManagedPathTestCases[] = {
765 { 783 {
766 // 0: Automatic Safe 784 // 0: Automatic Safe
767 AUTOMATIC, 785 AUTOMATIC,
768 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 786 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
769 "http://example.com/foo.txt", "text/plain", 787 "http://example.com/foo.txt", "text/plain",
770 FILE_PATH_LITERAL(""), 788 FILE_PATH_LITERAL(""),
771 789
772 FILE_PATH_LITERAL("foo.txt"), 790 FILE_PATH_LITERAL("foo.txt"),
791 FILE_PATH_LITERAL(""),
773 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 792 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
774 793
775 EXPECT_CRDOWNLOAD 794 EXPECT_CRDOWNLOAD
776 }, 795 },
777 796
778 { 797 {
779 // 1: Save_As Safe 798 // 1: Save_As Safe
780 SAVE_AS, 799 SAVE_AS,
781 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 800 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
782 "http://example.com/foo.txt", "text/plain", 801 "http://example.com/foo.txt", "text/plain",
783 FILE_PATH_LITERAL(""), 802 FILE_PATH_LITERAL(""),
784 803
785 FILE_PATH_LITERAL("foo.txt"), 804 FILE_PATH_LITERAL("foo.txt"),
805 FILE_PATH_LITERAL(""),
786 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 806 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
787 807
788 EXPECT_CRDOWNLOAD 808 EXPECT_CRDOWNLOAD
789 }, 809 },
790 }; 810 };
791 811
792 SetManagedDownloadPath(default_download_path()); 812 SetManagedDownloadPath(default_download_path());
793 ASSERT_TRUE(download_prefs()->IsDownloadPathManaged()); 813 ASSERT_TRUE(download_prefs()->IsDownloadPathManaged());
794 RunTestCases(kManagedPathTestCases, arraysize(kManagedPathTestCases)); 814 RunTestCases(kManagedPathTestCases, arraysize(kManagedPathTestCases));
795 } 815 }
796 816
817 // Test whether the last saved directory is saved if the user was presented with
818 // a file chooser.
819 TEST_F(ChromeDownloadManagerDelegateTest, StartDownload_LastSavePath) {
820 const DownloadTestCase kLastSavePathTestCases[] = {
821 {
822 // Initially the last saved directory is the user's default download path.
823
824 // 0: Start a Save As download. Then respond to the file chooser with
825 // foo/bar.txt as the target directory. This should cause the foo/
826 // directory to be remembered as the last used save directory.
827 SAVE_AS,
828 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
829 "http://example.com/foo.txt", "text/plain",
830 FILE_PATH_LITERAL(""),
831
832 FILE_PATH_LITERAL("foo/bar.txt"),
833 FILE_PATH_LITERAL("foo.txt"),
834 DownloadItem::TARGET_DISPOSITION_PROMPT,
835
836 EXPECT_CRDOWNLOAD
837 },
838
839 {
840 // 1: Start another Save As download. This time the suggested path should
841 // be in the foo/ directory.
842 SAVE_AS,
843 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
844 "http://example.com/foo.txt", "text/plain",
845 FILE_PATH_LITERAL(""),
846
847 FILE_PATH_LITERAL("bar/foo.txt"),
848 FILE_PATH_LITERAL("foo/foo.txt"),
849 DownloadItem::TARGET_DISPOSITION_PROMPT,
850
851 EXPECT_CRDOWNLOAD
852 },
853
854 {
855 // 2: Start an automatic download. This should be saved to the user's
856 // default download directory and not the last used Save As directory.
857 AUTOMATIC,
858 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
859 "http://example.com/foo.txt", "text/plain",
860 FILE_PATH_LITERAL(""),
861
862 FILE_PATH_LITERAL("foo.txt"),
863 FILE_PATH_LITERAL(""),
864 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
865
866 EXPECT_CRDOWNLOAD
867 },
868 };
869
870 RunTestCases(kLastSavePathTestCases, arraysize(kLastSavePathTestCases));
871
872 // Now clear the last download path.
873 delegate()->ClearLastDownloadPath();
874
875 // Run the first test case again. Since the last download path was cleared,
876 // this test case should behave identically to the first time it was run.
877 RunTestCases(kLastSavePathTestCases, 1);
878 }
879
797 // TODO(asanka): Add more tests. 880 // TODO(asanka): Add more tests.
798 // * Default download path is not writable. 881 // * Default download path is not writable.
799 // * Download path doesn't exist. 882 // * Download path doesn't exist.
800 // * IsDangerousFile(). 883 // * IsDangerousFile().
801 // * Filename generation. 884 // * Filename generation.
OLDNEW
« no previous file with comments | « chrome/browser/download/chrome_download_manager_delegate.cc ('k') | chrome/browser/download/download_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698