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

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

Issue 12850002: Move download filename determintion into a separate class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 8 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/file_util.h"
6 #include "base/files/file_path.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/message_loop.h"
9 #include "base/observer_list.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h"
12 #include "base/string_util.h"
13 #include "base/value_conversions.h"
14 #include "chrome/browser/download/chrome_download_manager_delegate.h"
15 #include "chrome/browser/download/download_extensions.h"
16 #include "chrome/browser/download/download_file_picker.h"
17 #include "chrome/browser/download/download_prefs.h"
18 #include "chrome/browser/download/download_target_determiner.h"
19 #include "chrome/browser/download/download_util.h"
20 #include "chrome/browser/history/history_service.h"
21 #include "chrome/browser/history/history_service_factory.h"
22 #include "chrome/browser/history/history_types.h"
23 #include "chrome/common/extensions/extension.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
26 #include "chrome/test/base/testing_pref_service_syncable.h"
27 #include "chrome/test/base/testing_profile.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/browser/web_contents_delegate.h"
30 #include "content/public/test/mock_download_item.h"
31 #include "content/public/test/test_browser_thread.h"
32 #include "content/public/test/test_renderer_host.h"
33 #include "content/public/test/web_contents_tester.h"
34 #include "testing/gmock/include/gmock/gmock.h"
35 #include "testing/gtest/include/gtest/gtest.h"
36
37 #if defined(FULL_SAFE_BROWSING)
38 #include "chrome/browser/safe_browsing/download_protection_service.h"
39 #endif
40
41 using ::testing::AnyNumber;
42 using ::testing::Invoke;
43 using ::testing::Ref;
44 using ::testing::Return;
45 using ::testing::ReturnRef;
46 using ::testing::ReturnRefOfCopy;
47 using ::testing::Truly;
48 using ::testing::WithArg;
49 using ::testing::_;
50 using content::DownloadItem;
51
52 namespace {
53
54 // No-op delegate.
55 class NullWebContentsDelegate : public content::WebContentsDelegate {
56 public:
57 NullWebContentsDelegate() {}
58 ~NullWebContentsDelegate() {}
59 };
60
61 // Google Mock action that posts a task to the current message loop that invokes
62 // the first argument of the mocked method as a callback. Said argument must be
63 // a base::Callback<void(ParamType)>. |result| must be of |ParamType| and is
64 // bound as that parameter.
65 // Example:
66 // class FooClass {
67 // public:
68 // virtual void Foo(base::Callback<void(bool)> callback);
69 // };
70 // ...
71 // EXPECT_CALL(mock_fooclass_instance, Foo(callback))
72 // .WillOnce(ScheduleCallback(false));
73 ACTION_P(ScheduleCallback, result0) {
74 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(arg0, result0));
75 }
76
77 // Similar to ScheduleCallback, but binds 2 arguments.
78 ACTION_P2(ScheduleCallback2, result0, result1) {
79 MessageLoop::current()->PostTask(
80 FROM_HERE, base::Bind(arg0, result0, result1));
81 }
82
83 // Used with DownloadTestCase. Indicates the type of test case. The expectations
84 // for the test is set based on the type.
85 enum TestCaseType {
86 SAVE_AS,
87 AUTOMATIC,
88 FORCED // Requires that forced_file_path be non-empty.
89 };
90
91 // Used with DownloadTestCase. Type of intermediate filename to expect.
92 enum TestCaseExpectIntermediate {
93 EXPECT_CRDOWNLOAD, // Expect path/to/target.crdownload.
94 EXPECT_UNCONFIRMED, // Expect path/to/Unconfirmed xxx.crdownload.
95 EXPECT_LOCAL_PATH, // Expect target path.
96 };
97
98 // Typical download test case. Used with
99 // DownloadTargetDeterminerTest::RunTestCase().
100 struct DownloadTestCase {
101 // Type of test.
102 TestCaseType test_type;
103
104 // Expected danger type. Verified at the end of target determination.
105 content::DownloadDangerType expected_danger_type;
106
107 // Value of DownloadItem::GetURL()
108 const char* url;
109
110 // Value of DownloadItem::GetMimeType()
111 const char* mime_type;
112
113 // Should be non-empty if |test_type| == FORCED. Value of GetForcedFilePath().
114 const base::FilePath::CharType* forced_file_path;
115
116 // Expected virtual path. Specified relative to the virtual download path. If
117 // empty, assumed to be the same as |expected_local_path|.
118 const base::FilePath::CharType* expected_virtual_path;
119
120 // Expected local path. Specified relative to the test download path.
121 const base::FilePath::CharType* expected_local_path;
122
123 // The path that DownloadTargetDeterminer will generate before handing off to
124 // the ReserveVirtualPath().
125 const base::FilePath::CharType* expected_generated_path;
126
127 // Expected target disposition. If this is TARGET_DISPOSITION_PROMPT, then the
128 // test run will expect ChromeDownloadManagerDelegate to prompt the user for a
129 // download location.
130 DownloadItem::TargetDisposition expected_disposition;
131
132 // Type of intermediate path to expect.
133 TestCaseExpectIntermediate expected_intermediate;
134 };
135
136 #if defined(FULL_SAFE_BROWSING)
137 // DownloadProtectionService with mock methods. Since the SafeBrowsingService is
138 // set to NULL, it is not safe to call any non-mocked methods other than
139 // SetEnabled() and enabled().
140 class TestDownloadProtectionService
141 : public safe_browsing::DownloadProtectionService {
142 public:
143 TestDownloadProtectionService()
144 : safe_browsing::DownloadProtectionService(NULL, NULL) {}
145 MOCK_METHOD2(CheckClientDownload,
146 void(DownloadItem*,
147 const CheckDownloadCallback&));
148 MOCK_METHOD2(CheckDownloadUrl,
149 void(const DownloadItem&,
150 const CheckDownloadCallback&));
151 MOCK_CONST_METHOD2(IsSupportedDownload,
152 bool(const DownloadItem&,
153 const base::FilePath&));
154
155 void SetupDefaults() {
156 ON_CALL(*this, CheckDownloadUrl(_, _))
157 .WillByDefault(WithArg<1>(ScheduleCallback(SAFE)));
158 ON_CALL(*this, IsSupportedDownload(_, _))
159 .WillByDefault(Return(false));
160 }
161 };
162 #endif
163
164 class MockDownloadFilePickerFactory : public DownloadFilePickerFactory {
165 public:
166 MOCK_METHOD3(Create,
167 void(DownloadItem*,
168 const base::FilePath&,
169 const DownloadFilePicker::FileSelectedCallback&));
170 };
171
172 class MockDownloadTargetDeterminerDelegate :
173 public DownloadTargetDeterminerDelegate {
174 public:
175 MOCK_METHOD0(GetDownloadProtectionService,
176 safe_browsing::DownloadProtectionService*());
177 MOCK_METHOD3(DetermineLocalPath,
178 void(DownloadItem*, const base::FilePath&,
179 const DownloadTargetDeterminerDelegate::LocalPathCallback&));
180 MOCK_METHOD4(ReserveVirtualPath,
181 void(DownloadItem*, const base::FilePath&, bool,
182 const DownloadTargetDeterminerDelegate::ReservedPathCallback&));
183
184 void SetupDefaults() {
185 ON_CALL(*this, GetDownloadProtectionService())
186 .WillByDefault(Return(
187 static_cast<safe_browsing::DownloadProtectionService*>(NULL)));
188 ON_CALL(*this, ReserveVirtualPath(_, _, _, _))
189 .WillByDefault(Invoke(
190 &MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath));
191 }
192 private:
193 static void NullReserveVirtualPath(
194 DownloadItem* download,
195 const base::FilePath& virtual_path,
196 bool override,
197 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback);
198 };
199
200 class DownloadTargetDeterminerTest : public ChromeRenderViewHostTestHarness {
201 public:
202 DownloadTargetDeterminerTest();
203
204 // ::testing::Test
205 virtual void SetUp() OVERRIDE;
206 virtual void TearDown() OVERRIDE;
207
208 // Creates MockDownloadItem and sets up default expectations.
209 content::MockDownloadItem* CreateActiveDownloadItem(int32 id);
210
211 // Sets the AutoOpenBasedOnExtension user preference for |path|.
212 void EnableAutoOpenBasedOnExtension(const base::FilePath& path);
213
214 // Set the kDownloadDefaultDirectory user preference to |path|.
215 void SetDefaultDownloadPath(const base::FilePath& path);
216
217 // Set the kDownloadDefaultDirectory managed preference to |path|.
218 void SetManagedDownloadPath(const base::FilePath& path);
219
220 // Set the kPromptForDownload user preference to |prompt|.
221 void SetPromptForDownload(bool prompt);
222
223 // Given the relative path |path|, returns the full path under the temporary
224 // downloads directory.
225 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path);
226
227 // Run |test_case| using |item|.
228 void RunTestCaseWithDownloadItem(const DownloadTestCase& test_case,
229 content::MockDownloadItem* item);
230
231 // Run through |test_case_count| tests in |test_cases|. A new MockDownloadItem
232 // will be created for each test case and destroyed when the test case is
233 // complete.
234 void RunTestCases(const DownloadTestCase test_cases[],
235 size_t test_case_count);
236
237 const base::FilePath& test_download_dir() const {
238 return test_download_dir_.path();
239 }
240
241 const base::FilePath& test_virtual_dir() const {
242 return test_virtual_dir_;
243 }
244
245 MockDownloadTargetDeterminerDelegate* delegate() {
246 return &delegate_;
247 }
248
249 DownloadPrefs* download_prefs() {
250 return download_prefs_.get();
251 }
252
253 void set_last_selected_directory(const base::FilePath& path) {
254 last_selected_directory_ = path;
255 }
256
257 private:
258 // Verifies that |target_path|, |disposition|, |expected_danger_type| and
259 // |intermediate_path| matches the expectations of |test_case|.
260 void DownloadTargetVerifier(const DownloadTestCase& test_case,
261 const base::FilePath& virtual_path,
262 const base::FilePath& local_path,
263 const base::FilePath& intermediate_path,
264 DownloadItem::TargetDisposition disposition,
265 content::DownloadDangerType danger_type);
266
267 scoped_ptr<DownloadPrefs> download_prefs_;
268 ::testing::StrictMock<MockDownloadTargetDeterminerDelegate> delegate_;
269 MockDownloadFilePickerFactory file_picker_factory_;
270 NullWebContentsDelegate web_contents_delegate_;
271 base::ScopedTempDir test_download_dir_;
272 base::FilePath test_virtual_dir_;
273 base::FilePath last_selected_directory_;
274 content::TestBrowserThread ui_thread_;
275 content::TestBrowserThread file_thread_;
276 };
277
278 DownloadTargetDeterminerTest::DownloadTargetDeterminerTest()
279 : ChromeRenderViewHostTestHarness(),
280 ui_thread_(content::BrowserThread::UI, &message_loop_),
281 file_thread_(content::BrowserThread::FILE, &message_loop_) {
282 }
283
284 void DownloadTargetDeterminerTest::SetUp() {
285 ChromeRenderViewHostTestHarness::SetUp();
286 CHECK(profile());
287 download_prefs_.reset(new DownloadPrefs(profile()));
288 web_contents()->SetDelegate(&web_contents_delegate_);
289 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir());
290 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual"));
291 SetDefaultDownloadPath(test_download_dir());
292 delegate_.SetupDefaults();
293 }
294
295 void DownloadTargetDeterminerTest::TearDown() {
296 download_prefs_.reset();
297 message_loop_.RunUntilIdle();
298 ChromeRenderViewHostTestHarness::TearDown();
299 }
300
301 content::MockDownloadItem*
302 DownloadTargetDeterminerTest::CreateActiveDownloadItem(int32 id) {
303 content::MockDownloadItem* item =
304 new ::testing::NiceMock<content::MockDownloadItem>();
305 ON_CALL(*item, GetDangerType())
306 .WillByDefault(Return(content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS));
307 ON_CALL(*item, GetFullPath())
308 .WillByDefault(ReturnRefOfCopy(base::FilePath()));
309 ON_CALL(*item, GetHash())
310 .WillByDefault(ReturnRefOfCopy(std::string()));
311 ON_CALL(*item, GetReferrerUrl())
312 .WillByDefault(ReturnRefOfCopy(GURL()));
313 ON_CALL(*item, GetTargetFilePath())
314 .WillByDefault(ReturnRefOfCopy(base::FilePath()));
315 ON_CALL(*item, GetTransitionType())
316 .WillByDefault(Return(content::PAGE_TRANSITION_LINK));
317 ON_CALL(*item, GetWebContents())
318 .WillByDefault(Return(web_contents()));
319 ON_CALL(*item, HasUserGesture())
320 .WillByDefault(Return(true));
321 ON_CALL(*item, IsDangerous())
322 .WillByDefault(Return(false));
323 ON_CALL(*item, IsInProgress())
324 .WillByDefault(Return(true));
325 ON_CALL(*item, IsTemporary())
326 .WillByDefault(Return(false));
327 EXPECT_CALL(*item, GetBrowserContext())
328 .WillRepeatedly(Return(profile()));
329 EXPECT_CALL(*item, GetId())
330 .WillRepeatedly(Return(id));
331 EXPECT_CALL(*item, GetState())
332 .WillRepeatedly(Return(DownloadItem::IN_PROGRESS));
333 EXPECT_CALL(*item, AddObserver(_)).WillRepeatedly(Return());
334 EXPECT_CALL(*item, RemoveObserver(_)).WillRepeatedly(Return());
335 return item;
336 }
337
338 void DownloadTargetDeterminerTest::EnableAutoOpenBasedOnExtension(
339 const base::FilePath& path) {
340 EXPECT_TRUE(download_prefs_->EnableAutoOpenBasedOnExtension(path));
341 }
342
343 void DownloadTargetDeterminerTest::SetDefaultDownloadPath(
344 const base::FilePath& path) {
345 profile()->GetTestingPrefService()->
346 SetFilePath(prefs::kDownloadDefaultDirectory, path);
347 }
348
349 void DownloadTargetDeterminerTest::SetManagedDownloadPath(
350 const base::FilePath& path) {
351 profile()->GetTestingPrefService()->
352 SetManagedPref(prefs::kDownloadDefaultDirectory,
353 base::CreateFilePathValue(path));
354 }
355
356 void DownloadTargetDeterminerTest::SetPromptForDownload(bool prompt) {
357 profile()->GetTestingPrefService()->
358 SetBoolean(prefs::kPromptForDownload, prompt);
359 }
360
361 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir(
362 const base::FilePath::StringType& relative_path) {
363 if (relative_path.empty())
364 return base::FilePath();
365 base::FilePath full_path(test_download_dir().Append(relative_path));
366 return full_path.NormalizePathSeparators();
367 }
368
369 void DownloadTargetDeterminerTest::RunTestCaseWithDownloadItem(
370 const DownloadTestCase& test_case,
371 content::MockDownloadItem* item) {
372 // SetUp DownloadItem
373 GURL download_url(test_case.url);
374 std::vector<GURL> url_chain;
375 url_chain.push_back(download_url);
376 base::FilePath expected_generated_path =
377 GetPathInDownloadDir(test_case.expected_generated_path);
378 base::FilePath forced_file_path =
379 GetPathInDownloadDir(test_case.forced_file_path);
380 base::FilePath expected_virtual_path =
381 GetPathInDownloadDir(test_case.expected_virtual_path);
382 base::FilePath expected_local_path =
383 GetPathInDownloadDir(test_case.expected_local_path);
384 // If the virtual path is empty, we assume it is the same as the local path.
385 if (expected_virtual_path.empty())
386 expected_virtual_path = expected_local_path;
387 if (test_case.test_type == FORCED)
388 ASSERT_FALSE(forced_file_path.empty());
389
390 // Expectations for the DownloadItem:
391 EXPECT_CALL(*item, GetReferrerUrl())
392 .WillRepeatedly(ReturnRef(download_url));
393 EXPECT_CALL(*item, GetURL())
394 .WillRepeatedly(ReturnRef(download_url));
395 EXPECT_CALL(*item, GetUrlChain())
396 .WillRepeatedly(ReturnRef(url_chain));
397 EXPECT_CALL(*item, GetForcedFilePath())
398 .WillRepeatedly(ReturnRef(forced_file_path));
399 EXPECT_CALL(*item, GetMimeType())
400 .WillRepeatedly(Return(test_case.mime_type));
401 DownloadItem::TargetDisposition initial_disposition =
402 (test_case.test_type == SAVE_AS) ?
403 DownloadItem::TARGET_DISPOSITION_PROMPT :
404 DownloadItem::TARGET_DISPOSITION_OVERWRITE;
405 EXPECT_CALL(*item, GetTargetDisposition())
406 .WillRepeatedly(Return(initial_disposition));
407
408 if (item->IsInProgress()) {
409 bool will_prompt = (test_case.expected_disposition ==
410 DownloadItem::TARGET_DISPOSITION_PROMPT);
411 bool has_virtual_path = !expected_virtual_path.empty();
412 bool needs_local_path = !will_prompt && has_virtual_path;
413 if (will_prompt) {
414 EXPECT_CALL(file_picker_factory_,
415 Create(item, expected_generated_path, _))
416 .WillOnce(WithArg<2>(ScheduleCallback2(expected_virtual_path,
417 expected_local_path)));
418 } else {
419 EXPECT_CALL(file_picker_factory_, Create(_, _, _))
420 .Times(0);
421 }
422 if (needs_local_path) {
423 EXPECT_CALL(*delegate(),
424 DetermineLocalPath(item, expected_virtual_path, _))
425 .WillOnce(WithArg<2>(ScheduleCallback(expected_local_path)));
426 }
427 EXPECT_CALL(*delegate(),
428 ReserveVirtualPath(item, expected_generated_path,
429 forced_file_path.empty(), _));
430 }
431 EXPECT_CALL(*delegate(), GetDownloadProtectionService())
432 .Times(AnyNumber());
433
434 // Kick off the test.
435 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this);
436 DownloadTargetDeterminer::Start(
437 item, download_prefs_.get(), &file_picker_factory_,
438 last_selected_directory_, delegate(),
439 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier,
440 factory.GetWeakPtr(), test_case));
441 message_loop_.RunUntilIdle();
442 ::testing::Mock::VerifyAndClearExpectations(delegate());
443 ::testing::Mock::VerifyAndClearExpectations(&file_picker_factory_);
444 }
445
446 void DownloadTargetDeterminerTest::RunTestCases(
447 const DownloadTestCase test_cases[],
448 size_t test_case_count) {
449 for (size_t i = 0; i < test_case_count; ++i) {
450 scoped_ptr<content::MockDownloadItem> item(CreateActiveDownloadItem(i));
451 SCOPED_TRACE(testing::Message() << "Running test case " << i);
452 RunTestCaseWithDownloadItem(test_cases[i], item.get());
453 }
454 }
455
456 void DownloadTargetDeterminerTest::DownloadTargetVerifier(
457 const DownloadTestCase& test_case,
458 const base::FilePath& virtual_path,
459 const base::FilePath& local_path,
460 const base::FilePath& intermediate_path,
461 DownloadItem::TargetDisposition disposition,
462 content::DownloadDangerType danger_type) {
463 base::FilePath expected_local_path(
464 GetPathInDownloadDir(test_case.expected_local_path));
465 base::FilePath expected_virtual_path(
466 GetPathInDownloadDir(test_case.expected_virtual_path));
467 if (expected_virtual_path.empty())
468 expected_virtual_path = expected_local_path;
469 EXPECT_EQ(expected_virtual_path.value(), virtual_path.value());
470 EXPECT_EQ(expected_local_path.value(), local_path.value());
471 EXPECT_EQ(test_case.expected_disposition, disposition);
472 EXPECT_EQ(test_case.expected_danger_type, danger_type);
473
474 switch (test_case.expected_intermediate) {
475 case EXPECT_CRDOWNLOAD:
476 EXPECT_EQ(download_util::GetCrDownloadPath(local_path).value(),
477 intermediate_path.value());
478 break;
479
480 case EXPECT_UNCONFIRMED:
481 // The paths (in English) look like: /path/Unconfirmed xxx.crdownload.
482 // Of this, we only check that the path is:
483 // 1. Not "/path/target.crdownload",
484 // 2. Points to the same directory as the target.
485 // 3. Has extension ".crdownload".
486 EXPECT_NE(download_util::GetCrDownloadPath(expected_local_path).value(),
487 intermediate_path.value());
488 EXPECT_EQ(expected_local_path.DirName().value(),
489 intermediate_path.DirName().value());
490 EXPECT_TRUE(intermediate_path.MatchesExtension(
491 FILE_PATH_LITERAL(".crdownload")));
492 break;
493
494 case EXPECT_LOCAL_PATH:
495 EXPECT_EQ(expected_local_path.value(), intermediate_path.value());
496 break;
497 }
498 }
499
500 // static
501 void MockDownloadTargetDeterminerDelegate::NullReserveVirtualPath(
502 DownloadItem* download,
503 const base::FilePath& virtual_path,
504 bool override,
505 const DownloadTargetDeterminerDelegate::ReservedPathCallback& callback) {
506 callback.Run(virtual_path, true);
507 }
508
509 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_Basic) {
510 const DownloadTestCase kBasicTestCases[] = {
511 {
512 // 0: Automatic Safe
513 AUTOMATIC,
514 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
515 "http://example.com/foo.txt", "text/plain",
516 FILE_PATH_LITERAL(""),
517
518 FILE_PATH_LITERAL(""),
519 FILE_PATH_LITERAL("foo.txt"),
520 FILE_PATH_LITERAL("foo.txt"),
521 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
522
523 EXPECT_CRDOWNLOAD
524 },
525
526 {
527 // 1: Save_As Safe
528 SAVE_AS,
529 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
530 "http://example.com/foo.txt", "text/plain",
531 FILE_PATH_LITERAL(""),
532
533 FILE_PATH_LITERAL(""),
534 FILE_PATH_LITERAL("foo.txt"),
535 FILE_PATH_LITERAL("foo.txt"),
536 DownloadItem::TARGET_DISPOSITION_PROMPT,
537
538 EXPECT_CRDOWNLOAD
539 },
540
541 {
542 // 2: Automatic Dangerous
543 AUTOMATIC,
544 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
545 "http://example.com/foo.html", "",
546 FILE_PATH_LITERAL(""),
547
548 FILE_PATH_LITERAL(""),
549 FILE_PATH_LITERAL("foo.html"),
550 FILE_PATH_LITERAL("foo.html"),
551 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
552
553 EXPECT_UNCONFIRMED
554 },
555
556 {
557 // 3: Forced Safe
558 FORCED,
559 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
560 "http://example.com/foo.txt", "",
561 FILE_PATH_LITERAL("forced-foo.txt"),
562
563 FILE_PATH_LITERAL(""),
564 FILE_PATH_LITERAL("forced-foo.txt"),
565 FILE_PATH_LITERAL("forced-foo.txt"),
566 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
567
568 EXPECT_LOCAL_PATH
569 },
570 };
571
572 // The test assumes that .html files have a danger level of
573 // AllowOnUserGesture.
574 ASSERT_EQ(download_util::AllowOnUserGesture,
575 download_util::GetFileDangerLevel(
576 base::FilePath(FILE_PATH_LITERAL("foo.html"))));
577 RunTestCases(kBasicTestCases, arraysize(kBasicTestCases));
578 }
579
580 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_CancelSaveAs) {
581 const DownloadTestCase kCancelSaveAsTestCases[] = {
582 {
583 // 2: Save_As Safe, Cancelled.
584 SAVE_AS,
585 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
586 "http://example.com/foo.txt", "text/plain",
587 FILE_PATH_LITERAL(""),
588
589 FILE_PATH_LITERAL(""),
590 FILE_PATH_LITERAL(""),
591 FILE_PATH_LITERAL("foo.txt"),
592 DownloadItem::TARGET_DISPOSITION_PROMPT,
593
594 EXPECT_LOCAL_PATH
595 }
596 };
597 RunTestCases(kCancelSaveAsTestCases, arraysize(kCancelSaveAsTestCases));
598 }
599
600 #if defined(FULL_SAFE_BROWSING)
601 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_MaybeDangerous) {
602 const DownloadTestCase kMaybeDangerousTestCases[] = {
603 // These test cases are run with foo.txt being considered a filetype
604 // supported by safe browsing.
605 {
606 // 0: Forced.
607 FORCED,
608 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
609 "http://example.com/foo.txt", "",
610 FILE_PATH_LITERAL("foo.txt"),
611
612 FILE_PATH_LITERAL(""),
613 FILE_PATH_LITERAL("foo.txt"),
614 FILE_PATH_LITERAL("foo.txt"),
615 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
616
617 EXPECT_UNCONFIRMED
618 },
619
620 {
621 // 1: Save_As.
622 SAVE_AS,
623 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
624 "http://example.com/foo.txt", "",
625 FILE_PATH_LITERAL(""),
626
627 FILE_PATH_LITERAL(""),
628 FILE_PATH_LITERAL("foo.txt"),
629 FILE_PATH_LITERAL("foo.txt"),
630 DownloadItem::TARGET_DISPOSITION_PROMPT,
631
632 EXPECT_UNCONFIRMED
633 },
634
635 {
636 // 2: Automatic.
637 AUTOMATIC,
638 content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT,
639 "http://example.com/foo.txt", "",
640 FILE_PATH_LITERAL("foo.txt"),
641
642 FILE_PATH_LITERAL(""),
643 FILE_PATH_LITERAL("foo.txt"),
644 FILE_PATH_LITERAL("foo.txt"),
645 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
646
647 EXPECT_UNCONFIRMED
648 },
649
650 {
651 // 3: Unsupported filetype.
652 AUTOMATIC,
653 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
654 "http://example.com/foo-unsupported.txt", "",
655 FILE_PATH_LITERAL(""),
656
657 FILE_PATH_LITERAL(""),
658 FILE_PATH_LITERAL("foo-unsupported.txt"),
659 FILE_PATH_LITERAL("foo-unsupported.txt"),
660 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
661
662 EXPECT_CRDOWNLOAD
663 },
664 };
665
666 base::FilePath supported_path(
667 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt")));
668 base::FilePath unsupported_path(
669 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-unsupported.txt")));
670 scoped_ptr<TestDownloadProtectionService> service(
671 new TestDownloadProtectionService);
672 service->SetupDefaults();
673 ON_CALL(*delegate(), GetDownloadProtectionService())
674 .WillByDefault(Return(service.get()));
675 EXPECT_CALL(*service.get(), IsSupportedDownload(_, supported_path))
676 .WillRepeatedly(Return(true));
677 EXPECT_CALL(*service.get(), IsSupportedDownload(_, unsupported_path))
678 .WillRepeatedly(Return(false));
679 EXPECT_CALL(*service.get(), CheckDownloadUrl(_, _))
680 .Times(arraysize(kMaybeDangerousTestCases));
681 RunTestCases(kMaybeDangerousTestCases, arraysize(kMaybeDangerousTestCases));
682 }
683
684 // The SafeBrowsing URL check is performed early. Make sure that a download item
685 // that has been marked as DANGEROUS_URL behaves correctly.
686 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_DangerousURL) {
687 const DownloadTestCase kDangerousURLTestCases[] = {
688 {
689 // 0: Automatic Dangerous URL
690 AUTOMATIC,
691 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
692 "http://phishing.example.com/foo.txt", "",
693 FILE_PATH_LITERAL(""),
694
695 FILE_PATH_LITERAL(""),
696 FILE_PATH_LITERAL("foo.txt"),
697 FILE_PATH_LITERAL("foo.txt"),
698 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
699
700 EXPECT_UNCONFIRMED
701 },
702
703 {
704 // 1: Save As Dangerous URL
705 SAVE_AS,
706 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
707 "http://phishing.example.com/foo.txt", "",
708 FILE_PATH_LITERAL(""),
709
710 FILE_PATH_LITERAL(""),
711 FILE_PATH_LITERAL("foo.txt"),
712 FILE_PATH_LITERAL("foo.txt"),
713 DownloadItem::TARGET_DISPOSITION_PROMPT,
714
715 EXPECT_UNCONFIRMED
716 },
717
718 {
719 // 2: Forced Dangerous URL
720 FORCED,
721 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
722 "http://phishing.example.com/foo.txt", "",
723 FILE_PATH_LITERAL("forced-foo.txt"),
724
725 FILE_PATH_LITERAL(""),
726 FILE_PATH_LITERAL("forced-foo.txt"),
727 FILE_PATH_LITERAL("forced-foo.txt"),
728 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
729
730 EXPECT_UNCONFIRMED
731 },
732
733 {
734 // 3: Automatic Dangerous URL + Dangerous file. Dangerous URL takes
735 // precendence.
736 AUTOMATIC,
737 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
738 "http://phishing.example.com/foo.html", "",
739 FILE_PATH_LITERAL(""),
740
741 FILE_PATH_LITERAL(""),
742 FILE_PATH_LITERAL("foo.html"),
743 FILE_PATH_LITERAL("foo.html"),
744 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
745
746 EXPECT_UNCONFIRMED
747 },
748
749 {
750 // 4: Save As Dangerous URL + Dangerous file
751 SAVE_AS,
752 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
753 "http://phishing.example.com/foo.html", "",
754 FILE_PATH_LITERAL(""),
755
756 FILE_PATH_LITERAL(""),
757 FILE_PATH_LITERAL("foo.html"),
758 FILE_PATH_LITERAL("foo.html"),
759 DownloadItem::TARGET_DISPOSITION_PROMPT,
760
761 EXPECT_UNCONFIRMED
762 },
763
764 {
765 // 5: Forced Dangerous URL + Dangerous file
766 FORCED,
767 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL,
768 "http://phishing.example.com/foo.html", "",
769 FILE_PATH_LITERAL("forced-foo.html"),
770
771 FILE_PATH_LITERAL(""),
772 FILE_PATH_LITERAL("forced-foo.html"),
773 FILE_PATH_LITERAL("forced-foo.html"),
774 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
775
776 EXPECT_UNCONFIRMED
777 },
778 };
779
780 scoped_ptr<TestDownloadProtectionService> service(
781 new ::testing::StrictMock<TestDownloadProtectionService>());
782 service->SetupDefaults();
783 ON_CALL(*delegate(), GetDownloadProtectionService())
784 .WillByDefault(Return(service.get()));
785 EXPECT_CALL(*service.get(), IsSupportedDownload(_, _))
786 .Times(AnyNumber());
787 EXPECT_CALL(*service.get(), CheckDownloadUrl(_, _))
788 .Times(AnyNumber())
789 .WillRepeatedly(WithArg<1>(ScheduleCallback(
790 safe_browsing::DownloadProtectionService::DANGEROUS)));
791 RunTestCases(kDangerousURLTestCases, arraysize(kDangerousURLTestCases));
792 }
793 #endif // FULL_SAFE_BROWSING
794
795 // Test whether the last saved directory is used for 'Save As' downloads.
796 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_LastSavePath) {
797 const DownloadTestCase kLastSavePathTestCasesPre[] = {
798 {
799 // 0: If the last save path is empty, then the default download directory
800 // should be used.
801 SAVE_AS,
802 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
803 "http://example.com/foo.txt", "text/plain",
804 FILE_PATH_LITERAL(""),
805
806 FILE_PATH_LITERAL(""),
807 FILE_PATH_LITERAL("bar.txt"),
808 FILE_PATH_LITERAL("foo.txt"),
809 DownloadItem::TARGET_DISPOSITION_PROMPT,
810
811 EXPECT_CRDOWNLOAD
812 }
813 };
814
815 // These test cases are run with a last save path set to a non-emtpy local
816 // download directory.
817 const DownloadTestCase kLastSavePathTestCasesPost[] = {
818 {
819 // 0: This test case is run with the last download directory set to
820 // '<test_download_dir()>/foo'.
821 SAVE_AS,
822 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
823 "http://example.com/foo.txt", "text/plain",
824 FILE_PATH_LITERAL(""),
825
826 FILE_PATH_LITERAL(""),
827 FILE_PATH_LITERAL("foo/bar.txt"),
828 FILE_PATH_LITERAL("foo/foo.txt"),
829 DownloadItem::TARGET_DISPOSITION_PROMPT,
830
831 EXPECT_CRDOWNLOAD
832 },
833
834 {
835 // 1: Start an automatic download. This should be saved to the user's
836 // default download directory and not the last used Save As directory.
837 AUTOMATIC,
838 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
839 "http://example.com/foo.txt", "text/plain",
840 FILE_PATH_LITERAL(""),
841
842 FILE_PATH_LITERAL(""),
843 FILE_PATH_LITERAL("foo.txt"),
844 FILE_PATH_LITERAL("foo.txt"),
845 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
846
847 EXPECT_CRDOWNLOAD
848 },
849 };
850
851 // This test case is run with the last save path set to a non-empty virtual
852 // directory.
853 const DownloadTestCase kLastSavePathTestCasesVirtual[] = {
854 {
855 SAVE_AS,
856 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
857 "http://example.com/foo.txt", "text/plain",
858 FILE_PATH_LITERAL(""),
859
860 FILE_PATH_LITERAL("virtual/foo/foo.txt"),
861 FILE_PATH_LITERAL("foo/bar.txt"),
862 FILE_PATH_LITERAL("virtual/foo/foo.txt"),
863 DownloadItem::TARGET_DISPOSITION_PROMPT,
864
865 EXPECT_LOCAL_PATH
866 },
867 };
868
869 {
870 SCOPED_TRACE(testing::Message()
871 << "Running with empty last_selected_directory");
872 RunTestCases(kLastSavePathTestCasesPre,
873 arraysize(kLastSavePathTestCasesPre));
874 }
875
876 // Try with a non-empty last save path.
877 {
878 SCOPED_TRACE(testing::Message()
879 << "Running with local last_selected_directory");
880 set_last_selected_directory(test_download_dir().AppendASCII("foo"));
881 RunTestCases(kLastSavePathTestCasesPost,
882 arraysize(kLastSavePathTestCasesPost));
883 }
884
885 // And again, but this time use a virtual directory.
886 {
887 SCOPED_TRACE(testing::Message()
888 << "Running with virtual last_selected_directory");
889 set_last_selected_directory(test_virtual_dir().AppendASCII("foo"));
890 RunTestCases(kLastSavePathTestCasesVirtual,
891 arraysize(kLastSavePathTestCasesVirtual));
892 }
893 }
894
895 // These tests are run with the default downloads folder set to a virtual
896 // directory.
897 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_DefaultVirtual) {
898 const DownloadTestCase kDefaultVirtualTestCases[] = {
899 {
900 // 0: Automatic Safe
901 AUTOMATIC,
902 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
903 "http://example.com/foo.txt", "text/plain",
904 FILE_PATH_LITERAL(""),
905
906 FILE_PATH_LITERAL("virtual/foo.txt"),
907 FILE_PATH_LITERAL("foo-local.txt"),
908 FILE_PATH_LITERAL("virtual/foo.txt"),
909 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
910
911 EXPECT_LOCAL_PATH
912 },
913
914 {
915 // 1: Save_As Safe.
916 SAVE_AS,
917 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
918 "http://example.com/foo.txt", "text/plain",
919 FILE_PATH_LITERAL(""),
920
921 // The response to the download prompt is to choose the 'foo.txt' virtual
922 // path. However, the virtual path shouldn't be passed through
923 // DetermineLocalPath(). So the 'foo-x.txt' local path should make it
924 // through without being modified.
925 FILE_PATH_LITERAL("virtual/foo.txt"),
926 FILE_PATH_LITERAL("foo-x.txt"),
927
928 // The last selected download path is empty. So 'Save As' should default
929 // to using the virtual path for the suggested path.
930 FILE_PATH_LITERAL("virtual/foo.txt"),
931
932 DownloadItem::TARGET_DISPOSITION_PROMPT,
933
934 EXPECT_LOCAL_PATH
935 },
936
937 {
938 // 2: Save_As Safe.
939 SAVE_AS,
940 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
941 "http://example.com/foo.txt", "text/plain",
942 FILE_PATH_LITERAL(""),
943
944 // Response to the 'Save As' is to choose the local path for 'foo-x.txt'.
945 FILE_PATH_LITERAL(""),
946 FILE_PATH_LITERAL("foo-x.txt"),
947
948 // The last selected download path is empty. So 'Save As' should default
949 // to using the virtual path for the suggested path.
950 FILE_PATH_LITERAL("virtual/foo.txt"),
951
952 DownloadItem::TARGET_DISPOSITION_PROMPT,
953
954 EXPECT_CRDOWNLOAD
955 },
956
957 {
958 // 3: Forced Safe. Doesn't override forced paths.
959 FORCED,
960 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
961 "http://example.com/foo.txt", "",
962 FILE_PATH_LITERAL("forced-foo.txt"),
963
964 FILE_PATH_LITERAL(""),
965 FILE_PATH_LITERAL("forced-foo.txt"),
966 FILE_PATH_LITERAL("forced-foo.txt"),
967 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
968
969 EXPECT_LOCAL_PATH
970 },
971 };
972
973 for (size_t i = 0; i < arraysize(kDefaultVirtualTestCases); ++i) {
974 scoped_ptr<content::MockDownloadItem> item(CreateActiveDownloadItem(i));
975 const DownloadTestCase& test_case = kDefaultVirtualTestCases[i];
976 SCOPED_TRACE(testing::Message() << "Running test case " << i);
977
978 // The default download directory is the virtual path.
979 SetDefaultDownloadPath(test_virtual_dir());
980
981 // The result of calling DetermineLocalPath is 'foo-local.txt' in the local
982 // downloads directory. It should only be called for automatic downloads.
983 if (test_case.test_type == AUTOMATIC) {
984 base::FilePath expected_virtual_path = GetPathInDownloadDir(
985 test_case.expected_virtual_path);
986 EXPECT_CALL(*delegate(), DetermineLocalPath(
987 item.get(), expected_virtual_path, _))
988 .WillRepeatedly(WithArg<2>(ScheduleCallback(
989 GetPathInDownloadDir(FILE_PATH_LITERAL("foo-local.txt")))));
990 } else {
991 EXPECT_CALL(*delegate(), DetermineLocalPath(_, _, _))
992 .Times(0);
993 }
994 RunTestCaseWithDownloadItem(test_case, item.get());
995 }
996 }
997
998 // Test that an inactive download will still get a virtual or local download
999 // path.
1000 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_InactiveDownload) {
1001 const DownloadTestCase kInactiveTestCases[] = {
1002 {
1003 AUTOMATIC,
1004 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1005 "http://example.com/foo.txt", "text/plain",
1006 FILE_PATH_LITERAL(""),
1007
1008 FILE_PATH_LITERAL("foo.txt"),
1009 FILE_PATH_LITERAL(""),
1010 FILE_PATH_LITERAL(""),
1011 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1012
1013 EXPECT_LOCAL_PATH
1014 },
1015
1016 {
1017 SAVE_AS,
1018 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1019 "http://example.com/foo.txt", "text/plain",
1020 FILE_PATH_LITERAL(""),
1021
1022 FILE_PATH_LITERAL("foo.txt"),
1023 FILE_PATH_LITERAL(""),
1024 // Even though this is a SAVE_AS download, no prompt will be displayed to
1025 // the user because the download is inactive.
1026 FILE_PATH_LITERAL(""),
1027 DownloadItem::TARGET_DISPOSITION_PROMPT,
1028
1029 EXPECT_LOCAL_PATH
1030 }
1031 };
1032
1033 for (size_t i = 0; i < arraysize(kInactiveTestCases); ++i) {
1034 scoped_ptr<content::MockDownloadItem> item(CreateActiveDownloadItem(i));
1035 SCOPED_TRACE(testing::Message() << "Running test case " << i);
1036 EXPECT_CALL(*item.get(), IsInProgress())
1037 .WillRepeatedly(Return(false));
1038 RunTestCaseWithDownloadItem(kInactiveTestCases[i], item.get());
1039 }
1040 }
1041
1042 // If the reserved path could not be verified, then the user should see a
1043 // prompt.
1044 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ReservationFailed) {
1045 const DownloadTestCase kReservationFailedCases[] = {
1046 {
1047 // 0: Automatic download. Since the reservation fails, the disposition of
1048 // the target is to prompt. Also the returned reserved path is ignored.
1049 AUTOMATIC,
1050 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1051 "http://example.com/foo.txt", "text/plain",
1052 FILE_PATH_LITERAL(""),
1053
1054 FILE_PATH_LITERAL(""),
1055 FILE_PATH_LITERAL("foo.txt"),
1056 FILE_PATH_LITERAL("foo.txt"),
1057 DownloadItem::TARGET_DISPOSITION_PROMPT,
1058
1059 EXPECT_CRDOWNLOAD
1060 },
1061 };
1062
1063 // Setup ReserveVirtualPath() to fail.
1064 ON_CALL(*delegate(), ReserveVirtualPath(_, _, _, _))
1065 .WillByDefault(WithArg<3>(ScheduleCallback2(
1066 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt")), false)));
1067 RunTestCases(kReservationFailedCases, arraysize(kReservationFailedCases));
1068 }
1069
1070 // If the local path could not be determined, the download should be cancelled.
1071 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_LocalPathFailed) {
1072 const DownloadTestCase kLocalPathFailedCases[] = {
1073 {
1074 // 0: Automatic download.
1075 AUTOMATIC,
1076 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1077 "http://example.com/foo.txt", "text/plain",
1078 FILE_PATH_LITERAL(""),
1079
1080 FILE_PATH_LITERAL(""),
1081 FILE_PATH_LITERAL(""),
1082 FILE_PATH_LITERAL("virtual/foo.txt"),
1083 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1084
1085 EXPECT_LOCAL_PATH
1086 },
1087 };
1088
1089 base::FilePath expected_virtual_path(
1090 GetPathInDownloadDir(FILE_PATH_LITERAL("virtual/foo.txt")));
1091 // The default download directory is the virtual path.
1092 SetDefaultDownloadPath(test_virtual_dir());
1093 // Simulate failed call to DetermineLocalPath.
1094 EXPECT_CALL(*delegate(), DetermineLocalPath(
1095 _, GetPathInDownloadDir(FILE_PATH_LITERAL("virtual/foo.txt")), _))
1096 .WillOnce(WithArg<2>(ScheduleCallback(base::FilePath())));
1097 RunTestCases(kLocalPathFailedCases, arraysize(kLocalPathFailedCases));
1098 }
1099
1100 // Downloads that have a danger level of AllowOnUserGesture should be marked as
1101 // safe depending on whether there was a user gesture associated with the
1102 // download and whether the referrer was visited prior to today.
1103 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_VisitedReferrer) {
1104 const DownloadTestCase kVisitedReferrerCases[] = {
1105 // http://visited.example.com/ is added to the history as a visit that
1106 // happened prior to today.
1107 {
1108 // 0: Safe download due to visiting referrer before.
1109 AUTOMATIC,
1110 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1111 "http://visited.example.com/foo.html", "text/html",
1112 FILE_PATH_LITERAL(""),
1113
1114 FILE_PATH_LITERAL(""),
1115 FILE_PATH_LITERAL("foo.html"),
1116 FILE_PATH_LITERAL("foo.html"),
1117 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1118
1119 EXPECT_CRDOWNLOAD
1120 },
1121
1122 {
1123 // 1: Dangerous due to not having visited referrer before.
1124 AUTOMATIC,
1125 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1126 "http://not-visited.example.com/foo.html", "text/html",
1127 FILE_PATH_LITERAL(""),
1128
1129 FILE_PATH_LITERAL(""),
1130 FILE_PATH_LITERAL("foo.html"),
1131 FILE_PATH_LITERAL("foo.html"),
1132 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1133
1134 EXPECT_UNCONFIRMED
1135 },
1136
1137 {
1138 // 2: Safe because the user is being prompted.
1139 SAVE_AS,
1140 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1141 "http://not-visited.example.com/foo.html", "text/html",
1142 FILE_PATH_LITERAL(""),
1143
1144 FILE_PATH_LITERAL(""),
1145 FILE_PATH_LITERAL("foo.html"),
1146 FILE_PATH_LITERAL("foo.html"),
1147 DownloadItem::TARGET_DISPOSITION_PROMPT,
1148
1149 EXPECT_CRDOWNLOAD
1150 },
1151
1152 {
1153 // 3: Safe because of forced path.
1154 FORCED,
1155 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1156 "http://not-visited.example.com/foo.html", "text/html",
1157 FILE_PATH_LITERAL("foo.html"),
1158
1159 FILE_PATH_LITERAL(""),
1160 FILE_PATH_LITERAL("foo.html"),
1161 FILE_PATH_LITERAL("foo.html"),
1162 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1163
1164 EXPECT_LOCAL_PATH
1165 },
1166 };
1167
1168 // This test assumes that the danger level of .html files is
1169 // AllowOnUserGesture.
1170 ASSERT_EQ(download_util::AllowOnUserGesture,
1171 download_util::GetFileDangerLevel(
1172 base::FilePath(FILE_PATH_LITERAL("foo.html"))));
1173
1174 // First the history service must exist.
1175 profile()->CreateHistoryService(false, false);
1176
1177 GURL url("http://visited.example.com/visited-link.html");
1178 // The time of visit is picked to be several seconds prior to the most recent
1179 // midnight.
1180 base::Time time_of_visit(
1181 base::Time::Now().LocalMidnight() - base::TimeDelta::FromSeconds(10));
1182 HistoryService* history_service =
1183 HistoryServiceFactory::GetForProfile(profile(), Profile::EXPLICIT_ACCESS);
1184 ASSERT_TRUE(history_service);
1185 history_service->AddPage(url, time_of_visit, history::SOURCE_BROWSED);
1186
1187 RunTestCases(kVisitedReferrerCases, arraysize(kVisitedReferrerCases));
1188 }
1189
1190 // These test cases are run with "Prompt for download" user preference set to
1191 // true. Even with the preference set, some of these downloads should not cause
1192 // a prompt to appear.
1193 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_PromptAlways) {
1194 const DownloadTestCase kPromptingTestCases[] = {
1195 {
1196 // 0: Safe Automatic - Should prompt because of "Prompt for download"
1197 // preference setting.
1198 AUTOMATIC,
1199 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1200 "http://example.com/foo.txt", "text/plain",
1201 FILE_PATH_LITERAL(""),
1202
1203 FILE_PATH_LITERAL(""),
1204 FILE_PATH_LITERAL("foo.txt"),
1205 FILE_PATH_LITERAL("foo.txt"),
1206 DownloadItem::TARGET_DISPOSITION_PROMPT,
1207
1208 EXPECT_CRDOWNLOAD
1209 },
1210
1211 {
1212 // 1: Automatic Browser Extension download. - Shouldn't prompt for browser
1213 // extension downloads even if "Prompt for download" preference is set.
1214 AUTOMATIC,
1215 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE,
1216 "http://example.com/foo.crx",
1217 extensions::Extension::kMimeType,
1218 FILE_PATH_LITERAL(""),
1219
1220 FILE_PATH_LITERAL(""),
1221 FILE_PATH_LITERAL("foo.crx"),
1222 FILE_PATH_LITERAL("foo.crx"),
1223 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1224
1225 EXPECT_UNCONFIRMED
1226 },
1227
1228 {
1229 // 2: Safe Forced - Shouldn't prompt.
1230 FORCED,
1231 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1232 "http://example.com/foo.txt", "text/plain",
1233 FILE_PATH_LITERAL("foo.txt"),
1234
1235 FILE_PATH_LITERAL(""),
1236 FILE_PATH_LITERAL("foo.txt"),
1237 FILE_PATH_LITERAL("foo.txt"),
1238 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1239
1240 EXPECT_LOCAL_PATH
1241 },
1242
1243 {
1244 // 3: Automatic User Script - Shouldn't prompt for user script downloads
1245 // even if "Prompt for download" preference is set.
1246 AUTOMATIC,
1247 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1248 "http://example.com/foo.user.js", "",
1249 FILE_PATH_LITERAL(""),
1250
1251 FILE_PATH_LITERAL(""),
1252 FILE_PATH_LITERAL("foo.user.js"),
1253 FILE_PATH_LITERAL("foo.user.js"),
1254 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1255
1256 EXPECT_CRDOWNLOAD
1257 },
1258
1259 {
1260 // 4: Automatic - The filename extension is marked as one that we will
1261 // open automatically. Shouldn't prompt.
1262 AUTOMATIC,
1263 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1264 "http://example.com/foo.dummy", "",
1265 FILE_PATH_LITERAL(""),
1266
1267 FILE_PATH_LITERAL(""),
1268 FILE_PATH_LITERAL("foo.dummy"),
1269 FILE_PATH_LITERAL("foo.dummy"),
1270 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1271
1272 EXPECT_CRDOWNLOAD
1273 },
1274 };
1275
1276 SetPromptForDownload(true);
1277 EnableAutoOpenBasedOnExtension(
1278 base::FilePath(FILE_PATH_LITERAL("dummy.dummy")));
1279 RunTestCases(kPromptingTestCases, arraysize(kPromptingTestCases));
1280 }
1281
1282 // If the download path is managed, then we don't show any prompts.
1283 // Note that if the download path is managed, then PromptForDownload() is false.
1284 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_ManagedPath) {
1285 const DownloadTestCase kManagedPathTestCases[] = {
1286 {
1287 // 0: Automatic Safe
1288 AUTOMATIC,
1289 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1290 "http://example.com/foo.txt", "text/plain",
1291 FILE_PATH_LITERAL(""),
1292
1293 FILE_PATH_LITERAL(""),
1294 FILE_PATH_LITERAL("foo.txt"),
1295 FILE_PATH_LITERAL("foo.txt"),
1296 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1297
1298 EXPECT_CRDOWNLOAD
1299 },
1300
1301 {
1302 // 1: Save_As Safe
1303 SAVE_AS,
1304 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
1305 "http://example.com/foo.txt", "text/plain",
1306 FILE_PATH_LITERAL(""),
1307
1308 FILE_PATH_LITERAL(""),
1309 FILE_PATH_LITERAL("foo.txt"),
1310 FILE_PATH_LITERAL("foo.txt"),
1311 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1312
1313 EXPECT_CRDOWNLOAD
1314 },
1315 };
1316
1317 SetManagedDownloadPath(test_download_dir());
1318 ASSERT_TRUE(download_prefs()->IsDownloadPathManaged());
1319 RunTestCases(kManagedPathTestCases, arraysize(kManagedPathTestCases));
1320 }
1321
1322 } // namespace
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698