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

Side by Side Diff: chrome/browser/extensions/api/file_system/consent_provider_unittest.cc

Issue 2924943003: Break ConsentProvider classes into own file (Closed)
Patch Set: rebase Created 3 years, 6 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "chrome/browser/extensions/api/file_system/file_system_api.h" 5 #include "chrome/browser/extensions/api/file_system/consent_provider.h"
6 6
7 #include <stddef.h> 7 #include <string>
8 8
9 #include <vector>
10
11 #include "base/files/file_path.h"
12 #include "base/macros.h" 9 #include "base/macros.h"
13 #include "base/run_loop.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
16 #include "base/strings/utf_string_conversions.h"
17 #include "build/build_config.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "ui/shell_dialogs/select_file_dialog.h"
20
21 #if defined(OS_CHROMEOS)
22 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
23 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/run_loop.h"
24 #include "chrome/browser/chromeos/file_manager/volume_manager.h" 13 #include "chrome/browser/chromeos/file_manager/volume_manager.h"
25 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h" 14 #include "chrome/browser/chromeos/login/users/fake_chrome_user_manager.h"
26 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h" 15 #include "chrome/browser/chromeos/login/users/scoped_user_manager_enabler.h"
27 #include "chrome/test/base/testing_browser_process.h" 16 #include "chrome/test/base/testing_browser_process.h"
28 #include "components/prefs/testing_pref_service.h" 17 #include "components/prefs/testing_pref_service.h"
29 #include "components/user_manager/user.h" 18 #include "components/user_manager/user.h"
30 #include "content/public/test/test_browser_thread_bundle.h" 19 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "extensions/common/extension.h" 20 #include "extensions/common/extension.h"
32 #include "extensions/common/extension_builder.h" 21 #include "extensions/common/extension_builder.h"
33 #include "extensions/common/manifest.h" 22 #include "extensions/common/manifest.h"
34 #include "extensions/common/test_util.h" 23 #include "extensions/common/test_util.h"
35 #include "extensions/common/value_builder.h" 24 #include "testing/gtest/include/gtest/gtest.h"
36 #endif
37 25
38 using extensions::FileSystemChooseEntryFunction;
39 using extensions::api::file_system::AcceptOption;
40
41 #if defined(OS_CHROMEOS)
42 using extensions::file_system_api::ConsentProvider; 26 using extensions::file_system_api::ConsentProvider;
43 using file_manager::Volume; 27 using file_manager::Volume;
44 #endif
45 28
46 namespace extensions { 29 namespace extensions {
47 namespace { 30 namespace {
48 31
49 void CheckExtensions(const std::vector<base::FilePath::StringType>& expected,
50 const std::vector<base::FilePath::StringType>& actual) {
51 EXPECT_EQ(expected.size(), actual.size());
52 if (expected.size() != actual.size())
53 return;
54
55 for (size_t i = 0; i < expected.size(); ++i) {
56 EXPECT_EQ(expected[i], actual[i]);
57 }
58 }
59
60 AcceptOption BuildAcceptOption(const std::string& description,
61 const std::string& mime_types,
62 const std::string& extensions) {
63 AcceptOption option;
64
65 if (!description.empty())
66 option.description.reset(new std::string(description));
67
68 if (!mime_types.empty()) {
69 option.mime_types.reset(new std::vector<std::string>(base::SplitString(
70 mime_types, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)));
71 }
72
73 if (!extensions.empty()) {
74 option.extensions.reset(new std::vector<std::string>(base::SplitString(
75 extensions, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)));
76 }
77
78 return option;
79 }
80
81 #if defined(OS_WIN)
82 #define ToStringType base::UTF8ToWide
83 #else
84 #define ToStringType
85 #endif
86
87 #if defined(OS_CHROMEOS)
88 class TestingConsentProviderDelegate 32 class TestingConsentProviderDelegate
89 : public ConsentProvider::DelegateInterface { 33 : public ConsentProvider::DelegateInterface {
90 public: 34 public:
91 TestingConsentProviderDelegate() 35 TestingConsentProviderDelegate()
92 : show_dialog_counter_(0), 36 : show_dialog_counter_(0),
93 show_notification_counter_(0), 37 show_notification_counter_(0),
94 dialog_button_(ui::DIALOG_BUTTON_NONE), 38 dialog_button_(ui::DIALOG_BUTTON_NONE),
95 is_auto_launched_(false) {} 39 is_auto_launched_(false) {}
96 40
97 ~TestingConsentProviderDelegate() {} 41 ~TestingConsentProviderDelegate() {}
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
144 std::string whitelisted_component_id_; 88 std::string whitelisted_component_id_;
145 89
146 DISALLOW_COPY_AND_ASSIGN(TestingConsentProviderDelegate); 90 DISALLOW_COPY_AND_ASSIGN(TestingConsentProviderDelegate);
147 }; 91 };
148 92
149 // Rewrites result of a consent request from |result| to |log|. 93 // Rewrites result of a consent request from |result| to |log|.
150 void OnConsentReceived(ConsentProvider::Consent* log, 94 void OnConsentReceived(ConsentProvider::Consent* log,
151 const ConsentProvider::Consent result) { 95 const ConsentProvider::Consent result) {
152 *log = result; 96 *log = result;
153 } 97 }
154 #endif
155 98
156 } // namespace 99 } // namespace
157 100
158 #if defined(OS_CHROMEOS)
159 class FileSystemApiConsentProviderTest : public testing::Test { 101 class FileSystemApiConsentProviderTest : public testing::Test {
160 public: 102 public:
161 FileSystemApiConsentProviderTest() {} 103 FileSystemApiConsentProviderTest() {}
162 104
163 void SetUp() override { 105 void SetUp() override {
164 testing_pref_service_.reset(new TestingPrefServiceSimple); 106 testing_pref_service_.reset(new TestingPrefServiceSimple);
165 TestingBrowserProcess::GetGlobal()->SetLocalState( 107 TestingBrowserProcess::GetGlobal()->SetLocalState(
166 testing_pref_service_.get()); 108 testing_pref_service_.get());
167 user_manager_ = new chromeos::FakeChromeUserManager; 109 user_manager_ = new chromeos::FakeChromeUserManager;
168 scoped_user_manager_enabler_.reset( 110 scoped_user_manager_enabler_.reset(
169 new chromeos::ScopedUserManagerEnabler(user_manager_)); 111 new chromeos::ScopedUserManagerEnabler(user_manager_));
170 } 112 }
171 113
172 void TearDown() override { 114 void TearDown() override {
173 scoped_user_manager_enabler_.reset(); 115 scoped_user_manager_enabler_.reset();
174 user_manager_ = nullptr; 116 user_manager_ = nullptr;
175 testing_pref_service_.reset(); 117 testing_pref_service_.reset();
176 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr); 118 TestingBrowserProcess::GetGlobal()->SetLocalState(nullptr);
177 } 119 }
178 120
179 protected: 121 protected:
180 base::WeakPtr<Volume> volume_; 122 base::WeakPtr<Volume> volume_;
181 std::unique_ptr<TestingPrefServiceSimple> testing_pref_service_; 123 std::unique_ptr<TestingPrefServiceSimple> testing_pref_service_;
182 chromeos::FakeChromeUserManager* 124 chromeos::FakeChromeUserManager*
183 user_manager_; // Owned by the scope enabler. 125 user_manager_; // Owned by the scope enabler.
184 std::unique_ptr<chromeos::ScopedUserManagerEnabler> 126 std::unique_ptr<chromeos::ScopedUserManagerEnabler>
185 scoped_user_manager_enabler_; 127 scoped_user_manager_enabler_;
186 content::TestBrowserThreadBundle thread_bundle_; 128 content::TestBrowserThreadBundle thread_bundle_;
187 }; 129 };
188 #endif
189 130
190 TEST(FileSystemApiUnitTest, FileSystemChooseEntryFunctionFileTypeInfoTest) {
191 // AcceptsAllTypes is ignored when no other extensions are available.
192 ui::SelectFileDialog::FileTypeInfo file_type_info;
193 bool acceptsAllTypes = false;
194 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
195 base::FilePath::StringType(), NULL, &acceptsAllTypes);
196 EXPECT_TRUE(file_type_info.include_all_files);
197 EXPECT_TRUE(file_type_info.extensions.empty());
198
199 // Test grouping of multiple types.
200 file_type_info = ui::SelectFileDialog::FileTypeInfo();
201 std::vector<AcceptOption> options;
202 options.push_back(BuildAcceptOption(std::string(),
203 "application/x-chrome-extension", "jso"));
204 acceptsAllTypes = false;
205 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
206 base::FilePath::StringType(), &options, &acceptsAllTypes);
207 EXPECT_FALSE(file_type_info.include_all_files);
208 ASSERT_EQ(file_type_info.extensions.size(), (size_t) 1);
209 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty()) <<
210 "No override must be specified for boring accept types";
211 // Note here (and below) that the expectedTypes are sorted, because we use a
212 // set internally to generate the output: thus, the output is sorted.
213 std::vector<base::FilePath::StringType> expectedTypes;
214 expectedTypes.push_back(ToStringType("crx"));
215 expectedTypes.push_back(ToStringType("jso"));
216 CheckExtensions(expectedTypes, file_type_info.extensions[0]);
217
218 // Test that not satisfying the extension will force all types.
219 file_type_info = ui::SelectFileDialog::FileTypeInfo();
220 options.clear();
221 options.push_back(
222 BuildAcceptOption(std::string(), std::string(), "unrelated"));
223 acceptsAllTypes = false;
224 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
225 ToStringType(".jso"), &options, &acceptsAllTypes);
226 EXPECT_TRUE(file_type_info.include_all_files);
227
228 // Test multiple list entries, all containing their own types.
229 file_type_info = ui::SelectFileDialog::FileTypeInfo();
230 options.clear();
231 options.push_back(BuildAcceptOption(std::string(), std::string(), "jso,js"));
232 options.push_back(BuildAcceptOption(std::string(), std::string(), "cpp,cc"));
233 acceptsAllTypes = false;
234 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
235 base::FilePath::StringType(), &options, &acceptsAllTypes);
236 ASSERT_EQ(file_type_info.extensions.size(), options.size());
237
238 expectedTypes.clear();
239 expectedTypes.push_back(ToStringType("js"));
240 expectedTypes.push_back(ToStringType("jso"));
241 CheckExtensions(expectedTypes, file_type_info.extensions[0]);
242
243 expectedTypes.clear();
244 expectedTypes.push_back(ToStringType("cc"));
245 expectedTypes.push_back(ToStringType("cpp"));
246 CheckExtensions(expectedTypes, file_type_info.extensions[1]);
247
248 // Test accept type that causes description override.
249 file_type_info = ui::SelectFileDialog::FileTypeInfo();
250 options.clear();
251 options.push_back(BuildAcceptOption(std::string(), "image/*", "html"));
252 acceptsAllTypes = false;
253 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
254 base::FilePath::StringType(), &options, &acceptsAllTypes);
255 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1);
256 EXPECT_FALSE(file_type_info.extension_description_overrides[0].empty()) <<
257 "Accept type \"image/*\" must generate description override";
258
259 // Test multiple accept types that cause description override causes us to
260 // still present the default.
261 file_type_info = ui::SelectFileDialog::FileTypeInfo();
262 options.clear();
263 options.push_back(BuildAcceptOption(std::string(), "image/*,audio/*,video/*",
264 std::string()));
265 acceptsAllTypes = false;
266 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
267 base::FilePath::StringType(), &options, &acceptsAllTypes);
268 ASSERT_EQ(file_type_info.extension_description_overrides.size(), (size_t) 1);
269 EXPECT_TRUE(file_type_info.extension_description_overrides[0].empty());
270
271 // Test explicit description override.
272 file_type_info = ui::SelectFileDialog::FileTypeInfo();
273 options.clear();
274 options.push_back(
275 BuildAcceptOption("File Types 101", "image/jpeg", std::string()));
276 acceptsAllTypes = false;
277 FileSystemChooseEntryFunction::BuildFileTypeInfo(&file_type_info,
278 base::FilePath::StringType(), &options, &acceptsAllTypes);
279 EXPECT_EQ(file_type_info.extension_description_overrides[0],
280 base::UTF8ToUTF16("File Types 101"));
281 }
282
283 TEST(FileSystemApiUnitTest, FileSystemChooseEntryFunctionSuggestionTest) {
284 std::string opt_name;
285 base::FilePath suggested_name;
286 base::FilePath::StringType suggested_extension;
287
288 opt_name = std::string("normal_path.txt");
289 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name,
290 &suggested_extension);
291 EXPECT_FALSE(suggested_name.IsAbsolute());
292 EXPECT_EQ(suggested_name.MaybeAsASCII(), "normal_path.txt");
293 EXPECT_EQ(suggested_extension, ToStringType("txt"));
294
295 // We should provide just the basename, i.e., "path".
296 opt_name = std::string("/a/bad/path");
297 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name,
298 &suggested_extension);
299 EXPECT_FALSE(suggested_name.IsAbsolute());
300 EXPECT_EQ(suggested_name.MaybeAsASCII(), "path");
301 EXPECT_TRUE(suggested_extension.empty());
302
303 #if !defined(OS_WIN)
304 // TODO(thorogood): Fix this test on Windows.
305 // Filter out absolute paths with no basename.
306 opt_name = std::string("/");
307 FileSystemChooseEntryFunction::BuildSuggestion(&opt_name, &suggested_name,
308 &suggested_extension);
309 EXPECT_FALSE(suggested_name.IsAbsolute());
310 EXPECT_TRUE(suggested_name.MaybeAsASCII().empty());
311 EXPECT_TRUE(suggested_extension.empty());
312 #endif
313 }
314
315 #if defined(OS_CHROMEOS)
316 TEST_F(FileSystemApiConsentProviderTest, ForNonKioskApps) { 131 TEST_F(FileSystemApiConsentProviderTest, ForNonKioskApps) {
317 // Component apps are not granted unless they are whitelisted. 132 // Component apps are not granted unless they are whitelisted.
318 { 133 {
319 scoped_refptr<Extension> component_extension( 134 scoped_refptr<Extension> component_extension(
320 test_util::BuildApp( 135 test_util::BuildApp(
321 std::move(ExtensionBuilder().SetLocation(Manifest::COMPONENT))) 136 std::move(ExtensionBuilder().SetLocation(Manifest::COMPONENT)))
322 .Build()); 137 .Build());
323 TestingConsentProviderDelegate delegate; 138 TestingConsentProviderDelegate delegate;
324 ConsentProvider provider(&delegate); 139 ConsentProvider provider(&delegate);
325 EXPECT_FALSE(provider.IsGrantable(*component_extension)); 140 EXPECT_FALSE(provider.IsGrantable(*component_extension));
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
433 provider.RequestConsent(*manual_launch_kiosk_app.get(), volume_, 248 provider.RequestConsent(*manual_launch_kiosk_app.get(), volume_,
434 true /* writable */, 249 true /* writable */,
435 base::Bind(&OnConsentReceived, &result)); 250 base::Bind(&OnConsentReceived, &result));
436 base::RunLoop().RunUntilIdle(); 251 base::RunLoop().RunUntilIdle();
437 252
438 EXPECT_EQ(1, delegate.show_dialog_counter()); 253 EXPECT_EQ(1, delegate.show_dialog_counter());
439 EXPECT_EQ(0, delegate.show_notification_counter()); 254 EXPECT_EQ(0, delegate.show_notification_counter());
440 EXPECT_EQ(ConsentProvider::CONSENT_REJECTED, result); 255 EXPECT_EQ(ConsentProvider::CONSENT_REJECTED, result);
441 } 256 }
442 } 257 }
443 #endif
444 258
445 } // namespace extensions 259 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698