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