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
3 // found in the LICENSE file.
4 
5 #include "ui/shell_dialogs/execute_select_file_win.h"
6 
7 #include <stddef.h>
8 
9 #include "base/macros.h"
10 #include "base/stl_util.h"
11 #include "base/strings/stringprintf.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "ui/shell_dialogs/select_file_dialog.h"
14 
TEST(ShellDialogsWin,AppendExtensionIfNeeded)15 TEST(ShellDialogsWin, AppendExtensionIfNeeded) {
16   struct AppendExtensionTestCase {
17     const wchar_t* filename;
18     const wchar_t* filter_selected;
19     const wchar_t* suggested_ext;
20     const wchar_t* expected_filename;
21   } test_cases[] = {
22       // Known extensions, with or without associated MIME types, should not get
23       // an extension appended.
24       {L"sample.html", L"*.txt", L"txt", L"sample.html"},
25       {L"sample.reg", L"*.txt", L"txt", L"sample.reg"},
26 
27       // An unknown extension, or no extension, should get the default extension
28       // appended.
29       {L"sample.unknown", L"*.txt", L"txt", L"sample.unknown.txt"},
30       {L"sample", L"*.txt", L"txt", L"sample.txt"},
31       // ...unless the unknown and default extensions match.
32       {L"sample.unknown", L"*.unknown", L"unknown", L"sample.unknown"},
33 
34       // The extension alone should be treated like a filename with no
35       // extension.
36       {L"txt", L"*.txt", L"txt", L"txt.txt"},
37 
38       // Trailing dots should cause us to append an extension.
39       {L"sample.txt.", L"*.txt", L"txt", L"sample.txt.txt"},
40       {L"...", L"*.txt", L"txt", L"...txt"},
41 
42       // If the filter is changed to "All files", we allow any filename.
43       {L"sample.unknown", L"*.*", L"", L"sample.unknown"},
44   };
45 
46   for (size_t i = 0; i < base::size(test_cases); ++i) {
47     SCOPED_TRACE(base::StringPrintf("i=%zu", i));
48 
49     EXPECT_EQ(base::string16(test_cases[i].expected_filename),
50               ui::AppendExtensionIfNeeded(test_cases[i].filename,
51                                           test_cases[i].filter_selected,
52                                           test_cases[i].suggested_ext));
53   }
54 }
55