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

Side by Side Diff: chrome/browser/media_gallery/media_galleries_preferences_unittest.cc

Issue 10821077: Add gallery permissions to Media Galleries Preferences (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix tests 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 | Annotate | Revision Log
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 // MediaGalleriesPreferences unit tests.
6
7 #include "chrome/browser/media_gallery/media_galleries_preferences.h"
8
9 #include "base/command_line.h"
10 #include "base/file_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/message_loop.h"
14 #include "base/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/extension_system.h"
18 #include "chrome/browser/extensions/test_extension_system.h"
19 #include "chrome/browser/media_gallery/media_file_system_registry.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/extensions/extension.h"
22 #include "chrome/common/extensions/extension_manifest_constants.h"
23 #include "chrome/test/base/testing_profile.h"
24 #include "content/public/test/test_browser_thread.h"
25 #include "testing/gtest/include/gtest/gtest.h"
26
27 namespace chrome {
28
29 namespace {
30
31 class TestMediaGalleriesPreferences : public MediaGalleriesPreferences {
32 public:
33 static string16 GetDisplayNameForPath(const FilePath& path) {
34 return MediaGalleriesPreferences::ComputeDisplayName(path);
35 }
36 };
37
38 class MediaGalleriesPreferencesTest : public testing::Test {
39 public:
40 MediaGalleriesPreferencesTest()
41 : ui_thread_(content::BrowserThread::UI, &loop_),
42 file_thread_(content::BrowserThread::FILE, &loop_),
43 profile_(new TestingProfile()),
44 extension_service_(NULL),
45 default_galleries_count_(0) {
46 }
47
48 virtual ~MediaGalleriesPreferencesTest() {}
49
50 virtual void SetUp() OVERRIDE {
51 CommandLine::ForCurrentProcess()->AppendSwitch(
52 switches::kEnableMediaGalleryUI);
53 extensions_dir_ = profile_->GetPath().AppendASCII("Extensions");
54 ASSERT_TRUE(file_util::CreateDirectory(extensions_dir_));
55
56 extensions::TestExtensionSystem* extension_system(
57 static_cast<extensions::TestExtensionSystem*>(
58 extensions::ExtensionSystem::Get(profile_.get())));
59 extension_service_ = extension_system->CreateExtensionService(
60 CommandLine::ForCurrentProcess(), extensions_dir_, false);
61
62 MediaGalleriesPreferences::RegisterUserPrefs(profile_->GetPrefs());
63 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
64
65 // Load the default galleries into the expectations.
66 if (gallery_prefs_->known_galleries().size()) {
67 const MediaGalleriesPrefInfoMap& known_galleries =
68 gallery_prefs_->known_galleries();
69 ASSERT_EQ(1U, known_galleries.size());
70 default_galleries_count_ = 1;
71 MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
72 expected_galleries_[it->first] = it->second;
73 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
74 expected_galleries_for_all.insert(it->first);
75 }
76
77 std::vector<std::string> all_permissions;
78 all_permissions.push_back("mediaGalleriesAllGalleries");
79 all_permissions.push_back("mediaGalleriesRead");
80 std::vector<std::string> read_permissions;
81 read_permissions.push_back("mediaGalleriesRead");
82
83 all_permission_extension = AddApp("all", all_permissions);
84 regular_permission_extension = AddApp("regular", read_permissions);
85 no_permissions_extension = AddApp("no", read_permissions);
86 }
87
88 virtual void TearDown() OVERRIDE {
89 Verify();
90 }
91
92 void Verify() {
93 const MediaGalleriesPrefInfoMap& known_galleries =
94 gallery_prefs_->known_galleries();
95 EXPECT_EQ(expected_galleries_.size(), known_galleries.size());
96 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
97 it != known_galleries.end();
98 ++it) {
99 VerifyGalleryInfo(&it->second, it->first);
100 }
101
102 std::set<MediaGalleryPrefId> galleries_for_all =
103 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get());
104 EXPECT_EQ(expected_galleries_for_all, galleries_for_all);
105
106 std::set<MediaGalleryPrefId> galleries_for_regular =
107 gallery_prefs_->GalleriesForExtension(
108 *regular_permission_extension.get());
109 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular);
110
111 std::set<MediaGalleryPrefId> galleries_for_no =
112 gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get());
113 EXPECT_EQ(0U, galleries_for_no.size());
114 }
115
116 void VerifyGalleryInfo(const MediaGalleryPrefInfo* actual,
117 MediaGalleryPrefId expected_id) const {
118 MediaGalleriesPrefInfoMap::const_iterator in_expectation =
119 expected_galleries_.find(expected_id);
120 EXPECT_FALSE(in_expectation == expected_galleries_.end());
121 EXPECT_EQ(in_expectation->second.pref_id, actual->pref_id);
122 EXPECT_EQ(in_expectation->second.display_name, actual->display_name);
123 EXPECT_EQ(in_expectation->second.device_id, actual->device_id);
124 EXPECT_EQ(in_expectation->second.path.value(), actual->path.value());
125 EXPECT_EQ(in_expectation->second.type, actual->type);
126 }
127
128 MediaGalleriesPreferences* gallery_prefs() {
129 return gallery_prefs_.get();
130 }
131
132 uint64 default_galleries_count() {
133 return default_galleries_count_;
134 }
135
136 void AddGalleryExpectation(MediaGalleryPrefId id, std::string display_name,
137 std::string device_id, FilePath::StringType path,
138 MediaGalleryPrefInfo::Type type) {
139 expected_galleries_[id].pref_id = id;
140 expected_galleries_[id].display_name = ASCIIToUTF16(display_name);
141 expected_galleries_[id].device_id = device_id;
142 expected_galleries_[id].path = FilePath(path);
143 expected_galleries_[id].type = type;
144
145 if (type == MediaGalleryPrefInfo::kAutoDetected)
146 expected_galleries_for_all.insert(id);
147 }
148
149 scoped_refptr<extensions::Extension> all_permission_extension;
150 scoped_refptr<extensions::Extension> regular_permission_extension;
151 scoped_refptr<extensions::Extension> no_permissions_extension;
152
153 std::set<MediaGalleryPrefId> expected_galleries_for_all;
154 std::set<MediaGalleryPrefId> expected_galleries_for_regular;
155
156 MediaGalleriesPrefInfoMap expected_galleries_;
157
158 private:
159 scoped_refptr<extensions::Extension> AddApp(
160 std::string name,
161 std::vector<std::string> permissions) {
162 scoped_ptr<DictionaryValue> manifest(new DictionaryValue);
163 manifest->SetString(extension_manifest_keys::kName, name);
164 manifest->SetString(extension_manifest_keys::kVersion, "0.1");
165 manifest->SetInteger(extension_manifest_keys::kManifestVersion, 2);
166 ListValue* background_script_list = new ListValue;;
167 background_script_list->Append(Value::CreateStringValue("background.js"));
168 manifest->Set(extension_manifest_keys::kPlatformAppBackgroundScripts,
169 background_script_list);
170 ListValue* permission_list = new ListValue;;
171 for (size_t i = 0; i < permissions.size(); i++)
172 permission_list->Append(Value::CreateStringValue(permissions[i]));
173 manifest->Set(extension_manifest_keys::kPermissions, permission_list);
174
175 FilePath path = extensions_dir_.AppendASCII(name);
176 std::string errors;
177 scoped_refptr<extensions::Extension> extension =
178 extensions::Extension::Create(path,
179 extensions::Extension::INTERNAL,
180 *manifest.get(),
181 extensions::Extension::NO_FLAGS,
182 &errors);
183 EXPECT_TRUE(extension.get() != NULL) << errors;
184 EXPECT_TRUE(extensions::Extension::IdIsValid(extension->id()));
185 if (!extension.get() ||
186 !extensions::Extension::IdIsValid(extension->id())) {
187 return NULL;
188 }
189
190 extension_service_->extension_prefs()->OnExtensionInstalled(
191 extension, extensions::Extension::ENABLED, false,
192 StringOrdinal::CreateInitialOrdinal());
193
194 return extension;
195 }
196
197 // Needed for extension service & friends to work.
198 MessageLoop loop_;
199 content::TestBrowserThread ui_thread_;
200 content::TestBrowserThread file_thread_;
201
202 scoped_ptr<TestingProfile> profile_;
203 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_;
204 FilePath extensions_dir_;
205 ExtensionService* extension_service_;
206
207 uint64 default_galleries_count_;
208
209 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest);
210 };
211
212 FilePath MakePath(std::string dir) {
213 #if defined(OS_WIN)
214 return FilePath(FILE_PATH_LITERAL("C:\\")).Append(UTF8ToWide(dir));
215 #elif defined(OS_POSIX)
216 return FilePath(FILE_PATH_LITERAL("/")).Append(dir);
217 #else
218 NOTREACHED();
219 #endif
220 }
221
222 TEST_F(MediaGalleriesPreferencesTest, GalleryManagement) {
223 MediaGalleryPrefId auto_id, user_added_id, id;
224 FilePath path;
225 std::string device_id;
226 Verify();
227
228 // Add a new auto detected gallery.
229 path = MakePath("new_auto");
230 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
231 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewAutoGallery"),
232 path, false /*auto*/);
233 EXPECT_EQ(default_galleries_count() + 1UL, id);
234 auto_id = id;
235 AddGalleryExpectation(id, "NewAutoGallery", device_id,
236 FILE_PATH_LITERAL("new_auto"),
237 MediaGalleryPrefInfo::kAutoDetected);
238 Verify();
239
240 // Add it again (as user), nothing should happen.
241 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewAutoGallery"),
242 path, true /*user*/);
243 EXPECT_EQ(auto_id, id);
244 Verify();
245
246 // Add a new user added gallery.
247 path = MakePath("new_user");
248 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
249 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewUserGallery"),
250 path, true /*user*/);
251 EXPECT_EQ(default_galleries_count() + 2UL, id);
252 user_added_id = id;
253 AddGalleryExpectation(id, "NewUserGallery", device_id,
254 FILE_PATH_LITERAL("new_user"),
255 MediaGalleryPrefInfo::kUserAdded);
256 Verify();
257
258 // Lookup some galleries.
259 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), NULL));
260 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), NULL));
261 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(MakePath("other"), NULL));
262
263 // Check that we always get the gallery info.
264 MediaGalleryPrefInfo gallery_info;
265 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"),
266 &gallery_info));
267 VerifyGalleryInfo(&gallery_info, auto_id);
268 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"),
269 &gallery_info));
270 VerifyGalleryInfo(&gallery_info, user_added_id);
271
272 path = MakePath("other");
273 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info));
274 EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id);
275 EXPECT_EQ(TestMediaGalleriesPreferences::GetDisplayNameForPath(path),
276 gallery_info.display_name);
277 EXPECT_EQ(MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path),
278 gallery_info.device_id);
279 EXPECT_EQ(FilePath(FILE_PATH_LITERAL("other")).value(),
280 gallery_info.path.value());
281
282 // Remove an auto added gallery (i.e. make it blacklisted).
283 gallery_prefs()->ForgetGalleryById(auto_id);
284 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
285 expected_galleries_for_all.erase(auto_id);
286 Verify();
287
288 // Remove a user added gallery and it should go away.
289 gallery_prefs()->ForgetGalleryById(user_added_id);
290 expected_galleries_.erase(user_added_id);
291 Verify();
292 }
293
294 TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) {
295 MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, id;
296 FilePath path;
297 std::string device_id;
298 Verify();
299
300 // Add some galleries to test with.
301 path = MakePath("new_user");
302 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
303 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewUserGallery"),
304 path, true /*user*/);
305 EXPECT_EQ(default_galleries_count() + 1UL, id);
306 user_added_id = id;
307 AddGalleryExpectation(id, "NewUserGallery", device_id,
308 FILE_PATH_LITERAL("new_user"),
309 MediaGalleryPrefInfo::kUserAdded);
310 Verify();
311
312 path = MakePath("new_auto");
313 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
314 id = gallery_prefs()->AddGallery(device_id, ASCIIToUTF16("NewAutoGallery"),
315 path, false /*auto*/);
316 EXPECT_EQ(default_galleries_count() + 2UL, id);
317 auto_id = id;
318 AddGalleryExpectation(id, "NewAutoGallery", device_id,
319 FILE_PATH_LITERAL("new_auto"),
320 MediaGalleryPrefInfo::kAutoDetected);
321 Verify();
322
323 path = MakePath("to_blacklist");
324 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
325 id = gallery_prefs()->AddGallery(device_id,
326 ASCIIToUTF16("ToBlacklistGallery"), path,
327 false /*auto*/);
328 EXPECT_EQ(default_galleries_count() + 3UL, id);
329 to_blacklist_id = id;
330 AddGalleryExpectation(id, "ToBlacklistGallery", device_id,
331 FILE_PATH_LITERAL("to_blacklist"),
332 MediaGalleryPrefInfo::kAutoDetected);
333 Verify();
334
335 // Remove permission for all galleries from the all-permission extension.
336 gallery_prefs()->SetGalleryPermissionForExtension(
337 *all_permission_extension.get(), auto_id, false);
338 expected_galleries_for_all.erase(auto_id);
339 Verify();
340
341 gallery_prefs()->SetGalleryPermissionForExtension(
342 *all_permission_extension.get(), user_added_id, false);
343 expected_galleries_for_all.erase(user_added_id);
344 Verify();
345
346 gallery_prefs()->SetGalleryPermissionForExtension(
347 *all_permission_extension.get(), to_blacklist_id, false);
348 expected_galleries_for_all.erase(to_blacklist_id);
349 Verify();
350
351 // Add permission back for all galleries to the all-permission extension.
352 gallery_prefs()->SetGalleryPermissionForExtension(
353 *all_permission_extension.get(), auto_id, true);
354 expected_galleries_for_all.insert(auto_id);
355 Verify();
356
357 gallery_prefs()->SetGalleryPermissionForExtension(
358 *all_permission_extension.get(), user_added_id, true);
359 expected_galleries_for_all.insert(user_added_id);
360 Verify();
361
362 gallery_prefs()->SetGalleryPermissionForExtension(
363 *all_permission_extension.get(), to_blacklist_id, true);
364 expected_galleries_for_all.insert(to_blacklist_id);
365 Verify();
366
367 // Add permission for all galleries to the regular permission extension.
368 gallery_prefs()->SetGalleryPermissionForExtension(
369 *regular_permission_extension.get(), auto_id, true);
370 expected_galleries_for_regular.insert(auto_id);
371 Verify();
372
373 gallery_prefs()->SetGalleryPermissionForExtension(
374 *regular_permission_extension.get(), user_added_id, true);
375 expected_galleries_for_regular.insert(user_added_id);
376 Verify();
377
378 gallery_prefs()->SetGalleryPermissionForExtension(
379 *regular_permission_extension.get(), to_blacklist_id, true);
380 expected_galleries_for_regular.insert(to_blacklist_id);
381 Verify();
382
383 // Blacklist the to be black listed gallery
384 gallery_prefs()->ForgetGalleryById(to_blacklist_id);
385 expected_galleries_[to_blacklist_id].type =
386 MediaGalleryPrefInfo::kBlackListed;
387 expected_galleries_for_all.erase(to_blacklist_id);
388 expected_galleries_for_regular.erase(to_blacklist_id);
389 Verify();
390
391 // Remove permission for all galleries to the regular permission extension.
392 gallery_prefs()->SetGalleryPermissionForExtension(
393 *regular_permission_extension.get(), auto_id, false);
394 expected_galleries_for_regular.erase(auto_id);
395 Verify();
396
397 gallery_prefs()->SetGalleryPermissionForExtension(
398 *regular_permission_extension.get(), user_added_id, false);
399 expected_galleries_for_regular.erase(user_added_id);
400 Verify();
401 }
402
403 } // namespace
404
405 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698