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 <stddef.h> |
| 6 #include <stdint.h> |
| 7 |
5 #include <string> | 8 #include <string> |
6 #include <vector> | 9 #include <vector> |
7 | 10 |
8 #include "base/bind.h" | 11 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
10 #include "base/command_line.h" | 13 #include "base/command_line.h" |
11 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
12 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
13 #include "base/files/scoped_temp_dir.h" | 16 #include "base/files/scoped_temp_dir.h" |
| 17 #include "base/macros.h" |
14 #include "base/path_service.h" | 18 #include "base/path_service.h" |
15 #include "base/prefs/pref_member.h" | 19 #include "base/prefs/pref_member.h" |
16 #include "base/prefs/pref_service.h" | 20 #include "base/prefs/pref_service.h" |
17 #include "base/run_loop.h" | 21 #include "base/run_loop.h" |
18 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
19 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
20 #include "base/test/test_file_util.h" | 24 #include "base/test/test_file_util.h" |
| 25 #include "build/build_config.h" |
21 #include "chrome/app/chrome_command_ids.h" | 26 #include "chrome/app/chrome_command_ids.h" |
22 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 27 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
23 #include "chrome/browser/download/download_history.h" | 28 #include "chrome/browser/download/download_history.h" |
24 #include "chrome/browser/download/download_prefs.h" | 29 #include "chrome/browser/download/download_prefs.h" |
25 #include "chrome/browser/download/download_service.h" | 30 #include "chrome/browser/download/download_service.h" |
26 #include "chrome/browser/download/download_service_factory.h" | 31 #include "chrome/browser/download/download_service_factory.h" |
27 #include "chrome/browser/download/save_package_file_picker.h" | 32 #include "chrome/browser/download/save_package_file_picker.h" |
28 #include "chrome/browser/net/url_request_mock_util.h" | 33 #include "chrome/browser/net/url_request_mock_util.h" |
29 #include "chrome/browser/profiles/profile.h" | 34 #include "chrome/browser/profiles/profile.h" |
30 #include "chrome/browser/ui/browser.h" | 35 #include "chrome/browser/ui/browser.h" |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 PersistedFilter filter_; | 128 PersistedFilter filter_; |
124 base::Closure quit_waiting_callback_; | 129 base::Closure quit_waiting_callback_; |
125 bool persisted_; | 130 bool persisted_; |
126 | 131 |
127 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); | 132 DISALLOW_COPY_AND_ASSIGN(DownloadPersistedObserver); |
128 }; | 133 }; |
129 | 134 |
130 // Waits for an item record to be removed from the downloads database. | 135 // Waits for an item record to be removed from the downloads database. |
131 class DownloadRemovedObserver : public DownloadPersistedObserver { | 136 class DownloadRemovedObserver : public DownloadPersistedObserver { |
132 public: | 137 public: |
133 DownloadRemovedObserver(Profile* profile, int32 download_id) | 138 DownloadRemovedObserver(Profile* profile, int32_t download_id) |
134 : DownloadPersistedObserver(profile, PersistedFilter()), | 139 : DownloadPersistedObserver(profile, PersistedFilter()), |
135 removed_(false), | 140 removed_(false), |
136 download_id_(download_id) {} | 141 download_id_(download_id) {} |
137 ~DownloadRemovedObserver() override {} | 142 ~DownloadRemovedObserver() override {} |
138 | 143 |
139 bool WaitForRemoved() { | 144 bool WaitForRemoved() { |
140 if (removed_) | 145 if (removed_) |
141 return true; | 146 return true; |
142 base::RunLoop run_loop; | 147 base::RunLoop run_loop; |
143 quit_waiting_callback_ = run_loop.QuitClosure(); | 148 quit_waiting_callback_ = run_loop.QuitClosure(); |
144 run_loop.Run(); | 149 run_loop.Run(); |
145 quit_waiting_callback_ = base::Closure(); | 150 quit_waiting_callback_ = base::Closure(); |
146 return removed_; | 151 return removed_; |
147 } | 152 } |
148 | 153 |
149 void OnDownloadStored(DownloadItem* item, | 154 void OnDownloadStored(DownloadItem* item, |
150 const history::DownloadRow& info) override {} | 155 const history::DownloadRow& info) override {} |
151 | 156 |
152 void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) override { | 157 void OnDownloadsRemoved(const DownloadHistory::IdSet& ids) override { |
153 removed_ = ids.find(download_id_) != ids.end(); | 158 removed_ = ids.find(download_id_) != ids.end(); |
154 if (removed_ && !quit_waiting_callback_.is_null()) | 159 if (removed_ && !quit_waiting_callback_.is_null()) |
155 quit_waiting_callback_.Run(); | 160 quit_waiting_callback_.Run(); |
156 } | 161 } |
157 | 162 |
158 private: | 163 private: |
159 bool removed_; | 164 bool removed_; |
160 base::Closure quit_waiting_callback_; | 165 base::Closure quit_waiting_callback_; |
161 int32 download_id_; | 166 int32_t download_id_; |
162 | 167 |
163 DISALLOW_COPY_AND_ASSIGN(DownloadRemovedObserver); | 168 DISALLOW_COPY_AND_ASSIGN(DownloadRemovedObserver); |
164 }; | 169 }; |
165 | 170 |
166 bool DownloadStoredProperly( | 171 bool DownloadStoredProperly(const GURL& expected_url, |
167 const GURL& expected_url, | 172 const base::FilePath& expected_path, |
168 const base::FilePath& expected_path, | 173 int64_t num_files, |
169 int64 num_files, | 174 history::DownloadState expected_state, |
170 history::DownloadState expected_state, | 175 DownloadItem* item, |
171 DownloadItem* item, | 176 const history::DownloadRow& info) { |
172 const history::DownloadRow& info) { | |
173 // This function may be called multiple times for a given test. Returning | 177 // This function may be called multiple times for a given test. Returning |
174 // false doesn't necessarily mean that the test has failed or will fail, it | 178 // false doesn't necessarily mean that the test has failed or will fail, it |
175 // might just mean that the test hasn't passed yet. | 179 // might just mean that the test hasn't passed yet. |
176 if (!expected_path.empty() && info.target_path != expected_path) { | 180 if (!expected_path.empty() && info.target_path != expected_path) { |
177 DVLOG(20) << __FUNCTION__ << " " << info.target_path.value() | 181 DVLOG(20) << __FUNCTION__ << " " << info.target_path.value() |
178 << " != " << expected_path.value(); | 182 << " != " << expected_path.value(); |
179 return false; | 183 return false; |
180 } | 184 } |
181 if (info.url_chain.size() != 1u) { | 185 if (info.url_chain.size() != 1u) { |
182 DVLOG(20) << __FUNCTION__ << " " << info.url_chain.size() << " != 1"; | 186 DVLOG(20) << __FUNCTION__ << " " << info.url_chain.size() << " != 1"; |
(...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 } | 694 } |
691 | 695 |
692 private: | 696 private: |
693 DISALLOW_COPY_AND_ASSIGN(SavePageAsMHTMLBrowserTest); | 697 DISALLOW_COPY_AND_ASSIGN(SavePageAsMHTMLBrowserTest); |
694 }; | 698 }; |
695 | 699 |
696 SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() { | 700 SavePageAsMHTMLBrowserTest::~SavePageAsMHTMLBrowserTest() { |
697 } | 701 } |
698 | 702 |
699 IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) { | 703 IN_PROC_BROWSER_TEST_F(SavePageAsMHTMLBrowserTest, SavePageAsMHTML) { |
700 static const int64 kFileSizeMin = 2758; | 704 static const int64_t kFileSizeMin = 2758; |
701 GURL url = NavigateToMockURL("b"); | 705 GURL url = NavigateToMockURL("b"); |
702 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( | 706 base::FilePath download_dir = DownloadPrefs::FromDownloadManager( |
703 GetDownloadManager())->DownloadPath(); | 707 GetDownloadManager())->DownloadPath(); |
704 base::FilePath full_file_name = download_dir.AppendASCII(std::string( | 708 base::FilePath full_file_name = download_dir.AppendASCII(std::string( |
705 "Test page for saving page feature.mhtml")); | 709 "Test page for saving page feature.mhtml")); |
706 SavePackageFilePicker::SetShouldPromptUser(false); | 710 SavePackageFilePicker::SetShouldPromptUser(false); |
707 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( | 711 DownloadPersistedObserver persisted(browser()->profile(), base::Bind( |
708 &DownloadStoredProperly, url, full_file_name, -1, | 712 &DownloadStoredProperly, url, full_file_name, -1, |
709 history::DownloadState::COMPLETE)); | 713 history::DownloadState::COMPLETE)); |
710 scoped_refptr<content::MessageLoopRunner> loop_runner( | 714 scoped_refptr<content::MessageLoopRunner> loop_runner( |
711 new content::MessageLoopRunner); | 715 new content::MessageLoopRunner); |
712 SavePackageFinishedObserver observer( | 716 SavePackageFinishedObserver observer( |
713 content::BrowserContext::GetDownloadManager(browser()->profile()), | 717 content::BrowserContext::GetDownloadManager(browser()->profile()), |
714 loop_runner->QuitClosure()); | 718 loop_runner->QuitClosure()); |
715 chrome::SavePage(browser()); | 719 chrome::SavePage(browser()); |
716 loop_runner->Run(); | 720 loop_runner->Run(); |
717 ASSERT_TRUE(VerifySavePackageExpectations(browser(), url)); | 721 ASSERT_TRUE(VerifySavePackageExpectations(browser(), url)); |
718 persisted.WaitForPersisted(); | 722 persisted.WaitForPersisted(); |
719 | 723 |
720 ASSERT_TRUE(base::PathExists(full_file_name)); | 724 ASSERT_TRUE(base::PathExists(full_file_name)); |
721 int64 actual_file_size = -1; | 725 int64_t actual_file_size = -1; |
722 EXPECT_TRUE(base::GetFileSize(full_file_name, &actual_file_size)); | 726 EXPECT_TRUE(base::GetFileSize(full_file_name, &actual_file_size)); |
723 EXPECT_LE(kFileSizeMin, actual_file_size); | 727 EXPECT_LE(kFileSizeMin, actual_file_size); |
724 } | 728 } |
725 | 729 |
726 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavePageBrowserTest_NonMHTML) { | 730 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SavePageBrowserTest_NonMHTML) { |
727 SavePackageFilePicker::SetShouldPromptUser(false); | 731 SavePackageFilePicker::SetShouldPromptUser(false); |
728 GURL url("data:text/plain,foo"); | 732 GURL url("data:text/plain,foo"); |
729 ui_test_utils::NavigateToURL(browser(), url); | 733 ui_test_utils::NavigateToURL(browser(), url); |
730 scoped_refptr<content::MessageLoopRunner> loop_runner( | 734 scoped_refptr<content::MessageLoopRunner> loop_runner( |
731 new content::MessageLoopRunner); | 735 new content::MessageLoopRunner); |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
816 base::FilePath expected_files[] = { | 820 base::FilePath expected_files[] = { |
817 full_file_name, | 821 full_file_name, |
818 dir.AppendASCII("a.html"), // From iframes.htm | 822 dir.AppendASCII("a.html"), // From iframes.htm |
819 dir.AppendASCII("b.html"), // From iframes.htm | 823 dir.AppendASCII("b.html"), // From iframes.htm |
820 dir.AppendASCII("1.css"), // From b.htm | 824 dir.AppendASCII("1.css"), // From b.htm |
821 dir.AppendASCII("1.png"), // Deduplicated from iframes.htm and b.htm. | 825 dir.AppendASCII("1.png"), // Deduplicated from iframes.htm and b.htm. |
822 }; | 826 }; |
823 for (auto file_path : expected_files) { | 827 for (auto file_path : expected_files) { |
824 EXPECT_TRUE(base::PathExists(file_path)) << "Does " << file_path.value() | 828 EXPECT_TRUE(base::PathExists(file_path)) << "Does " << file_path.value() |
825 << " exist?"; | 829 << " exist?"; |
826 int64 actual_file_size = 0; | 830 int64_t actual_file_size = 0; |
827 EXPECT_TRUE(base::GetFileSize(file_path, &actual_file_size)); | 831 EXPECT_TRUE(base::GetFileSize(file_path, &actual_file_size)); |
828 EXPECT_NE(0, actual_file_size) << "Is " << file_path.value() | 832 EXPECT_NE(0, actual_file_size) << "Is " << file_path.value() |
829 << " non-empty?"; | 833 << " non-empty?"; |
830 } | 834 } |
831 | 835 |
832 // Verify that local links got correctly replaced with local paths | 836 // Verify that local links got correctly replaced with local paths |
833 // (most importantly for iframe elements, which are only exercised | 837 // (most importantly for iframe elements, which are only exercised |
834 // by this particular test). | 838 // by this particular test). |
835 std::string main_contents; | 839 std::string main_contents; |
836 ASSERT_TRUE(base::ReadFileToString(full_file_name, &main_contents)); | 840 ASSERT_TRUE(base::ReadFileToString(full_file_name, &main_contents)); |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1188 TestMultiFramePage(save_page_type, url, 6, expected_substrings); | 1192 TestMultiFramePage(save_page_type, url, 6, expected_substrings); |
1189 } | 1193 } |
1190 | 1194 |
1191 INSTANTIATE_TEST_CASE_P( | 1195 INSTANTIATE_TEST_CASE_P( |
1192 SaveType, | 1196 SaveType, |
1193 SavePageMultiFrameBrowserTest, | 1197 SavePageMultiFrameBrowserTest, |
1194 ::testing::Values(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, | 1198 ::testing::Values(content::SAVE_PAGE_TYPE_AS_COMPLETE_HTML, |
1195 content::SAVE_PAGE_TYPE_AS_MHTML)); | 1199 content::SAVE_PAGE_TYPE_AS_MHTML)); |
1196 | 1200 |
1197 } // namespace | 1201 } // namespace |
OLD | NEW |