diff options
author | paul@chromium.org <paul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-10 22:47:08 +0000 |
---|---|---|
committer | paul@chromium.org <paul@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-10 22:47:08 +0000 |
commit | 4e58b466275032dd9cdf3b7416015c025d85622d (patch) | |
tree | 94ec2250f805f3ab3e1a40882f3f095942c275bf /chrome/browser/download/save_package_unittest.cc | |
parent | 905075182a7867d5345284951980fb5ceb09bff7 (diff) | |
download | chromium_src-4e58b466275032dd9cdf3b7416015c025d85622d.zip chromium_src-4e58b466275032dd9cdf3b7416015c025d85622d.tar.gz chromium_src-4e58b466275032dd9cdf3b7416015c025d85622d.tar.bz2 |
Ensure proper paths when saving pages with no title.
When saving a page with no title, such as a text file, attempt to
use a sane value for the save name by retrieving the last component
of the URL (if one exists).
This prevents the case where a page http://www.foo.com/a/path/name.txt
is saved as file "http www" with extension type "foo.com a path name.txt".
This change also makes the SavePackage code a little more flexible
and simpler to test.
BUG=none
TEST=Covered by unittest.
Review URL: http://codereview.chromium.org/155266
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@20432 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/download/save_package_unittest.cc')
-rw-r--r-- | chrome/browser/download/save_package_unittest.cc | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/chrome/browser/download/save_package_unittest.cc b/chrome/browser/download/save_package_unittest.cc index 6522a80..d13bc61 100644 --- a/chrome/browser/download/save_package_unittest.cc +++ b/chrome/browser/download/save_package_unittest.cc @@ -92,6 +92,11 @@ class SavePackageTest : public testing::Test { return SavePackage::EnsureHtmlExtension(name); } + FilePath GetSuggestedNameForSaveAs(const FilePath& title, + bool ensure_html_extension) { + return SavePackage::GetSuggestedNameForSaveAs(title, ensure_html_extension); + } + private: // SavePackage for successfully generating file name. scoped_refptr<SavePackage> save_package_success_; @@ -213,3 +218,33 @@ TEST_F(SavePackageTest, TestEnsureHtmlExtension) { kExtensionTestCases[i].page_title; } } + +// Test that the suggested names generated by SavePackage are reasonable: +// If the name is a URL, retrieve only the path component since the path name +// generation code will turn the entire URL into the file name leading to bad +// extension names. For example, a page with no title and a URL: +// http://www.foo.com/a/path/name.txt will turn into file: +// "http www.foo.com a path name.txt", when we want to save it as "name.txt". + +static const struct { + const FilePath::CharType* page_title; + const FilePath::CharType* expected_name; + bool ensure_html_extension; +} kSuggestedSaveNames[] = { + {FPL("A page title"), FPL("A page title") FPL_HTML_EXTENSION, true}, + {FPL("A page title with.ext"), FPL("A page title with.ext"), false}, + {FPL("http://www.foo.com/path/title.txt"), FPL("title.txt"), false}, + {FPL("http://www.foo.com/path/"), FPL("path"), false}, + {FPL("http://www.foo.com/"), FPL("www.foo.com"), false}, +}; + +TEST_F(SavePackageTest, TestSuggestedSaveNames) { + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSuggestedSaveNames); ++i) { + FilePath title = FilePath(kSuggestedSaveNames[i].page_title); + FilePath save_name = + GetSuggestedNameForSaveAs(title, + kSuggestedSaveNames[i].ensure_html_extension); + EXPECT_EQ(save_name.value(), kSuggestedSaveNames[i].expected_name); + } +} + |