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" | |
| 9 #include "chrome/app/chrome_command_ids.h" | 11 #include "chrome/app/chrome_command_ids.h" |
| 10 #include "chrome/browser/net/url_request_mock_http_job.h" | 12 #include "chrome/browser/net/url_request_mock_http_job.h" |
| 11 #include "chrome/browser/ui/browser.h" | 13 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_window.h" | 14 #include "chrome/browser/ui/browser_window.h" |
| 13 #include "chrome/browser/ui/download/download_tab_helper.h" | 15 #include "chrome/browser/ui/download/download_tab_helper.h" |
| 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 15 #include "chrome/common/chrome_paths.h" | 17 #include "chrome/common/chrome_paths.h" |
| 16 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 17 #include "chrome/test/in_process_browser_test.h" | 19 #include "chrome/test/in_process_browser_test.h" |
| 18 #include "chrome/test/ui_test_utils.h" | 20 #include "chrome/test/ui_test_utils.h" |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 134 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), | 136 test_dir_.Append(FilePath(kTestDir)).AppendASCII("b.saved1.htm"), |
| 135 full_file_name)); | 137 full_file_name)); |
| 136 EXPECT_TRUE(file_util::ContentsEqual( | 138 EXPECT_TRUE(file_util::ContentsEqual( |
| 137 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 139 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
| 138 dir.AppendASCII("1.png"))); | 140 dir.AppendASCII("1.png"))); |
| 139 EXPECT_TRUE(file_util::ContentsEqual( | 141 EXPECT_TRUE(file_util::ContentsEqual( |
| 140 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 142 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
| 141 dir.AppendASCII("1.css"))); | 143 dir.AppendASCII("1.css"))); |
| 142 } | 144 } |
| 143 | 145 |
| 146 // Checks if an HTML page is saved to the default folder for saving HTML | |
| 147 // in the following situation: | |
| 148 // The default folder for saving HTML exists. | |
| 149 // The default folder for downloaded files exists. | |
| 150 // The user's "Downloads" folder exists. | |
| 151 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveToFolderForHTMLPage) { | |
| 152 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | |
| 153 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
| 154 FilePath(kTestDir).Append(file_name)); | |
| 155 ui_test_utils::NavigateToURL(browser(), url); | |
| 156 | |
| 157 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); | |
| 158 ASSERT_TRUE(current_tab); | |
| 159 | |
| 160 ScopedTempDir website_save_dir, download_save_dir, default_downloads_dir; | |
| 161 // Prepare the default folder for saving HTML. | |
| 162 ASSERT_TRUE(website_save_dir.CreateUniqueTempDir()); | |
| 163 // Prepare the default folder for downloaded files. | |
| 164 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir()); | |
| 165 // Prepare the user's "Downloads" folder. | |
| 166 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir()); | |
| 167 | |
| 168 // Changes the default prefs. | |
| 169 current_tab->download_tab_helper()->ChangeSaveDirectoryPrefs( | |
| 170 website_save_dir.path(), | |
| 171 download_save_dir.path(), | |
| 172 default_downloads_dir.path(), | |
| 173 SavePackage::SAVE_AS_ONLY_HTML); | |
| 174 | |
| 175 std::string title = UTF16ToASCII( | |
| 176 current_tab->download_tab_helper()->SavePageWithoutDialog()); | |
| 177 file_util::ReplaceIllegalCharactersInPath(&title, ' '); | |
| 178 FilePath full_file_name = | |
| 179 website_save_dir.path().Append(FilePath(title + ".html")); | |
| 180 | |
| 181 EXPECT_EQ(url, WaitForSavePackageToFinish()); | |
| 182 | |
| 183 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF)) | |
| 184 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | |
| 185 | |
| 186 // Is the file downloaded to the default folder for saving HTML? | |
| 187 EXPECT_TRUE(file_util::PathExists(full_file_name)); | |
| 188 EXPECT_TRUE(file_util::ContentsEqual( | |
| 189 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | |
| 190 full_file_name)); | |
| 191 | |
| 192 current_tab->download_tab_helper()->RestoreSaveDirectoryPrefs(); | |
| 193 } | |
| 194 | |
| 195 // Checks if an HTML page is saved to the default folder for downloaded files | |
| 196 // in the following situation: | |
| 197 // The default folder for saving HTML does not exist. | |
| 198 // The default folder for downloaded files exists. | |
| 199 // The user's "Downloads" folder exists. | |
| 200 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveToFolderForDownloadedFiles) { | |
| 201 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | |
| 202 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
| 203 FilePath(kTestDir).Append(file_name)); | |
| 204 ui_test_utils::NavigateToURL(browser(), url); | |
| 205 | |
| 206 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); | |
| 207 ASSERT_TRUE(current_tab); | |
| 208 | |
| 209 ScopedTempDir download_save_dir, default_downloads_dir; | |
| 210 // Prepare the default folder for saving downloaded files. | |
| 211 ASSERT_TRUE(download_save_dir.CreateUniqueTempDir()); | |
| 212 // Prepare the user's "Downloads" folder. | |
| 213 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir()); | |
| 214 // Prepare non-existent folder. | |
| 215 FilePath nonexistent_path("/koakuma/mikity/moemoe/nyannyan"); | |
| 216 ASSERT_FALSE(file_util::PathExists(nonexistent_path)); | |
| 217 | |
| 218 // Changes the default prefs. | |
| 219 current_tab->download_tab_helper()->ChangeSaveDirectoryPrefs( | |
| 220 nonexistent_path, | |
| 221 download_save_dir.path(), | |
| 222 default_downloads_dir.path(), | |
| 223 SavePackage::SAVE_AS_ONLY_HTML); | |
| 224 | |
| 225 std::string title = UTF16ToASCII( | |
| 226 current_tab->download_tab_helper()->SavePageWithoutDialog()); | |
| 227 file_util::ReplaceIllegalCharactersInPath(&title, ' '); | |
| 228 FilePath full_file_name = | |
| 229 download_save_dir.path().Append(FilePath(title + ".html")); | |
| 230 | |
| 231 EXPECT_EQ(url, WaitForSavePackageToFinish()); | |
| 232 | |
| 233 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF)) | |
| 234 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | |
| 235 | |
| 236 // Is the file downloaded to the default folder for downloaded files? | |
| 237 EXPECT_TRUE(file_util::PathExists(full_file_name)); | |
| 238 EXPECT_TRUE(file_util::ContentsEqual( | |
| 239 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | |
| 240 full_file_name)); | |
| 241 | |
| 242 current_tab->download_tab_helper()->RestoreSaveDirectoryPrefs(); | |
| 243 } | |
| 244 | |
| 245 // Checks if an HTML page is saved to the user's "Downloads" directory | |
| 246 // in the following situation: | |
| 247 // The default folder for saving HTML does not exist. | |
| 248 // The default folder for downloaded files does not exist. | |
| 249 // The user's "Downloads" folder exists. | |
|
Randy Smith (Not in Mondays)
2011/06/02 19:13:57
Should we also test the fourth case where none of
haraken1
2011/06/03 06:50:27
I added a new test, SavePageBrowserTest.SavedFolde
| |
| 250 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, SaveToUsersDownloadsFolder) { | |
| 251 FilePath file_name(FILE_PATH_LITERAL("a.htm")); | |
| 252 GURL url = URLRequestMockHTTPJob::GetMockUrl( | |
| 253 FilePath(kTestDir).Append(file_name)); | |
| 254 ui_test_utils::NavigateToURL(browser(), url); | |
| 255 | |
| 256 TabContentsWrapper* current_tab = browser()->GetSelectedTabContentsWrapper(); | |
| 257 ASSERT_TRUE(current_tab); | |
| 258 | |
| 259 ScopedTempDir default_downloads_dir; | |
| 260 // Prepare the user's "Downloads" folder. | |
| 261 ASSERT_TRUE(default_downloads_dir.CreateUniqueTempDir()); | |
| 262 // Prepare non-existent folder. | |
| 263 FilePath nonexistent_path("/koakuma/mikity/moemoe/nyannyan"); | |
| 264 ASSERT_FALSE(file_util::PathExists(nonexistent_path)); | |
| 265 | |
| 266 // Changes the default prefs. | |
| 267 current_tab->download_tab_helper()->ChangeSaveDirectoryPrefs( | |
| 268 nonexistent_path, | |
| 269 nonexistent_path, | |
| 270 default_downloads_dir.path(), | |
| 271 SavePackage::SAVE_AS_ONLY_HTML); | |
| 272 | |
| 273 std::string title = UTF16ToASCII( | |
| 274 current_tab->download_tab_helper()->SavePageWithoutDialog()); | |
| 275 file_util::ReplaceIllegalCharactersInPath(&title, ' '); | |
| 276 FilePath full_file_name = | |
| 277 default_downloads_dir.path().Append(FilePath(title + ".html")); | |
| 278 | |
| 279 EXPECT_EQ(url, WaitForSavePackageToFinish()); | |
| 280 | |
| 281 if (browser()->SupportsWindowFeature(Browser::FEATURE_DOWNLOADSHELF)) | |
| 282 EXPECT_TRUE(browser()->window()->IsDownloadShelfVisible()); | |
| 283 | |
| 284 // Is the file downloaded to the user's "Downloads" directory? | |
| 285 EXPECT_TRUE(file_util::PathExists(full_file_name)); | |
| 286 EXPECT_TRUE(file_util::ContentsEqual( | |
| 287 test_dir_.Append(FilePath(kTestDir)).Append(file_name), | |
| 288 full_file_name)); | |
| 289 | |
| 290 current_tab->download_tab_helper()->RestoreSaveDirectoryPrefs(); | |
| 291 } | |
| 292 | |
| 144 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { | 293 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, NoSave) { |
| 145 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); | 294 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kAboutBlankURL)); |
| 146 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); | 295 ASSERT_TRUE(browser()->command_updater()->SupportsCommand(IDC_SAVE_PAGE)); |
| 147 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); | 296 EXPECT_FALSE(browser()->command_updater()->IsCommandEnabled(IDC_SAVE_PAGE)); |
| 148 } | 297 } |
| 149 | 298 |
| 150 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { | 299 IN_PROC_BROWSER_TEST_F(SavePageBrowserTest, FileNameFromPageTitle) { |
| 151 FilePath file_name(FILE_PATH_LITERAL("b.htm")); | 300 FilePath file_name(FILE_PATH_LITERAL("b.htm")); |
| 152 | 301 |
| 153 GURL url = URLRequestMockHTTPJob::GetMockUrl( | 302 GURL url = URLRequestMockHTTPJob::GetMockUrl( |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 177 full_file_name)); | 326 full_file_name)); |
| 178 EXPECT_TRUE(file_util::ContentsEqual( | 327 EXPECT_TRUE(file_util::ContentsEqual( |
| 179 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), | 328 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.png"), |
| 180 dir.AppendASCII("1.png"))); | 329 dir.AppendASCII("1.png"))); |
| 181 EXPECT_TRUE(file_util::ContentsEqual( | 330 EXPECT_TRUE(file_util::ContentsEqual( |
| 182 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), | 331 test_dir_.Append(FilePath(kTestDir)).AppendASCII("1.css"), |
| 183 dir.AppendASCII("1.css"))); | 332 dir.AppendASCII("1.css"))); |
| 184 } | 333 } |
| 185 | 334 |
| 186 } // namespace | 335 } // namespace |
| OLD | NEW |