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

Side by Side 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, 4 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 unified diff | Download patch
OLDNEW
(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
99 TEST_F(FileSystemApiUnitTest, FileSystemChooseFileFunctionSuggestionTest) {
100 std::string opt_name;
101 FilePath suggested_name;
102 FilePath::StringType suggested_extension;
103
104 opt_name = std::string("normal_path.txt");
105 FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name,
106 &suggested_extension);
107 EXPECT_FALSE(suggested_name.IsAbsolute());
108 EXPECT_EQ(suggested_name.MaybeAsASCII(), "normal_path.txt");
109 EXPECT_EQ(suggested_extension, "txt");
110
111 // We should provide just the basename, i.e., "path".
112 opt_name = std::string("/a/bad/path");
113 FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name,
114 &suggested_extension);
115 EXPECT_FALSE(suggested_name.IsAbsolute());
116 EXPECT_EQ(suggested_name.MaybeAsASCII(), "path");
117 EXPECT_TRUE(suggested_extension.empty());
118
119 // Filter out absolute paths with no basename.
120 opt_name = std::string("/");
121 FileSystemChooseFileFunction::BuildSuggestion(&opt_name, &suggested_name,
122 &suggested_extension);
123 EXPECT_FALSE(suggested_name.IsAbsolute());
124 EXPECT_TRUE(suggested_name.MaybeAsASCII().empty());
125 EXPECT_TRUE(suggested_extension.empty());
126 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698