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/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/scoped_temp_dir.h" | 8 #include "base/scoped_temp_dir.h" |
9 #include "chrome/app/chrome_command_ids.h" | 9 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/download/download_history.h" |
10 #include "chrome/browser/download/download_item.h" | 11 #include "chrome/browser/download/download_item.h" |
| 12 #include "chrome/browser/download/download_manager.h" |
| 13 #include "chrome/browser/history/download_history_info.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_window.h" | 16 #include "chrome/browser/ui/browser_window.h" |
13 #include "chrome/browser/ui/download/download_tab_helper.h" | 17 #include "chrome/browser/ui/download/download_tab_helper.h" |
14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 18 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
15 #include "chrome/browser/ui/webui/active_downloads_ui.h" | 19 #include "chrome/browser/ui/webui/active_downloads_ui.h" |
16 #include "chrome/common/chrome_notification_types.h" | 20 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/chrome_paths.h" | 21 #include "chrome/common/chrome_paths.h" |
18 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
19 #include "chrome/test/in_process_browser_test.h" | 23 #include "chrome/test/in_process_browser_test.h" |
20 #include "chrome/test/ui_test_utils.h" | 24 #include "chrome/test/ui_test_utils.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 found = true; | 71 found = true; |
68 break; | 72 break; |
69 } | 73 } |
70 } | 74 } |
71 EXPECT_TRUE(found); | 75 EXPECT_TRUE(found); |
72 #else | 76 #else |
73 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | 77 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); |
74 #endif | 78 #endif |
75 } | 79 } |
76 | 80 |
| 81 void QueryDownloadHistory() { |
| 82 DownloadManager* download_manager = |
| 83 browser()->profile()->GetDownloadManager(); |
| 84 ASSERT_TRUE(download_manager); |
| 85 |
| 86 // Query the history system. |
| 87 download_manager->download_history()->Load( |
| 88 NewCallback(this, |
| 89 &SavePageBrowserTest::OnQueryDownloadEntriesComplete)); |
| 90 |
| 91 // Run message loop until a quit message is sent from |
| 92 // OnQueryDownloadEntriesComplete(). |
| 93 ui_test_utils::RunMessageLoop(); |
| 94 } |
| 95 |
| 96 void OnQueryDownloadEntriesComplete( |
| 97 std::vector<DownloadHistoryInfo>* entries) { |
| 98 history_entries_ = *entries; |
| 99 |
| 100 // Indicate thet we have received the history and can continue. |
| 101 MessageLoopForUI::current()->Quit(); |
| 102 } |
| 103 |
| 104 struct DownloadHistoryInfoMatch |
| 105 : public std::unary_function<DownloadHistoryInfo, bool> { |
| 106 |
| 107 DownloadHistoryInfoMatch(const GURL& url, |
| 108 const FilePath& path, |
| 109 int64 num_files) |
| 110 : url_(url), |
| 111 path_(path), |
| 112 num_files_(num_files) { |
| 113 } |
| 114 |
| 115 bool operator() (const DownloadHistoryInfo& info) const { |
| 116 return info.url == url_ && |
| 117 info.path == path_ && |
| 118 // For save packages, received bytes is actually the number of files. |
| 119 info.received_bytes == num_files_ && |
| 120 info.total_bytes == 0 && |
| 121 info.state == DownloadItem::COMPLETE; |
| 122 } |
| 123 |
| 124 GURL url_; |
| 125 FilePath path_; |
| 126 int64 num_files_; |
| 127 }; |
| 128 |
| 129 void CheckDownloadHistory(const GURL& url, |
| 130 const FilePath& path, |
| 131 int64 num_files_) { |
| 132 QueryDownloadHistory(); |
| 133 |
| 134 EXPECT_NE(std::find_if(history_entries_.begin(), history_entries_.end(), |
| 135 DownloadHistoryInfoMatch(url, path, num_files_)), |
| 136 history_entries_.end()); |
| 137 } |
| 138 |
| 139 std::vector<DownloadHistoryInfo> history_entries_; |
| 140 |
77 // Path to directory containing test data. | 141 // Path to directory containing test data. |
78 FilePath test_dir_; | 142 FilePath test_dir_; |
79 | 143 |
80 // Temporary directory we will save pages to. | 144 // Temporary directory we will save pages to. |
81 ScopedTempDir save_dir_; | 145 ScopedTempDir save_dir_; |
82 }; | 146 }; |
83 | 147 |
84 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { | 148 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveHTMLOnly) { |
85 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 149 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
86 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 150 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
87 FilePath(kTestDir).Append(file_name)); | 151 FilePath(kTestDir).Append(file_name)); |
88 ui_test_utils::NavigateToURL(browser(), url); | 152 ui_test_utils::NavigateToURL(browser(), url); |
89 | 153 |
90 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); | 154 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); |
91 ASSERT_TRUE(current_tab); | 155 ASSERT_TRUE(current_tab); |
92 | 156 |
93 FilePath full_file_name = save_dir_.path().Append(file_name); | 157 FilePath full_file_name = save_dir_.path().Append(file_name); |
94 FilePath dir = save_dir_.path().AppendASCII("a_files"); | 158 FilePath dir = save_dir_.path().AppendASCII("a_files"); |
95 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( | 159 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( |
96 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML)); | 160 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML)); |
97 | 161 |
98 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 162 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
99 | 163 |
100 CheckDownloadUI(full_file_name); | 164 CheckDownloadUI(full_file_name); |
| 165 CheckDownloadHistory(url, full_file_name, 1); // a.htm is 1 file. |
101 | 166 |
102 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 167 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
103 EXPECT_FALSE(file_util::PathExists(dir)); | 168 EXPECT_FALSE(file_util::PathExists(dir)); |
104 EXPECT_TRUE(file_util::ContentsEqual( | 169 EXPECT_TRUE(file_util::ContentsEqual( |
105 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 170 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
106 full_file_name)); | 171 full_file_name)); |
107 } | 172 } |
108 | 173 |
109 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { | 174 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveViewSourceHTMLOnly) { |
110 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | 175 FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
111 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( | 176 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( |
112 FilePath(kTestDir).Append(file_name)); | 177 FilePath(kTestDir).Append(file_name)); |
113 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( | 178 GURL actual_page_url = URLRequestMockHTTPJob::GetMockUrl( |
114 FilePath(kTestDir).Append(file_name)); | 179 FilePath(kTestDir).Append(file_name)); |
115 ui_test_utils::NavigateToURL(browser(), view_source_url); | 180 ui_test_utils::NavigateToURL(browser(), view_source_url); |
116 | 181 |
117 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); | 182 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); |
118 ASSERT_TRUE(current_tab); | 183 ASSERT_TRUE(current_tab); |
119 | 184 |
120 FilePath full_file_name = save_dir_.path().Append(file_name); | 185 FilePath full_file_name = save_dir_.path().Append(file_name); |
121 FilePath dir = save_dir_.path().AppendASCII("a_files"); | 186 FilePath dir = save_dir_.path().AppendASCII("a_files"); |
122 | 187 |
123 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( | 188 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( |
124 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML)); | 189 full_file_name, dir, SavePackage::SAVE_AS_ONLY_HTML)); |
125 | 190 |
126 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); | 191 EXPECT_EQ(actual_page_url, WaitForSavePackageToFinish()); |
127 | 192 |
128 CheckDownloadUI(full_file_name); | 193 CheckDownloadUI(full_file_name); |
| 194 CheckDownloadHistory(actual_page_url, full_file_name, 1); // a.htm is 1 file. |
129 | 195 |
130 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 196 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
131 EXPECT_FALSE(file_util::PathExists(dir)); | 197 EXPECT_FALSE(file_util::PathExists(dir)); |
132 EXPECT_TRUE(file_util::ContentsEqual( | 198 EXPECT_TRUE(file_util::ContentsEqual( |
133 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | 199 test_dir_.Append(FilePath(kTestDir)).Append(file_name), |
134 full_file_name)); | 200 full_file_name)); |
135 } | 201 } |
136 | 202 |
137 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { | 203 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveCompleteHTML) { |
138 FilePath file_name(FILE_PATH_LITERAL("b.htm")); | 204 FilePath file_name(FILE_PATH_LITERAL("b.htm")); |
139 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 205 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
140 FilePath(kTestDir).Append(file_name)); | 206 FilePath(kTestDir).Append(file_name)); |
141 ui_test_utils::NavigateToURL(browser(), url); | 207 ui_test_utils::NavigateToURL(browser(), url); |
142 | 208 |
143 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); | 209 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); |
144 ASSERT_TRUE(current_tab); | 210 ASSERT_TRUE(current_tab); |
145 | 211 |
146 FilePath full_file_name = save_dir_.path().Append(file_name); | 212 FilePath full_file_name = save_dir_.path().Append(file_name); |
147 FilePath dir = save_dir_.path().AppendASCII("b_files"); | 213 FilePath dir = save_dir_.path().AppendASCII("b_files"); |
148 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( | 214 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( |
149 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML)); | 215 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML)); |
150 | 216 |
151 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 217 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
152 | 218 |
153 CheckDownloadUI(full_file_name); | 219 CheckDownloadUI(full_file_name); |
| 220 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. |
154 | 221 |
155 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 222 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
156 EXPECT_TRUE(file_util::PathExists(dir)); | 223 EXPECT_TRUE(file_util::PathExists(dir)); |
157 EXPECT_TRUE(file_util::TextContentsEqual( | 224 EXPECT_TRUE(file_util::TextContentsEqual( |
158 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), | 225 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), |
159 full_file_name)); | 226 full_file_name)); |
160 EXPECT_TRUE(file_util::ContentsEqual( | 227 EXPECT_TRUE(file_util::ContentsEqual( |
161 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 228 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
162 dir.AppendASCII("1.png"))); | 229 dir.AppendASCII("1.png"))); |
163 EXPECT_TRUE(file_util::ContentsEqual( | 230 EXPECT_TRUE(file_util::ContentsEqual( |
(...skipping 21 matching lines...) Expand all Loading... |
185 | 252 |
186 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); | 253 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); |
187 ASSERT_TRUE(current_tab); | 254 ASSERT_TRUE(current_tab); |
188 | 255 |
189 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( | 256 ASSERT_TRUE(current_tab->download_tab_helper()->SavePage( |
190 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML)); | 257 full_file_name, dir, SavePackage::SAVE_AS_COMPLETE_HTML)); |
191 | 258 |
192 EXPECT_EQ(url, WaitForSavePackageToFinish()); | 259 EXPECT_EQ(url, WaitForSavePackageToFinish()); |
193 | 260 |
194 CheckDownloadUI(full_file_name); | 261 CheckDownloadUI(full_file_name); |
| 262 CheckDownloadHistory(url, full_file_name, 3); // b.htm is 3 files. |
195 | 263 |
196 EXPECT_TRUE(file_util::PathExists(full_file_name)); | 264 EXPECT_TRUE(file_util::PathExists(full_file_name)); |
197 EXPECT_TRUE(file_util::PathExists(dir)); | 265 EXPECT_TRUE(file_util::PathExists(dir)); |
198 EXPECT_TRUE(file_util::TextContentsEqual( | 266 EXPECT_TRUE(file_util::TextContentsEqual( |
199 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), | 267 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved2.htm"), |
200 full_file_name)); | 268 full_file_name)); |
201 EXPECT_TRUE(file_util::ContentsEqual( | 269 EXPECT_TRUE(file_util::ContentsEqual( |
202 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 270 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
203 dir.AppendASCII("1.png"))); | 271 dir.AppendASCII("1.png"))); |
204 EXPECT_TRUE(file_util::ContentsEqual( | 272 EXPECT_TRUE(file_util::ContentsEqual( |
205 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 273 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
206 dir.AppendASCII("1.css"))); | 274 dir.AppendASCII("1.css"))); |
207 } | 275 } |
208 | 276 |
209 } // namespace | 277 } // namespace |
OLD | NEW |