| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/download/save_package.h" | 10 #include "chrome/browser/download/save_package.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 else | 85 else |
| 86 save_package = save_package_fail_.get(); | 86 save_package = save_package_fail_.get(); |
| 87 return save_package->GenerateFilename(disposition, GURL(url), need_htm_ext, | 87 return save_package->GenerateFilename(disposition, GURL(url), need_htm_ext, |
| 88 generated_name); | 88 generated_name); |
| 89 } | 89 } |
| 90 | 90 |
| 91 FilePath EnsureHtmlExtension(const FilePath& name) { | 91 FilePath EnsureHtmlExtension(const FilePath& name) { |
| 92 return SavePackage::EnsureHtmlExtension(name); | 92 return SavePackage::EnsureHtmlExtension(name); |
| 93 } | 93 } |
| 94 | 94 |
| 95 FilePath GetSuggestedNameForSaveAs(const FilePath& title, |
| 96 bool ensure_html_extension) { |
| 97 return SavePackage::GetSuggestedNameForSaveAs(title, ensure_html_extension); |
| 98 } |
| 99 |
| 95 private: | 100 private: |
| 96 // SavePackage for successfully generating file name. | 101 // SavePackage for successfully generating file name. |
| 97 scoped_refptr<SavePackage> save_package_success_; | 102 scoped_refptr<SavePackage> save_package_success_; |
| 98 // SavePackage for failed generating file name. | 103 // SavePackage for failed generating file name. |
| 99 scoped_refptr<SavePackage> save_package_fail_; | 104 scoped_refptr<SavePackage> save_package_fail_; |
| 100 | 105 |
| 101 DISALLOW_COPY_AND_ASSIGN(SavePackageTest); | 106 DISALLOW_COPY_AND_ASSIGN(SavePackageTest); |
| 102 }; | 107 }; |
| 103 | 108 |
| 104 static const struct { | 109 static const struct { |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 | 211 |
| 207 TEST_F(SavePackageTest, TestEnsureHtmlExtension) { | 212 TEST_F(SavePackageTest, TestEnsureHtmlExtension) { |
| 208 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExtensionTestCases); ++i) { | 213 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExtensionTestCases); ++i) { |
| 209 FilePath original = FilePath(kExtensionTestCases[i].page_title); | 214 FilePath original = FilePath(kExtensionTestCases[i].page_title); |
| 210 FilePath expected = FilePath(kExtensionTestCases[i].expected_name); | 215 FilePath expected = FilePath(kExtensionTestCases[i].expected_name); |
| 211 FilePath actual = EnsureHtmlExtension(original); | 216 FilePath actual = EnsureHtmlExtension(original); |
| 212 EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " << | 217 EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " << |
| 213 kExtensionTestCases[i].page_title; | 218 kExtensionTestCases[i].page_title; |
| 214 } | 219 } |
| 215 } | 220 } |
| 221 |
| 222 // Test that the suggested names generated by SavePackage are reasonable: |
| 223 // If the name is a URL, retrieve only the path component since the path name |
| 224 // generation code will turn the entire URL into the file name leading to bad |
| 225 // extension names. For example, a page with no title and a URL: |
| 226 // http://www.foo.com/a/path/name.txt will turn into file: |
| 227 // "http www.foo.com a path name.txt", when we want to save it as "name.txt". |
| 228 |
| 229 static const struct { |
| 230 const FilePath::CharType* page_title; |
| 231 const FilePath::CharType* expected_name; |
| 232 bool ensure_html_extension; |
| 233 } kSuggestedSaveNames[] = { |
| 234 {FPL("A page title"), FPL("A page title") FPL_HTML_EXTENSION, true}, |
| 235 {FPL("A page title with.ext"), FPL("A page title with.ext"), false}, |
| 236 {FPL("http://www.foo.com/path/title.txt"), FPL("title.txt"), false}, |
| 237 {FPL("http://www.foo.com/path/"), FPL("path"), false}, |
| 238 {FPL("http://www.foo.com/"), FPL("www.foo.com"), false}, |
| 239 }; |
| 240 |
| 241 TEST_F(SavePackageTest, TestSuggestedSaveNames) { |
| 242 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kSuggestedSaveNames); ++i) { |
| 243 FilePath title = FilePath(kSuggestedSaveNames[i].page_title); |
| 244 FilePath save_name = |
| 245 GetSuggestedNameForSaveAs(title, |
| 246 kSuggestedSaveNames[i].ensure_html_extension); |
| 247 EXPECT_EQ(save_name.value(), kSuggestedSaveNames[i].expected_name); |
| 248 } |
| 249 } |
| 250 |
| OLD | NEW |