OLD | NEW |
(Empty) | |
| 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 "base/file_path.h" |
| 6 #include "base/file_util.h" |
| 7 #include "base/string_split.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/base/dialogs/select_file_dialog.h" |
| 12 |
| 13 using extensions::FileSystemChooseFileFunction; |
| 14 |
| 15 namespace { |
| 16 |
| 17 void CheckExtensions(std::vector<FilePath::StringType>& expected, |
| 18 std::vector<FilePath::StringType>& actual) { |
| 19 EXPECT_EQ(expected.size(), actual.size()); |
| 20 if (expected.size() != actual.size()) |
| 21 return; |
| 22 |
| 23 for (size_t i = 0; i < expected.size(); ++i) { |
| 24 EXPECT_EQ(expected[i], actual[i]); |
| 25 } |
| 26 } |
| 27 |
| 28 file_system::AcceptOption* BuildAcceptOption(std::string description, |
| 29 std::string mime_types, |
| 30 std::string extensions) { |
| 31 file_system::AcceptOption* option = new file_system::AcceptOption(); |
| 32 |
| 33 if (!description.empty()) |
| 34 option->description.reset(new std::string(description)); |
| 35 |
| 36 if (!mime_types.empty()) { |
| 37 option->mime_types.reset(new std::vector<std::string>()); |
| 38 base::SplitString(mime_types, ',', option->mime_types.get()); |
| 39 } |
| 40 |
| 41 if (!extensions.empty()) { |
| 42 option->extensions.reset(new std::vector<std::string>()); |
| 43 base::SplitString(extensions, ',', option->extensions.get()); |
| 44 } |
| 45 |
| 46 return option; |
| 47 } |
| 48 |
| 49 } // namespace |
| 50 |
| 51 class FileSystemApiUnitTest : public testing::Test { |
| 52 }; |
| 53 |
| 54 TEST_F(FileSystemApiUnitTest, |
| 55 FileSystemChooseFileFunctionFileTypeInfoTest) { |
| 56 // AcceptsAllTypes is ignored when no other extensions are available. |
| 57 ui::SelectFileDialog::FileTypeInfo file_type_info; |
| 58 bool acceptsAllTypes = false; |
| 59 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, |
| 60 FilePath::StringType(), NULL, &acceptsAllTypes); |
| 61 EXPECT_TRUE(file_type_info.include_all_files); |
| 62 EXPECT_TRUE(file_type_info.extensions.empty()); |
| 63 |
| 64 // Test grouping of multiple types. |
| 65 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 66 std::vector<linked_ptr<file_system::AcceptOption> > options; |
| 67 options.push_back(linked_ptr<file_system::AcceptOption>( |
| 68 BuildAcceptOption("", "application/x-chrome-extension", "jso"))); |
| 69 acceptsAllTypes = false; |
| 70 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, |
| 71 FilePath::StringType(), &options, &acceptsAllTypes); |
| 72 EXPECT_FALSE(file_type_info.include_all_files); |
| 73 ASSERT_EQ(file_type_info.extensions.size(), (size_t) 1); |
| 74 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()) << |
| 75 "No override must be specified for boring accept types"; |
| 76 // Note here (and below) that the expectedTypes are sorted, because we use a |
| 77 // set internally to generate the output: thus, the output is sorted. |
| 78 std::vector<FilePath::StringType> expectedTypes; |
| 79 expectedTypes.push_back(FilePath::StringType("crx")); |
| 80 expectedTypes.push_back(FilePath::StringType("jso")); |
| 81 CheckExtensions(expectedTypes, file_type_info.extensions[0]); |
| 82 |
| 83 // Test that not satisfying the extension will force all types. |
| 84 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 85 options.clear(); |
| 86 options.push_back(linked_ptr<file_system::AcceptOption>( |
| 87 BuildAcceptOption("", "", "unrelated"))); |
| 88 acceptsAllTypes = false; |
| 89 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, |
| 90 FilePath::StringType(".jso"), &options, &acceptsAllTypes); |
| 91 EXPECT_TRUE(file_type_info.include_all_files); |
| 92 |
| 93 // Test multiple list entries, all containing their own types. |
| 94 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 95 options.clear(); |
| 96 options.push_back(linked_ptr<file_system::AcceptOption>( |
| 97 BuildAcceptOption("", "", "jso,js"))); |
| 98 options.push_back(linked_ptr<file_system::AcceptOption>( |
| 99 BuildAcceptOption("", "", "cpp,cc"))); |
| 100 acceptsAllTypes = false; |
| 101 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, |
| 102 FilePath::StringType(), &options, &acceptsAllTypes); |
| 103 ASSERT_EQ(file_type_info.extensions.size(), options.size()); |
| 104 |
| 105 expectedTypes.clear(); |
| 106 expectedTypes.push_back(FilePath::StringType("js")); |
| 107 expectedTypes.push_back(FilePath::StringType("jso")); |
| 108 CheckExtensions(expectedTypes, file_type_info.extensions[0]); |
| 109 |
| 110 expectedTypes.clear(); |
| 111 expectedTypes.push_back(FilePath::StringType("cc")); |
| 112 expectedTypes.push_back(FilePath::StringType("cpp")); |
| 113 CheckExtensions(expectedTypes, file_type_info.extensions[1]); |
| 114 |
| 115 // Test accept type that causes description override. |
| 116 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 117 options.clear(); |
| 118 options.push_back(linked_ptr<file_system::AcceptOption>( |
| 119 BuildAcceptOption("", "image/*", "html"))); |
| 120 acceptsAllTypes = false; |
| 121 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, |
| 122 FilePath::StringType(), &options, &acceptsAllTypes); |
| 123 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); |
| 124 EXPECT_FALSE(file_type_info.extension_description_overrides[0].empty()) << |
| 125 "Accept type \"image/*\" must generate description override"; |
| 126 |
| 127 // Test multiple accept types that cause description override causes us to |
| 128 // still present the default. |
| 129 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 130 options.clear(); |
| 131 options.push_back(linked_ptr<file_system::AcceptOption>( |
| 132 BuildAcceptOption("", "image/*,audio/*,video/*", ""))); |
| 133 acceptsAllTypes = false; |
| 134 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, |
| 135 FilePath::StringType(), &options, &acceptsAllTypes); |
| 136 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); |
| 137 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()); |
| 138 |
| 139 // Test explicit description override. |
| 140 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 141 options.clear(); |
| 142 options.push_back(linked_ptr<file_system::AcceptOption>( |
| 143 BuildAcceptOption("File Types 101", "image/jpeg", ""))); |
| 144 acceptsAllTypes = false; |
| 145 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, |
| 146 FilePath::StringType(), &options, &acceptsAllTypes); |
| 147 EXPECT_EQ(file_type_info.extension_description_overrides[0], |
| 148 UTF8ToUTF16("File Types 101")); |
| 149 } |
| 150 |
| 151 TEST_F(FileSystemApiUnitTest, FileSystemChooseFileFunctionSuggestionTest) { |
| 152 std::string opt_name; |
| 153 FilePath suggested_name; |
| 154 FilePath::StringType suggested_extension; |
| 155 |
| 156 opt_name = std::string("normal_path.txt"); |
| 157 FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name, |
| 158 &suggested_extension); |
| 159 EXPECT_FALSE(suggested_name.IsAbsolute()); |
| 160 EXPECT_EQ(suggested_name.MaybeAsASCII(), "normal_path.txt"); |
| 161 EXPECT_EQ(suggested_extension, "txt"); |
| 162 |
| 163 // We should provide just the basename, i.e., "path". |
| 164 opt_name = std::string("/a/bad/path"); |
| 165 FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name, |
| 166 &suggested_extension); |
| 167 EXPECT_FALSE(suggested_name.IsAbsolute()); |
| 168 EXPECT_EQ(suggested_name.MaybeAsASCII(), "path"); |
| 169 EXPECT_TRUE(suggested_extension.empty()); |
| 170 |
| 171 // Filter out absolute paths with no basename. |
| 172 opt_name = std::string("/"); |
| 173 FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name, |
| 174 &suggested_extension); |
| 175 EXPECT_FALSE(suggested_name.IsAbsolute()); |
| 176 EXPECT_TRUE(suggested_name.MaybeAsASCII().empty()); |
| 177 EXPECT_TRUE(suggested_extension.empty()); |
| 178 } |
OLD | NEW |