Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/i18n/file_util_icu.h" | |
| 7 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 8 #include "base/scoped_temp_dir.h" | 9 #include "base/scoped_temp_dir.h" |
| 10 #include "base/string_util.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 9 #include "chrome/app/chrome_command_ids.h" | 12 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/download/download_history.h" | 13 #include "chrome/browser/download/download_history.h" |
| 11 #include "chrome/browser/download/download_item.h" | 14 #include "chrome/browser/download/download_item.h" |
| 12 #include "chrome/browser/download/download_manager.h" | 15 #include "chrome/browser/download/download_manager.h" |
| 13 #include "chrome/browser/history/download_history_info.h" | 16 #include "chrome/browser/history/download_history_info.h" |
| 17 #include "chrome/browser/download/download_prefs.h" | |
| 18 #include "chrome/browser/download/download_util.h" | |
| 19 #include "chrome/browser/prefs/pref_service.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/browser.h" | 21 #include "chrome/browser/ui/browser.h" |
| 16 #include "chrome/browser/ui/browser_window.h" | 22 #include "chrome/browser/ui/browser_window.h" |
| 17 #include "chrome/browser/ui/webui/active_downloads_ui.h" | 23 #include "chrome/browser/ui/webui/active_downloads_ui.h" |
| 18 #include "chrome/common/chrome_paths.h" | 24 #include "chrome/common/chrome_paths.h" |
| 25 #include "chrome/common/pref_names.h" | |
| 26 #include "chrome/common/random.h" | |
| 19 #include "chrome/common/url_constants.h" | 27 #include "chrome/common/url_constants.h" |
| 20 #include "chrome/test/base/in_process_browser_test.h" | 28 #include "chrome/test/base/in_process_browser_test.h" |
| 21 #include "chrome/test/base/ui_test_utils.h" | 29 #include "chrome/test/base/ui_test_utils.h" |
| 22 #include "content/browser/net/url_request_mock_http_job.h" | 30 #include "content/browser/net/url_request_mock_http_job.h" |
| 23 #include "content/browser/tab_contents/tab_contents.h" | 31 #include "content/browser/tab_contents/tab_contents.h" |
| 24 #include "content/common/content_notification_types.h" | 32 #include "content/common/content_notification_types.h" |
| 25 #include "content/common/notification_service.h" | 33 #include "content/common/notification_service.h" |
| 34 #include "net/url_request/url_request_filter.h" | |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 35 #include "testing/gtest/include/gtest/gtest.h" |
| 27 | 36 |
| 28 namespace { | 37 namespace { |
| 29 | 38 |
| 30 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); | 39 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); |
| 31 | 40 |
| 41 // The test file used in SavePageBrowserTest.SaveFolder3. | |
| 42 static const FilePath kTestFile(FILE_PATH_LITERAL("a.htm")); | |
| 43 | |
| 32 static const char* kAppendedExtension = | 44 static const char* kAppendedExtension = |
| 33 #if defined(OS_WIN) | 45 #if defined(OS_WIN) |
| 34 ".htm"; | 46 ".htm"; |
| 35 #else | 47 #else |
| 36 ".html"; | 48 ".html"; |
| 37 #endif | 49 #endif |
| 38 | 50 |
| 39 class SavePageBrowserTest : public InProcessBrowserTest { | 51 class SavePageBrowserTest : public InProcessBrowserTest { |
| 40 protected: | 52 protected: |
| 41 void SetUp() { | 53 void SetUp() { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 found = true; | 86 found = true; |
| 75 break; | 87 break; |
| 76 } | 88 } |
| 77 } | 89 } |
| 78 EXPECT_TRUE(found); | 90 EXPECT_TRUE(found); |
| 79 #else | 91 #else |
| 80 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 92 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
| 81 #endif | 93 #endif |
| 82 } | 94 } |
| 83 | 95 |
| 96 // Changes the default folder prefs. This method saves the current folder | |
| 97 // for saving HTML, the current folder for saving downloaded files, | |
| 98 // the current user's "Downloads" folder and a save type (HTML only or | |
| 99 // complete HTML files), and then changes them to |website_save_dir|, | |
| 100 // |download_save_dir| and |save_type|, respectively. | |
| 101 // If we call this method, we must call RestoreDirectoryPrefs() | |
| 102 // after the test to restore the default folder prefs. | |
| 103 void ChangeDirectoryPrefs( | |
| 104 Profile* profile, | |
| 105 const FilePath& website_save_dir, | |
| 106 const FilePath& download_save_dir, | |
| 107 const SavePackage::SavePackageType save_type) { | |
| 108 DCHECK(profile); | |
| 109 PrefService* prefs = profile->GetPrefs(); | |
| 110 | |
| 111 DCHECK(prefs->FindPreference(prefs::kDownloadDefaultDirectory)); | |
|
Paweł Hajdan Jr.
2011/08/12 17:00:59
ASSERT_TRUE.
haraken1
2011/08/15 00:44:15
Done.
| |
| 112 prev_download_save_dir_ = prefs->GetFilePath( | |
| 113 prefs::kDownloadDefaultDirectory); | |
| 114 | |
| 115 // Check whether the preference has the default folder for saving HTML. | |
| 116 // If not, initialize it with the default folder for downloaded files. | |
| 117 if (!prefs->FindPreference(prefs::kSaveFileDefaultDirectory)) { | |
| 118 prefs->RegisterFilePathPref(prefs::kSaveFileDefaultDirectory, | |
| 119 prev_download_save_dir_, | |
| 120 PrefService::UNSYNCABLE_PREF); | |
| 121 } | |
| 122 prev_website_save_dir_ = prefs->GetFilePath( | |
| 123 prefs::kSaveFileDefaultDirectory); | |
| 124 | |
| 125 DownloadPrefs* download_prefs = | |
| 126 profile->GetDownloadManager()->download_prefs(); | |
| 127 prev_save_type_ = | |
| 128 static_cast<SavePackage::SavePackageType> | |
| 129 (download_prefs->save_file_type()); | |
| 130 | |
| 131 prefs->SetFilePath( | |
| 132 prefs::kSaveFileDefaultDirectory, website_save_dir); | |
| 133 prefs->SetFilePath( | |
| 134 prefs::kDownloadDefaultDirectory, download_save_dir); | |
| 135 prefs->SetInteger(prefs::kSaveFileType, save_type); | |
| 136 } | |
| 137 | |
| 138 // Restores the default folder prefs. | |
| 139 void RestoreDirectoryPrefs(Profile* profile) { | |
| 140 DCHECK(profile); | |
| 141 PrefService* prefs = profile->GetPrefs(); | |
| 142 prefs->SetFilePath( | |
| 143 prefs::kSaveFileDefaultDirectory, prev_website_save_dir_); | |
| 144 prefs->SetFilePath( | |
| 145 prefs::kDownloadDefaultDirectory, prev_download_save_dir_); | |
| 146 prefs->SetInteger(prefs::kSaveFileType, prev_save_type_); | |
| 147 } | |
| 148 | |
| 84 DownloadManager* GetDownloadManager() const { | 149 DownloadManager* GetDownloadManager() const { |
| 85 DownloadManager* download_manager = | 150 DownloadManager* download_manager = |
| 86 browser()->profile()->GetDownloadManager(); | 151 browser()->profile()->GetDownloadManager(); |
| 87 EXPECT_TRUE(download_manager); | 152 EXPECT_TRUE(download_manager); |
| 88 return download_manager; | 153 return download_manager; |
| 89 } | 154 } |
| 90 | 155 |
| 91 void QueryDownloadHistory() { | 156 void QueryDownloadHistory() { |
| 92 // Query the history system. | 157 // Query the history system. |
| 93 GetDownloadManager()->download_history()->Load( | 158 GetDownloadManager()->download_history()->Load( |
| 94 NewCallback(this, | 159 NewCallback(this, |
| 95 &SavePageBrowserTest::OnQueryDownloadEntriesComplete)); | 160 &SavePageBrowserTest::OnQueryDownloadEntriesComplete)); |
| 96 | 161 |
| 97 // Run message loop until a quit message is sent from | 162 // Run message loop until a quit message is sent from |
| 98 // OnQueryDownloadEntriesComplete(). | 163 // OnQueryDownloadEntriesComplete(). |
| 99 ui_test_utils::RunMessageLoop(); | 164 ui_test_utils::RunMessageLoop(); |
| 100 } | 165 } |
| 101 | 166 |
| 102 void OnQueryDownloadEntriesComplete( | 167 void OnQueryDownloadEntriesComplete( |
| 103 std::vector<DownloadHistoryInfo>* entries) { | 168 std::vector<DownloadHistoryInfo>* entries) { |
| 104 history_entries_ = *entries; | 169 history_entries_ = *entries; |
| 105 | 170 |
| 106 // Indicate thet we have received the history and can continue. | 171 // Indicate thet we have received the history and can continue. |
| 107 MessageLoopForUI::current()->Quit(); | 172 MessageLoopForUI::current()->Quit(); |
| 108 } | 173 } |
| 109 | 174 |
| 175 // Returns "src/chrome/test/data/{kTestFile}", whatever URL is given. | |
| 176 static net::URLRequestJob* FactoryForTestFile( | |
| 177 net::URLRequest* request, const std::string& scheme) { | |
| 178 FilePath test_dir; | |
| 179 if (!PathService::Get(chrome::DIR_TEST_DATA, &test_dir)) | |
| 180 NOTREACHED(); | |
|
Paweł Hajdan Jr.
2011/08/12 17:00:59
EXPECT_TRUE, applies to entire CL.
haraken1
2011/08/15 00:44:15
Done.
| |
| 181 return new URLRequestMockHTTPJob( | |
| 182 request, test_dir.Append(kTestDir).Append(kTestFile)); | |
| 183 } | |
| 184 | |
| 110 struct DownloadHistoryInfoMatch | 185 struct DownloadHistoryInfoMatch |
| 111 : public std::unary_function<DownloadHistoryInfo, bool> { | 186 : public std::unary_function<DownloadHistoryInfo, bool> { |
| 112 | 187 |
| 113 DownloadHistoryInfoMatch(const GURL& url, | 188 DownloadHistoryInfoMatch(const GURL& url, |
| 114 const FilePath& path, | 189 const FilePath& path, |
| 115 int64 num_files) | 190 int64 num_files) |
| 116 : url_(url), | 191 : url_(url), |
| 117 path_(path), | 192 path_(path), |
| 118 num_files_(num_files) { | 193 num_files_(num_files) { |
| 119 } | 194 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 142 history_entries_.end()); | 217 history_entries_.end()); |
| 143 } | 218 } |
| 144 | 219 |
| 145 std::vector<DownloadHistoryInfo> history_entries_; | 220 std::vector<DownloadHistoryInfo> history_entries_; |
| 146 | 221 |
| 147 // Path to directory containing test data. | 222 // Path to directory containing test data. |
| 148 FilePath test_dir_; | 223 FilePath test_dir_; |
| 149 | 224 |
| 150 // Temporary directory we will save pages to. | 225 // Temporary directory we will save pages to. |
| 151 ScopedTempDir save_dir_; | 226 ScopedTempDir save_dir_; |
| 227 | |
| 228 // Temporarily stores the default folder prefs. | |
| 229 FilePath prev_website_save_dir_; | |
| 230 FilePath prev_download_save_dir_; | |
| 231 SavePackage::SavePackageType prev_save_type_; | |
| 152 }; | 232 }; |
| 153 | 233 |
| 234 } // namespace | |
| 235 | |
| 154 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | 236 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
| 155 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 237 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
| 156 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 238 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
| 157 FilePath(kTestDir).Append(file_name)); | 239 FilePath(kTestDir).Append(file_name)); |
| 158 ui_test_utils::NavigateToURL(browser(), url); | 240 ui_test_utils::NavigateToURL(browser(), url); |
| 159 | 241 |
| 160 TabContents* current_tab = browser()->GetSelectedTabContents(); | 242 TabContents* current_tab = browser()->GetSelectedTabContents(); |
| 161 ASSERT_TRUE(current_tab); | 243 ASSERT_TRUE(current_tab); |
| 162 | 244 |
| 163 FilePath full_file_name = save_dir_.path().Append(file_name); | 245 FilePath full_file_name = save_dir_.path().Append(file_name); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 231 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), | 313 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), |
| 232 full_file_name)); | 314 full_file_name)); |
| 233 EXPECT_TRUE(file_util::ContentsEqual( | 315 EXPECT_TRUE(file_util::ContentsEqual( |
| 234 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 316 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
| 235 dir.AppendASCII("1.png"))); | 317 dir.AppendASCII("1.png"))); |
| 236 EXPECT_TRUE(file_util::ContentsEqual( | 318 EXPECT_TRUE(file_util::ContentsEqual( |
| 237 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 319 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
| 238 dir.AppendASCII("1.css"))); | 320 dir.AppendASCII("1.css"))); |
| 239 } | 321 } |
| 240 | 322 |
| 323 // Checks if an HTML page is saved to the default folder for saving HTML | |
| 324 // in the following situation: | |
| 325 // The default folder for saving HTML exists. | |
| 326 // The default folder for downloaded files exists. | |
| 327 // The user's "Downloads" folder exists. | |
| 328 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder1) { | |
| 329 FilePath file(FILE_PATH_LITERAL("a.htm")); | |
| 330 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
| 331 FilePath(kTestDir).Append(file)); | |
| 332 ui_test_utils::NavigateToURL(browser(), url); | |
| 333 | |
| 334 TabContents* current_tab = browser()->GetSelectedTabContents(); | |
| 335 ASSERT_TRUE(current_tab); | |
| 336 ASSERT_TRUE(browser()->profile()); | |
| 337 | |
| 338 ScopedTempDir website_save_dir, download_save_dir; | |
| 339 // Prepare the default folder for saving HTML. | |
| 340 ASSERT_TRUE(website_save_dir.CreateUniqueTempDir()); | |
| 341 // Prepare the default folder for downloaded files. | |
| 342 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir()); | |
| 343 | |
| 344 // Changes the default prefs. | |
| 345 ChangeDirectoryPrefs( | |
| 346 browser()->profile(), | |
| 347 website_save_dir.path(), | |
| 348 download_save_dir.path(), | |
| 349 SavePackage::SAVE_AS_ONLY_HTML); | |
| 350 | |
| 351 string16 title = current_tab->SavePageBasedOnDefaultPrefs(); | |
| 352 FilePath::StringType basename; | |
| 353 #if defined(OS_WIN) | |
| 354 basename = UTF16ToWide(title); | |
| 355 basename.append(FILE_PATH_LITERAL(".htm")); | |
| 356 #else | |
| 357 basename = UTF16ToASCII(title); | |
| 358 basename.append(FILE_PATH_LITERAL(".html")); | |
| 359 #endif | |
| 360 file_util::ReplaceIllegalCharactersInPath(&basename, ' '); | |
| 361 FilePath downloaded_file = website_save_dir.path().Append(FilePath(basename)); | |
| 362 | |
| 363 EXPECT_EQ(url, WaitForSavePackageToFinish()); | |
| 364 | |
| 365 CheckDownloadUI(downloaded_file); | |
| 366 | |
| 367 // Is the file downloaded to the default folder for saving HTML? | |
| 368 EXPECT_TRUE(file_util::PathExists(downloaded_file)); | |
| 369 EXPECT_TRUE(file_util::ContentsEqual( | |
| 370 test_dir_.Append(FilePath(kTestDir)).Append(file), | |
| 371 downloaded_file)); | |
| 372 | |
| 373 RestoreDirectoryPrefs(browser()->profile()); | |
| 374 } | |
| 375 | |
| 376 // Checks if an HTML page is saved to the default folder for downloaded files | |
| 377 // in the following situation: | |
| 378 // The default folder for saving HTML does not exist. | |
| 379 // The default folder for downloaded files exists. | |
| 380 // The user's "Downloads" folder exists. | |
| 381 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder2) { | |
| 382 FilePath file(FILE_PATH_LITERAL("a.htm")); | |
| 383 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
| 384 FilePath(kTestDir).Append(file)); | |
| 385 ui_test_utils::NavigateToURL(browser(), url); | |
| 386 | |
| 387 TabContents* current_tab = browser()->GetSelectedTabContents(); | |
| 388 ASSERT_TRUE(current_tab); | |
| 389 ASSERT_TRUE(browser()->profile()); | |
| 390 | |
| 391 ScopedTempDir download_save_dir; | |
| 392 // Prepare the default folder for saving downloaded files. | |
| 393 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir()); | |
| 394 // Prepare non-existent folder. | |
| 395 FilePath nonexistent_path( | |
| 396 FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_nyannyan")); | |
| 397 ASSERT_FALSE(file_util::PathExists(nonexistent_path)); | |
| 398 | |
| 399 // Changes the default prefs. | |
| 400 ChangeDirectoryPrefs( | |
| 401 browser()->profile(), | |
| 402 nonexistent_path, | |
| 403 download_save_dir.path(), | |
| 404 SavePackage::SAVE_AS_ONLY_HTML); | |
| 405 | |
| 406 string16 title = current_tab->SavePageBasedOnDefaultPrefs(); | |
| 407 FilePath::StringType basename; | |
| 408 #if defined(OS_WIN) | |
| 409 basename = UTF16ToWide(title); | |
| 410 basename.append(FILE_PATH_LITERAL(".htm")); | |
| 411 #else | |
| 412 basename = UTF16ToASCII(title); | |
| 413 basename.append(FILE_PATH_LITERAL(".html")); | |
| 414 #endif | |
| 415 file_util::ReplaceIllegalCharactersInPath(&basename, ' '); | |
| 416 FilePath downloaded_file = | |
| 417 download_save_dir.path().Append(FilePath(basename)); | |
| 418 | |
| 419 EXPECT_EQ(url, WaitForSavePackageToFinish()); | |
| 420 | |
| 421 CheckDownloadUI(downloaded_file); | |
| 422 | |
| 423 // Is the file downloaded to the default folder for downloaded files? | |
| 424 EXPECT_TRUE(file_util::PathExists(downloaded_file)); | |
| 425 EXPECT_FALSE(file_util::PathExists(nonexistent_path)); | |
| 426 EXPECT_TRUE(file_util::ContentsEqual( | |
| 427 test_dir_.Append(FilePath(kTestDir)).Append(file), | |
| 428 downloaded_file)); | |
| 429 | |
| 430 RestoreDirectoryPrefs(browser()->profile()); | |
| 431 } | |
| 432 | |
| 433 // Checks if an HTML page is saved to the user's "Downloads" folder | |
| 434 // in the following situation: | |
| 435 // The default folder for saving HTML does not exist. | |
| 436 // The default folder for downloaded files does not exist. | |
| 437 // The user's "Downloads" folder exists. | |
| 438 // | |
| 439 // This test creates and deletes a file on the user's real "Downloads" folder, | |
| 440 // which is globally shared among all tests on the testing environment. | |
| 441 // Therefore, if we run browser tests in parallel, the file created by one | |
| 442 // browser test may be deleted by another broswer test when the file name | |
| 443 // conflicts. In order to avoid this problem, we use a special mock URL | |
| 444 // "http://mock.testfile.http/<random path>" for this download test. | |
| 445 // Since we redirect "http://mock.testfile.http/<random path>" to | |
| 446 // "chrome/test/data/{kTestFile}" using FactoryForTestFile(), | |
| 447 // "chrome/test/data/{kTestFile}" is used for the file to be downloaded. | |
| 448 // Then, the downloaded file is saved as a name "Downloads/<random path>", | |
| 449 // which is a unique file name in the user's real "Downloads" folder. | |
| 450 // | |
| 451 // Ideally, in the first place, we should not use the user's "Downloads" folder. | |
| 452 // Instead, we should create a temporary "Downloads" folder for each test. | |
| 453 // However, we concluded that creating temporary "Downloads" folder for each | |
| 454 // test requires very invasive code changes to many places. See also here: | |
| 455 // http://codereview.chromium.org/6973052/ | |
| 456 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveFolder3) { | |
| 457 FilePath file(kTestFile); | |
| 458 | |
| 459 // Redirects "http://mock.testfile.http/<random path>" | |
| 460 // to "src/chrome/test/data/{kTestFile}", whatever the <random path> is. | |
| 461 std::string kMockHostnameForTestFile = "mock.testfile.http"; | |
| 462 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | |
| 463 filter->AddHostnameHandler( | |
| 464 "http", kMockHostnameForTestFile, FactoryForTestFile); | |
| 465 std::string random_string = Generate128BitRandomBase64String(); | |
| 466 RemoveChars(random_string, "/", &random_string); | |
| 467 GURL url("http://" + kMockHostnameForTestFile + "/" + random_string); | |
| 468 ui_test_utils::NavigateToURL(browser(), url); | |
| 469 | |
| 470 TabContents* current_tab = browser()->GetSelectedTabContents(); | |
| 471 ASSERT_TRUE(current_tab); | |
| 472 ASSERT_TRUE(browser()->profile()); | |
| 473 | |
| 474 // Prepare non-existent folder. | |
| 475 FilePath nonexistent_path1( | |
| 476 FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_nyannyan")); | |
| 477 FilePath nonexistent_path2( | |
| 478 FILE_PATH_LITERAL("/tmp/koakuma_mikity_moemoe_pyonpyon")); | |
| 479 ASSERT_FALSE(file_util::PathExists(nonexistent_path1)); | |
| 480 ASSERT_FALSE(file_util::PathExists(nonexistent_path2)); | |
| 481 | |
| 482 // Changes the default prefs. | |
| 483 ChangeDirectoryPrefs( | |
| 484 browser()->profile(), | |
| 485 nonexistent_path1, | |
| 486 nonexistent_path2, | |
| 487 SavePackage::SAVE_AS_ONLY_HTML); | |
| 488 | |
| 489 string16 title = current_tab->SavePageBasedOnDefaultPrefs(); | |
| 490 FilePath::StringType basename; | |
| 491 #if defined(OS_WIN) | |
| 492 basename = UTF16ToWide(title); | |
| 493 basename.append(FILE_PATH_LITERAL(".htm")); | |
| 494 #else | |
| 495 basename = UTF16ToASCII(title); | |
| 496 basename.append(FILE_PATH_LITERAL(".html")); | |
| 497 #endif | |
| 498 file_util::ReplaceIllegalCharactersInPath(&basename, ' '); | |
| 499 FilePath default_download_dir = | |
| 500 download_util::GetDefaultDownloadDirectoryFromPathService(); | |
| 501 FilePath downloaded_file = | |
| 502 default_download_dir.Append(FilePath(basename)); | |
| 503 // Make sure that the target file does not exist. | |
| 504 file_util::Delete(downloaded_file, false); | |
| 505 // Make sure that the temporary file does not exist. | |
| 506 FilePath temporary_file = | |
| 507 default_download_dir.Append(FilePath( | |
| 508 basename + FILE_PATH_LITERAL(".crdownload"))); | |
| 509 file_util::Delete(temporary_file, false); | |
| 510 | |
| 511 EXPECT_EQ(url, WaitForSavePackageToFinish()); | |
| 512 | |
| 513 CheckDownloadUI(downloaded_file); | |
| 514 | |
| 515 // Is the file downloaded to the user's "Downloads" directory? | |
| 516 EXPECT_TRUE(file_util::PathExists(downloaded_file)); | |
| 517 EXPECT_FALSE(file_util::PathExists(nonexistent_path1)); | |
| 518 EXPECT_FALSE(file_util::PathExists(nonexistent_path2)); | |
| 519 EXPECT_TRUE(file_util::ContentsEqual( | |
| 520 test_dir_.Append(FilePath(kTestDir)).Append(file), downloaded_file)); | |
| 521 | |
| 522 // Clean up the generated files. | |
| 523 file_util::Delete(downloaded_file, false); | |
| 524 file_util::Delete(temporary_file, false); | |
| 525 | |
| 526 RestoreDirectoryPrefs(browser()->profile()); | |
| 527 } | |
| 528 | |
| 241 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { | 529 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { |
| 242 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); | 530 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); |
| 243 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); | 531 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); |
| 244 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); | 532 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); |
| 245 } | 533 } |
| 246 | 534 |
| 247 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { | 535 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { |
| 248 FilePath file_name(FILE_PATH_LITERAL("b.htm")); | 536 FilePath file_name(FILE_PATH_LITERAL("b.htm")); |
| 249 | 537 |
| 250 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 538 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 310 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), | 598 EXPECT_EQ(std::find_if(history_entries_.begin(), history_entries_.end(), |
| 311 DownloadHistoryInfoMatch(url, full_file_name, 1)), | 599 DownloadHistoryInfoMatch(url, full_file_name, 1)), |
| 312 history_entries_.end()); | 600 history_entries_.end()); |
| 313 | 601 |
| 314 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 602 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
| 315 EXPECT_FALSE(file_util::PathExists(dir)); | 603 EXPECT_FALSE(file_util::PathExists(dir)); |
| 316 EXPECT_TRUE(file_util::ContentsEqual( | 604 EXPECT_TRUE(file_util::ContentsEqual( |
| 317 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 605 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
| 318 full_file_name)); | 606 full_file_name)); |
| 319 } | 607 } |
| 320 | |
| 321 } | |
| OLD | NEW |