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