Chromium Code Reviews| 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/files/file_path.h" | 5 #include "base/files/file_path.h" |
| 6 #include "base/strings/string_split.h" | 6 #include "base/strings/string_split.h" |
|
Devlin
2014/05/02 16:33:59
You should also include "base/string_util.h" here
| |
| 7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/api/file_system/file_system_api.h" | 8 #include "chrome/browser/extensions/api/file_system/file_system_api.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "ui/shell_dialogs/select_file_dialog.h" | 10 #include "ui/shell_dialogs/select_file_dialog.h" |
| 11 | 11 |
| 12 using extensions::FileSystemChooseEntryFunction; | 12 using extensions::FileSystemChooseEntryFunction; |
| 13 using extensions::api::file_system::AcceptOption; | 13 using extensions::api::file_system::AcceptOption; |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 void CheckExtensions(const std::vector<base::FilePath::StringType>& expected, | 17 void CheckExtensions(const std::vector<base::FilePath::StringType>& expected, |
| 18 std::vector<base::FilePath::StringType>& actual) { | 18 std::vector<base::FilePath::StringType>& actual) { |
| 19 EXPECT_EQ(expected.size(), actual.size()); | 19 EXPECT_EQ(expected.size(), actual.size()); |
| 20 if (expected.size() != actual.size()) | 20 if (expected.size() != actual.size()) |
| 21 return; | 21 return; |
| 22 | 22 |
| 23 for (size_t i = 0; i < expected.size(); ++i) { | 23 for (size_t i = 0; i < expected.size(); ++i) { |
| 24 EXPECT_EQ(expected[i], actual[i]); | 24 EXPECT_EQ(expected[i], actual[i]); |
| 25 } | 25 } |
| 26 } | 26 } |
| 27 | 27 |
| 28 AcceptOption* BuildAcceptOption(std::string description, | 28 AcceptOption* BuildAcceptOption(const std::string& description, |
| 29 std::string mime_types, | 29 const std::string& mime_types, |
| 30 std::string extensions) { | 30 const std::string& extensions) { |
| 31 AcceptOption* option = new AcceptOption(); | 31 AcceptOption* option = new AcceptOption(); |
| 32 | 32 |
| 33 if (!description.empty()) | 33 if (!description.empty()) |
| 34 option->description.reset(new std::string(description)); | 34 option->description.reset(new std::string(description)); |
| 35 | 35 |
| 36 if (!mime_types.empty()) { | 36 if (!mime_types.empty()) { |
| 37 option->mime_types.reset(new std::vector<std::string>()); | 37 option->mime_types.reset(new std::vector<std::string>()); |
| 38 base::SplitString(mime_types, ',', option->mime_types.get()); | 38 base::SplitString(mime_types, ',', option->mime_types.get()); |
| 39 } | 39 } |
| 40 | 40 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 63 bool acceptsAllTypes = false; | 63 bool acceptsAllTypes = false; |
| 64 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, | 64 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 65 base::FilePath::StringType(), NULL, &acceptsAllTypes); | 65 base::FilePath::StringType(), NULL, &acceptsAllTypes); |
| 66 EXPECT_TRUE(file_type_info.include_all_files); | 66 EXPECT_TRUE(file_type_info.include_all_files); |
| 67 EXPECT_TRUE(file_type_info.extensions.empty()); | 67 EXPECT_TRUE(file_type_info.extensions.empty()); |
| 68 | 68 |
| 69 // Test grouping of multiple types. | 69 // Test grouping of multiple types. |
| 70 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 70 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 71 std::vector<linked_ptr<AcceptOption> > options; | 71 std::vector<linked_ptr<AcceptOption> > options; |
| 72 options.push_back(linked_ptr<AcceptOption>(BuildAcceptOption( | 72 options.push_back(linked_ptr<AcceptOption>(BuildAcceptOption( |
| 73 std::string(), "application/x-chrome-extension", "jso"))); | 73 base::EmptyString(), "application/x-chrome-extension", "jso"))); |
| 74 acceptsAllTypes = false; | 74 acceptsAllTypes = false; |
| 75 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, | 75 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 76 base::FilePath::StringType(), &options, &acceptsAllTypes); | 76 base::FilePath::StringType(), &options, &acceptsAllTypes); |
| 77 EXPECT_FALSE(file_type_info.include_all_files); | 77 EXPECT_FALSE(file_type_info.include_all_files); |
| 78 ASSERT_EQ(file_type_info.extensions.size(), (size_t) 1); | 78 ASSERT_EQ(file_type_info.extensions.size(), (size_t) 1); |
| 79 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()) << | 79 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()) << |
| 80 "No override must be specified for boring accept types"; | 80 "No override must be specified for boring accept types"; |
| 81 // Note here (and below) that the expectedTypes are sorted, because we use a | 81 // Note here (and below) that the expectedTypes are sorted, because we use a |
| 82 // set internally to generate the output: thus, the output is sorted. | 82 // set internally to generate the output: thus, the output is sorted. |
| 83 std::vector<base::FilePath::StringType> expectedTypes; | 83 std::vector<base::FilePath::StringType> expectedTypes; |
| 84 expectedTypes.push_back(ToStringType("crx")); | 84 expectedTypes.push_back(ToStringType("crx")); |
| 85 expectedTypes.push_back(ToStringType("jso")); | 85 expectedTypes.push_back(ToStringType("jso")); |
| 86 CheckExtensions(expectedTypes, file_type_info.extensions[0]); | 86 CheckExtensions(expectedTypes, file_type_info.extensions[0]); |
| 87 | 87 |
| 88 // Test that not satisfying the extension will force all types. | 88 // Test that not satisfying the extension will force all types. |
| 89 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 89 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 90 options.clear(); | 90 options.clear(); |
| 91 options.push_back(linked_ptr<AcceptOption>( | 91 options.push_back(linked_ptr<AcceptOption>(BuildAcceptOption( |
| 92 BuildAcceptOption(std::string(), std::string(), "unrelated"))); | 92 base::EmptyString(), base::EmptyString(), "unrelated"))); |
| 93 acceptsAllTypes = false; | 93 acceptsAllTypes = false; |
| 94 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, | 94 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 95 ToStringType(".jso"), &options, &acceptsAllTypes); | 95 ToStringType(".jso"), &options, &acceptsAllTypes); |
| 96 EXPECT_TRUE(file_type_info.include_all_files); | 96 EXPECT_TRUE(file_type_info.include_all_files); |
| 97 | 97 |
| 98 // Test multiple list entries, all containing their own types. | 98 // Test multiple list entries, all containing their own types. |
| 99 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 99 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 100 options.clear(); | 100 options.clear(); |
| 101 options.push_back(linked_ptr<AcceptOption>( | 101 options.push_back(linked_ptr<AcceptOption>( |
| 102 BuildAcceptOption(std::string(), std::string(), "jso,js"))); | 102 BuildAcceptOption(base::EmptyString(), base::EmptyString(), "jso,js"))); |
| 103 options.push_back(linked_ptr<AcceptOption>( | 103 options.push_back(linked_ptr<AcceptOption>( |
| 104 BuildAcceptOption(std::string(), std::string(), "cpp,cc"))); | 104 BuildAcceptOption(base::EmptyString(), base::EmptyString(), "cpp,cc"))); |
| 105 acceptsAllTypes = false; | 105 acceptsAllTypes = false; |
| 106 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, | 106 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 107 base::FilePath::StringType(), &options, &acceptsAllTypes); | 107 base::FilePath::StringType(), &options, &acceptsAllTypes); |
| 108 ASSERT_EQ(file_type_info.extensions.size(), options.size()); | 108 ASSERT_EQ(file_type_info.extensions.size(), options.size()); |
| 109 | 109 |
| 110 expectedTypes.clear(); | 110 expectedTypes.clear(); |
| 111 expectedTypes.push_back(ToStringType("js")); | 111 expectedTypes.push_back(ToStringType("js")); |
| 112 expectedTypes.push_back(ToStringType("jso")); | 112 expectedTypes.push_back(ToStringType("jso")); |
| 113 CheckExtensions(expectedTypes, file_type_info.extensions[0]); | 113 CheckExtensions(expectedTypes, file_type_info.extensions[0]); |
| 114 | 114 |
| 115 expectedTypes.clear(); | 115 expectedTypes.clear(); |
| 116 expectedTypes.push_back(ToStringType("cc")); | 116 expectedTypes.push_back(ToStringType("cc")); |
| 117 expectedTypes.push_back(ToStringType("cpp")); | 117 expectedTypes.push_back(ToStringType("cpp")); |
| 118 CheckExtensions(expectedTypes, file_type_info.extensions[1]); | 118 CheckExtensions(expectedTypes, file_type_info.extensions[1]); |
| 119 | 119 |
| 120 // Test accept type that causes description override. | 120 // Test accept type that causes description override. |
| 121 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 121 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 122 options.clear(); | 122 options.clear(); |
| 123 options.push_back(linked_ptr<AcceptOption>( | 123 options.push_back(linked_ptr<AcceptOption>( |
| 124 BuildAcceptOption(std::string(), "image/*", "html"))); | 124 BuildAcceptOption(base::EmptyString(), "image/*", "html"))); |
| 125 acceptsAllTypes = false; | 125 acceptsAllTypes = false; |
| 126 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, | 126 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 127 base::FilePath::StringType(), &options, &acceptsAllTypes); | 127 base::FilePath::StringType(), &options, &acceptsAllTypes); |
| 128 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); | 128 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); |
| 129 EXPECT_FALSE(file_type_info.extension_description_overrides[0].empty()) << | 129 EXPECT_FALSE(file_type_info.extension_description_overrides[0].empty()) << |
| 130 "Accept type \"image/*\" must generate description override"; | 130 "Accept type \"image/*\" must generate description override"; |
| 131 | 131 |
| 132 // Test multiple accept types that cause description override causes us to | 132 // Test multiple accept types that cause description override causes us to |
| 133 // still present the default. | 133 // still present the default. |
| 134 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 134 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 135 options.clear(); | 135 options.clear(); |
| 136 options.push_back(linked_ptr<AcceptOption>(BuildAcceptOption( | 136 options.push_back(linked_ptr<AcceptOption>(BuildAcceptOption( |
| 137 std::string(), "image/*,audio/*,video/*", std::string()))); | 137 base::EmptyString(), "image/*,audio/*,video/*", base::EmptyString()))); |
| 138 acceptsAllTypes = false; | 138 acceptsAllTypes = false; |
| 139 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, | 139 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 140 base::FilePath::StringType(), &options, &acceptsAllTypes); | 140 base::FilePath::StringType(), &options, &acceptsAllTypes); |
| 141 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); | 141 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1); |
| 142 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()); | 142 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()); |
| 143 | 143 |
| 144 // Test explicit description override. | 144 // Test explicit description override. |
| 145 file_type_info = ui::SelectFileDialog::FileTypeInfo(); | 145 file_type_info = ui::SelectFileDialog::FileTypeInfo(); |
| 146 options.clear(); | 146 options.clear(); |
| 147 options.push_back(linked_ptr<AcceptOption>( | 147 options.push_back(linked_ptr<AcceptOption>( |
| 148 BuildAcceptOption("File Types 101", "image/jpeg", std::string()))); | 148 BuildAcceptOption("File Types 101", "image/jpeg", base::EmptyString()))); |
| 149 acceptsAllTypes = false; | 149 acceptsAllTypes = false; |
| 150 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, | 150 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info, |
| 151 base::FilePath::StringType(), &options, &acceptsAllTypes); | 151 base::FilePath::StringType(), &options, &acceptsAllTypes); |
| 152 EXPECT_EQ(file_type_info.extension_description_overrides[0], | 152 EXPECT_EQ(file_type_info.extension_description_overrides[0], |
| 153 base::UTF8ToUTF16("File Types 101")); | 153 base::UTF8ToUTF16("File Types 101")); |
| 154 } | 154 } |
| 155 | 155 |
| 156 TEST_F(FileSystemApiUnitTest, FileSystemChooseEntryFunctionSuggestionTest) { | 156 TEST_F(FileSystemApiUnitTest, FileSystemChooseEntryFunctionSuggestionTest) { |
| 157 std::string opt_name; | 157 std::string opt_name; |
| 158 base::FilePath suggested_name; | 158 base::FilePath suggested_name; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 177 // TODO(thorogood): Fix this test on Windows. | 177 // TODO(thorogood): Fix this test on Windows. |
| 178 // Filter out absolute paths with no basename. | 178 // Filter out absolute paths with no basename. |
| 179 opt_name = std::string("/"); | 179 opt_name = std::string("/"); |
| 180 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name, | 180 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name, |
| 181 &suggested_extension); | 181 &suggested_extension); |
| 182 EXPECT_FALSE(suggested_name.IsAbsolute()); | 182 EXPECT_FALSE(suggested_name.IsAbsolute()); |
| 183 EXPECT_TRUE(suggested_name.MaybeAsASCII().empty()); | 183 EXPECT_TRUE(suggested_name.MaybeAsASCII().empty()); |
| 184 EXPECT_TRUE(suggested_extension.empty()); | 184 EXPECT_TRUE(suggested_extension.empty()); |
| 185 #endif | 185 #endif |
| 186 } | 186 } |
| OLD | NEW |