OLD | NEW |
---|---|
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_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file_path.h" | 6 #include "base/files/file_path.h" |
7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
9 #include "base/observer_list.h" | 9 #include "base/observer_list.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 } | 226 } |
227 | 227 |
228 MockDownloadTargetDeterminerDelegate* delegate() { | 228 MockDownloadTargetDeterminerDelegate* delegate() { |
229 return &delegate_; | 229 return &delegate_; |
230 } | 230 } |
231 | 231 |
232 DownloadPrefs* download_prefs() { | 232 DownloadPrefs* download_prefs() { |
233 return download_prefs_.get(); | 233 return download_prefs_.get(); |
234 } | 234 } |
235 | 235 |
236 void set_last_selected_directory(const base::FilePath& path) { | |
237 last_selected_directory_ = path; | |
238 } | |
239 | |
240 private: | 236 private: |
241 // Verifies that |target_path|, |disposition|, |expected_danger_type| and | 237 // Verifies that |target_path|, |disposition|, |expected_danger_type| and |
242 // |intermediate_path| matches the expectations of |test_case|. Posts | 238 // |intermediate_path| matches the expectations of |test_case|. Posts |
243 // |closure| to the current message loop when done. | 239 // |closure| to the current message loop when done. |
244 void DownloadTargetVerifier(const base::Closure& closure, | 240 void DownloadTargetVerifier(const base::Closure& closure, |
245 const DownloadTestCase& test_case, | 241 const DownloadTestCase& test_case, |
246 const base::FilePath& local_path, | 242 const base::FilePath& local_path, |
247 DownloadItem::TargetDisposition disposition, | 243 DownloadItem::TargetDisposition disposition, |
248 content::DownloadDangerType danger_type, | 244 content::DownloadDangerType danger_type, |
249 const base::FilePath& intermediate_path); | 245 const base::FilePath& intermediate_path); |
250 | 246 |
251 scoped_ptr<DownloadPrefs> download_prefs_; | 247 scoped_ptr<DownloadPrefs> download_prefs_; |
252 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_; | 248 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_; |
253 NullWebContentsDelegate web_contents_delegate_; | 249 NullWebContentsDelegate web_contents_delegate_; |
254 base::ScopedTempDir test_download_dir_; | 250 base::ScopedTempDir test_download_dir_; |
255 base::FilePath test_virtual_dir_; | 251 base::FilePath test_virtual_dir_; |
256 base::FilePath last_selected_directory_; | |
257 content::TestBrowserThread ui_thread_; | 252 content::TestBrowserThread ui_thread_; |
258 content::TestBrowserThread file_thread_; | 253 content::TestBrowserThread file_thread_; |
259 }; | 254 }; |
260 | 255 |
261 DownloadTargetDeterminerTest::DownloadTargetDeterminerTest() | 256 DownloadTargetDeterminerTest::DownloadTargetDeterminerTest() |
262 : ChromeRenderViewHostTestHarness(), | 257 : ChromeRenderViewHostTestHarness(), |
263 ui_thread_(content::BrowserThread::UI, &message_loop_), | 258 ui_thread_(content::BrowserThread::UI, &message_loop_), |
264 file_thread_(content::BrowserThread::FILE, &message_loop_) { | 259 file_thread_(content::BrowserThread::FILE, &message_loop_) { |
265 } | 260 } |
266 | 261 |
267 void DownloadTargetDeterminerTest::SetUp() { | 262 void DownloadTargetDeterminerTest::SetUp() { |
268 ChromeRenderViewHostTestHarness::SetUp(); | 263 ChromeRenderViewHostTestHarness::SetUp(); |
269 CHECK(profile()); | 264 CHECK(profile()); |
270 download_prefs_.reset(new DownloadPrefs(profile())); | 265 download_prefs_.reset(new DownloadPrefs(profile())); |
271 web_contents()->SetDelegate(&web_contents_delegate_); | 266 web_contents()->SetDelegate(&web_contents_delegate_); |
272 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); | 267 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); |
273 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual")); | 268 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual")); |
269 download_prefs_->SetDownloadPath(test_download_dir()); | |
270 download_prefs_->SetSaveFilePath(test_download_dir()); | |
274 SetDefaultDownloadPath(test_download_dir()); | 271 SetDefaultDownloadPath(test_download_dir()); |
275 delegate_.SetupDefaults(); | 272 delegate_.SetupDefaults(); |
276 } | 273 } |
277 | 274 |
278 void DownloadTargetDeterminerTest::TearDown() { | 275 void DownloadTargetDeterminerTest::TearDown() { |
279 download_prefs_.reset(); | 276 download_prefs_.reset(); |
280 message_loop_.RunUntilIdle(); | 277 message_loop_.RunUntilIdle(); |
281 ChromeRenderViewHostTestHarness::TearDown(); | 278 ChromeRenderViewHostTestHarness::TearDown(); |
282 } | 279 } |
283 | 280 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
340 return item; | 337 return item; |
341 } | 338 } |
342 | 339 |
343 void DownloadTargetDeterminerTest::EnableAutoOpenBasedOnExtension( | 340 void DownloadTargetDeterminerTest::EnableAutoOpenBasedOnExtension( |
344 const base::FilePath& path) { | 341 const base::FilePath& path) { |
345 EXPECT_TRUE(download_prefs_->EnableAutoOpenBasedOnExtension(path)); | 342 EXPECT_TRUE(download_prefs_->EnableAutoOpenBasedOnExtension(path)); |
346 } | 343 } |
347 | 344 |
348 void DownloadTargetDeterminerTest::SetDefaultDownloadPath( | 345 void DownloadTargetDeterminerTest::SetDefaultDownloadPath( |
349 const base::FilePath& path) { | 346 const base::FilePath& path) { |
350 profile()->GetTestingPrefService()-> | 347 download_prefs()->SetDownloadPath(path); |
351 SetFilePath(prefs::kDownloadDefaultDirectory, path); | 348 download_prefs()->SetSaveFilePath(path); |
352 } | 349 } |
353 | 350 |
354 void DownloadTargetDeterminerTest::SetManagedDownloadPath( | 351 void DownloadTargetDeterminerTest::SetManagedDownloadPath( |
355 const base::FilePath& path) { | 352 const base::FilePath& path) { |
356 profile()->GetTestingPrefService()-> | 353 profile()->GetTestingPrefService()-> |
357 SetManagedPref(prefs::kDownloadDefaultDirectory, | 354 SetManagedPref(prefs::kDownloadDefaultDirectory, |
358 base::CreateFilePathValue(path)); | 355 base::CreateFilePathValue(path)); |
359 } | 356 } |
360 | 357 |
361 void DownloadTargetDeterminerTest::SetPromptForDownload(bool prompt) { | 358 void DownloadTargetDeterminerTest::SetPromptForDownload(bool prompt) { |
362 profile()->GetTestingPrefService()-> | 359 profile()->GetTestingPrefService()-> |
363 SetBoolean(prefs::kPromptForDownload, prompt); | 360 SetBoolean(prefs::kPromptForDownload, prompt); |
364 } | 361 } |
365 | 362 |
366 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir( | 363 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir( |
367 const base::FilePath::StringType& relative_path) { | 364 const base::FilePath::StringType& relative_path) { |
368 if (relative_path.empty()) | 365 if (relative_path.empty()) |
369 return base::FilePath(); | 366 return base::FilePath(); |
370 base::FilePath full_path(test_download_dir().Append(relative_path)); | 367 base::FilePath full_path(test_download_dir().Append(relative_path)); |
371 return full_path.NormalizePathSeparators(); | 368 return full_path.NormalizePathSeparators(); |
372 } | 369 } |
373 | 370 |
374 void DownloadTargetDeterminerTest::RunTestCase( | 371 void DownloadTargetDeterminerTest::RunTestCase( |
375 const DownloadTestCase& test_case, | 372 const DownloadTestCase& test_case, |
376 content::MockDownloadItem* item) { | 373 content::MockDownloadItem* item) { |
377 // Kick off the test. | 374 // Kick off the test. |
378 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this); | 375 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this); |
379 base::RunLoop run_loop; | 376 base::RunLoop run_loop; |
380 DownloadTargetDeterminer::Start( | 377 DownloadTargetDeterminer::Start( |
381 item, download_prefs_.get(), last_selected_directory_, delegate(), | 378 item, download_prefs_.get(), delegate(), |
382 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier, | 379 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier, |
383 factory.GetWeakPtr(), run_loop.QuitClosure(), test_case)); | 380 factory.GetWeakPtr(), run_loop.QuitClosure(), test_case)); |
384 run_loop.Run(); | 381 run_loop.Run(); |
385 ::testing::Mock::VerifyAndClearExpectations(delegate()); | 382 ::testing::Mock::VerifyAndClearExpectations(delegate()); |
386 } | 383 } |
387 | 384 |
388 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem( | 385 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem( |
389 const DownloadTestCase test_cases[], | 386 const DownloadTestCase test_cases[], |
390 size_t test_case_count) { | 387 size_t test_case_count) { |
391 for (size_t i = 0; i < test_case_count; ++i) { | 388 for (size_t i = 0; i < test_case_count; ++i) { |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
795 | 792 |
796 FILE_PATH_LITERAL("virtual/foo/foo.txt"), | 793 FILE_PATH_LITERAL("virtual/foo/foo.txt"), |
797 FILE_PATH_LITERAL("bar.txt"), | 794 FILE_PATH_LITERAL("bar.txt"), |
798 DownloadItem::TARGET_DISPOSITION_PROMPT, | 795 DownloadItem::TARGET_DISPOSITION_PROMPT, |
799 | 796 |
800 EXPECT_LOCAL_PATH | 797 EXPECT_LOCAL_PATH |
801 }, | 798 }, |
802 }; | 799 }; |
803 | 800 |
804 { | 801 { |
805 SCOPED_TRACE(testing::Message() | |
asanka
2013/05/07 15:15:12
Can you add a descriptive SCOPED_TRACE() here?
benjhayden
2013/05/17 20:31:54
Done.
| |
806 << "Running with empty last_selected_directory"); | |
807 base::FilePath prompt_path = | 802 base::FilePath prompt_path = |
808 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt")); | 803 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt")); |
809 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _)); | 804 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _)); |
810 RunTestCasesWithActiveItem(kLastSavePathTestCasesPre, | 805 RunTestCasesWithActiveItem(kLastSavePathTestCasesPre, |
811 arraysize(kLastSavePathTestCasesPre)); | 806 arraysize(kLastSavePathTestCasesPre)); |
812 } | 807 } |
813 | 808 |
814 // Try with a non-empty last save path. | 809 // Try with a non-empty last save path. |
815 { | 810 { |
816 SCOPED_TRACE(testing::Message() | 811 SCOPED_TRACE(testing::Message() |
817 << "Running with local last_selected_directory"); | 812 << "Running with local last_selected_directory"); |
818 set_last_selected_directory(test_download_dir().AppendASCII("foo")); | 813 download_prefs()->SetSaveFilePath(test_download_dir().AppendASCII("foo")); |
819 base::FilePath prompt_path = | 814 base::FilePath prompt_path = |
820 GetPathInDownloadDir(FILE_PATH_LITERAL("foo/foo.txt")); | 815 GetPathInDownloadDir(FILE_PATH_LITERAL("foo/foo.txt")); |
821 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _)); | 816 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _)); |
822 RunTestCasesWithActiveItem(kLastSavePathTestCasesPost, | 817 RunTestCasesWithActiveItem(kLastSavePathTestCasesPost, |
823 arraysize(kLastSavePathTestCasesPost)); | 818 arraysize(kLastSavePathTestCasesPost)); |
824 } | 819 } |
825 | 820 |
826 // And again, but this time use a virtual directory. | 821 // And again, but this time use a virtual directory. |
827 { | 822 { |
828 SCOPED_TRACE(testing::Message() | 823 SCOPED_TRACE(testing::Message() |
829 << "Running with virtual last_selected_directory"); | 824 << "Running with virtual last_selected_directory"); |
830 base::FilePath last_selected_dir = test_virtual_dir().AppendASCII("foo"); | 825 base::FilePath last_selected_dir = test_virtual_dir().AppendASCII("foo"); |
831 base::FilePath virtual_path = last_selected_dir.AppendASCII("foo.txt"); | 826 base::FilePath virtual_path = last_selected_dir.AppendASCII("foo.txt"); |
832 set_last_selected_directory(last_selected_dir); | 827 download_prefs()->SetSaveFilePath(last_selected_dir); |
833 EXPECT_CALL(*delegate(), PromptUserForDownloadPath( | 828 EXPECT_CALL(*delegate(), PromptUserForDownloadPath( |
834 _, last_selected_dir.AppendASCII("foo.txt"), _)); | 829 _, last_selected_dir.AppendASCII("foo.txt"), _)); |
835 EXPECT_CALL(*delegate(), DetermineLocalPath(_, virtual_path, _)) | 830 EXPECT_CALL(*delegate(), DetermineLocalPath(_, virtual_path, _)) |
836 .WillOnce(WithArg<2>(ScheduleCallback( | 831 .WillOnce(WithArg<2>(ScheduleCallback( |
837 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt"))))); | 832 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt"))))); |
838 RunTestCasesWithActiveItem(kLastSavePathTestCasesVirtual, | 833 RunTestCasesWithActiveItem(kLastSavePathTestCasesVirtual, |
839 arraysize(kLastSavePathTestCasesVirtual)); | 834 arraysize(kLastSavePathTestCasesVirtual)); |
840 } | 835 } |
841 } | 836 } |
842 | 837 |
(...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1456 EXPECT_CRDOWNLOAD | 1451 EXPECT_CRDOWNLOAD |
1457 }; | 1452 }; |
1458 | 1453 |
1459 const DownloadTestCase& test_case = kNotifyExtensionsTestCase; | 1454 const DownloadTestCase& test_case = kNotifyExtensionsTestCase; |
1460 scoped_ptr<content::MockDownloadItem> item( | 1455 scoped_ptr<content::MockDownloadItem> item( |
1461 CreateActiveDownloadItem(0, test_case)); | 1456 CreateActiveDownloadItem(0, test_case)); |
1462 base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt")); | 1457 base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt")); |
1463 base::FilePath full_overridden_path = | 1458 base::FilePath full_overridden_path = |
1464 GetPathInDownloadDir(overridden_path.value()); | 1459 GetPathInDownloadDir(overridden_path.value()); |
1465 | 1460 |
1466 // The last selected directory is this one. Since the test case is a SAVE_AS | |
1467 // download, it should use this directory for the generated path. | |
1468 set_last_selected_directory(GetPathInDownloadDir( | |
asanka
2013/05/07 15:15:12
The purpose of this test is to verify that that wh
benjhayden
2013/05/17 20:31:54
Done.
| |
1469 FILE_PATH_LITERAL("last_selected"))); | |
1470 | |
1471 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) | 1461 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) |
1472 .WillOnce(WithArg<2>( | 1462 .WillOnce(WithArg<2>( |
1473 ScheduleCallback2(overridden_path, | 1463 ScheduleCallback2(overridden_path, |
1474 DownloadPathReservationTracker::UNIQUIFY))); | 1464 DownloadPathReservationTracker::UNIQUIFY))); |
1475 EXPECT_CALL(*delegate(), | 1465 EXPECT_CALL(*delegate(), |
1476 PromptUserForDownloadPath(_, full_overridden_path, _)) | 1466 PromptUserForDownloadPath(_, full_overridden_path, _)) |
1477 .WillOnce(WithArg<2>( | 1467 .WillOnce(WithArg<2>( |
1478 ScheduleCallback(full_overridden_path))); | 1468 ScheduleCallback(full_overridden_path))); |
1479 RunTestCase(test_case, item.get()); | 1469 RunTestCase(test_case, item.get()); |
1480 } | 1470 } |
1481 } // namespace | 1471 } // namespace |
OLD | NEW |