Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1577)

Unified Diff: chrome/browser/extensions/api/file_system/file_system_api_unittest.cc

Issue 10692105: Updates file type selector for fileSystem API (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: more unittests, nit fixes Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/file_system/file_system_api_unittest.cc
diff --git a/chrome/browser/extensions/api/file_system/file_system_api_unittest.cc b/chrome/browser/extensions/api/file_system/file_system_api_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1c39b1a0c08a9d6de8fe13a11bce88a292faf8db
--- /dev/null
+++ b/chrome/browser/extensions/api/file_system/file_system_api_unittest.cc
@@ -0,0 +1,126 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/file_path.h"
+#include "base/file_util.h"
+#include "chrome/browser/extensions/api/file_system/file_system_api.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using extensions::FileSystemChooseFileFunction;
+
+namespace {
+
+void CheckExtensions(std::vector<FilePath::StringType>& expected,
+ std::vector<FilePath::StringType>& actual) {
+ EXPECT_EQ(expected.size(), actual.size());
+ if (expected.size() != actual.size())
+ return;
+
+ for (size_t i = 0; i < expected.size(); ++i) {
+ EXPECT_EQ(expected[i], actual[i]);
+ }
+}
+
+} // namespace
+
+class FileSystemApiUnitTest : public testing::Test {
+};
+
+TEST_F(FileSystemApiUnitTest,
+ FileSystemChooseFileFunctionFileTypeInfoTest) {
+ // AcceptsAllTypes is ignored when no other extensions are available.
+ SelectFileDialog::FileTypeInfo file_type_info;
+ bool acceptsAllTypes = false;
+ FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info,
+ FilePath::StringType(), NULL, &acceptsAllTypes);
+ EXPECT_TRUE(file_type_info.include_all_files);
+ EXPECT_TRUE(file_type_info.extensions.empty());
+
+ // Test grouping of multiple types.
+ file_type_info = SelectFileDialog::FileTypeInfo();
+ std::vector<std::string> accepts;
+ accepts.push_back(".jso,application/x-chrome-extension");
+ acceptsAllTypes = false;
+ FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info,
+ FilePath::StringType(), &accepts, &acceptsAllTypes);
+ EXPECT_FALSE(file_type_info.include_all_files);
+ ASSERT_EQ(file_type_info.extensions.size(), (size_t) 1);
+ EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()) <<
+ "No override must be specified for boring accept types";
+ // Note here (and below) that the expectedTypes are sorted, because we use a
+ // set internally to generate the output: thus, the output is sorted.
+ std::vector<FilePath::StringType> expectedTypes;
+ expectedTypes.push_back(FilePath::StringType("crx"));
+ expectedTypes.push_back(FilePath::StringType("jso"));
+ CheckExtensions(expectedTypes, file_type_info.extensions[0]);
+
+ // Test that not satisfying the extension will force all types.
+ file_type_info = SelectFileDialog::FileTypeInfo();
+ accepts.clear();
+ accepts.push_back(".unrelated");
+ acceptsAllTypes = false;
+ FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info,
+ FilePath::StringType(".jso"), &accepts, &acceptsAllTypes);
+ EXPECT_TRUE(file_type_info.include_all_files);
+
+ // Test multiple list entries, all containing their own types.
+ file_type_info = SelectFileDialog::FileTypeInfo();
+ accepts.clear();
+ accepts.push_back(".jso,.js");
+ accepts.push_back(".cpp,.cc");
+ acceptsAllTypes = false;
+ FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info,
+ FilePath::StringType(), &accepts, &acceptsAllTypes);
+ ASSERT_EQ(file_type_info.extensions.size(), accepts.size());
+
+ expectedTypes.clear();
+ expectedTypes.push_back(FilePath::StringType("js"));
+ expectedTypes.push_back(FilePath::StringType("jso"));
+ CheckExtensions(expectedTypes, file_type_info.extensions[0]);
+
+ expectedTypes.clear();
+ expectedTypes.push_back(FilePath::StringType("cc"));
+ expectedTypes.push_back(FilePath::StringType("cpp"));
+ CheckExtensions(expectedTypes, file_type_info.extensions[1]);
+
+ // Test accept type that causes description override.
+ file_type_info = SelectFileDialog::FileTypeInfo();
+ accepts.clear();
+ accepts.push_back("image/*");
+ acceptsAllTypes = false;
+ FileSystemChooseFileFunction::BuildFileTypeInfo(&file_type_info,
+ FilePath::StringType(), &accepts, &acceptsAllTypes);
+ ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1);
+ EXPECT_FALSE(file_type_info.extension_description_overrides[0].empty()) <<
+ "Accept type \"image/*\" must generate description override";
+}
+
+TEST_F(FileSystemApiUnitTest, FileSystemChooseFileFunctionSuggestionTest) {
+ std::string opt_name;
+ FilePath suggested_name;
+ FilePath::StringType suggested_extension;
+
+ opt_name = std::string("normal_path.txt");
+ FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name,
+ &suggested_extension);
+ EXPECT_FALSE(suggested_name.IsAbsolute());
+ EXPECT_EQ(suggested_name.MaybeAsASCII(), "normal_path.txt");
+ EXPECT_EQ(suggested_extension, "txt");
+
+ // We should provide just the basename, i.e., "path".
+ opt_name = std::string("/a/bad/path");
+ FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name,
+ &suggested_extension);
+ EXPECT_FALSE(suggested_name.IsAbsolute());
+ EXPECT_EQ(suggested_name.MaybeAsASCII(), "path");
+ EXPECT_TRUE(suggested_extension.empty());
+
+ // Filter out absolute paths with no basename.
+ opt_name = std::string("/");
+ FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name,
+ &suggested_extension);
+ EXPECT_FALSE(suggested_name.IsAbsolute());
+ EXPECT_TRUE(suggested_name.MaybeAsASCII().empty());
+ EXPECT_TRUE(suggested_extension.empty());
+}

Powered by Google App Engine
This is Rietveld 408576698