save_page_browsertest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
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/bind.h" 6#include "base/bind_helpers.h" 7#include "base/command_line.h" 8#include "base/file_util.h" 9#include "base/files/file_path.h" 10#include "base/files/scoped_temp_dir.h" 11#include "base/path_service.h" 12#include "base/prefs/pref_member.h" 13#include "base/prefs/pref_service.h" 14#include "base/test/test_file_util.h" 15#include "chrome/app/chrome_command_ids.h" 16#include "chrome/browser/download/chrome_download_manager_delegate.h" 17#include "chrome/browser/download/download_history.h" 18#include "chrome/browser/download/download_prefs.h" 19#include "chrome/browser/download/download_service.h" 20#include "chrome/browser/download/download_service_factory.h" 21#include "chrome/browser/download/save_package_file_picker.h" 22#include "chrome/browser/history/download_row.h" 23#include "chrome/browser/net/url_request_mock_util.h" 24#include "chrome/browser/profiles/profile.h" 25#include "chrome/browser/ui/browser.h" 26#include "chrome/browser/ui/browser_commands.h" 27#include "chrome/browser/ui/browser_window.h" 28#include "chrome/browser/ui/tabs/tab_strip_model.h" 29#include "chrome/common/chrome_paths.h" 30#include "chrome/common/chrome_switches.h" 31#include "chrome/common/pref_names.h" 32#include "chrome/common/url_constants.h" 33#include "chrome/test/base/in_process_browser_test.h" 34#include "chrome/test/base/ui_test_utils.h" 35#include "content/public/browser/download_item.h" 36#include "content/public/browser/download_manager.h" 37#include "content/public/browser/notification_service.h" 38#include "content/public/browser/notification_types.h" 39#include "content/public/browser/web_contents.h" 40#include "content/public/test/test_utils.h" 41#include "content/test/net/url_request_mock_http_job.h" 42#include "testing/gtest/include/gtest/gtest.h" 43 44using content::BrowserContext; 45using content::BrowserThread; 46using content::DownloadItem; 47using content::DownloadManager; 48using content::URLRequestMockHTTPJob; 49using content::WebContents; 50 51namespace { 52 53// Waits for an item record in the downloads database to match |filter|. See 54// DownloadStoredProperly() below for an example filter. 55class DownloadPersistedObserver : public DownloadHistory::Observer { 56 public: 57 typedef base::Callback<bool( 58 DownloadItem* item, 59 const history::DownloadRow&)> PersistedFilter; 60 61 DownloadPersistedObserver(Profile* profile, const PersistedFilter& filter) 62 : profile_(profile), 63 filter_(filter), 64 waiting_(false), 65 persisted_(false) { 66 DownloadServiceFactory::GetForProfile(profile_)-> 67 GetDownloadHistory()->AddObserver(this); 68 } 69 70 virtual ~DownloadPersistedObserver() { 71 DownloadService* service = DownloadServiceFactory::GetForProfile(profile_); 72 if (service && service->GetDownloadHistory()) 73 service->GetDownloadHistory()->RemoveObserver(this); 74 } 75 76 bool WaitForPersisted() { 77 if (persisted_) 78 return true; 79 waiting_ = true; 80 content::RunMessageLoop(); 81 waiting_ = false; 82 return persisted_; 83 } 84 85 virtual void OnDownloadStored(DownloadItem* item, 86 const history::DownloadRow& info) OVERRIDE { 87 persisted_ = persisted_ || filter_.Run(item, info); 88 if (persisted_ && waiting_) 89 base::MessageLoopForUI::current()->Quit(); 90 } 91 92 private: 93 Profile* profile_; 94 DownloadItem* item_; 95 PersistedFilter filter_; 96 bool waiting_; 97 bool persisted_; 98 99 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); 100}; 101 102// Waits for an item record to be removed from the downloads database. 103class DownloadRemovedObserver : public DownloadPersistedObserver { 104 public: 105 DownloadRemovedObserver(Profile* profile, int32 download_id) 106 : DownloadPersistedObserver(profile, PersistedFilter()), 107 removed_(false), 108 waiting_(false), 109 download_id_(download_id) { 110 } 111 virtual ~DownloadRemovedObserver() {} 112 113 bool WaitForRemoved() { 114 if (removed_) 115 return true; 116 waiting_ = true; 117 content::RunMessageLoop(); 118 waiting_ = false; 119 return removed_; 120 } 121 122 virtual void OnDownloadStored(DownloadItem* item, 123 const history::DownloadRow& info) OVERRIDE { 124 } 125 126 virtual void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) OVERRIDE { 127 removed_ = ids.find(download_id_) != ids.end(); 128 if (removed_ && waiting_) 129 base::MessageLoopForUI::current()->Quit(); 130 } 131 132 private: 133 bool removed_; 134 bool waiting_; 135 int32 download_id_; 136 137 DISALLOW_COPY_AND_ASSIGN(DownloadRemovedObserver); 138}; 139 140bool DownloadStoredProperly( 141 const GURL& expected_url, 142 const base::FilePath& expected_path, 143 int64 num_files, 144 DownloadItem::DownloadState expected_state, 145 DownloadItem* item, 146 const history::DownloadRow& info) { 147 // This function may be called multiple times for a given test. Returning 148 // false doesn't necessarily mean that the test has failed or will fail, it 149 // might just mean that the test hasn't passed yet. 150 if (info.target_path != expected_path) { 151 VLOG(20) << __FUNCTION__ << " " << info.target_path.value() 152 << " != " << expected_path.value(); 153 return false; 154 } 155 if (info.url_chain.size() != 1u) { 156 VLOG(20) << __FUNCTION__ << " " << info.url_chain.size() 157 << " != 1"; 158 return false; 159 } 160 if (info.url_chain[0] != expected_url) { 161 VLOG(20) << __FUNCTION__ << " " << info.url_chain[0].spec() 162 << " != " << expected_url.spec(); 163 return false; 164 } 165 if ((num_files >= 0) && (info.received_bytes != num_files)) { 166 VLOG(20) << __FUNCTION__ << " " << num_files 167 << " != " << info.received_bytes; 168 return false; 169 } 170 if (info.state != expected_state) { 171 VLOG(20) << __FUNCTION__ << " " << info.state 172 << " != " << expected_state; 173 return false; 174 } 175 return true; 176} 177 178const base::FilePath::CharType kTestDir[] = FILE_PATH_LITERAL("save_page"); 179 180static const char kAppendedExtension[] = 181#if defined(OS_WIN) 182 ".htm"; 183#else 184 ".html"; 185#endif 186 187// Loosely based on logic in DownloadTestObserver. 188class DownloadItemCreatedObserver : public DownloadManager::Observer { 189 public: 190 explicit DownloadItemCreatedObserver(DownloadManager* manager) 191 : waiting_(false), manager_(manager) { 192 manager->AddObserver(this); 193 } 194 195 virtual ~DownloadItemCreatedObserver() { 196 if (manager_) 197 manager_->RemoveObserver(this); 198 } 199 200 // Wait for the first download item created after object creation. 201 // Note that this class provides no protection against the download 202 // being destroyed between creation and return of WaitForNewDownloadItem(); 203 // the caller must guarantee that in some other fashion. 204 void WaitForDownloadItem(std::vector<DownloadItem*>* items_seen) { 205 if (!manager_) { 206 // The manager went away before we were asked to wait; return 207 // what we have, even if it's null. 208 *items_seen = items_seen_; 209 return; 210 } 211 212 if (items_seen_.empty()) { 213 waiting_ = true; 214 content::RunMessageLoop(); 215 waiting_ = false; 216 } 217 218 *items_seen = items_seen_; 219 return; 220 } 221 222 private: 223 // DownloadManager::Observer 224 virtual void OnDownloadCreated( 225 DownloadManager* manager, DownloadItem* item) OVERRIDE { 226 DCHECK_EQ(manager, manager_); 227 items_seen_.push_back(item); 228 229 if (waiting_) 230 base::MessageLoopForUI::current()->Quit(); 231 } 232 233 virtual void ManagerGoingDown(DownloadManager* manager) OVERRIDE { 234 manager_->RemoveObserver(this); 235 manager_ = NULL; 236 if (waiting_) 237 base::MessageLoopForUI::current()->Quit(); 238 } 239 240 bool waiting_; 241 DownloadManager* manager_; 242 std::vector<DownloadItem*> items_seen_; 243 244 DISALLOW_COPY_AND_ASSIGN(DownloadItemCreatedObserver); 245}; 246 247class SavePackageFinishedObserver : public content::DownloadManager::Observer { 248 public: 249 SavePackageFinishedObserver(content::DownloadManager* manager, 250 const base::Closure& callback) 251 : download_manager_(manager), 252 callback_(callback) { 253 download_manager_->AddObserver(this); 254 } 255 256 virtual ~SavePackageFinishedObserver() { 257 if (download_manager_) 258 download_manager_->RemoveObserver(this); 259 } 260 261 // DownloadManager::Observer: 262 virtual void OnSavePackageSuccessfullyFinished( 263 content::DownloadManager* manager, content::DownloadItem* item) OVERRIDE { 264 callback_.Run(); 265 } 266 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE { 267 download_manager_->RemoveObserver(this); 268 download_manager_ = NULL; 269 } 270 271 private: 272 content::DownloadManager* download_manager_; 273 base::Closure callback_; 274 275 DISALLOW_COPY_AND_ASSIGN(SavePackageFinishedObserver); 276}; 277 278class SavePageBrowserTest : public InProcessBrowserTest { 279 public: 280 SavePageBrowserTest() {} 281 virtual ~SavePageBrowserTest(); 282 283 protected: 284 virtual void SetUp() OVERRIDE { 285 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_)); 286 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); 287 InProcessBrowserTest::SetUp(); 288 } 289 290 virtual void SetUpOnMainThread() OVERRIDE { 291 browser()->profile()->GetPrefs()->SetFilePath( 292 prefs::kDownloadDefaultDirectory, save_dir_.path()); 293 browser()->profile()->GetPrefs()->SetFilePath( 294 prefs::kSaveFileDefaultDirectory, save_dir_.path()); 295 BrowserThread::PostTask( 296 BrowserThread::IO, FROM_HERE, 297 base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); 298 } 299 300 GURL NavigateToMockURL(const std::string& prefix) { 301 GURL url = URLRequestMockHTTPJob::GetMockUrl( 302 base::FilePath(kTestDir).AppendASCII(prefix + ".htm")); 303 ui_test_utils::NavigateToURL(browser(), url); 304 return url; 305 } 306 307 // Returns full paths of destination file and directory. 308 void GetDestinationPaths(const std::string& prefix, 309 base::FilePath* full_file_name, 310 base::FilePath* dir) { 311 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); 312 *dir = save_dir_.path().AppendASCII(prefix + "_files"); 313 } 314 315 WebContents* GetCurrentTab(Browser* browser) const { 316 WebContents* current_tab = 317 browser->tab_strip_model()->GetActiveWebContents(); 318 EXPECT_TRUE(current_tab); 319 return current_tab; 320 } 321 322 // Returns true if and when there was a single download created, and its url 323 // is |expected_url|. 324 bool VerifySavePackageExpectations( 325 Browser* browser, 326 const GURL& expected_url) const { 327 // Generally, there should only be one download item created 328 // in all of these tests. If it's already here, grab it; if not, 329 // wait for it to show up. 330 std::vector<DownloadItem*> items; 331 DownloadManager* manager( 332 BrowserContext::GetDownloadManager(browser->profile())); 333 manager->GetAllDownloads(&items); 334 if (items.size() == 0u) { 335 DownloadItemCreatedObserver(manager).WaitForDownloadItem(&items); 336 } 337 338 EXPECT_EQ(1u, items.size()); 339 if (1u != items.size()) 340 return false; 341 DownloadItem* download_item(items[0]); 342 343 return (expected_url == download_item->GetOriginalUrl()); 344 } 345 346 // Note on synchronization: 347 // 348 // For each Save Page As operation, we create a corresponding shell 349 // DownloadItem to display progress to the user. That DownloadItem goes 350 // through its own state transitions, including being persisted out to the 351 // history database, and the download shelf is not shown until after the 352 // persistence occurs. Save Package completion (and marking the DownloadItem 353 // as completed) occurs asynchronously from persistence. Thus if we want to 354 // examine either UI state or DB state, we need to wait until both the save 355 // package operation is complete and the relevant download item has been 356 // persisted. 357 358 DownloadManager* GetDownloadManager() const { 359 DownloadManager* download_manager = 360 BrowserContext::GetDownloadManager(browser()->profile()); 361 EXPECT_TRUE(download_manager); 362 return download_manager; 363 } 364 365 // Path to directory containing test data. 366 base::FilePath test_dir_; 367 368 // Temporary directory we will save pages to. 369 base::ScopedTempDir save_dir_; 370 371 private: 372 DISALLOW_COPY_AND_ASSIGN(SavePageBrowserTest); 373}; 374 375SavePageBrowserTest::~SavePageBrowserTest() { 376} 377 378// Disabled on Windows due to flakiness. http://crbug.com/162323 379// TODO(linux_aura) http://crbug.com/163931 380#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)) 381#define MAYBE_SaveHTMLOnly DISABLED_SaveHTMLOnly 382#else 383#define MAYBE_SaveHTMLOnly SaveHTMLOnly 384#endif 385IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveHTMLOnly) { 386 GURL url = NavigateToMockURL("a"); 387 388 base::FilePath full_file_name, dir; 389 GetDestinationPaths("a", &full_file_name, &dir); 390 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( 391 &DownloadStoredProperly, url, full_file_name, 1, 392 DownloadItem::COMPLETE)); 393 scoped_refptr<content::MessageLoopRunner> loop_runner( 394 new content::MessageLoopRunner); 395 SavePackageFinishedObserver observer( 396 content::BrowserContext::GetDownloadManager(browser()->profile()), 397 loop_runner->QuitClosure()); 398 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 399 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 400 loop_runner->Run(); 401 ASSERT_TRUE(VerifySavePackageExpectations(browser(), url)); 402 persisted.WaitForPersisted(); 403 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 404 EXPECT_TRUE(file_util::PathExists(full_file_name)); 405 EXPECT_FALSE(file_util::PathExists(dir)); 406 EXPECT_TRUE(file_util::ContentsEqual(test_dir_.Append(base::FilePath( 407 kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), full_file_name)); 408} 409 410// Disabled on Windows due to flakiness. http://crbug.com/162323 411// TODO(linux_aura) http://crbug.com/163931 412#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)) 413#define MAYBE_SaveHTMLOnlyCancel DISABLED_SaveHTMLOnlyCancel 414#else 415#define MAYBE_SaveHTMLOnlyCancel SaveHTMLOnlyCancel 416#endif 417IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveHTMLOnlyCancel) { 418 GURL url = NavigateToMockURL("a"); 419 DownloadManager* manager(GetDownloadManager()); 420 std::vector<DownloadItem*> downloads; 421 manager->GetAllDownloads(&downloads); 422 ASSERT_EQ(0u, downloads.size()); 423 424 base::FilePath full_file_name, dir; 425 GetDestinationPaths("a", &full_file_name, &dir); 426 DownloadItemCreatedObserver creation_observer(manager); 427 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( 428 &DownloadStoredProperly, url, full_file_name, -1, 429 DownloadItem::CANCELLED)); 430 // -1 to disable number of files check; we don't update after cancel, and 431 // we don't know when the single file completed in relationship to 432 // the cancel. 433 434 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 435 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 436 std::vector<DownloadItem*> items; 437 creation_observer.WaitForDownloadItem(&items); 438 ASSERT_EQ(1UL, items.size()); 439 ASSERT_EQ(url.spec(), items[0]->GetOriginalUrl().spec()); 440 items[0]->Cancel(true); 441 // TODO(rdsmith): Fix DII::Cancel() to actually cancel the save package. 442 // Currently it's ignored. 443 444 persisted.WaitForPersisted(); 445 446 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 447 448 // TODO(benjhayden): Figure out how to safely wait for SavePackage's finished 449 // notification, then expect the contents of the downloaded file. 450} 451 452IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnlyTabDestroy) { 453 GURL url = NavigateToMockURL("a"); 454 DownloadManager* manager(GetDownloadManager()); 455 std::vector<DownloadItem*> downloads; 456 manager->GetAllDownloads(&downloads); 457 ASSERT_EQ(0u, downloads.size()); 458 459 base::FilePath full_file_name, dir; 460 GetDestinationPaths("a", &full_file_name, &dir); 461 DownloadItemCreatedObserver creation_observer(manager); 462 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 463 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 464 std::vector<DownloadItem*> items; 465 creation_observer.WaitForDownloadItem(&items); 466 ASSERT_TRUE(items.size() == 1); 467 468 // Close the tab; does this cancel the download? 469 GetCurrentTab(browser())->Close(); 470 EXPECT_EQ(DownloadItem::CANCELLED, items[0]->GetState()); 471 472 EXPECT_FALSE(file_util::PathExists(full_file_name)); 473 EXPECT_FALSE(file_util::PathExists(dir)); 474} 475 476// Disabled on Windows due to flakiness. http://crbug.com/162323 477// TODO(linux_aura) http://crbug.com/163931 478#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)) 479#define MAYBE_SaveViewSourceHTMLOnly DISABLED_SaveViewSourceHTMLOnly 480#else 481#define MAYBE_SaveViewSourceHTMLOnly SaveViewSourceHTMLOnly 482#endif 483IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveViewSourceHTMLOnly) { 484 base::FilePath file_name(FILE_PATH_LITERAL("a.htm")); 485 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( 486 base::FilePath(kTestDir).Append(file_name)); 487 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( 488 base::FilePath(kTestDir).Append(file_name)); 489 ui_test_utils::NavigateToURL(browser(), view_source_url); 490 491 base::FilePath full_file_name, dir; 492 GetDestinationPaths("a", &full_file_name, &dir); 493 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( 494 &DownloadStoredProperly, actual_page_url, full_file_name, 1, 495 DownloadItem::COMPLETE)); 496 scoped_refptr<content::MessageLoopRunner> loop_runner( 497 new content::MessageLoopRunner); 498 SavePackageFinishedObserver observer( 499 content::BrowserContext::GetDownloadManager(browser()->profile()), 500 loop_runner->QuitClosure()); 501 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 502 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 503 loop_runner->Run(); 504 ASSERT_TRUE(VerifySavePackageExpectations(browser(), actual_page_url)); 505 persisted.WaitForPersisted(); 506 507 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 508 509 EXPECT_TRUE(file_util::PathExists(full_file_name)); 510 EXPECT_FALSE(file_util::PathExists(dir)); 511 EXPECT_TRUE(file_util::ContentsEqual( 512 test_dir_.Append(base::FilePath(kTestDir)).Append(file_name), 513 full_file_name)); 514} 515 516// Disabled on Windows due to flakiness. http://crbug.com/162323 517// TODO(linux_aura) http://crbug.com/163931 518#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)) 519#define MAYBE_SaveCompleteHTML DISABLED_SaveCompleteHTML 520#else 521#define MAYBE_SaveCompleteHTML SaveCompleteHTML 522#endif 523IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_SaveCompleteHTML) { 524 GURL url = NavigateToMockURL("b"); 525 526 base::FilePath full_file_name, dir; 527 GetDestinationPaths("b", &full_file_name, &dir); 528 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( 529 &DownloadStoredProperly, url, full_file_name, 3, 530 DownloadItem::COMPLETE)); 531 scoped_refptr<content::MessageLoopRunner> loop_runner( 532 new content::MessageLoopRunner); 533 SavePackageFinishedObserver observer( 534 content::BrowserContext::GetDownloadManager(browser()->profile()), 535 loop_runner->QuitClosure()); 536 ASSERT_TRUE(GetCurrentTab(browser())->SavePage( 537 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 538 loop_runner->Run(); 539 ASSERT_TRUE(VerifySavePackageExpectations(browser(), url)); 540 persisted.WaitForPersisted(); 541 542 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 543 544 EXPECT_TRUE(file_util::PathExists(full_file_name)); 545 EXPECT_TRUE(file_util::PathExists(dir)); 546 EXPECT_TRUE(file_util::TextContentsEqual( 547 test_dir_.Append(base::FilePath(kTestDir)).AppendASCII("b.saved1.htm"), 548 full_file_name)); 549 EXPECT_TRUE(file_util::ContentsEqual( 550 test_dir_.Append(base::FilePath(kTestDir)).AppendASCII("1.png"), 551 dir.AppendASCII("1.png"))); 552 EXPECT_TRUE(file_util::ContentsEqual( 553 test_dir_.Append(base::FilePath(kTestDir)).AppendASCII("1.css"), 554 dir.AppendASCII("1.css"))); 555} 556 557// Invoke a save page during the initial navigation. 558// (Regression test for http://crbug.com/156538). 559// Disabled on Windows due to flakiness. http://crbug.com/162323 560// TODO(linux_aura) http://crbug.com/163931 561#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)) 562#define MAYBE_SaveDuringInitialNavigationIncognito DISABLED_SaveDuringInitialNavigationIncognito 563#else 564#define MAYBE_SaveDuringInitialNavigationIncognito SaveDuringInitialNavigationIncognito 565#endif 566IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, 567 MAYBE_SaveDuringInitialNavigationIncognito) { 568 // Open an Incognito window. 569 Browser* incognito = CreateIncognitoBrowser(); // Waits. 570 ASSERT_TRUE(incognito); 571 572 // Create a download item creation waiter on that window. 573 DownloadItemCreatedObserver creation_observer( 574 BrowserContext::GetDownloadManager(incognito->profile())); 575 576 // Navigate, unblocking with new tab. 577 GURL url = URLRequestMockHTTPJob::GetMockUrl( 578 base::FilePath(kTestDir).AppendASCII("b.htm")); 579 NavigateToURLWithDisposition(incognito, url, NEW_FOREGROUND_TAB, 580 ui_test_utils::BROWSER_TEST_WAIT_FOR_TAB); 581 582 // Save the page before completion. 583 base::FilePath full_file_name, dir; 584 GetDestinationPaths("b", &full_file_name, &dir); 585 scoped_refptr<content::MessageLoopRunner> loop_runner( 586 new content::MessageLoopRunner); 587 SavePackageFinishedObserver observer( 588 content::BrowserContext::GetDownloadManager(incognito->profile()), 589 loop_runner->QuitClosure()); 590 ASSERT_TRUE(GetCurrentTab(incognito)->SavePage( 591 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 592 593 loop_runner->Run(); 594 ASSERT_TRUE(VerifySavePackageExpectations(incognito, url)); 595 596 // Confirm download shelf is visible. 597 EXPECT_TRUE(incognito->window()->IsDownloadShelfVisible()); 598 599 // We can't check more than this because SavePackage is racing with 600 // the page load. If the page load won the race, then SavePackage 601 // might have completed. If the page load lost the race, then 602 // SavePackage will cancel because there aren't any resources to 603 // save. 604} 605 606IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { 607 ui_test_utils::NavigateToURL(browser(), GURL(content::kAboutBlankURL)); 608 EXPECT_FALSE(chrome::CanSavePage(browser())); 609} 610 611// Disabled on Windows due to flakiness. http://crbug.com/162323 612// TODO(linux_aura) http://crbug.com/163931 613#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)) 614#define MAYBE_FileNameFromPageTitle DISABLED_FileNameFromPageTitle 615#else 616#define MAYBE_FileNameFromPageTitle FileNameFromPageTitle 617#endif 618IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_FileNameFromPageTitle) { 619 GURL url = NavigateToMockURL("b"); 620 621 base::FilePath full_file_name = save_dir_.path().AppendASCII( 622 std::string("Test page for saving page feature") + kAppendedExtension); 623 base::FilePath dir = save_dir_.path().AppendASCII( 624 "Test page for saving page feature_files"); 625 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( 626 &DownloadStoredProperly, url, full_file_name, 3, 627 DownloadItem::COMPLETE)); 628 scoped_refptr<content::MessageLoopRunner> loop_runner( 629 new content::MessageLoopRunner); 630 SavePackageFinishedObserver observer( 631 content::BrowserContext::GetDownloadManager(browser()->profile()), 632 loop_runner->QuitClosure()); 633 ASSERT_TRUE(GetCurrentTab(browser())->SavePage( 634 full_file_name, dir, content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); 635 636 loop_runner->Run(); 637 ASSERT_TRUE(VerifySavePackageExpectations(browser(), url)); 638 persisted.WaitForPersisted(); 639 640 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 641 642 EXPECT_TRUE(file_util::PathExists(full_file_name)); 643 EXPECT_TRUE(file_util::PathExists(dir)); 644 EXPECT_TRUE(file_util::TextContentsEqual( 645 test_dir_.Append(base::FilePath(kTestDir)).AppendASCII("b.saved2.htm"), 646 full_file_name)); 647 EXPECT_TRUE(file_util::ContentsEqual( 648 test_dir_.Append(base::FilePath(kTestDir)).AppendASCII("1.png"), 649 dir.AppendASCII("1.png"))); 650 EXPECT_TRUE(file_util::ContentsEqual( 651 test_dir_.Append(base::FilePath(kTestDir)).AppendASCII("1.css"), 652 dir.AppendASCII("1.css"))); 653} 654 655// Disabled on Windows due to flakiness. http://crbug.com/162323 656// TODO(linux_aura) http://crbug.com/163931 657#if defined(OS_WIN) || (defined(OS_LINUX) && !defined(OS_CHROMEOS) && defined(USE_AURA)) 658#define MAYBE_RemoveFromList DISABLED_RemoveFromList 659#else 660#define MAYBE_RemoveFromList RemoveFromList 661#endif 662IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, MAYBE_RemoveFromList) { 663 GURL url = NavigateToMockURL("a"); 664 665 base::FilePath full_file_name, dir; 666 GetDestinationPaths("a", &full_file_name, &dir); 667 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( 668 &DownloadStoredProperly, url, full_file_name, 1, 669 DownloadItem::COMPLETE)); 670 scoped_refptr<content::MessageLoopRunner> loop_runner( 671 new content::MessageLoopRunner); 672 SavePackageFinishedObserver observer( 673 content::BrowserContext::GetDownloadManager(browser()->profile()), 674 loop_runner->QuitClosure()); 675 ASSERT_TRUE(GetCurrentTab(browser())->SavePage(full_file_name, dir, 676 content::SAVE_PAGE_TYPE_AS_ONLY_HTML)); 677 678 loop_runner->Run(); 679 ASSERT_TRUE(VerifySavePackageExpectations(browser(), url)); 680 persisted.WaitForPersisted(); 681 682 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); 683 684 DownloadManager* manager(GetDownloadManager()); 685 std::vector<DownloadItem*> downloads; 686 manager->GetAllDownloads(&downloads); 687 ASSERT_EQ(1UL, downloads.size()); 688 DownloadRemovedObserver removed(browser()->profile(), downloads[0]->GetId()); 689 690 EXPECT_EQ(manager->RemoveAllDownloads(), 1); 691 692 removed.WaitForRemoved(); 693 694 EXPECT_TRUE(file_util::PathExists(full_file_name)); 695 EXPECT_FALSE(file_util::PathExists(dir)); 696 EXPECT_TRUE(file_util::ContentsEqual(test_dir_.Append(base::FilePath( 697 kTestDir)).Append(FILE_PATH_LITERAL("a.htm")), full_file_name)); 698} 699 700// This tests that a webpage with the title "test.exe" is saved as 701// "test.exe.htm". 702// We probably don't care to handle this on Linux or Mac. 703#if defined(OS_WIN) 704IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, CleanFilenameFromPageTitle) { 705 const base::FilePath file_name(FILE_PATH_LITERAL("c.htm")); 706 base::FilePath download_dir = 707 DownloadPrefs::FromDownloadManager(GetDownloadManager())-> 708 DownloadPath(); 709 base::FilePath full_file_name = 710 download_dir.AppendASCII(std::string("test.exe") + kAppendedExtension); 711 base::FilePath dir = download_dir.AppendASCII("test.exe_files"); 712 713 EXPECT_FALSE(file_util::PathExists(full_file_name)); 714 GURL url = URLRequestMockHTTPJob::GetMockUrl( 715 base::FilePath(kTestDir).Append(file_name)); 716 ui_test_utils::NavigateToURL(browser(), url); 717 718 SavePackageFilePicker::SetShouldPromptUser(false); 719 scoped_refptr<content::MessageLoopRunner> loop_runner( 720 new content::MessageLoopRunner); 721 SavePackageFinishedObserver observer( 722 content::BrowserContext::GetDownloadManager(browser()->profile()), 723 loop_runner->QuitClosure()); 724 chrome::SavePage(browser()); 725 loop_runner->Run(); 726 727 EXPECT_TRUE(file_util::PathExists(full_file_name)); 728 729 EXPECT_TRUE(file_util::DieFileDie(full_file_name, false)); 730 EXPECT_TRUE(file_util::DieFileDie(dir, true)); 731} 732#endif 733 734class SavePageAsMHTMLBrowserTest : public SavePageBrowserTest { 735 public: 736 SavePageAsMHTMLBrowserTest() {} 737 virtual ~SavePageAsMHTMLBrowserTest(); 738 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 739 command_line->AppendSwitch(switches::kSavePageAsMHTML); 740 } 741 742 private: 743 DISALLOW_COPY_AND_ASSIGN(SavePageAsMHTMLBrowserTest); 744}; 745 746SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() { 747} 748 749IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) { 750 static const int64 kFileSizeMin = 2758; 751 GURL url = NavigateToMockURL("b"); 752 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( 753 GetDownloadManager())->DownloadPath(); 754 base::FilePath full_file_name = download_dir.AppendASCII(std::string( 755 "Test page for saving page feature.mhtml")); 756 SavePackageFilePicker::SetShouldPromptUser(false); 757 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( 758 &DownloadStoredProperly, url, full_file_name, -1, 759 DownloadItem::COMPLETE)); 760 scoped_refptr<content::MessageLoopRunner> loop_runner( 761 new content::MessageLoopRunner); 762 SavePackageFinishedObserver observer( 763 content::BrowserContext::GetDownloadManager(browser()->profile()), 764 loop_runner->QuitClosure()); 765 chrome::SavePage(browser()); 766 loop_runner->Run(); 767 ASSERT_TRUE(VerifySavePackageExpectations(browser(), url)); 768 persisted.WaitForPersisted(); 769 770 ASSERT_TRUE(file_util::PathExists(full_file_name)); 771 int64 actual_file_size = -1; 772 EXPECT_TRUE(file_util::GetFileSize(full_file_name, &actual_file_size)); 773 EXPECT_LE(kFileSizeMin, actual_file_size); 774} 775 776IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavePageBrowserTest_NonMHTML) { 777 SavePackageFilePicker::SetShouldPromptUser(false); 778 GURL url("data:text/plain,foo"); 779 ui_test_utils::NavigateToURL(browser(), url); 780 scoped_refptr<content::MessageLoopRunner> loop_runner( 781 new content::MessageLoopRunner); 782 SavePackageFinishedObserver observer( 783 content::BrowserContext::GetDownloadManager(browser()->profile()), 784 loop_runner->QuitClosure()); 785 chrome::SavePage(browser()); 786 loop_runner->Run(); 787 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( 788 GetDownloadManager())->DownloadPath(); 789 base::FilePath filename = download_dir.AppendASCII("dataurl.txt"); 790 ASSERT_TRUE(file_util::PathExists(filename)); 791 std::string contents; 792 EXPECT_TRUE(file_util::ReadFileToString(filename, &contents)); 793 EXPECT_EQ("foo", contents); 794} 795 796} // namespace 797 798