Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(157)

Side by Side Diff: content/browser/download/save_package_unittest.cc

Issue 12213066: Use base namespace for FilePath in content/browser (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/files/scoped_temp_dir.h" 8 #include "base/files/scoped_temp_dir.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "base/string_util.h" 10 #include "base/string_util.h"
(...skipping 29 matching lines...) Expand all
40 const uint32 kMaxFileNameLength = NAME_MAX; 40 const uint32 kMaxFileNameLength = NAME_MAX;
41 #endif 41 #endif
42 42
43 // Used to make long filenames. 43 // Used to make long filenames.
44 std::string long_file_name( 44 std::string long_file_name(
45 "EFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz01234567" 45 "EFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz01234567"
46 "89ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz012345" 46 "89ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz012345"
47 "6789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123" 47 "6789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz0123"
48 "456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789a"); 48 "456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789a");
49 49
50 bool HasOrdinalNumber(const FilePath::StringType& filename) { 50 bool HasOrdinalNumber(const base::FilePath::StringType& filename) {
51 FilePath::StringType::size_type r_paren_index = filename.rfind(FPL(')')); 51 base::FilePath::StringType::size_type r_paren_index =
52 FilePath::StringType::size_type l_paren_index = filename.rfind(FPL('(')); 52 filename.rfind(FPL(')'));
53 base::FilePath::StringType::size_type l_paren_index =
54 filename.rfind(FPL('('));
53 if (l_paren_index >= r_paren_index) 55 if (l_paren_index >= r_paren_index)
54 return false; 56 return false;
55 57
56 for (FilePath::StringType::size_type i = l_paren_index + 1; 58 for (base::FilePath::StringType::size_type i = l_paren_index + 1;
57 i != r_paren_index; ++i) { 59 i != r_paren_index; ++i) {
58 if (!IsAsciiDigit(filename[i])) 60 if (!IsAsciiDigit(filename[i]))
59 return false; 61 return false;
60 } 62 }
61 63
62 return true; 64 return true;
63 } 65 }
64 66
65 } // namespace 67 } // namespace
66 68
67 class SavePackageTest : public RenderViewHostImplTestHarness { 69 class SavePackageTest : public RenderViewHostImplTestHarness {
68 public: 70 public:
69 SavePackageTest() : browser_thread_(BrowserThread::UI, &message_loop_) { 71 SavePackageTest() : browser_thread_(BrowserThread::UI, &message_loop_) {
70 } 72 }
71 73
72 bool GetGeneratedFilename(bool need_success_generate_filename, 74 bool GetGeneratedFilename(bool need_success_generate_filename,
73 const std::string& disposition, 75 const std::string& disposition,
74 const std::string& url, 76 const std::string& url,
75 bool need_htm_ext, 77 bool need_htm_ext,
76 FilePath::StringType* generated_name) { 78 base::FilePath::StringType* generated_name) {
77 SavePackage* save_package; 79 SavePackage* save_package;
78 if (need_success_generate_filename) 80 if (need_success_generate_filename)
79 save_package = save_package_success_.get(); 81 save_package = save_package_success_.get();
80 else 82 else
81 save_package = save_package_fail_.get(); 83 save_package = save_package_fail_.get();
82 return save_package->GenerateFileName(disposition, GURL(url), need_htm_ext, 84 return save_package->GenerateFileName(disposition, GURL(url), need_htm_ext,
83 generated_name); 85 generated_name);
84 } 86 }
85 87
86 FilePath EnsureHtmlExtension(const FilePath& name) { 88 base::FilePath EnsureHtmlExtension(const base::FilePath& name) {
87 return SavePackage::EnsureHtmlExtension(name); 89 return SavePackage::EnsureHtmlExtension(name);
88 } 90 }
89 91
90 FilePath EnsureMimeExtension(const FilePath& name, 92 base::FilePath EnsureMimeExtension(const base::FilePath& name,
91 const std::string& content_mime_type) { 93 const std::string& content_mime_type) {
92 return SavePackage::EnsureMimeExtension(name, content_mime_type); 94 return SavePackage::EnsureMimeExtension(name, content_mime_type);
93 } 95 }
94 96
95 GURL GetUrlToBeSaved() { 97 GURL GetUrlToBeSaved() {
96 return save_package_success_->GetUrlToBeSaved(); 98 return save_package_success_->GetUrlToBeSaved();
97 } 99 }
98 100
99 protected: 101 protected:
100 virtual void SetUp() { 102 virtual void SetUp() {
(...skipping 28 matching lines...) Expand all
129 scoped_refptr<SavePackage> save_package_fail_; 131 scoped_refptr<SavePackage> save_package_fail_;
130 132
131 base::ScopedTempDir temp_dir_; 133 base::ScopedTempDir temp_dir_;
132 134
133 DISALLOW_COPY_AND_ASSIGN(SavePackageTest); 135 DISALLOW_COPY_AND_ASSIGN(SavePackageTest);
134 }; 136 };
135 137
136 static const struct { 138 static const struct {
137 const char* disposition; 139 const char* disposition;
138 const char* url; 140 const char* url;
139 const FilePath::CharType* expected_name; 141 const base::FilePath::CharType* expected_name;
140 bool need_htm_ext; 142 bool need_htm_ext;
141 } kGeneratedFiles[] = { 143 } kGeneratedFiles[] = {
142 // We mainly focus on testing duplicated names here, since retrieving file 144 // We mainly focus on testing duplicated names here, since retrieving file
143 // name from disposition and url has been tested in DownloadManagerTest. 145 // name from disposition and url has been tested in DownloadManagerTest.
144 146
145 // No useful information in disposition or URL, use default. 147 // No useful information in disposition or URL, use default.
146 {"1.html", "http://www.savepage.com/", 148 {"1.html", "http://www.savepage.com/",
147 FPL("saved_resource") FPL_HTML_EXTENSION, true}, 149 FPL("saved_resource") FPL_HTML_EXTENSION, true},
148 150
149 // No duplicate occurs. 151 // No duplicate occurs.
(...skipping 26 matching lines...) Expand all
176 178
177 {"filename=READme.txt", "http://www.savepage.com", 179 {"filename=READme.txt", "http://www.savepage.com",
178 FPL("readme(2).txt"), false}, 180 FPL("readme(2).txt"), false},
179 181
180 {"filename=Readme(1).txt", "http://www.savepage.com", 182 {"filename=Readme(1).txt", "http://www.savepage.com",
181 FPL("readme(3).txt"), false}, 183 FPL("readme(3).txt"), false},
182 }; 184 };
183 185
184 TEST_F(SavePackageTest, TestSuccessfullyGenerateSavePackageFilename) { 186 TEST_F(SavePackageTest, TestSuccessfullyGenerateSavePackageFilename) {
185 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kGeneratedFiles); ++i) { 187 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kGeneratedFiles); ++i) {
186 FilePath::StringType file_name; 188 base::FilePath::StringType file_name;
187 bool ok = GetGeneratedFilename(true, 189 bool ok = GetGeneratedFilename(true,
188 kGeneratedFiles[i].disposition, 190 kGeneratedFiles[i].disposition,
189 kGeneratedFiles[i].url, 191 kGeneratedFiles[i].url,
190 kGeneratedFiles[i].need_htm_ext, 192 kGeneratedFiles[i].need_htm_ext,
191 &file_name); 193 &file_name);
192 ASSERT_TRUE(ok); 194 ASSERT_TRUE(ok);
193 EXPECT_EQ(kGeneratedFiles[i].expected_name, file_name); 195 EXPECT_EQ(kGeneratedFiles[i].expected_name, file_name);
194 } 196 }
195 } 197 }
196 198
197 TEST_F(SavePackageTest, TestUnSuccessfullyGenerateSavePackageFilename) { 199 TEST_F(SavePackageTest, TestUnSuccessfullyGenerateSavePackageFilename) {
198 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kGeneratedFiles); ++i) { 200 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kGeneratedFiles); ++i) {
199 FilePath::StringType file_name; 201 base::FilePath::StringType file_name;
200 bool ok = GetGeneratedFilename(false, 202 bool ok = GetGeneratedFilename(false,
201 kGeneratedFiles[i].disposition, 203 kGeneratedFiles[i].disposition,
202 kGeneratedFiles[i].url, 204 kGeneratedFiles[i].url,
203 kGeneratedFiles[i].need_htm_ext, 205 kGeneratedFiles[i].need_htm_ext,
204 &file_name); 206 &file_name);
205 ASSERT_FALSE(ok); 207 ASSERT_FALSE(ok);
206 } 208 }
207 } 209 }
208 210
209 // Crashing on Windows, see http://crbug.com/79365 211 // Crashing on Windows, see http://crbug.com/79365
210 #if defined(OS_WIN) 212 #if defined(OS_WIN)
211 #define MAYBE_TestLongSavePackageFilename DISABLED_TestLongSavePackageFilename 213 #define MAYBE_TestLongSavePackageFilename DISABLED_TestLongSavePackageFilename
212 #else 214 #else
213 #define MAYBE_TestLongSavePackageFilename TestLongSavePackageFilename 215 #define MAYBE_TestLongSavePackageFilename TestLongSavePackageFilename
214 #endif 216 #endif
215 TEST_F(SavePackageTest, MAYBE_TestLongSavePackageFilename) { 217 TEST_F(SavePackageTest, MAYBE_TestLongSavePackageFilename) {
216 const std::string base_url("http://www.google.com/"); 218 const std::string base_url("http://www.google.com/");
217 const std::string long_file = long_file_name + ".css"; 219 const std::string long_file = long_file_name + ".css";
218 const std::string url = base_url + long_file; 220 const std::string url = base_url + long_file;
219 221
220 FilePath::StringType filename; 222 base::FilePath::StringType filename;
221 // Test that the filename is successfully shortened to fit. 223 // Test that the filename is successfully shortened to fit.
222 ASSERT_TRUE(GetGeneratedFilename(true, "", url, false, &filename)); 224 ASSERT_TRUE(GetGeneratedFilename(true, "", url, false, &filename));
223 EXPECT_TRUE(filename.length() < long_file.length()); 225 EXPECT_TRUE(filename.length() < long_file.length());
224 EXPECT_FALSE(HasOrdinalNumber(filename)); 226 EXPECT_FALSE(HasOrdinalNumber(filename));
225 227
226 // Test that the filename is successfully shortened to fit, and gets an 228 // Test that the filename is successfully shortened to fit, and gets an
227 // an ordinal appended. 229 // an ordinal appended.
228 ASSERT_TRUE(GetGeneratedFilename(true, "", url, false, &filename)); 230 ASSERT_TRUE(GetGeneratedFilename(true, "", url, false, &filename));
229 EXPECT_TRUE(filename.length() < long_file.length()); 231 EXPECT_TRUE(filename.length() < long_file.length());
230 EXPECT_TRUE(HasOrdinalNumber(filename)); 232 EXPECT_TRUE(HasOrdinalNumber(filename));
231 233
232 // Test that the filename is successfully shortened to fit, and gets a 234 // Test that the filename is successfully shortened to fit, and gets a
233 // different ordinal appended. 235 // different ordinal appended.
234 FilePath::StringType filename2; 236 base::FilePath::StringType filename2;
235 ASSERT_TRUE(GetGeneratedFilename(true, "", url, false, &filename2)); 237 ASSERT_TRUE(GetGeneratedFilename(true, "", url, false, &filename2));
236 EXPECT_TRUE(filename2.length() < long_file.length()); 238 EXPECT_TRUE(filename2.length() < long_file.length());
237 EXPECT_TRUE(HasOrdinalNumber(filename2)); 239 EXPECT_TRUE(HasOrdinalNumber(filename2));
238 EXPECT_NE(filename, filename2); 240 EXPECT_NE(filename, filename2);
239 } 241 }
240 242
241 // Crashing on Windows, see http://crbug.com/79365 243 // Crashing on Windows, see http://crbug.com/79365
242 #if defined(OS_WIN) 244 #if defined(OS_WIN)
243 #define MAYBE_TestLongSafePureFilename DISABLED_TestLongSafePureFilename 245 #define MAYBE_TestLongSafePureFilename DISABLED_TestLongSafePureFilename
244 #else 246 #else
245 #define MAYBE_TestLongSafePureFilename TestLongSafePureFilename 247 #define MAYBE_TestLongSafePureFilename TestLongSafePureFilename
246 #endif 248 #endif
247 TEST_F(SavePackageTest, MAYBE_TestLongSafePureFilename) { 249 TEST_F(SavePackageTest, MAYBE_TestLongSafePureFilename) {
248 const FilePath save_dir(FPL("test_dir")); 250 const base::FilePath save_dir(FPL("test_dir"));
249 const FilePath::StringType ext(FPL_HTML_EXTENSION); 251 const base::FilePath::StringType ext(FPL_HTML_EXTENSION);
250 FilePath::StringType filename = 252 base::FilePath::StringType filename =
251 #if defined(OS_WIN) 253 #if defined(OS_WIN)
252 ASCIIToWide(long_file_name); 254 ASCIIToWide(long_file_name);
253 #else 255 #else
254 long_file_name; 256 long_file_name;
255 #endif 257 #endif
256 258
257 // Test that the filename + extension doesn't exceed kMaxFileNameLength 259 // Test that the filename + extension doesn't exceed kMaxFileNameLength
258 uint32 max_path = SavePackage::GetMaxPathLengthForDirectory(save_dir); 260 uint32 max_path = SavePackage::GetMaxPathLengthForDirectory(save_dir);
259 ASSERT_TRUE(SavePackage::GetSafePureFileName(save_dir, ext, max_path, 261 ASSERT_TRUE(SavePackage::GetSafePureFileName(save_dir, ext, max_path,
260 &filename)); 262 &filename));
261 EXPECT_TRUE(filename.length() <= kMaxFileNameLength-ext.length()); 263 EXPECT_TRUE(filename.length() <= kMaxFileNameLength-ext.length());
262 } 264 }
263 265
264 static const struct { 266 static const struct {
265 const FilePath::CharType* page_title; 267 const base::FilePath::CharType* page_title;
266 const FilePath::CharType* expected_name; 268 const base::FilePath::CharType* expected_name;
267 } kExtensionTestCases[] = { 269 } kExtensionTestCases[] = {
268 // Extension is preserved if it is already proper for HTML. 270 // Extension is preserved if it is already proper for HTML.
269 {FPL("filename.html"), FPL("filename.html")}, 271 {FPL("filename.html"), FPL("filename.html")},
270 {FPL("filename.HTML"), FPL("filename.HTML")}, 272 {FPL("filename.HTML"), FPL("filename.HTML")},
271 {FPL("filename.XHTML"), FPL("filename.XHTML")}, 273 {FPL("filename.XHTML"), FPL("filename.XHTML")},
272 {FPL("filename.xhtml"), FPL("filename.xhtml")}, 274 {FPL("filename.xhtml"), FPL("filename.xhtml")},
273 {FPL("filename.htm"), FPL("filename.htm")}, 275 {FPL("filename.htm"), FPL("filename.htm")},
274 // ".htm" is added if the extension is improper for HTML. 276 // ".htm" is added if the extension is improper for HTML.
275 {FPL("hello.world"), FPL("hello.world") FPL_HTML_EXTENSION}, 277 {FPL("hello.world"), FPL("hello.world") FPL_HTML_EXTENSION},
276 {FPL("hello.txt"), FPL("hello.txt") FPL_HTML_EXTENSION}, 278 {FPL("hello.txt"), FPL("hello.txt") FPL_HTML_EXTENSION},
277 {FPL("is.html.good"), FPL("is.html.good") FPL_HTML_EXTENSION}, 279 {FPL("is.html.good"), FPL("is.html.good") FPL_HTML_EXTENSION},
278 // ".htm" is added if the name doesn't have an extension. 280 // ".htm" is added if the name doesn't have an extension.
279 {FPL("helloworld"), FPL("helloworld") FPL_HTML_EXTENSION}, 281 {FPL("helloworld"), FPL("helloworld") FPL_HTML_EXTENSION},
280 {FPL("helloworld."), FPL("helloworld.") FPL_HTML_EXTENSION}, 282 {FPL("helloworld."), FPL("helloworld.") FPL_HTML_EXTENSION},
281 }; 283 };
282 284
283 // Crashing on Windows, see http://crbug.com/79365 285 // Crashing on Windows, see http://crbug.com/79365
284 #if defined(OS_WIN) 286 #if defined(OS_WIN)
285 #define MAYBE_TestEnsureHtmlExtension DISABLED_TestEnsureHtmlExtension 287 #define MAYBE_TestEnsureHtmlExtension DISABLED_TestEnsureHtmlExtension
286 #else 288 #else
287 #define MAYBE_TestEnsureHtmlExtension TestEnsureHtmlExtension 289 #define MAYBE_TestEnsureHtmlExtension TestEnsureHtmlExtension
288 #endif 290 #endif
289 TEST_F(SavePackageTest, MAYBE_TestEnsureHtmlExtension) { 291 TEST_F(SavePackageTest, MAYBE_TestEnsureHtmlExtension) {
290 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExtensionTestCases); ++i) { 292 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kExtensionTestCases); ++i) {
291 FilePath original = FilePath(kExtensionTestCases[i].page_title); 293 base::FilePath original = base::FilePath(kExtensionTestCases[i].page_title);
292 FilePath expected = FilePath(kExtensionTestCases[i].expected_name); 294 base::FilePath expected =
293 FilePath actual = EnsureHtmlExtension(original); 295 base::FilePath(kExtensionTestCases[i].expected_name);
296 base::FilePath actual = EnsureHtmlExtension(original);
294 EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " << 297 EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " <<
295 kExtensionTestCases[i].page_title; 298 kExtensionTestCases[i].page_title;
296 } 299 }
297 } 300 }
298 301
299 // Crashing on Windows, see http://crbug.com/79365 302 // Crashing on Windows, see http://crbug.com/79365
300 #if defined(OS_WIN) 303 #if defined(OS_WIN)
301 #define MAYBE_TestEnsureMimeExtension DISABLED_TestEnsureMimeExtension 304 #define MAYBE_TestEnsureMimeExtension DISABLED_TestEnsureMimeExtension
302 #else 305 #else
303 #define MAYBE_TestEnsureMimeExtension TestEnsureMimeExtension 306 #define MAYBE_TestEnsureMimeExtension TestEnsureMimeExtension
304 #endif 307 #endif
305 TEST_F(SavePackageTest, MAYBE_TestEnsureMimeExtension) { 308 TEST_F(SavePackageTest, MAYBE_TestEnsureMimeExtension) {
306 static const struct { 309 static const struct {
307 const FilePath::CharType* page_title; 310 const base::FilePath::CharType* page_title;
308 const FilePath::CharType* expected_name; 311 const base::FilePath::CharType* expected_name;
309 const char* contents_mime_type; 312 const char* contents_mime_type;
310 } kExtensionTests[] = { 313 } kExtensionTests[] = {
311 { FPL("filename.html"), FPL("filename.html"), "text/html" }, 314 { FPL("filename.html"), FPL("filename.html"), "text/html" },
312 { FPL("filename.htm"), FPL("filename.htm"), "text/html" }, 315 { FPL("filename.htm"), FPL("filename.htm"), "text/html" },
313 { FPL("filename.xhtml"), FPL("filename.xhtml"), "text/html" }, 316 { FPL("filename.xhtml"), FPL("filename.xhtml"), "text/html" },
314 #if defined(OS_WIN) 317 #if defined(OS_WIN)
315 { FPL("filename"), FPL("filename.htm"), "text/html" }, 318 { FPL("filename"), FPL("filename.htm"), "text/html" },
316 #else // defined(OS_WIN) 319 #else // defined(OS_WIN)
317 { FPL("filename"), FPL("filename.html"), "text/html" }, 320 { FPL("filename"), FPL("filename.html"), "text/html" },
318 #endif // defined(OS_WIN) 321 #endif // defined(OS_WIN)
319 { FPL("filename.html"), FPL("filename.html"), "text/xml" }, 322 { FPL("filename.html"), FPL("filename.html"), "text/xml" },
320 { FPL("filename.xml"), FPL("filename.xml"), "text/xml" }, 323 { FPL("filename.xml"), FPL("filename.xml"), "text/xml" },
321 { FPL("filename"), FPL("filename.xml"), "text/xml" }, 324 { FPL("filename"), FPL("filename.xml"), "text/xml" },
322 { FPL("filename.xhtml"), FPL("filename.xhtml"), 325 { FPL("filename.xhtml"), FPL("filename.xhtml"),
323 "application/xhtml+xml" }, 326 "application/xhtml+xml" },
324 { FPL("filename.html"), FPL("filename.html"), 327 { FPL("filename.html"), FPL("filename.html"),
325 "application/xhtml+xml" }, 328 "application/xhtml+xml" },
326 { FPL("filename"), FPL("filename.xhtml"), "application/xhtml+xml" }, 329 { FPL("filename"), FPL("filename.xhtml"), "application/xhtml+xml" },
327 { FPL("filename.txt"), FPL("filename.txt"), "text/plain" }, 330 { FPL("filename.txt"), FPL("filename.txt"), "text/plain" },
328 { FPL("filename"), FPL("filename.txt"), "text/plain" }, 331 { FPL("filename"), FPL("filename.txt"), "text/plain" },
329 { FPL("filename.css"), FPL("filename.css"), "text/css" }, 332 { FPL("filename.css"), FPL("filename.css"), "text/css" },
330 { FPL("filename"), FPL("filename.css"), "text/css" }, 333 { FPL("filename"), FPL("filename.css"), "text/css" },
331 { FPL("filename.abc"), FPL("filename.abc"), "unknown/unknown" }, 334 { FPL("filename.abc"), FPL("filename.abc"), "unknown/unknown" },
332 { FPL("filename"), FPL("filename"), "unknown/unknown" }, 335 { FPL("filename"), FPL("filename"), "unknown/unknown" },
333 }; 336 };
334 for (uint32 i = 0; i < ARRAYSIZE_UNSAFE(kExtensionTests); ++i) { 337 for (uint32 i = 0; i < ARRAYSIZE_UNSAFE(kExtensionTests); ++i) {
335 FilePath original = FilePath(kExtensionTests[i].page_title); 338 base::FilePath original = base::FilePath(kExtensionTests[i].page_title);
336 FilePath expected = FilePath(kExtensionTests[i].expected_name); 339 base::FilePath expected = base::FilePath(kExtensionTests[i].expected_name);
337 std::string mime_type(kExtensionTests[i].contents_mime_type); 340 std::string mime_type(kExtensionTests[i].contents_mime_type);
338 FilePath actual = EnsureMimeExtension(original, mime_type); 341 base::FilePath actual = EnsureMimeExtension(original, mime_type);
339 EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " << 342 EXPECT_EQ(expected.value(), actual.value()) << "Failed for page title: " <<
340 kExtensionTests[i].page_title << " MIME:" << mime_type; 343 kExtensionTests[i].page_title << " MIME:" << mime_type;
341 } 344 }
342 } 345 }
343 346
344 // Test that the suggested names generated by SavePackage are reasonable: 347 // Test that the suggested names generated by SavePackage are reasonable:
345 // If the name is a URL, retrieve only the path component since the path name 348 // If the name is a URL, retrieve only the path component since the path name
346 // generation code will turn the entire URL into the file name leading to bad 349 // generation code will turn the entire URL into the file name leading to bad
347 // extension names. For example, a page with no title and a URL: 350 // extension names. For example, a page with no title and a URL:
348 // http://www.foo.com/a/path/name.txt will turn into file: 351 // http://www.foo.com/a/path/name.txt will turn into file:
349 // "http www.foo.com a path name.txt", when we want to save it as "name.txt". 352 // "http www.foo.com a path name.txt", when we want to save it as "name.txt".
350 353
351 static const struct SuggestedSaveNameTestCase { 354 static const struct SuggestedSaveNameTestCase {
352 const char* page_url; 355 const char* page_url;
353 const string16 page_title; 356 const string16 page_title;
354 const FilePath::CharType* expected_name; 357 const base::FilePath::CharType* expected_name;
355 bool ensure_html_extension; 358 bool ensure_html_extension;
356 } kSuggestedSaveNames[] = { 359 } kSuggestedSaveNames[] = {
357 // Title overrides the URL. 360 // Title overrides the URL.
358 { "http://foo.com", 361 { "http://foo.com",
359 ASCIIToUTF16("A page title"), 362 ASCIIToUTF16("A page title"),
360 FPL("A page title") FPL_HTML_EXTENSION, 363 FPL("A page title") FPL_HTML_EXTENSION,
361 true 364 true
362 }, 365 },
363 // Extension is preserved. 366 // Extension is preserved.
364 { "http://foo.com", 367 { "http://foo.com",
(...skipping 30 matching lines...) Expand all
395 398
396 // Crashing on Windows, see http://crbug.com/79365 399 // Crashing on Windows, see http://crbug.com/79365
397 #if defined(OS_WIN) 400 #if defined(OS_WIN)
398 #define MAYBE_TestSuggestedSaveNames DISABLED_TestSuggestedSaveNames 401 #define MAYBE_TestSuggestedSaveNames DISABLED_TestSuggestedSaveNames
399 #else 402 #else
400 #define MAYBE_TestSuggestedSaveNames TestSuggestedSaveNames 403 #define MAYBE_TestSuggestedSaveNames TestSuggestedSaveNames
401 #endif 404 #endif
402 TEST_F(SavePackageTest, MAYBE_TestSuggestedSaveNames) { 405 TEST_F(SavePackageTest, MAYBE_TestSuggestedSaveNames) {
403 for (size_t i = 0; i < arraysize(kSuggestedSaveNames); ++i) { 406 for (size_t i = 0; i < arraysize(kSuggestedSaveNames); ++i) {
404 scoped_refptr<SavePackage> save_package( 407 scoped_refptr<SavePackage> save_package(
405 new SavePackage(contents(), FilePath(), FilePath())); 408 new SavePackage(contents(), base::FilePath(), base::FilePath()));
406 save_package->page_url_ = GURL(kSuggestedSaveNames[i].page_url); 409 save_package->page_url_ = GURL(kSuggestedSaveNames[i].page_url);
407 save_package->title_ = kSuggestedSaveNames[i].page_title; 410 save_package->title_ = kSuggestedSaveNames[i].page_title;
408 411
409 FilePath save_name = save_package->GetSuggestedNameForSaveAs( 412 base::FilePath save_name = save_package->GetSuggestedNameForSaveAs(
410 kSuggestedSaveNames[i].ensure_html_extension, 413 kSuggestedSaveNames[i].ensure_html_extension,
411 std::string(), std::string()); 414 std::string(), std::string());
412 EXPECT_EQ(kSuggestedSaveNames[i].expected_name, save_name.value()) << 415 EXPECT_EQ(kSuggestedSaveNames[i].expected_name, save_name.value()) <<
413 "Test case " << i; 416 "Test case " << i;
414 } 417 }
415 } 418 }
416 419
417 static const FilePath::CharType* kTestDir = FILE_PATH_LITERAL("save_page"); 420 static const base::FilePath::CharType* kTestDir =
421 FILE_PATH_LITERAL("save_page");
418 422
419 // GetUrlToBeSaved method should return correct url to be saved. 423 // GetUrlToBeSaved method should return correct url to be saved.
420 TEST_F(SavePackageTest, TestGetUrlToBeSaved) { 424 TEST_F(SavePackageTest, TestGetUrlToBeSaved) {
421 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 425 base::FilePath file_name(FILE_PATH_LITERAL("a.htm"));
422 GURL url = URLRequestMockHTTPJob::GetMockUrl( 426 GURL url = URLRequestMockHTTPJob::GetMockUrl(
423 FilePath(kTestDir).Append(file_name)); 427 base::FilePath(kTestDir).Append(file_name));
424 NavigateAndCommit(url); 428 NavigateAndCommit(url);
425 EXPECT_EQ(url, GetUrlToBeSaved()); 429 EXPECT_EQ(url, GetUrlToBeSaved());
426 } 430 }
427 431
428 // GetUrlToBeSaved method sould return actual url to be saved, 432 // GetUrlToBeSaved method sould return actual url to be saved,
429 // instead of the displayed url used to view source of a page. 433 // instead of the displayed url used to view source of a page.
430 // Ex:GetUrlToBeSaved method should return http://www.google.com 434 // Ex:GetUrlToBeSaved method should return http://www.google.com
431 // when user types view-source:http://www.google.com 435 // when user types view-source:http://www.google.com
432 TEST_F(SavePackageTest, TestGetUrlToBeSavedViewSource) { 436 TEST_F(SavePackageTest, TestGetUrlToBeSavedViewSource) {
433 FilePath file_name(FILE_PATH_LITERAL("a.htm")); 437 base::FilePath file_name(FILE_PATH_LITERAL("a.htm"));
434 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl( 438 GURL view_source_url = URLRequestMockHTTPJob::GetMockViewSourceUrl(
435 FilePath(kTestDir).Append(file_name)); 439 base::FilePath(kTestDir).Append(file_name));
436 GURL actual_url = URLRequestMockHTTPJob::GetMockUrl( 440 GURL actual_url = URLRequestMockHTTPJob::GetMockUrl(
437 FilePath(kTestDir).Append(file_name)); 441 base::FilePath(kTestDir).Append(file_name));
438 NavigateAndCommit(view_source_url); 442 NavigateAndCommit(view_source_url);
439 EXPECT_EQ(actual_url, GetUrlToBeSaved()); 443 EXPECT_EQ(actual_url, GetUrlToBeSaved());
440 EXPECT_EQ(view_source_url, contents()->GetURL()); 444 EXPECT_EQ(view_source_url, contents()->GetURL());
441 } 445 }
442 446
443 } // namespace content 447 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/save_package_browsertest.cc ('k') | content/browser/download/save_types.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698