[email protected] | 46a32b9 | 2012-03-22 13:04:48 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 5 | #include <string> |
| 6 | |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 7 | #include "base/files/file_path.h" |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 8 | #include "base/files/scoped_temp_dir.h" |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 9 | #include "base/strings/string_util.h" |
[email protected] | 74ebfb1 | 2013-06-07 20:48:00 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 0ffaa48 | 2011-07-14 23:41:28 | [diff] [blame] | 11 | #include "content/browser/download/save_package.h" |
xunjieli | 0332c19 | 2014-09-10 23:23:31 | [diff] [blame] | 12 | #include "content/public/common/url_constants.h" |
[email protected] | 4bfd461 | 2013-12-05 18:12:48 | [diff] [blame] | 13 | #include "content/test/test_render_view_host.h" |
[email protected] | 4172b08 | 2013-02-25 18:07:34 | [diff] [blame] | 14 | #include "content/test/test_web_contents.h" |
xunjieli | 0332c19 | 2014-09-10 23:23:31 | [diff] [blame] | 15 | #include "net/test/url_request/url_request_mock_http_job.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 707e1c4 | 2013-07-09 21:18:58 | [diff] [blame] | 17 | #include "url/gurl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 19 | namespace content { |
[email protected] | 9214555 | 2011-10-31 16:28:03 | [diff] [blame] | 20 | |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 21 | #define FPL FILE_PATH_LITERAL |
[email protected] | 42fdf61 | 2009-07-07 00:34:25 | [diff] [blame] | 22 | #define HTML_EXTENSION ".html" |
[email protected] | 0147df8 | 2014-06-28 06:13:15 | [diff] [blame] | 23 | #if defined(OS_WIN) |
| 24 | #define FPL_HTML_EXTENSION L".html" |
| 25 | #else |
[email protected] | 42fdf61 | 2009-07-07 00:34:25 | [diff] [blame] | 26 | #define FPL_HTML_EXTENSION ".html" |
| 27 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 29 | namespace { |
| 30 | |
| 31 | // This constant copied from save_package.cc. |
| 32 | #if defined(OS_WIN) |
| 33 | const uint32 kMaxFilePathLength = MAX_PATH - 1; |
[email protected] | ae52b19 | 2011-02-11 22:19:47 | [diff] [blame] | 34 | const uint32 kMaxFileNameLength = MAX_PATH - 1; |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 35 | #elif defined(OS_POSIX) |
| 36 | const uint32 kMaxFilePathLength = PATH_MAX - 1; |
[email protected] | ae52b19 | 2011-02-11 22:19:47 | [diff] [blame] | 37 | const uint32 kMaxFileNameLength = NAME_MAX; |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 38 | #endif |
| 39 | |
| 40 | // Used to make long filenames. |
| 41 | std::string long_file_name( |
| 42 | "EFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz01234567" |
| 43 | "89ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz012345" |
| 44 | "6789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123" |
| 45 | "456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789a"); |
| 46 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 47 | bool HasOrdinalNumber(const base::FilePath::StringType& filename) { |
| 48 | base::FilePath::StringType::size_type r_paren_index = |
| 49 | filename.rfind(FPL(')')); |
| 50 | base::FilePath::StringType::size_type l_paren_index = |
| 51 | filename.rfind(FPL('(')); |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 52 | if (l_paren_index >= r_paren_index) |
| 53 | return false; |
| 54 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 55 | for (base::FilePath::StringType::size_type i = l_paren_index + 1; |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 56 | i != r_paren_index; ++i) { |
| 57 | if (!IsAsciiDigit(filename[i])) |
| 58 | return false; |
| 59 | } |
| 60 | |
| 61 | return true; |
| 62 | } |
| 63 | |
| 64 | } // namespace |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | |
[email protected] | 46a32b9 | 2012-03-22 13:04:48 | [diff] [blame] | 66 | class SavePackageTest : public RenderViewHostImplTestHarness { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | public: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | bool GetGeneratedFilename(bool need_success_generate_filename, |
| 69 | const std::string& disposition, |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 70 | const std::string& url, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 71 | bool need_htm_ext, |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 72 | base::FilePath::StringType* generated_name) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | SavePackage* save_package; |
| 74 | if (need_success_generate_filename) |
| 75 | save_package = save_package_success_.get(); |
| 76 | else |
| 77 | save_package = save_package_fail_.get(); |
[email protected] | 6aa4a1c0 | 2010-01-15 18:49:58 | [diff] [blame] | 78 | return save_package->GenerateFileName(disposition, GURL(url), need_htm_ext, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 79 | generated_name); |
| 80 | } |
| 81 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 82 | base::FilePath EnsureHtmlExtension(const base::FilePath& name) { |
[email protected] | daa3ec58 | 2009-05-19 01:58:01 | [diff] [blame] | 83 | return SavePackage::EnsureHtmlExtension(name); |
| 84 | } |
| 85 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 86 | base::FilePath EnsureMimeExtension(const base::FilePath& name, |
[email protected] | a8e94034 | 2010-11-05 21:23:49 | [diff] [blame] | 87 | const std::string& content_mime_type) { |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 88 | return SavePackage::EnsureMimeExtension(name, content_mime_type); |
| 89 | } |
| 90 | |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 91 | GURL GetUrlToBeSaved() { |
| 92 | return save_package_success_->GetUrlToBeSaved(); |
| 93 | } |
| 94 | |
| 95 | protected: |
dcheng | fa85b15 | 2014-10-28 01:13:42 | [diff] [blame] | 96 | void SetUp() override { |
[email protected] | 46a32b9 | 2012-03-22 13:04:48 | [diff] [blame] | 97 | RenderViewHostImplTestHarness::SetUp(); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 98 | |
| 99 | // Do the initialization in SetUp so contents() is initialized by |
[email protected] | 46a32b9 | 2012-03-22 13:04:48 | [diff] [blame] | 100 | // RenderViewHostImplTestHarness::SetUp. |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 101 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 102 | |
[email protected] | c7dd2f6 | 2011-07-18 15:57:59 | [diff] [blame] | 103 | save_package_success_ = new SavePackage(contents(), |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 104 | temp_dir_.path().AppendASCII("testfile" HTML_EXTENSION), |
| 105 | temp_dir_.path().AppendASCII("testfile_files")); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 106 | |
| 107 | // We need to construct a path that is *almost* kMaxFilePathLength long |
[email protected] | aeae59f | 2013-01-28 13:47:55 | [diff] [blame] | 108 | long_file_name.reserve(kMaxFilePathLength + long_file_name.length()); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 109 | while (long_file_name.length() < kMaxFilePathLength) |
| 110 | long_file_name += long_file_name; |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 111 | long_file_name.resize( |
| 112 | kMaxFilePathLength - 9 - temp_dir_.path().value().length()); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 113 | |
[email protected] | c7dd2f6 | 2011-07-18 15:57:59 | [diff] [blame] | 114 | save_package_fail_ = new SavePackage(contents(), |
[email protected] | 3a305db | 2011-04-12 13:40:53 | [diff] [blame] | 115 | temp_dir_.path().AppendASCII(long_file_name + HTML_EXTENSION), |
| 116 | temp_dir_.path().AppendASCII(long_file_name + "_files")); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 117 | } |
| 118 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | private: |
| 120 | // SavePackage for successfully generating file name. |
| 121 | scoped_refptr<SavePackage> save_package_success_; |
| 122 | // SavePackage for failed generating file name. |
| 123 | scoped_refptr<SavePackage> save_package_fail_; |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 124 | |
[email protected] | ea1a3f6 | 2012-11-16 20:34:23 | [diff] [blame] | 125 | base::ScopedTempDir temp_dir_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static const struct { |
| 129 | const char* disposition; |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 130 | const char* url; |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 131 | const base::FilePath::CharType* expected_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | bool need_htm_ext; |
| 133 | } kGeneratedFiles[] = { |
| 134 | // We mainly focus on testing duplicated names here, since retrieving file |
| 135 | // name from disposition and url has been tested in DownloadManagerTest. |
| 136 | |
| 137 | // No useful information in disposition or URL, use default. |
[email protected] | 42fdf61 | 2009-07-07 00:34:25 | [diff] [blame] | 138 | {"1.html", "http://www.savepage.com/", |
| 139 | FPL("saved_resource") FPL_HTML_EXTENSION, true}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | |
| 141 | // No duplicate occurs. |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 142 | {"filename=1.css", "http://www.savepage.com", FPL("1.css"), false}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 143 | |
| 144 | // No duplicate occurs. |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 145 | {"filename=1.js", "http://www.savepage.com", FPL("1.js"), false}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 146 | |
| 147 | // Append numbers for duplicated names. |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 148 | {"filename=1.css", "http://www.savepage.com", FPL("1(1).css"), false}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 149 | |
| 150 | // No duplicate occurs. |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 151 | {"filename=1(1).js", "http://www.savepage.com", FPL("1(1).js"), false}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | |
| 153 | // Append numbers for duplicated names. |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 154 | {"filename=1.css", "http://www.savepage.com", FPL("1(2).css"), false}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | |
| 156 | // Change number for duplicated names. |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 157 | {"filename=1(1).css", "http://www.savepage.com", FPL("1(3).css"), false}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | |
| 159 | // No duplicate occurs. |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 160 | {"filename=1(11).css", "http://www.savepage.com", FPL("1(11).css"), false}, |
[email protected] | f3ccf0c | 2012-11-15 21:01:06 | [diff] [blame] | 161 | |
| 162 | // Test for case-insensitive file names. |
| 163 | {"filename=readme.txt", "http://www.savepage.com", |
| 164 | FPL("readme.txt"), false}, |
| 165 | |
| 166 | {"filename=readme.TXT", "http://www.savepage.com", |
| 167 | FPL("readme(1).TXT"), false}, |
| 168 | |
| 169 | {"filename=READme.txt", "http://www.savepage.com", |
| 170 | FPL("readme(2).txt"), false}, |
| 171 | |
| 172 | {"filename=Readme(1).txt", "http://www.savepage.com", |
| 173 | FPL("readme(3).txt"), false}, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 174 | }; |
| 175 | |
| 176 | TEST_F(SavePackageTest, TestSuccessfullyGenerateSavePackageFilename) { |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 177 | for (size_t i = 0; i < arraysize(kGeneratedFiles); ++i) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 178 | base::FilePath::StringType file_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 179 | bool ok = GetGeneratedFilename(true, |
| 180 | kGeneratedFiles[i].disposition, |
| 181 | kGeneratedFiles[i].url, |
| 182 | kGeneratedFiles[i].need_htm_ext, |
| 183 | &file_name); |
| 184 | ASSERT_TRUE(ok); |
[email protected] | 24ce7d3 | 2009-02-11 02:19:22 | [diff] [blame] | 185 | EXPECT_EQ(kGeneratedFiles[i].expected_name, file_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | TEST_F(SavePackageTest, TestUnSuccessfullyGenerateSavePackageFilename) { |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 190 | for (size_t i = 0; i < arraysize(kGeneratedFiles); ++i) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 191 | base::FilePath::StringType file_name; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 192 | bool ok = GetGeneratedFilename(false, |
| 193 | kGeneratedFiles[i].disposition, |
| 194 | kGeneratedFiles[i].url, |
| 195 | kGeneratedFiles[i].need_htm_ext, |
| 196 | &file_name); |
| 197 | ASSERT_FALSE(ok); |
| 198 | } |
| 199 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 200 | |
[email protected] | fc26e9f | 2011-04-13 18:45:34 | [diff] [blame] | 201 | // Crashing on Windows, see http://crbug.com/79365 |
| 202 | #if defined(OS_WIN) |
| 203 | #define MAYBE_TestLongSavePackageFilename DISABLED_TestLongSavePackageFilename |
| 204 | #else |
| 205 | #define MAYBE_TestLongSavePackageFilename TestLongSavePackageFilename |
| 206 | #endif |
| 207 | TEST_F(SavePackageTest, MAYBE_TestLongSavePackageFilename) { |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 208 | const std::string base_url("http://www.google.com/"); |
| 209 | const std::string long_file = long_file_name + ".css"; |
| 210 | const std::string url = base_url + long_file; |
| 211 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 212 | base::FilePath::StringType filename; |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 213 | // Test that the filename is successfully shortened to fit. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 214 | ASSERT_TRUE(GetGeneratedFilename(true, std::string(), url, false, &filename)); |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 215 | EXPECT_TRUE(filename.length() < long_file.length()); |
| 216 | EXPECT_FALSE(HasOrdinalNumber(filename)); |
| 217 | |
| 218 | // Test that the filename is successfully shortened to fit, and gets an |
| 219 | // an ordinal appended. |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 220 | ASSERT_TRUE(GetGeneratedFilename(true, std::string(), url, false, &filename)); |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 221 | EXPECT_TRUE(filename.length() < long_file.length()); |
| 222 | EXPECT_TRUE(HasOrdinalNumber(filename)); |
| 223 | |
| 224 | // Test that the filename is successfully shortened to fit, and gets a |
| 225 | // different ordinal appended. |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 226 | base::FilePath::StringType filename2; |
[email protected] | 007b3f8 | 2013-04-09 08:46:45 | [diff] [blame] | 227 | ASSERT_TRUE( |
| 228 | GetGeneratedFilename(true, std::string(), url, false, &filename2)); |
[email protected] | 7fe07d07 | 2009-02-20 00:45:16 | [diff] [blame] | 229 | EXPECT_TRUE(filename2.length() < long_file.length()); |
| 230 | EXPECT_TRUE(HasOrdinalNumber(filename2)); |
| 231 | EXPECT_NE(filename, filename2); |
| 232 | } |
[email protected] | daa3ec58 | 2009-05-19 01:58:01 | [diff] [blame] | 233 | |
[email protected] | fc26e9f | 2011-04-13 18:45:34 | [diff] [blame] | 234 | // Crashing on Windows, see http://crbug.com/79365 |
| 235 | #if defined(OS_WIN) |
| 236 | #define MAYBE_TestLongSafePureFilename DISABLED_TestLongSafePureFilename |
| 237 | #else |
| 238 | #define MAYBE_TestLongSafePureFilename TestLongSafePureFilename |
| 239 | #endif |
| 240 | TEST_F(SavePackageTest, MAYBE_TestLongSafePureFilename) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 241 | const base::FilePath save_dir(FPL("test_dir")); |
| 242 | const base::FilePath::StringType ext(FPL_HTML_EXTENSION); |
| 243 | base::FilePath::StringType filename = |
[email protected] | ae52b19 | 2011-02-11 22:19:47 | [diff] [blame] | 244 | #if defined(OS_WIN) |
thestig | e5c64d9 | 2014-11-07 01:19:24 | [diff] [blame] | 245 | base::ASCIIToUTF16(long_file_name); |
[email protected] | ae52b19 | 2011-02-11 22:19:47 | [diff] [blame] | 246 | #else |
| 247 | long_file_name; |
| 248 | #endif |
| 249 | |
| 250 | // Test that the filename + extension doesn't exceed kMaxFileNameLength |
| 251 | uint32 max_path = SavePackage::GetMaxPathLengthForDirectory(save_dir); |
| 252 | ASSERT_TRUE(SavePackage::GetSafePureFileName(save_dir, ext, max_path, |
| 253 | &filename)); |
| 254 | EXPECT_TRUE(filename.length() <= kMaxFileNameLength-ext.length()); |
| 255 | } |
| 256 | |
[email protected] | daa3ec58 | 2009-05-19 01:58:01 | [diff] [blame] | 257 | static const struct { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 258 | const base::FilePath::CharType* page_title; |
| 259 | const base::FilePath::CharType* expected_name; |
[email protected] | daa3ec58 | 2009-05-19 01:58:01 | [diff] [blame] | 260 | } kExtensionTestCases[] = { |
| 261 | // Extension is preserved if it is already proper for HTML. |
| 262 | {FPL("filename.html"), FPL("filename.html")}, |
| 263 | {FPL("filename.HTML"), FPL("filename.HTML")}, |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 264 | {FPL("filename.XHTML"), FPL("filename.XHTML")}, |
| 265 | {FPL("filename.xhtml"), FPL("filename.xhtml")}, |
[email protected] | daa3ec58 | 2009-05-19 01:58:01 | [diff] [blame] | 266 | {FPL("filename.htm"), FPL("filename.htm")}, |
| 267 | // ".htm" is added if the extension is improper for HTML. |
[email protected] | 42fdf61 | 2009-07-07 00:34:25 | [diff] [blame] | 268 | {FPL("hello.world"), FPL("hello.world") FPL_HTML_EXTENSION}, |
| 269 | {FPL("hello.txt"), FPL("hello.txt") FPL_HTML_EXTENSION}, |
| 270 | {FPL("is.html.good"), FPL("is.html.good") FPL_HTML_EXTENSION}, |
[email protected] | daa3ec58 | 2009-05-19 01:58:01 | [diff] [blame] | 271 | // ".htm" is added if the name doesn't have an extension. |
[email protected] | 42fdf61 | 2009-07-07 00:34:25 | [diff] [blame] | 272 | {FPL("helloworld"), FPL("helloworld") FPL_HTML_EXTENSION}, |
| 273 | {FPL("helloworld."), FPL("helloworld.") FPL_HTML_EXTENSION}, |
[email protected] | daa3ec58 | 2009-05-19 01:58:01 | [diff] [blame] | 274 | }; |
| 275 | |
[email protected] | fc26e9f | 2011-04-13 18:45:34 | [diff] [blame] | 276 | // Crashing on Windows, see http://crbug.com/79365 |
| 277 | #if defined(OS_WIN) |
| 278 | #define MAYBE_TestEnsureHtmlExtension DISABLED_TestEnsureHtmlExtension |
| 279 | #else |
| 280 | #define MAYBE_TestEnsureHtmlExtension TestEnsureHtmlExtension |
| 281 | #endif |
| 282 | TEST_F(SavePackageTest, MAYBE_TestEnsureHtmlExtension) { |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 283 | for (size_t i = 0; i < arraysize(kExtensionTestCases); ++i) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 284 | base::FilePath original = base::FilePath(kExtensionTestCases[i].page_title); |
| 285 | base::FilePath expected = |
| 286 | base::FilePath(kExtensionTestCases[i].expected_name); |
| 287 | base::FilePath actual = EnsureHtmlExtension(original); |
[email protected] | daa3ec58 | 2009-05-19 01:58:01 | [diff] [blame] | 288 | EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " << |
| 289 | kExtensionTestCases[i].page_title; |
| 290 | } |
| 291 | } |
[email protected] | 4e58b46 | 2009-07-10 22:47:08 | [diff] [blame] | 292 | |
[email protected] | fc26e9f | 2011-04-13 18:45:34 | [diff] [blame] | 293 | // Crashing on Windows, see http://crbug.com/79365 |
| 294 | #if defined(OS_WIN) |
| 295 | #define MAYBE_TestEnsureMimeExtension DISABLED_TestEnsureMimeExtension |
| 296 | #else |
| 297 | #define MAYBE_TestEnsureMimeExtension TestEnsureMimeExtension |
| 298 | #endif |
| 299 | TEST_F(SavePackageTest, MAYBE_TestEnsureMimeExtension) { |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 300 | static const struct { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 301 | const base::FilePath::CharType* page_title; |
| 302 | const base::FilePath::CharType* expected_name; |
[email protected] | a8e94034 | 2010-11-05 21:23:49 | [diff] [blame] | 303 | const char* contents_mime_type; |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 304 | } kExtensionTests[] = { |
[email protected] | a8e94034 | 2010-11-05 21:23:49 | [diff] [blame] | 305 | { FPL("filename.html"), FPL("filename.html"), "text/html" }, |
| 306 | { FPL("filename.htm"), FPL("filename.htm"), "text/html" }, |
| 307 | { FPL("filename.xhtml"), FPL("filename.xhtml"), "text/html" }, |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 308 | #if defined(OS_WIN) |
[email protected] | a8e94034 | 2010-11-05 21:23:49 | [diff] [blame] | 309 | { FPL("filename"), FPL("filename.htm"), "text/html" }, |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 310 | #else // defined(OS_WIN) |
[email protected] | a8e94034 | 2010-11-05 21:23:49 | [diff] [blame] | 311 | { FPL("filename"), FPL("filename.html"), "text/html" }, |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 312 | #endif // defined(OS_WIN) |
[email protected] | a8e94034 | 2010-11-05 21:23:49 | [diff] [blame] | 313 | { FPL("filename.html"), FPL("filename.html"), "text/xml" }, |
| 314 | { FPL("filename.xml"), FPL("filename.xml"), "text/xml" }, |
| 315 | { FPL("filename"), FPL("filename.xml"), "text/xml" }, |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 316 | { FPL("filename.xhtml"), FPL("filename.xhtml"), |
[email protected] | a8e94034 | 2010-11-05 21:23:49 | [diff] [blame] | 317 | "application/xhtml+xml" }, |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 318 | { FPL("filename.html"), FPL("filename.html"), |
[email protected] | a8e94034 | 2010-11-05 21:23:49 | [diff] [blame] | 319 | "application/xhtml+xml" }, |
| 320 | { FPL("filename"), FPL("filename.xhtml"), "application/xhtml+xml" }, |
| 321 | { FPL("filename.txt"), FPL("filename.txt"), "text/plain" }, |
| 322 | { FPL("filename"), FPL("filename.txt"), "text/plain" }, |
| 323 | { FPL("filename.css"), FPL("filename.css"), "text/css" }, |
| 324 | { FPL("filename"), FPL("filename.css"), "text/css" }, |
| 325 | { FPL("filename.abc"), FPL("filename.abc"), "unknown/unknown" }, |
| 326 | { FPL("filename"), FPL("filename"), "unknown/unknown" }, |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 327 | }; |
viettrungluu | 2dfaba7 | 2014-10-16 05:30:25 | [diff] [blame] | 328 | for (uint32 i = 0; i < arraysize(kExtensionTests); ++i) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 329 | base::FilePath original = base::FilePath(kExtensionTests[i].page_title); |
| 330 | base::FilePath expected = base::FilePath(kExtensionTests[i].expected_name); |
[email protected] | a8e94034 | 2010-11-05 21:23:49 | [diff] [blame] | 331 | std::string mime_type(kExtensionTests[i].contents_mime_type); |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 332 | base::FilePath actual = EnsureMimeExtension(original, mime_type); |
[email protected] | 40878e4 | 2010-02-23 23:54:47 | [diff] [blame] | 333 | EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " << |
| 334 | kExtensionTests[i].page_title << " MIME:" << mime_type; |
| 335 | } |
| 336 | } |
| 337 | |
[email protected] | 4e58b46 | 2009-07-10 22:47:08 | [diff] [blame] | 338 | // Test that the suggested names generated by SavePackage are reasonable: |
| 339 | // If the name is a URL, retrieve only the path component since the path name |
| 340 | // generation code will turn the entire URL into the file name leading to bad |
| 341 | // extension names. For example, a page with no title and a URL: |
| 342 | // http://www.foo.com/a/path/name.txt will turn into file: |
| 343 | // "http www.foo.com a path name.txt", when we want to save it as "name.txt". |
| 344 | |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 345 | static const struct SuggestedSaveNameTestCase { |
| 346 | const char* page_url; |
[email protected] | fcf75d4 | 2013-12-03 20:11:26 | [diff] [blame] | 347 | const base::string16 page_title; |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 348 | const base::FilePath::CharType* expected_name; |
[email protected] | 4e58b46 | 2009-07-10 22:47:08 | [diff] [blame] | 349 | bool ensure_html_extension; |
| 350 | } kSuggestedSaveNames[] = { |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 351 | // Title overrides the URL. |
| 352 | { "http://foo.com", |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 353 | base::ASCIIToUTF16("A page title"), |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 354 | FPL("A page title") FPL_HTML_EXTENSION, |
| 355 | true |
| 356 | }, |
| 357 | // Extension is preserved. |
| 358 | { "http://foo.com", |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 359 | base::ASCIIToUTF16("A page title with.ext"), |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 360 | FPL("A page title with.ext"), |
| 361 | false |
| 362 | }, |
| 363 | // If the title matches the URL, use the last component of the URL. |
| 364 | { "http://foo.com/bar", |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 365 | base::ASCIIToUTF16("foo.com/bar"), |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 366 | FPL("bar"), |
| 367 | false |
| 368 | }, |
| 369 | // If the title matches the URL, but there is no "filename" component, |
| 370 | // use the domain. |
| 371 | { "http://foo.com", |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 372 | base::ASCIIToUTF16("foo.com"), |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 373 | FPL("foo.com"), |
| 374 | false |
| 375 | }, |
| 376 | // Make sure fuzzy matching works. |
| 377 | { "http://foo.com/bar", |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 378 | base::ASCIIToUTF16("foo.com/bar"), |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 379 | FPL("bar"), |
| 380 | false |
| 381 | }, |
| 382 | // A URL-like title that does not match the title is respected in full. |
| 383 | { "http://foo.com", |
[email protected] | 3295612 | 2013-12-25 07:29:24 | [diff] [blame] | 384 | base::ASCIIToUTF16("http://www.foo.com/path/title.txt"), |
asanka | 92a354f | 2015-01-31 00:37:40 | [diff] [blame] | 385 | FPL("http___www.foo.com_path_title.txt"), |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 386 | false |
| 387 | }, |
[email protected] | 4e58b46 | 2009-07-10 22:47:08 | [diff] [blame] | 388 | }; |
| 389 | |
[email protected] | fc26e9f | 2011-04-13 18:45:34 | [diff] [blame] | 390 | // Crashing on Windows, see http://crbug.com/79365 |
| 391 | #if defined(OS_WIN) |
| 392 | #define MAYBE_TestSuggestedSaveNames DISABLED_TestSuggestedSaveNames |
| 393 | #else |
| 394 | #define MAYBE_TestSuggestedSaveNames TestSuggestedSaveNames |
| 395 | #endif |
| 396 | TEST_F(SavePackageTest, MAYBE_TestSuggestedSaveNames) { |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 397 | for (size_t i = 0; i < arraysize(kSuggestedSaveNames); ++i) { |
| 398 | scoped_refptr<SavePackage> save_package( |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 399 | new SavePackage(contents(), base::FilePath(), base::FilePath())); |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 400 | save_package->page_url_ = GURL(kSuggestedSaveNames[i].page_url); |
| 401 | save_package->title_ = kSuggestedSaveNames[i].page_title; |
| 402 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 403 | base::FilePath save_name = save_package->GetSuggestedNameForSaveAs( |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 404 | kSuggestedSaveNames[i].ensure_html_extension, |
[email protected] | 6c70897 | 2011-07-25 21:12:27 | [diff] [blame] | 405 | std::string(), std::string()); |
[email protected] | b746947 | 2010-08-13 21:17:44 | [diff] [blame] | 406 | EXPECT_EQ(kSuggestedSaveNames[i].expected_name, save_name.value()) << |
| 407 | "Test case " << i; |
[email protected] | 4e58b46 | 2009-07-10 22:47:08 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 411 | static const base::FilePath::CharType* kTestDir = |
| 412 | FILE_PATH_LITERAL("save_page"); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 413 | |
| 414 | // GetUrlToBeSaved method should return correct url to be saved. |
| 415 | TEST_F(SavePackageTest, TestGetUrlToBeSaved) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 416 | base::FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
xunjieli | 0332c19 | 2014-09-10 23:23:31 | [diff] [blame] | 417 | GURL url = net::URLRequestMockHTTPJob::GetMockUrl( |
| 418 | base::FilePath(kTestDir).Append(file_name)); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 419 | NavigateAndCommit(url); |
| 420 | EXPECT_EQ(url, GetUrlToBeSaved()); |
| 421 | } |
| 422 | |
| 423 | // GetUrlToBeSaved method sould return actual url to be saved, |
| 424 | // instead of the displayed url used to view source of a page. |
| 425 | // Ex:GetUrlToBeSaved method should return http://www.google.com |
| 426 | // when user types view-source:http://www.google.com |
| 427 | TEST_F(SavePackageTest, TestGetUrlToBeSavedViewSource) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 428 | base::FilePath file_name(FILE_PATH_LITERAL("a.htm")); |
xunjieli | 0332c19 | 2014-09-10 23:23:31 | [diff] [blame] | 429 | GURL mock_url = net::URLRequestMockHTTPJob::GetMockUrl( |
| 430 | base::FilePath(kTestDir).Append(file_name)); |
| 431 | GURL view_source_url = |
| 432 | GURL(kViewSourceScheme + std::string(":") + mock_url.spec()); |
| 433 | GURL actual_url = net::URLRequestMockHTTPJob::GetMockUrl( |
| 434 | base::FilePath(kTestDir).Append(file_name)); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 435 | NavigateAndCommit(view_source_url); |
| 436 | EXPECT_EQ(actual_url, GetUrlToBeSaved()); |
[email protected] | 4e3c752 | 2013-08-13 11:53:18 | [diff] [blame] | 437 | EXPECT_EQ(view_source_url, contents()->GetLastCommittedURL()); |
[email protected] | 3184770a | 2010-04-08 08:00:02 | [diff] [blame] | 438 | } |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 439 | |
| 440 | } // namespace content |