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

Side by Side Diff: ui/shell_dialogs/select_file_dialog_unittest.cc

Issue 2804793002: Print Preview: Fix failure to save with long page title (Closed)
Patch Set: Add test Created 3 years, 8 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
OLDNEW
(Empty)
1 // Copyright (c) 2017 The Chromium Authors. All rights reserved.
Lei Zhang 2017/04/15 22:19:34 No (c)
rbpotter 2017/04/17 17:48:49 Done.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stddef.h>
6
7 #include "base/macros.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9 #include "ui/shell_dialogs/select_file_dialog.h"
10
11 namespace {
12 #if defined(OS_WIN)
Lei Zhang 2017/04/15 22:19:34 Can you get rid of the ifdefs with the help of FIL
rbpotter 2017/04/17 17:48:49 Done.
13 const base::FilePath::StringType folder1 = L"folder1111\\";
Lei Zhang 2017/04/15 22:19:34 These should be const base::FilePath::CharType var
rbpotter 2017/04/17 17:48:49 I put these in the test directly using FILE_PATH_L
14 const base::FilePath::StringType folder2 = L"folder2222\\";
15 const base::FilePath::StringType short_name = L"file1";
16 const base::FilePath::StringType medium_name =
17 L"file12345678901234567890123456789012345678901234567890";
18 const base::FilePath::StringType long_name =
19 L"abcdefghijklmnopqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz1234abcdefghijklm"
20 L"nopqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz"
21 L"1234abcdefghijklmnopqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz1234abcdefghi"
22 L"jklmnopqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz1234";
23 const base::FilePath::StringType html_ext = L".html";
24 const base::FilePath::StringType pdf_ext = L".pdf";
25 const base::FilePath::StringType long_ext =
26 long_name.substr(0, 210).insert(0, L".");
Lei Zhang 2017/04/15 22:19:34 So just write this out.
rbpotter 2017/04/17 17:48:49 Done.
27 #else
28 const base::FilePath::StringType folder1 = "folder1111/";
29 const base::FilePath::StringType folder2 = "folder2222/";
30 const base::FilePath::StringType short_name = "file1";
31 const base::FilePath::StringType medium_name =
32 "file12345678901234567890123456789012345678901234567890";
33 const base::FilePath::StringType long_name =
34 "abcdefghijklmnopqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz1234abcdefghijklmn"
35 "opqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz12"
36 "34abcdefghijklmnopqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz1234abcdefghijkl"
37 "mnopqrstuvwxyz1234abcdefghijklmnopqrstuvwxyz1234";
38 const base::FilePath::StringType html_ext = ".html";
39 const base::FilePath::StringType pdf_ext = ".pdf";
40 const base::FilePath::StringType long_ext =
41 long_name.substr(0, 210).insert(0, ".");
42 #endif
43 } // namespace
44
45 TEST(ShellDialogs, ShortenFileNameIfNeeded) {
46 struct ShortenFileNameTestCase {
47 base::FilePath::StringType path;
48 base::FilePath::StringType expected;
49 } test_cases[] = {{folder1, folder1}, {folder1, folder1}, {folder1, folder1},
Lei Zhang 2017/04/15 22:19:34 Don't compute the inputs / expected outputs in tes
rbpotter 2017/04/17 17:48:49 Done.
50 {folder1, folder1}, {folder1, folder1}, {folder1, folder1}};
51 // Short enough file names do not get truncated.
52 test_cases[0].path.append(folder2).append(short_name).append(html_ext);
53 test_cases[0].expected = test_cases[0].path;
54
55 // Long name should be truncated to 255 chars including extension.
56 test_cases[1].path.append(long_name).append(html_ext);
57 test_cases[1].expected.append(long_name.substr(0, 250)).append(html_ext);
58
59 // Long path but short file name is also not truncated (handled by system
60 // dialog).
61 for (int i = 0; i < 26; i++)
62 test_cases[2].path.append(folder2);
63 test_cases[2].path.append(short_name).append(pdf_ext);
64 test_cases[2].expected = test_cases[2].path;
65
66 // Long extension on a short path with total length < 255 is not truncated.
67 test_cases[3].path.append(folder2).append(short_name).append(long_ext);
68 test_cases[3].expected = test_cases[3].path;
69
70 // Long extension on a longer filename is truncated to length needed to get
71 // total length = 255
72 test_cases[4].path.append(folder2).append(medium_name).append(long_ext);
73 test_cases[4]
74 .expected.append(folder2)
75 .append(medium_name)
76 .append(long_ext.substr(0, 255 - 54)); // medium file name has length 54.
77
78 // Long extension on a very long filename leads to extension truncated to 13
79 // chars and long filename truncated to 255 - 13 chars.
80 test_cases[5].path.append(long_name).append(long_ext);
81 test_cases[5]
82 .expected.append(long_name.substr(0, 255 - 13))
83 .append(long_ext.substr(0, 13));
84
85 for (size_t i = 0; i < arraysize(test_cases); ++i) {
86 EXPECT_EQ(test_cases[i].expected,
87 ui::SelectFileDialog::GetShortenedFilePath(
88 base::FilePath(test_cases[i].path))
89 .value());
90 EXPECT_LE(ui::SelectFileDialog::GetShortenedFilePath(
91 base::FilePath(test_cases[i].path))
92 .BaseName()
93 .value()
94 .length(),
95 255u);
96 }
97 }
OLDNEW
« ui/shell_dialogs/select_file_dialog.cc ('K') | « ui/shell_dialogs/select_file_dialog.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698