| 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/files/scoped_temp_dir.h" | 5 #include "base/files/scoped_temp_dir.h" |
| 6 #include "content/browser/download/save_package.h" | 6 #include "content/browser/download/save_package.h" |
| 7 #include "content/public/browser/browser_context.h" |
| 8 #include "content/public/browser/download_manager.h" |
| 9 #include "content/public/browser/web_contents.h" |
| 7 #include "content/public/test/content_browser_test.h" | 10 #include "content/public/test/content_browser_test.h" |
| 8 #include "content/public/test/content_browser_test_utils.h" | 11 #include "content/public/test/content_browser_test_utils.h" |
| 12 #include "content/public/test/test_utils.h" |
| 9 #include "content/shell/browser/shell.h" | 13 #include "content/shell/browser/shell.h" |
| 10 | 14 |
| 11 namespace content { | 15 namespace content { |
| 12 | 16 |
| 13 const char kTestFile[] = "files/simple_page.html"; | 17 const char kTestFile[] = "files/simple_page.html"; |
| 18 const char kIframeDocumentWrite[] = "files/iframe_doc_write.htm"; |
| 14 | 19 |
| 15 class SavePackageBrowserTest : public ContentBrowserTest { | 20 class SavePackageBrowserTest : public ContentBrowserTest { |
| 16 protected: | 21 protected: |
| 17 virtual void SetUp() OVERRIDE { | 22 virtual void SetUp() OVERRIDE { |
| 18 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); | 23 ASSERT_TRUE(save_dir_.CreateUniqueTempDir()); |
| 19 ContentBrowserTest::SetUp(); | 24 ContentBrowserTest::SetUp(); |
| 20 } | 25 } |
| 21 | 26 |
| 22 // Returns full paths of destination file and directory. | 27 // Returns full paths of destination file and directory. |
| 23 void GetDestinationPaths(const std::string& prefix, | 28 void GetDestinationPaths(const std::string& prefix, |
| 24 base::FilePath* full_file_name, | 29 base::FilePath* full_file_name, |
| 25 base::FilePath* dir) { | 30 base::FilePath* dir) { |
| 26 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); | 31 *full_file_name = save_dir_.path().AppendASCII(prefix + ".htm"); |
| 27 *dir = save_dir_.path().AppendASCII(prefix + "_files"); | 32 *dir = save_dir_.path().AppendASCII(prefix + "_files"); |
| 28 } | 33 } |
| 29 | 34 |
| 30 // Temporary directory we will save pages to. | 35 // Temporary directory we will save pages to. |
| 31 base::ScopedTempDir save_dir_; | 36 base::ScopedTempDir save_dir_; |
| 32 }; | 37 }; |
| 33 | 38 |
| 39 class SavePackageFinishedObserver : public DownloadManager::Observer { |
| 40 public: |
| 41 SavePackageFinishedObserver(content::DownloadManager* manager, |
| 42 const base::Closure& callback) |
| 43 : download_manager_(manager), |
| 44 callback_(callback), |
| 45 successfully_finished_(false) { |
| 46 download_manager_->AddObserver(this); |
| 47 } |
| 48 |
| 49 virtual ~SavePackageFinishedObserver() { |
| 50 if (download_manager_) |
| 51 download_manager_->RemoveObserver(this); |
| 52 } |
| 53 |
| 54 // DownloadManager::Observer: |
| 55 virtual void OnSavePackageFinished( |
| 56 bool success, |
| 57 content::DownloadManager* manager, |
| 58 content::DownloadItem* item) OVERRIDE{ |
| 59 successfully_finished_ = success; |
| 60 callback_.Run(); |
| 61 } |
| 62 |
| 63 virtual void ManagerGoingDown(content::DownloadManager* manager) OVERRIDE{ |
| 64 download_manager_->RemoveObserver(this); |
| 65 download_manager_ = NULL; |
| 66 } |
| 67 |
| 68 bool successfully_finished() const { return successfully_finished_; } |
| 69 |
| 70 private: |
| 71 content::DownloadManager* download_manager_; |
| 72 base::Closure callback_; |
| 73 |
| 74 bool successfully_finished_; |
| 75 |
| 76 DISALLOW_COPY_AND_ASSIGN(SavePackageFinishedObserver); |
| 77 }; |
| 78 |
| 34 // Create a SavePackage and delete it without calling Init. | 79 // Create a SavePackage and delete it without calling Init. |
| 35 // SavePackage dtor has various asserts/checks that should not fire. | 80 // SavePackage dtor has various asserts/checks that should not fire. |
| 36 IN_PROC_BROWSER_TEST_F(SavePackageBrowserTest, ImplicitCancel) { | 81 IN_PROC_BROWSER_TEST_F(SavePackageBrowserTest, ImplicitCancel) { |
| 37 ASSERT_TRUE(test_server()->Start()); | 82 ASSERT_TRUE(test_server()->Start()); |
| 38 GURL url = test_server()->GetURL(kTestFile); | 83 GURL url = test_server()->GetURL(kTestFile); |
| 39 NavigateToURL(shell(), url); | 84 NavigateToURL(shell(), url); |
| 40 base::FilePath full_file_name, dir; | 85 base::FilePath full_file_name, dir; |
| 41 GetDestinationPaths("a", &full_file_name, &dir); | 86 GetDestinationPaths("a", &full_file_name, &dir); |
| 42 scoped_refptr<SavePackage> save_package(new SavePackage( | 87 scoped_refptr<SavePackage> save_package(new SavePackage( |
| 43 shell()->web_contents(), SAVE_PAGE_TYPE_AS_ONLY_HTML, full_file_name, | 88 shell()->web_contents(), SAVE_PAGE_TYPE_AS_ONLY_HTML, full_file_name, |
| 44 dir)); | 89 dir)); |
| 45 ASSERT_TRUE(test_server()->Stop()); | 90 ASSERT_TRUE(test_server()->Stop()); |
| 46 } | 91 } |
| 47 | 92 |
| 48 // Create a SavePackage, call Cancel, then delete it. | 93 // Create a SavePackage, call Cancel, then delete it. |
| 49 // SavePackage dtor has various asserts/checks that should not fire. | 94 // SavePackage dtor has various asserts/checks that should not fire. |
| 50 IN_PROC_BROWSER_TEST_F(SavePackageBrowserTest, ExplicitCancel) { | 95 IN_PROC_BROWSER_TEST_F(SavePackageBrowserTest, ExplicitCancel) { |
| 51 ASSERT_TRUE(test_server()->Start()); | 96 ASSERT_TRUE(test_server()->Start()); |
| 52 GURL url = test_server()->GetURL(kTestFile); | 97 GURL url = test_server()->GetURL(kTestFile); |
| 53 NavigateToURL(shell(), url); | 98 NavigateToURL(shell(), url); |
| 54 base::FilePath full_file_name, dir; | 99 base::FilePath full_file_name, dir; |
| 55 GetDestinationPaths("a", &full_file_name, &dir); | 100 GetDestinationPaths("a", &full_file_name, &dir); |
| 56 scoped_refptr<SavePackage> save_package(new SavePackage( | 101 scoped_refptr<SavePackage> save_package(new SavePackage( |
| 57 shell()->web_contents(), SAVE_PAGE_TYPE_AS_ONLY_HTML, full_file_name, | 102 shell()->web_contents(), SAVE_PAGE_TYPE_AS_ONLY_HTML, full_file_name, |
| 58 dir)); | 103 dir)); |
| 59 save_package->Cancel(true); | 104 save_package->Cancel(true); |
| 60 ASSERT_TRUE(test_server()->Stop()); | 105 ASSERT_TRUE(test_server()->Stop()); |
| 61 } | 106 } |
| 62 | 107 |
| 108 // We should not cancel saving a main document html file in case of an |
| 109 // having the same url. That is possible in the used TC. |
| 110 IN_PROC_BROWSER_TEST_F(SavePackageBrowserTest, DuplicateURLForFrames) { |
| 111 ASSERT_TRUE(test_server()->Start()); |
| 112 GURL url = test_server()->GetURL(kIframeDocumentWrite); |
| 113 NavigateToURL(shell(), url); |
| 114 |
| 115 scoped_refptr<content::MessageLoopRunner> loop_runner( |
| 116 new MessageLoopRunner); |
| 117 SavePackageFinishedObserver observer( |
| 118 BrowserContext::GetDownloadManager( |
| 119 shell()->web_contents()->GetBrowserContext()), |
| 120 loop_runner->QuitClosure()); |
| 121 |
| 122 base::FilePath full_file_name, dir; |
| 123 GetDestinationPaths("a", &full_file_name, &dir); |
| 124 ASSERT_TRUE(shell()->web_contents()->SavePage( |
| 125 full_file_name, |
| 126 dir, |
| 127 SAVE_PAGE_TYPE_AS_COMPLETE_HTML)); |
| 128 |
| 129 loop_runner->Run(); |
| 130 |
| 131 EXPECT_TRUE(observer.successfully_finished()); |
| 132 } |
| 133 |
| 63 } // namespace content | 134 } // namespace content |
| OLD | NEW |