| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/file_path.h" | 5 #include "base/file_path.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/string_split.h" | 7 #include "base/string_split.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | 9 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 #include "ui/base/dialogs/select_file_dialog.h" | 11 #include "ui/base/dialogs/select_file_dialog.h" |
| 12 | 12 |
| 13 using extensions::FileSystemChooseFileFunction; | 13 using extensions::FileSystemChooseEntryFunction; |
| 14 using extensions::api::file_system::AcceptOption; | 14 using extensions::api::file_system::AcceptOption; |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 void CheckExtensions(std::vector<FilePath::StringType>& expected, | 18 void CheckExtensions(std::vector<FilePath::StringType>& expected, |
| 19 std::vector<FilePath::StringType>& actual) { | 19 std::vector<FilePath::StringType>& actual) { |
| 20 EXPECT_EQ(expected.size(), actual.size()); | 20 EXPECT_EQ(expected.size(), actual.size()); |
| 21 if (expected.size() != actual.size()) | 21 if (expected.size() != actual.size()) |
| 22 return; | 22 return; |
| 23 | 23 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 51 #define ToStringType UTF8ToWide | 51 #define ToStringType UTF8ToWide |
| 52 #else | 52 #else |
| 53 #define ToStringType | 53 #define ToStringType |
| 54 #endif | 54 #endif |
| 55 | 55 |
| 56 } // namespace | 56 } // namespace |
| 57 | 57 |
| 58 class FileSystemApiUnitTest : public testing::Test { | 58 class FileSystemApiUnitTest : public testing::Test { |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 TEST_F(FileSystemApiUnitTest, FileSystemChooseFileFunctionFileTypeInfoTest) { | 61 TEST_F(FileSystemApiUnitTest, FileSystemChooseEntryFunctionFileTypeInfoTest) { |
| 62 // AcceptsAllTypes is ignored when no other extensions are available. | 62 // AcceptsAllTypes is ignored when no other extensions are available. |
| 63 ui::SelectFileDialog::FileTypeInfo file_type_info; | 63 ui::SelectFileDialog::FileTypeInfo file_type_info; |
| 64 bool acceptsAllTypes = false; | 64 bool acceptsAllTypes = false; |
| 65 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, | 65 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 66 FilePath::StringType(), NULL, &acceptsAllTypes); | 66 FilePath::StringType(), NULL, &acceptsAllTypes); |
| 67 EXPECT_TRUE(file_type_info.include_all_files); | 67 EXPECT_TRUE(file_type_info.include_all_files); |
| 68 EXPECT_TRUE(file_type_info.extensions.empty()); | 68 EXPECT_TRUE(file_type_info.extensions.empty()); |
| 69 | 69 |
| 70 // Test grouping of multiple types. | 70 // Test grouping of multiple types. |
| 71 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 71 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 72 std::vector<linked_ptr<AcceptOption> > options; | 72 std::vector<linked_ptr<AcceptOption> > options; |
| 73 options.push_back(linked_ptr<AcceptOption>( | 73 options.push_back(linked_ptr<AcceptOption>( |
| 74 BuildAcceptOption("", "application/x-chrome-extension", "jso"))); | 74 BuildAcceptOption("", "application/x-chrome-extension", "jso"))); |
| 75 acceptsAllTypes = false; | 75 acceptsAllTypes = false; |
| 76 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, | 76 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 77 FilePath::StringType(), &options, &acceptsAllTypes); | 77 FilePath::StringType(), &options, &acceptsAllTypes); |
| 78 EXPECT_FALSE(file_type_info.include_all_files); | 78 EXPECT_FALSE(file_type_info.include_all_files); |
| 79 ASSERT_EQ(file_type_info.extensions.size(), (size_t) 1); | 79 ASSERT_EQ(file_type_info.extensions.size(), (size_t) 1); |
| 80 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()) << | 80 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()) << |
| 81 "No override must be specified for boring accept types"; | 81 "No override must be specified for boring accept types"; |
| 82 // Note here (and below) that the expectedTypes are sorted, because we use a | 82 // Note here (and below) that the expectedTypes are sorted, because we use a |
| 83 // set internally to generate the output: thus, the output is sorted. | 83 // set internally to generate the output: thus, the output is sorted. |
| 84 std::vector<FilePath::StringType> expectedTypes; | 84 std::vector<FilePath::StringType> expectedTypes; |
| 85 expectedTypes.push_back(ToStringType("crx")); | 85 expectedTypes.push_back(ToStringType("crx")); |
| 86 expectedTypes.push_back(ToStringType("jso")); | 86 expectedTypes.push_back(ToStringType("jso")); |
| 87 CheckExtensions(expectedTypes, file_type_info.extensions[0]); | 87 CheckExtensions(expectedTypes, file_type_info.extensions[0]); |
| 88 | 88 |
| 89 // Test that not satisfying the extension will force all types. | 89 // Test that not satisfying the extension will force all types. |
| 90 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 90 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 91 options.clear(); | 91 options.clear(); |
| 92 options.push_back(linked_ptr<AcceptOption>( | 92 options.push_back(linked_ptr<AcceptOption>( |
| 93 BuildAcceptOption("", "", "unrelated"))); | 93 BuildAcceptOption("", "", "unrelated"))); |
| 94 acceptsAllTypes = false; | 94 acceptsAllTypes = false; |
| 95 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, | 95 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 96 ToStringType(".jso"), &options, &acceptsAllTypes); | 96 ToStringType(".jso"), &options, &acceptsAllTypes); |
| 97 EXPECT_TRUE(file_type_info.include_all_files); | 97 EXPECT_TRUE(file_type_info.include_all_files); |
| 98 | 98 |
| 99 // Test multiple list entries, all containing their own types. | 99 // Test multiple list entries, all containing their own types. |
| 100 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 100 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 101 options.clear(); | 101 options.clear(); |
| 102 options.push_back(linked_ptr<AcceptOption>( | 102 options.push_back(linked_ptr<AcceptOption>( |
| 103 BuildAcceptOption("", "", "jso,js"))); | 103 BuildAcceptOption("", "", "jso,js"))); |
| 104 options.push_back(linked_ptr<AcceptOption>( | 104 options.push_back(linked_ptr<AcceptOption>( |
| 105 BuildAcceptOption("", "", "cpp,cc"))); | 105 BuildAcceptOption("", "", "cpp,cc"))); |
| 106 acceptsAllTypes = false; | 106 acceptsAllTypes = false; |
| 107 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, | 107 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 108 FilePath::StringType(), &options, &acceptsAllTypes); | 108 FilePath::StringType(), &options, &acceptsAllTypes); |
| 109 ASSERT_EQ(file_type_info.extensions.size(), options.size()); | 109 ASSERT_EQ(file_type_info.extensions.size(), options.size()); |
| 110 | 110 |
| 111 expectedTypes.clear(); | 111 expectedTypes.clear(); |
| 112 expectedTypes.push_back(ToStringType("js")); | 112 expectedTypes.push_back(ToStringType("js")); |
| 113 expectedTypes.push_back(ToStringType("jso")); | 113 expectedTypes.push_back(ToStringType("jso")); |
| 114 CheckExtensions(expectedTypes, file_type_info.extensions[0]); | 114 CheckExtensions(expectedTypes, file_type_info.extensions[0]); |
| 115 | 115 |
| 116 expectedTypes.clear(); | 116 expectedTypes.clear(); |
| 117 expectedTypes.push_back(ToStringType("cc")); | 117 expectedTypes.push_back(ToStringType("cc")); |
| 118 expectedTypes.push_back(ToStringType("cpp")); | 118 expectedTypes.push_back(ToStringType("cpp")); |
| 119 CheckExtensions(expectedTypes, file_type_info.extensions[1]); | 119 CheckExtensions(expectedTypes, file_type_info.extensions[1]); |
| 120 | 120 |
| 121 // Test accept type that causes description override. | 121 // Test accept type that causes description override. |
| 122 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 122 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 123 options.clear(); | 123 options.clear(); |
| 124 options.push_back(linked_ptr<AcceptOption>( | 124 options.push_back(linked_ptr<AcceptOption>( |
| 125 BuildAcceptOption("", "image/*", "html"))); | 125 BuildAcceptOption("", "image/*", "html"))); |
| 126 acceptsAllTypes = false; | 126 acceptsAllTypes = false; |
| 127 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, | 127 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 128 FilePath::StringType(), &options, &acceptsAllTypes); | 128 FilePath::StringType(), &options, &acceptsAllTypes); |
| 129 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); | 129 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); |
| 130 EXPECT_FALSE(file_type_info.extension_description_overrides[0].empty()) << | 130 EXPECT_FALSE(file_type_info.extension_description_overrides[0].empty()) << |
| 131 "Accept type \"image/*\" must generate description override"; | 131 "Accept type \"image/*\" must generate description override"; |
| 132 | 132 |
| 133 // Test multiple accept types that cause description override causes us to | 133 // Test multiple accept types that cause description override causes us to |
| 134 // still present the default. | 134 // still present the default. |
| 135 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 135 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 136 options.clear(); | 136 options.clear(); |
| 137 options.push_back(linked_ptr<AcceptOption>( | 137 options.push_back(linked_ptr<AcceptOption>( |
| 138 BuildAcceptOption("", "image/*,audio/*,video/*", ""))); | 138 BuildAcceptOption("", "image/*,audio/*,video/*", ""))); |
| 139 acceptsAllTypes = false; | 139 acceptsAllTypes = false; |
| 140 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, | 140 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 141 FilePath::StringType(), &options, &acceptsAllTypes); | 141 FilePath::StringType(), &options, &acceptsAllTypes); |
| 142 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); | 142 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); |
| 143 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()); | 143 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()); |
| 144 | 144 |
| 145 // Test explicit description override. | 145 // Test explicit description override. |
| 146 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 146 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 147 options.clear(); | 147 options.clear(); |
| 148 options.push_back(linked_ptr<AcceptOption>( | 148 options.push_back(linked_ptr<AcceptOption>( |
| 149 BuildAcceptOption("File Types 101", "image/jpeg", ""))); | 149 BuildAcceptOption("File Types 101", "image/jpeg", ""))); |
| 150 acceptsAllTypes = false; | 150 acceptsAllTypes = false; |
| 151 FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info, | 151 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 152 FilePath::StringType(), &options, &acceptsAllTypes); | 152 FilePath::StringType(), &options, &acceptsAllTypes); |
| 153 EXPECT_EQ(file_type_info.extension_description_overrides[0], | 153 EXPECT_EQ(file_type_info.extension_description_overrides[0], |
| 154 UTF8ToUTF16("File Types 101")); | 154 UTF8ToUTF16("File Types 101")); |
| 155 } | 155 } |
| 156 | 156 |
| 157 TEST_F(FileSystemApiUnitTest, FileSystemChooseFileFunctionSuggestionTest) { | 157 TEST_F(FileSystemApiUnitTest, FileSystemChooseEntryFunctionSuggestionTest) { |
| 158 std::string opt_name; | 158 std::string opt_name; |
| 159 FilePath suggested_name; | 159 FilePath suggested_name; |
| 160 FilePath::StringType suggested_extension; | 160 FilePath::StringType suggested_extension; |
| 161 | 161 |
| 162 opt_name = std::string("normal_path.txt"); | 162 opt_name = std::string("normal_path.txt"); |
| 163 FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name, | 163 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name, |
| 164 &suggested_extension); | 164 &suggested_extension); |
| 165 EXPECT_FALSE(suggested_name.IsAbsolute()); | 165 EXPECT_FALSE(suggested_name.IsAbsolute()); |
| 166 EXPECT_EQ(suggested_name.MaybeAsASCII(), "normal_path.txt"); | 166 EXPECT_EQ(suggested_name.MaybeAsASCII(), "normal_path.txt"); |
| 167 EXPECT_EQ(suggested_extension, ToStringType("txt")); | 167 EXPECT_EQ(suggested_extension, ToStringType("txt")); |
| 168 | 168 |
| 169 // We should provide just the basename, i.e., "path". | 169 // We should provide just the basename, i.e., "path". |
| 170 opt_name = std::string("/a/bad/path"); | 170 opt_name = std::string("/a/bad/path"); |
| 171 FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name, | 171 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name, |
| 172 &suggested_extension); | 172 &suggested_extension); |
| 173 EXPECT_FALSE(suggested_name.IsAbsolute()); | 173 EXPECT_FALSE(suggested_name.IsAbsolute()); |
| 174 EXPECT_EQ(suggested_name.MaybeAsASCII(), "path"); | 174 EXPECT_EQ(suggested_name.MaybeAsASCII(), "path"); |
| 175 EXPECT_TRUE(suggested_extension.empty()); | 175 EXPECT_TRUE(suggested_extension.empty()); |
| 176 | 176 |
| 177 #if !defined(OS_WIN) | 177 #if !defined(OS_WIN) |
| 178 // TODO(thorogood): Fix this test on Windows. | 178 // TODO(thorogood): Fix this test on Windows. |
| 179 // Filter out absolute paths with no basename. | 179 // Filter out absolute paths with no basename. |
| 180 opt_name = std::string("/"); | 180 opt_name = std::string("/"); |
| 181 FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name, | 181 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name, |
| 182 &suggested_extension); | 182 &suggested_extension); |
| 183 EXPECT_FALSE(suggested_name.IsAbsolute()); | 183 EXPECT_FALSE(suggested_name.IsAbsolute()); |
| 184 EXPECT_TRUE(suggested_name.MaybeAsASCII().empty()); | 184 EXPECT_TRUE(suggested_name.MaybeAsASCII().empty()); |
| 185 EXPECT_TRUE(suggested_extension.empty()); | 185 EXPECT_TRUE(suggested_extension.empty()); |
| 186 #endif | 186 #endif |
| 187 } | 187 } |
| OLD | NEW |