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

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

Issue 12662032: Merge SavePackageFilePicker{,ChromeOS} (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: @r202870 Created 7 years, 6 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/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/files/scoped_temp_dir.h" 6 #include "base/files/scoped_temp_dir.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/prefs/pref_service.h" 9 #include "base/prefs/pref_service.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 virtual void TearDown() OVERRIDE; 186 virtual void TearDown() OVERRIDE;
187 187
188 // Creates MockDownloadItem and sets up default expectations. 188 // Creates MockDownloadItem and sets up default expectations.
189 content::MockDownloadItem* CreateActiveDownloadItem( 189 content::MockDownloadItem* CreateActiveDownloadItem(
190 int32 id, 190 int32 id,
191 const DownloadTestCase& test_case); 191 const DownloadTestCase& test_case);
192 192
193 // Sets the AutoOpenBasedOnExtension user preference for |path|. 193 // Sets the AutoOpenBasedOnExtension user preference for |path|.
194 void EnableAutoOpenBasedOnExtension(const base::FilePath& path); 194 void EnableAutoOpenBasedOnExtension(const base::FilePath& path);
195 195
196 // Set the kDownloadDefaultDirectory user preference to |path|.
197 void SetDefaultDownloadPath(const base::FilePath& path);
198
199 // Set the kDownloadDefaultDirectory managed preference to |path|. 196 // Set the kDownloadDefaultDirectory managed preference to |path|.
200 void SetManagedDownloadPath(const base::FilePath& path); 197 void SetManagedDownloadPath(const base::FilePath& path);
201 198
202 // Set the kPromptForDownload user preference to |prompt|. 199 // Set the kPromptForDownload user preference to |prompt|.
203 void SetPromptForDownload(bool prompt); 200 void SetPromptForDownload(bool prompt);
204 201
205 // Given the relative path |path|, returns the full path under the temporary 202 // Given the relative path |path|, returns the full path under the temporary
206 // downloads directory. 203 // downloads directory.
207 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path); 204 base::FilePath GetPathInDownloadDir(const base::FilePath::StringType& path);
208 205
(...skipping 16 matching lines...) Expand all
225 } 222 }
226 223
227 MockDownloadTargetDeterminerDelegate* delegate() { 224 MockDownloadTargetDeterminerDelegate* delegate() {
228 return &delegate_; 225 return &delegate_;
229 } 226 }
230 227
231 DownloadPrefs* download_prefs() { 228 DownloadPrefs* download_prefs() {
232 return download_prefs_.get(); 229 return download_prefs_.get();
233 } 230 }
234 231
235 void set_last_selected_directory(const base::FilePath& path) {
236 last_selected_directory_ = path;
237 }
238
239 private: 232 private:
240 // Verifies that |target_path|, |disposition|, |expected_danger_type| and 233 // Verifies that |target_path|, |disposition|, |expected_danger_type| and
241 // |intermediate_path| matches the expectations of |test_case|. Posts 234 // |intermediate_path| matches the expectations of |test_case|. Posts
242 // |closure| to the current message loop when done. 235 // |closure| to the current message loop when done.
243 void DownloadTargetVerifier(const base::Closure& closure, 236 void DownloadTargetVerifier(const base::Closure& closure,
244 const DownloadTestCase& test_case, 237 const DownloadTestCase& test_case,
245 const base::FilePath& local_path, 238 const base::FilePath& local_path,
246 DownloadItem::TargetDisposition disposition, 239 DownloadItem::TargetDisposition disposition,
247 content::DownloadDangerType danger_type, 240 content::DownloadDangerType danger_type,
248 const base::FilePath& intermediate_path); 241 const base::FilePath& intermediate_path);
249 242
250 scoped_ptr<DownloadPrefs> download_prefs_; 243 scoped_ptr<DownloadPrefs> download_prefs_;
251 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_; 244 ::testing::NiceMock<MockDownloadTargetDeterminerDelegate> delegate_;
252 NullWebContentsDelegate web_contents_delegate_; 245 NullWebContentsDelegate web_contents_delegate_;
253 base::ScopedTempDir test_download_dir_; 246 base::ScopedTempDir test_download_dir_;
254 base::FilePath test_virtual_dir_; 247 base::FilePath test_virtual_dir_;
255 base::FilePath last_selected_directory_;
256 content::TestBrowserThread ui_thread_; 248 content::TestBrowserThread ui_thread_;
257 content::TestBrowserThread file_thread_; 249 content::TestBrowserThread file_thread_;
258 }; 250 };
259 251
260 DownloadTargetDeterminerTest::DownloadTargetDeterminerTest() 252 DownloadTargetDeterminerTest::DownloadTargetDeterminerTest()
261 : ChromeRenderViewHostTestHarness(), 253 : ChromeRenderViewHostTestHarness(),
262 ui_thread_(content::BrowserThread::UI, &message_loop_), 254 ui_thread_(content::BrowserThread::UI, &message_loop_),
263 file_thread_(content::BrowserThread::FILE, &message_loop_) { 255 file_thread_(content::BrowserThread::FILE, &message_loop_) {
264 } 256 }
265 257
266 void DownloadTargetDeterminerTest::SetUp() { 258 void DownloadTargetDeterminerTest::SetUp() {
267 ChromeRenderViewHostTestHarness::SetUp(); 259 ChromeRenderViewHostTestHarness::SetUp();
268 CHECK(profile()); 260 CHECK(profile());
269 download_prefs_.reset(new DownloadPrefs(profile())); 261 download_prefs_.reset(new DownloadPrefs(profile()));
270 web_contents()->SetDelegate(&web_contents_delegate_); 262 web_contents()->SetDelegate(&web_contents_delegate_);
271 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir()); 263 ASSERT_TRUE(test_download_dir_.CreateUniqueTempDir());
272 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual")); 264 test_virtual_dir_ = test_download_dir().Append(FILE_PATH_LITERAL("virtual"));
273 SetDefaultDownloadPath(test_download_dir()); 265 download_prefs_->SetDownloadPath(test_download_dir());
274 delegate_.SetupDefaults(); 266 delegate_.SetupDefaults();
275 } 267 }
276 268
277 void DownloadTargetDeterminerTest::TearDown() { 269 void DownloadTargetDeterminerTest::TearDown() {
278 download_prefs_.reset(); 270 download_prefs_.reset();
279 message_loop_.RunUntilIdle(); 271 message_loop_.RunUntilIdle();
280 ChromeRenderViewHostTestHarness::TearDown(); 272 ChromeRenderViewHostTestHarness::TearDown();
281 } 273 }
282 274
283 content::MockDownloadItem* 275 content::MockDownloadItem*
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 ON_CALL(*item, IsTemporary()) 327 ON_CALL(*item, IsTemporary())
336 .WillByDefault(Return(false)); 328 .WillByDefault(Return(false));
337 return item; 329 return item;
338 } 330 }
339 331
340 void DownloadTargetDeterminerTest::EnableAutoOpenBasedOnExtension( 332 void DownloadTargetDeterminerTest::EnableAutoOpenBasedOnExtension(
341 const base::FilePath& path) { 333 const base::FilePath& path) {
342 EXPECT_TRUE(download_prefs_->EnableAutoOpenBasedOnExtension(path)); 334 EXPECT_TRUE(download_prefs_->EnableAutoOpenBasedOnExtension(path));
343 } 335 }
344 336
345 void DownloadTargetDeterminerTest::SetDefaultDownloadPath(
346 const base::FilePath& path) {
347 profile()->GetTestingPrefService()->
348 SetFilePath(prefs::kDownloadDefaultDirectory, path);
349 }
350
351 void DownloadTargetDeterminerTest::SetManagedDownloadPath( 337 void DownloadTargetDeterminerTest::SetManagedDownloadPath(
352 const base::FilePath& path) { 338 const base::FilePath& path) {
353 profile()->GetTestingPrefService()-> 339 profile()->GetTestingPrefService()->
354 SetManagedPref(prefs::kDownloadDefaultDirectory, 340 SetManagedPref(prefs::kDownloadDefaultDirectory,
355 base::CreateFilePathValue(path)); 341 base::CreateFilePathValue(path));
356 } 342 }
357 343
358 void DownloadTargetDeterminerTest::SetPromptForDownload(bool prompt) { 344 void DownloadTargetDeterminerTest::SetPromptForDownload(bool prompt) {
359 profile()->GetTestingPrefService()-> 345 profile()->GetTestingPrefService()->
360 SetBoolean(prefs::kPromptForDownload, prompt); 346 SetBoolean(prefs::kPromptForDownload, prompt);
361 } 347 }
362 348
363 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir( 349 base::FilePath DownloadTargetDeterminerTest::GetPathInDownloadDir(
364 const base::FilePath::StringType& relative_path) { 350 const base::FilePath::StringType& relative_path) {
365 if (relative_path.empty()) 351 if (relative_path.empty())
366 return base::FilePath(); 352 return base::FilePath();
367 base::FilePath full_path(test_download_dir().Append(relative_path)); 353 base::FilePath full_path(test_download_dir().Append(relative_path));
368 return full_path.NormalizePathSeparators(); 354 return full_path.NormalizePathSeparators();
369 } 355 }
370 356
371 void DownloadTargetDeterminerTest::RunTestCase( 357 void DownloadTargetDeterminerTest::RunTestCase(
372 const DownloadTestCase& test_case, 358 const DownloadTestCase& test_case,
373 content::MockDownloadItem* item) { 359 content::MockDownloadItem* item) {
374 // Kick off the test. 360 // Kick off the test.
375 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this); 361 base::WeakPtrFactory<DownloadTargetDeterminerTest> factory(this);
376 base::RunLoop run_loop; 362 base::RunLoop run_loop;
377 DownloadTargetDeterminer::Start( 363 DownloadTargetDeterminer::Start(
378 item, download_prefs_.get(), last_selected_directory_, delegate(), 364 item, download_prefs_.get(), delegate(),
379 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier, 365 base::Bind(&DownloadTargetDeterminerTest::DownloadTargetVerifier,
380 factory.GetWeakPtr(), run_loop.QuitClosure(), test_case)); 366 factory.GetWeakPtr(), run_loop.QuitClosure(), test_case));
381 run_loop.Run(); 367 run_loop.Run();
382 ::testing::Mock::VerifyAndClearExpectations(delegate()); 368 ::testing::Mock::VerifyAndClearExpectations(delegate());
383 } 369 }
384 370
385 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem( 371 void DownloadTargetDeterminerTest::RunTestCasesWithActiveItem(
386 const DownloadTestCase test_cases[], 372 const DownloadTestCase test_cases[],
387 size_t test_case_count) { 373 size_t test_case_count) {
388 for (size_t i = 0; i < test_case_count; ++i) { 374 for (size_t i = 0; i < test_case_count; ++i) {
(...skipping 405 matching lines...) Expand 10 before | Expand all | Expand 10 after
794 FILE_PATH_LITERAL("virtual/foo/foo.txt"), 780 FILE_PATH_LITERAL("virtual/foo/foo.txt"),
795 FILE_PATH_LITERAL("bar.txt"), 781 FILE_PATH_LITERAL("bar.txt"),
796 DownloadItem::TARGET_DISPOSITION_PROMPT, 782 DownloadItem::TARGET_DISPOSITION_PROMPT,
797 783
798 EXPECT_LOCAL_PATH 784 EXPECT_LOCAL_PATH
799 }, 785 },
800 }; 786 };
801 787
802 { 788 {
803 SCOPED_TRACE(testing::Message() 789 SCOPED_TRACE(testing::Message()
804 << "Running with empty last_selected_directory"); 790 << "Running with default download path");
805 base::FilePath prompt_path = 791 base::FilePath prompt_path =
806 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt")); 792 GetPathInDownloadDir(FILE_PATH_LITERAL("foo.txt"));
807 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _)); 793 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _));
808 RunTestCasesWithActiveItem(kLastSavePathTestCasesPre, 794 RunTestCasesWithActiveItem(kLastSavePathTestCasesPre,
809 arraysize(kLastSavePathTestCasesPre)); 795 arraysize(kLastSavePathTestCasesPre));
810 } 796 }
811 797
812 // Try with a non-empty last save path. 798 // Try with a non-empty last save path.
813 { 799 {
814 SCOPED_TRACE(testing::Message() 800 SCOPED_TRACE(testing::Message()
815 << "Running with local last_selected_directory"); 801 << "Running with local last_selected_directory");
816 set_last_selected_directory(test_download_dir().AppendASCII("foo")); 802 download_prefs()->SetSaveFilePath(test_download_dir().AppendASCII("foo"));
817 base::FilePath prompt_path = 803 base::FilePath prompt_path =
818 GetPathInDownloadDir(FILE_PATH_LITERAL("foo/foo.txt")); 804 GetPathInDownloadDir(FILE_PATH_LITERAL("foo/foo.txt"));
819 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _)); 805 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(_, prompt_path, _));
820 RunTestCasesWithActiveItem(kLastSavePathTestCasesPost, 806 RunTestCasesWithActiveItem(kLastSavePathTestCasesPost,
821 arraysize(kLastSavePathTestCasesPost)); 807 arraysize(kLastSavePathTestCasesPost));
822 } 808 }
823 809
824 // And again, but this time use a virtual directory. 810 // And again, but this time use a virtual directory.
825 { 811 {
826 SCOPED_TRACE(testing::Message() 812 SCOPED_TRACE(testing::Message()
827 << "Running with virtual last_selected_directory"); 813 << "Running with virtual last_selected_directory");
828 base::FilePath last_selected_dir = test_virtual_dir().AppendASCII("foo"); 814 base::FilePath last_selected_dir = test_virtual_dir().AppendASCII("foo");
829 base::FilePath virtual_path = last_selected_dir.AppendASCII("foo.txt"); 815 base::FilePath virtual_path = last_selected_dir.AppendASCII("foo.txt");
830 set_last_selected_directory(last_selected_dir); 816 download_prefs()->SetSaveFilePath(last_selected_dir);
831 EXPECT_CALL(*delegate(), PromptUserForDownloadPath( 817 EXPECT_CALL(*delegate(), PromptUserForDownloadPath(
832 _, last_selected_dir.AppendASCII("foo.txt"), _)); 818 _, last_selected_dir.AppendASCII("foo.txt"), _));
833 EXPECT_CALL(*delegate(), DetermineLocalPath(_, virtual_path, _)) 819 EXPECT_CALL(*delegate(), DetermineLocalPath(_, virtual_path, _))
834 .WillOnce(WithArg<2>(ScheduleCallback( 820 .WillOnce(WithArg<2>(ScheduleCallback(
835 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt"))))); 821 GetPathInDownloadDir(FILE_PATH_LITERAL("bar.txt")))));
836 RunTestCasesWithActiveItem(kLastSavePathTestCasesVirtual, 822 RunTestCasesWithActiveItem(kLastSavePathTestCasesVirtual,
837 arraysize(kLastSavePathTestCasesVirtual)); 823 arraysize(kLastSavePathTestCasesVirtual));
838 } 824 }
839 } 825 }
840 826
841 // These tests are run with the default downloads folder set to a virtual 827 // These tests are run with the default downloads folder set to a virtual
842 // directory. 828 // directory.
843 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_DefaultVirtual) { 829 TEST_F(DownloadTargetDeterminerTest, TargetDeterminer_DefaultVirtual) {
844 // The default download directory is the virutal path. 830 // The default download directory is the virutal path.
845 SetDefaultDownloadPath(test_virtual_dir()); 831 download_prefs()->SetDownloadPath(test_virtual_dir());
846 832
847 { 833 {
848 SCOPED_TRACE(testing::Message() << "Automatic Safe Download"); 834 SCOPED_TRACE(testing::Message() << "Automatic Safe Download");
849 const DownloadTestCase kAutomaticDownloadToVirtualDir = { 835 const DownloadTestCase kAutomaticDownloadToVirtualDir = {
850 AUTOMATIC, 836 AUTOMATIC,
851 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, 837 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS,
852 "http://example.com/foo.txt", "text/plain", 838 "http://example.com/foo.txt", "text/plain",
853 FILE_PATH_LITERAL(""), 839 FILE_PATH_LITERAL(""),
854 840
855 // Downloaded to default virtual directory. 841 // Downloaded to default virtual directory.
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
1020 FILE_PATH_LITERAL(""), 1006 FILE_PATH_LITERAL(""),
1021 DownloadItem::TARGET_DISPOSITION_OVERWRITE, 1007 DownloadItem::TARGET_DISPOSITION_OVERWRITE,
1022 1008
1023 EXPECT_LOCAL_PATH 1009 EXPECT_LOCAL_PATH
1024 }, 1010 },
1025 }; 1011 };
1026 1012
1027 base::FilePath expected_virtual_path( 1013 base::FilePath expected_virtual_path(
1028 GetPathInDownloadDir(FILE_PATH_LITERAL("virtual/foo.txt"))); 1014 GetPathInDownloadDir(FILE_PATH_LITERAL("virtual/foo.txt")));
1029 // The default download directory is the virtual path. 1015 // The default download directory is the virtual path.
1030 SetDefaultDownloadPath(test_virtual_dir()); 1016 download_prefs()->SetDownloadPath(test_virtual_dir());
1031 // Simulate failed call to DetermineLocalPath. 1017 // Simulate failed call to DetermineLocalPath.
1032 EXPECT_CALL(*delegate(), DetermineLocalPath( 1018 EXPECT_CALL(*delegate(), DetermineLocalPath(
1033 _, GetPathInDownloadDir(FILE_PATH_LITERAL("virtual/foo.txt")), _)) 1019 _, GetPathInDownloadDir(FILE_PATH_LITERAL("virtual/foo.txt")), _))
1034 .WillOnce(WithArg<2>(ScheduleCallback(base::FilePath()))); 1020 .WillOnce(WithArg<2>(ScheduleCallback(base::FilePath())));
1035 RunTestCasesWithActiveItem(kLocalPathFailedCases, 1021 RunTestCasesWithActiveItem(kLocalPathFailedCases,
1036 arraysize(kLocalPathFailedCases)); 1022 arraysize(kLocalPathFailedCases));
1037 } 1023 }
1038 1024
1039 // Downloads that have a danger level of AllowOnUserGesture should be marked as 1025 // Downloads that have a danger level of AllowOnUserGesture should be marked as
1040 // safe depending on whether there was a user gesture associated with the 1026 // safe depending on whether there was a user gesture associated with the
(...skipping 413 matching lines...) Expand 10 before | Expand all | Expand 10 after
1454 EXPECT_CRDOWNLOAD 1440 EXPECT_CRDOWNLOAD
1455 }; 1441 };
1456 1442
1457 const DownloadTestCase& test_case = kNotifyExtensionsTestCase; 1443 const DownloadTestCase& test_case = kNotifyExtensionsTestCase;
1458 scoped_ptr<content::MockDownloadItem> item( 1444 scoped_ptr<content::MockDownloadItem> item(
1459 CreateActiveDownloadItem(0, test_case)); 1445 CreateActiveDownloadItem(0, test_case));
1460 base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt")); 1446 base::FilePath overridden_path(FILE_PATH_LITERAL("overridden/foo.txt"));
1461 base::FilePath full_overridden_path = 1447 base::FilePath full_overridden_path =
1462 GetPathInDownloadDir(overridden_path.value()); 1448 GetPathInDownloadDir(overridden_path.value());
1463 1449
1464 // The last selected directory is this one. Since the test case is a SAVE_AS 1450 download_prefs()->SetSaveFilePath(GetPathInDownloadDir(
1465 // download, it should use this directory for the generated path.
1466 set_last_selected_directory(GetPathInDownloadDir(
1467 FILE_PATH_LITERAL("last_selected"))); 1451 FILE_PATH_LITERAL("last_selected")));
1468 1452
1469 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _)) 1453 EXPECT_CALL(*delegate(), NotifyExtensions(_, _, _))
1470 .WillOnce(WithArg<2>( 1454 .WillOnce(WithArg<2>(
1471 ScheduleCallback2(overridden_path, 1455 ScheduleCallback2(overridden_path,
1472 DownloadPathReservationTracker::UNIQUIFY))); 1456 DownloadPathReservationTracker::UNIQUIFY)));
1473 EXPECT_CALL(*delegate(), 1457 EXPECT_CALL(*delegate(),
1474 PromptUserForDownloadPath(_, full_overridden_path, _)) 1458 PromptUserForDownloadPath(_, full_overridden_path, _))
1475 .WillOnce(WithArg<2>( 1459 .WillOnce(WithArg<2>(
1476 ScheduleCallback(full_overridden_path))); 1460 ScheduleCallback(full_overridden_path)));
1477 RunTestCase(test_case, item.get()); 1461 RunTestCase(test_case, item.get());
1478 } 1462 }
1479 } // namespace 1463 } // namespace
OLDNEW
« no previous file with comments | « chrome/browser/download/download_target_determiner.cc ('k') | chrome/browser/download/save_package_file_picker.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698