blob: c6c862c4e7549ea2dd5bdd2e6f630553bc17df8c [file] [log] [blame]
[email protected]46a32b92012-03-22 13:04:481// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]7fe07d072009-02-20 00:45:165#include <string>
6
[email protected]57999812013-02-24 05:40:527#include "base/files/file_path.h"
[email protected]ea1a3f62012-11-16 20:34:238#include "base/files/scoped_temp_dir.h"
[email protected]10994d132013-06-11 07:16:189#include "base/strings/string_util.h"
[email protected]74ebfb12013-06-07 20:48:0010#include "base/strings/utf_string_conversions.h"
[email protected]0ffaa482011-07-14 23:41:2811#include "content/browser/download/save_package.h"
xunjieli0332c192014-09-10 23:23:3112#include "content/public/common/url_constants.h"
[email protected]4bfd4612013-12-05 18:12:4813#include "content/test/test_render_view_host.h"
[email protected]4172b082013-02-25 18:07:3414#include "content/test/test_web_contents.h"
xunjieli0332c192014-09-10 23:23:3115#include "net/test/url_request/url_request_mock_http_job.h"
initial.commit09911bf2008-07-26 23:55:2916#include "testing/gtest/include/gtest/gtest.h"
[email protected]707e1c42013-07-09 21:18:5817#include "url/gurl.h"
initial.commit09911bf2008-07-26 23:55:2918
[email protected]35869622012-10-26 23:23:5519namespace content {
[email protected]92145552011-10-31 16:28:0320
[email protected]7fe07d072009-02-20 00:45:1621#define FPL FILE_PATH_LITERAL
[email protected]42fdf612009-07-07 00:34:2522#define HTML_EXTENSION ".html"
[email protected]0147df82014-06-28 06:13:1523#if defined(OS_WIN)
24#define FPL_HTML_EXTENSION L".html"
25#else
[email protected]42fdf612009-07-07 00:34:2526#define FPL_HTML_EXTENSION ".html"
27#endif
initial.commit09911bf2008-07-26 23:55:2928
[email protected]7fe07d072009-02-20 00:45:1629namespace {
30
31// This constant copied from save_package.cc.
32#if defined(OS_WIN)
33const uint32 kMaxFilePathLength = MAX_PATH - 1;
[email protected]ae52b192011-02-11 22:19:4734const uint32 kMaxFileNameLength = MAX_PATH - 1;
[email protected]7fe07d072009-02-20 00:45:1635#elif defined(OS_POSIX)
36const uint32 kMaxFilePathLength = PATH_MAX - 1;
[email protected]ae52b192011-02-11 22:19:4737const uint32 kMaxFileNameLength = NAME_MAX;
[email protected]7fe07d072009-02-20 00:45:1638#endif
39
40// Used to make long filenames.
41std::string long_file_name(
42 "EFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz01234567"
43 "89ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz012345"
44 "6789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123"
45 "456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789a");
46
[email protected]2dec8ec2013-02-07 19:20:3447bool 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]7fe07d072009-02-20 00:45:1652 if (l_paren_index >= r_paren_index)
53 return false;
54
[email protected]2dec8ec2013-02-07 19:20:3455 for (base::FilePath::StringType::size_type i = l_paren_index + 1;
[email protected]7fe07d072009-02-20 00:45:1656 i != r_paren_index; ++i) {
57 if (!IsAsciiDigit(filename[i]))
58 return false;
59 }
60
61 return true;
62}
63
64} // namespace
initial.commit09911bf2008-07-26 23:55:2965
[email protected]46a32b92012-03-22 13:04:4866class SavePackageTest : public RenderViewHostImplTestHarness {
initial.commit09911bf2008-07-26 23:55:2967 public:
initial.commit09911bf2008-07-26 23:55:2968 bool GetGeneratedFilename(bool need_success_generate_filename,
69 const std::string& disposition,
[email protected]7fe07d072009-02-20 00:45:1670 const std::string& url,
initial.commit09911bf2008-07-26 23:55:2971 bool need_htm_ext,
[email protected]2dec8ec2013-02-07 19:20:3472 base::FilePath::StringType* generated_name) {
initial.commit09911bf2008-07-26 23:55:2973 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]6aa4a1c02010-01-15 18:49:5878 return save_package->GenerateFileName(disposition, GURL(url), need_htm_ext,
initial.commit09911bf2008-07-26 23:55:2979 generated_name);
80 }
81
[email protected]2dec8ec2013-02-07 19:20:3482 base::FilePath EnsureHtmlExtension(const base::FilePath& name) {
[email protected]daa3ec582009-05-19 01:58:0183 return SavePackage::EnsureHtmlExtension(name);
84 }
85
[email protected]2dec8ec2013-02-07 19:20:3486 base::FilePath EnsureMimeExtension(const base::FilePath& name,
[email protected]a8e940342010-11-05 21:23:4987 const std::string& content_mime_type) {
[email protected]40878e42010-02-23 23:54:4788 return SavePackage::EnsureMimeExtension(name, content_mime_type);
89 }
90
[email protected]3184770a2010-04-08 08:00:0291 GURL GetUrlToBeSaved() {
92 return save_package_success_->GetUrlToBeSaved();
93 }
94
95 protected:
dchengfa85b152014-10-28 01:13:4296 void SetUp() override {
[email protected]46a32b92012-03-22 13:04:4897 RenderViewHostImplTestHarness::SetUp();
[email protected]3184770a2010-04-08 08:00:0298
99 // Do the initialization in SetUp so contents() is initialized by
[email protected]46a32b92012-03-22 13:04:48100 // RenderViewHostImplTestHarness::SetUp.
[email protected]3a305db2011-04-12 13:40:53101 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
[email protected]3184770a2010-04-08 08:00:02102
[email protected]c7dd2f62011-07-18 15:57:59103 save_package_success_ = new SavePackage(contents(),
[email protected]3a305db2011-04-12 13:40:53104 temp_dir_.path().AppendASCII("testfile" HTML_EXTENSION),
105 temp_dir_.path().AppendASCII("testfile_files"));
[email protected]3184770a2010-04-08 08:00:02106
107 // We need to construct a path that is *almost* kMaxFilePathLength long
[email protected]aeae59f2013-01-28 13:47:55108 long_file_name.reserve(kMaxFilePathLength + long_file_name.length());
[email protected]3184770a2010-04-08 08:00:02109 while (long_file_name.length() < kMaxFilePathLength)
110 long_file_name += long_file_name;
[email protected]3a305db2011-04-12 13:40:53111 long_file_name.resize(
112 kMaxFilePathLength - 9 - temp_dir_.path().value().length());
[email protected]3184770a2010-04-08 08:00:02113
[email protected]c7dd2f62011-07-18 15:57:59114 save_package_fail_ = new SavePackage(contents(),
[email protected]3a305db2011-04-12 13:40:53115 temp_dir_.path().AppendASCII(long_file_name + HTML_EXTENSION),
116 temp_dir_.path().AppendASCII(long_file_name + "_files"));
[email protected]3184770a2010-04-08 08:00:02117 }
118
initial.commit09911bf2008-07-26 23:55:29119 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]7fe07d072009-02-20 00:45:16124
[email protected]ea1a3f62012-11-16 20:34:23125 base::ScopedTempDir temp_dir_;
initial.commit09911bf2008-07-26 23:55:29126};
127
128static const struct {
129 const char* disposition;
[email protected]7fe07d072009-02-20 00:45:16130 const char* url;
[email protected]2dec8ec2013-02-07 19:20:34131 const base::FilePath::CharType* expected_name;
initial.commit09911bf2008-07-26 23:55:29132 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]42fdf612009-07-07 00:34:25138 {"1.html", "http://www.savepage.com/",
139 FPL("saved_resource") FPL_HTML_EXTENSION, true},
initial.commit09911bf2008-07-26 23:55:29140
141 // No duplicate occurs.
[email protected]7fe07d072009-02-20 00:45:16142 {"filename=1.css", "http://www.savepage.com", FPL("1.css"), false},
initial.commit09911bf2008-07-26 23:55:29143
144 // No duplicate occurs.
[email protected]7fe07d072009-02-20 00:45:16145 {"filename=1.js", "http://www.savepage.com", FPL("1.js"), false},
initial.commit09911bf2008-07-26 23:55:29146
147 // Append numbers for duplicated names.
[email protected]7fe07d072009-02-20 00:45:16148 {"filename=1.css", "http://www.savepage.com", FPL("1(1).css"), false},
initial.commit09911bf2008-07-26 23:55:29149
150 // No duplicate occurs.
[email protected]7fe07d072009-02-20 00:45:16151 {"filename=1(1).js", "http://www.savepage.com", FPL("1(1).js"), false},
initial.commit09911bf2008-07-26 23:55:29152
153 // Append numbers for duplicated names.
[email protected]7fe07d072009-02-20 00:45:16154 {"filename=1.css", "http://www.savepage.com", FPL("1(2).css"), false},
initial.commit09911bf2008-07-26 23:55:29155
156 // Change number for duplicated names.
[email protected]7fe07d072009-02-20 00:45:16157 {"filename=1(1).css", "http://www.savepage.com", FPL("1(3).css"), false},
initial.commit09911bf2008-07-26 23:55:29158
159 // No duplicate occurs.
[email protected]7fe07d072009-02-20 00:45:16160 {"filename=1(11).css", "http://www.savepage.com", FPL("1(11).css"), false},
[email protected]f3ccf0c2012-11-15 21:01:06161
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.commit09911bf2008-07-26 23:55:29174};
175
176TEST_F(SavePackageTest, TestSuccessfullyGenerateSavePackageFilename) {
viettrungluu2dfaba72014-10-16 05:30:25177 for (size_t i = 0; i < arraysize(kGeneratedFiles); ++i) {
[email protected]2dec8ec2013-02-07 19:20:34178 base::FilePath::StringType file_name;
initial.commit09911bf2008-07-26 23:55:29179 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]24ce7d32009-02-11 02:19:22185 EXPECT_EQ(kGeneratedFiles[i].expected_name, file_name);
initial.commit09911bf2008-07-26 23:55:29186 }
187}
188
189TEST_F(SavePackageTest, TestUnSuccessfullyGenerateSavePackageFilename) {
viettrungluu2dfaba72014-10-16 05:30:25190 for (size_t i = 0; i < arraysize(kGeneratedFiles); ++i) {
[email protected]2dec8ec2013-02-07 19:20:34191 base::FilePath::StringType file_name;
initial.commit09911bf2008-07-26 23:55:29192 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.botbf09a502008-08-24 00:55:55200
[email protected]fc26e9f2011-04-13 18:45:34201// 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
207TEST_F(SavePackageTest, MAYBE_TestLongSavePackageFilename) {
[email protected]7fe07d072009-02-20 00:45:16208 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]2dec8ec2013-02-07 19:20:34212 base::FilePath::StringType filename;
[email protected]7fe07d072009-02-20 00:45:16213 // Test that the filename is successfully shortened to fit.
[email protected]007b3f82013-04-09 08:46:45214 ASSERT_TRUE(GetGeneratedFilename(true, std::string(), url, false, &filename));
[email protected]7fe07d072009-02-20 00:45:16215 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]007b3f82013-04-09 08:46:45220 ASSERT_TRUE(GetGeneratedFilename(true, std::string(), url, false, &filename));
[email protected]7fe07d072009-02-20 00:45:16221 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]2dec8ec2013-02-07 19:20:34226 base::FilePath::StringType filename2;
[email protected]007b3f82013-04-09 08:46:45227 ASSERT_TRUE(
228 GetGeneratedFilename(true, std::string(), url, false, &filename2));
[email protected]7fe07d072009-02-20 00:45:16229 EXPECT_TRUE(filename2.length() < long_file.length());
230 EXPECT_TRUE(HasOrdinalNumber(filename2));
231 EXPECT_NE(filename, filename2);
232}
[email protected]daa3ec582009-05-19 01:58:01233
[email protected]fc26e9f2011-04-13 18:45:34234// 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
240TEST_F(SavePackageTest, MAYBE_TestLongSafePureFilename) {
[email protected]2dec8ec2013-02-07 19:20:34241 const base::FilePath save_dir(FPL("test_dir"));
242 const base::FilePath::StringType ext(FPL_HTML_EXTENSION);
243 base::FilePath::StringType filename =
[email protected]ae52b192011-02-11 22:19:47244#if defined(OS_WIN)
thestige5c64d92014-11-07 01:19:24245 base::ASCIIToUTF16(long_file_name);
[email protected]ae52b192011-02-11 22:19:47246#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]daa3ec582009-05-19 01:58:01257static const struct {
[email protected]2dec8ec2013-02-07 19:20:34258 const base::FilePath::CharType* page_title;
259 const base::FilePath::CharType* expected_name;
[email protected]daa3ec582009-05-19 01:58:01260} 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]40878e42010-02-23 23:54:47264 {FPL("filename.XHTML"), FPL("filename.XHTML")},
265 {FPL("filename.xhtml"), FPL("filename.xhtml")},
[email protected]daa3ec582009-05-19 01:58:01266 {FPL("filename.htm"), FPL("filename.htm")},
267 // ".htm" is added if the extension is improper for HTML.
[email protected]42fdf612009-07-07 00:34:25268 {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]daa3ec582009-05-19 01:58:01271 // ".htm" is added if the name doesn't have an extension.
[email protected]42fdf612009-07-07 00:34:25272 {FPL("helloworld"), FPL("helloworld") FPL_HTML_EXTENSION},
273 {FPL("helloworld."), FPL("helloworld.") FPL_HTML_EXTENSION},
[email protected]daa3ec582009-05-19 01:58:01274};
275
[email protected]fc26e9f2011-04-13 18:45:34276// 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
282TEST_F(SavePackageTest, MAYBE_TestEnsureHtmlExtension) {
viettrungluu2dfaba72014-10-16 05:30:25283 for (size_t i = 0; i < arraysize(kExtensionTestCases); ++i) {
[email protected]2dec8ec2013-02-07 19:20:34284 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]daa3ec582009-05-19 01:58:01288 EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " <<
289 kExtensionTestCases[i].page_title;
290 }
291}
[email protected]4e58b462009-07-10 22:47:08292
[email protected]fc26e9f2011-04-13 18:45:34293// 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
299TEST_F(SavePackageTest, MAYBE_TestEnsureMimeExtension) {
[email protected]40878e42010-02-23 23:54:47300 static const struct {
[email protected]2dec8ec2013-02-07 19:20:34301 const base::FilePath::CharType* page_title;
302 const base::FilePath::CharType* expected_name;
[email protected]a8e940342010-11-05 21:23:49303 const char* contents_mime_type;
[email protected]40878e42010-02-23 23:54:47304 } kExtensionTests[] = {
[email protected]a8e940342010-11-05 21:23:49305 { 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]40878e42010-02-23 23:54:47308#if defined(OS_WIN)
[email protected]a8e940342010-11-05 21:23:49309 { FPL("filename"), FPL("filename.htm"), "text/html" },
[email protected]40878e42010-02-23 23:54:47310#else // defined(OS_WIN)
[email protected]a8e940342010-11-05 21:23:49311 { FPL("filename"), FPL("filename.html"), "text/html" },
[email protected]40878e42010-02-23 23:54:47312#endif // defined(OS_WIN)
[email protected]a8e940342010-11-05 21:23:49313 { 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]40878e42010-02-23 23:54:47316 { FPL("filename.xhtml"), FPL("filename.xhtml"),
[email protected]a8e940342010-11-05 21:23:49317 "application/xhtml+xml" },
[email protected]40878e42010-02-23 23:54:47318 { FPL("filename.html"), FPL("filename.html"),
[email protected]a8e940342010-11-05 21:23:49319 "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]40878e42010-02-23 23:54:47327 };
viettrungluu2dfaba72014-10-16 05:30:25328 for (uint32 i = 0; i < arraysize(kExtensionTests); ++i) {
[email protected]2dec8ec2013-02-07 19:20:34329 base::FilePath original = base::FilePath(kExtensionTests[i].page_title);
330 base::FilePath expected = base::FilePath(kExtensionTests[i].expected_name);
[email protected]a8e940342010-11-05 21:23:49331 std::string mime_type(kExtensionTests[i].contents_mime_type);
[email protected]2dec8ec2013-02-07 19:20:34332 base::FilePath actual = EnsureMimeExtension(original, mime_type);
[email protected]40878e42010-02-23 23:54:47333 EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " <<
334 kExtensionTests[i].page_title << " MIME:" << mime_type;
335 }
336}
337
[email protected]4e58b462009-07-10 22:47:08338// 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]b7469472010-08-13 21:17:44345static const struct SuggestedSaveNameTestCase {
346 const char* page_url;
[email protected]fcf75d42013-12-03 20:11:26347 const base::string16 page_title;
[email protected]2dec8ec2013-02-07 19:20:34348 const base::FilePath::CharType* expected_name;
[email protected]4e58b462009-07-10 22:47:08349 bool ensure_html_extension;
350} kSuggestedSaveNames[] = {
[email protected]b7469472010-08-13 21:17:44351 // Title overrides the URL.
352 { "http://foo.com",
[email protected]32956122013-12-25 07:29:24353 base::ASCIIToUTF16("A page title"),
[email protected]b7469472010-08-13 21:17:44354 FPL("A page title") FPL_HTML_EXTENSION,
355 true
356 },
357 // Extension is preserved.
358 { "http://foo.com",
[email protected]32956122013-12-25 07:29:24359 base::ASCIIToUTF16("A page title with.ext"),
[email protected]b7469472010-08-13 21:17:44360 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]32956122013-12-25 07:29:24365 base::ASCIIToUTF16("foo.com/bar"),
[email protected]b7469472010-08-13 21:17:44366 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]32956122013-12-25 07:29:24372 base::ASCIIToUTF16("foo.com"),
[email protected]b7469472010-08-13 21:17:44373 FPL("foo.com"),
374 false
375 },
376 // Make sure fuzzy matching works.
377 { "http://foo.com/bar",
[email protected]32956122013-12-25 07:29:24378 base::ASCIIToUTF16("foo.com/bar"),
[email protected]b7469472010-08-13 21:17:44379 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]32956122013-12-25 07:29:24384 base::ASCIIToUTF16("http://www.foo.com/path/title.txt"),
asanka92a354f2015-01-31 00:37:40385 FPL("http___www.foo.com_path_title.txt"),
[email protected]b7469472010-08-13 21:17:44386 false
387 },
[email protected]4e58b462009-07-10 22:47:08388};
389
[email protected]fc26e9f2011-04-13 18:45:34390// 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
396TEST_F(SavePackageTest, MAYBE_TestSuggestedSaveNames) {
[email protected]b7469472010-08-13 21:17:44397 for (size_t i = 0; i < arraysize(kSuggestedSaveNames); ++i) {
398 scoped_refptr<SavePackage> save_package(
[email protected]2dec8ec2013-02-07 19:20:34399 new SavePackage(contents(), base::FilePath(), base::FilePath()));
[email protected]b7469472010-08-13 21:17:44400 save_package->page_url_ = GURL(kSuggestedSaveNames[i].page_url);
401 save_package->title_ = kSuggestedSaveNames[i].page_title;
402
[email protected]2dec8ec2013-02-07 19:20:34403 base::FilePath save_name = save_package->GetSuggestedNameForSaveAs(
[email protected]b7469472010-08-13 21:17:44404 kSuggestedSaveNames[i].ensure_html_extension,
[email protected]6c708972011-07-25 21:12:27405 std::string(), std::string());
[email protected]b7469472010-08-13 21:17:44406 EXPECT_EQ(kSuggestedSaveNames[i].expected_name, save_name.value()) <<
407 "Test case " << i;
[email protected]4e58b462009-07-10 22:47:08408 }
409}
410
[email protected]2dec8ec2013-02-07 19:20:34411static const base::FilePath::CharType* kTestDir =
412 FILE_PATH_LITERAL("save_page");
[email protected]3184770a2010-04-08 08:00:02413
414// GetUrlToBeSaved method should return correct url to be saved.
415TEST_F(SavePackageTest, TestGetUrlToBeSaved) {
[email protected]2dec8ec2013-02-07 19:20:34416 base::FilePath file_name(FILE_PATH_LITERAL("a.htm"));
xunjieli0332c192014-09-10 23:23:31417 GURL url = net::URLRequestMockHTTPJob::GetMockUrl(
418 base::FilePath(kTestDir).Append(file_name));
[email protected]3184770a2010-04-08 08:00:02419 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
427TEST_F(SavePackageTest, TestGetUrlToBeSavedViewSource) {
[email protected]2dec8ec2013-02-07 19:20:34428 base::FilePath file_name(FILE_PATH_LITERAL("a.htm"));
xunjieli0332c192014-09-10 23:23:31429 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]3184770a2010-04-08 08:00:02435 NavigateAndCommit(view_source_url);
436 EXPECT_EQ(actual_url, GetUrlToBeSaved());
[email protected]4e3c7522013-08-13 11:53:18437 EXPECT_EQ(view_source_url, contents()->GetLastCommittedURL());
[email protected]3184770a2010-04-08 08:00:02438}
[email protected]35869622012-10-26 23:23:55439
440} // namespace content