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

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

Issue 10836199: Clean up media gallery preferences. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unit tets x2 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
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 // MediaGalleriesPreferences unit tests. 5 // MediaGalleriesPreferences unit tests.
6 6
7 #include "chrome/browser/media_gallery/media_galleries_preferences.h" 7 #include "chrome/browser/media_gallery/media_galleries_preferences.h"
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/file_util.h" 10 #include "base/file_util.h"
(...skipping 19 matching lines...) Expand all
30 30
31 class TestMediaGalleriesPreferences : public MediaGalleriesPreferences { 31 class TestMediaGalleriesPreferences : public MediaGalleriesPreferences {
32 public: 32 public:
33 static string16 GetDisplayNameForPath(const FilePath& path) { 33 static string16 GetDisplayNameForPath(const FilePath& path) {
34 return MediaGalleriesPreferences::ComputeDisplayName(path); 34 return MediaGalleriesPreferences::ComputeDisplayName(path);
35 } 35 }
36 }; 36 };
37 37
38 class MediaGalleriesPreferencesTest : public testing::Test { 38 class MediaGalleriesPreferencesTest : public testing::Test {
39 public: 39 public:
40 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet>
41 DeviceIdPrefIdsMap;
42
40 MediaGalleriesPreferencesTest() 43 MediaGalleriesPreferencesTest()
41 : ui_thread_(content::BrowserThread::UI, &loop_), 44 : ui_thread_(content::BrowserThread::UI, &loop_),
42 file_thread_(content::BrowserThread::FILE, &loop_), 45 file_thread_(content::BrowserThread::FILE, &loop_),
43 profile_(new TestingProfile()), 46 profile_(new TestingProfile()),
44 extension_service_(NULL), 47 extension_service_(NULL),
45 default_galleries_count_(0) { 48 default_galleries_count_(0) {
46 } 49 }
47 50
48 virtual ~MediaGalleriesPreferencesTest() { 51 virtual ~MediaGalleriesPreferencesTest() {
49 // TestExtensionSystem uses DeleteSoon, so we need to delete the profile 52 // TestExtensionSystem uses DeleteSoon, so we need to delete the profile
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 void Verify() { 100 void Verify() {
98 const MediaGalleriesPrefInfoMap& known_galleries = 101 const MediaGalleriesPrefInfoMap& known_galleries =
99 gallery_prefs_->known_galleries(); 102 gallery_prefs_->known_galleries();
100 EXPECT_EQ(expected_galleries_.size(), known_galleries.size()); 103 EXPECT_EQ(expected_galleries_.size(), known_galleries.size());
101 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); 104 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
102 it != known_galleries.end(); 105 it != known_galleries.end();
103 ++it) { 106 ++it) {
104 VerifyGalleryInfo(&it->second, it->first); 107 VerifyGalleryInfo(&it->second, it->first);
105 } 108 }
106 109
110 for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin();
111 it != expected_device_map.end();
112 ++it) {
113 MediaGalleryPrefIdSet actual_id_set =
114 gallery_prefs_->LookUpGalleriesByDeviceId(it->first);
115 EXPECT_EQ(it->second, actual_id_set);
116 }
117
107 std::set<MediaGalleryPrefId> galleries_for_all = 118 std::set<MediaGalleryPrefId> galleries_for_all =
108 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get()); 119 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get());
109 EXPECT_EQ(expected_galleries_for_all, galleries_for_all); 120 EXPECT_EQ(expected_galleries_for_all, galleries_for_all);
110 121
111 std::set<MediaGalleryPrefId> galleries_for_regular = 122 std::set<MediaGalleryPrefId> galleries_for_regular =
112 gallery_prefs_->GalleriesForExtension( 123 gallery_prefs_->GalleriesForExtension(
113 *regular_permission_extension.get()); 124 *regular_permission_extension.get());
114 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular); 125 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular);
115 126
116 std::set<MediaGalleryPrefId> galleries_for_no = 127 std::set<MediaGalleryPrefId> galleries_for_no =
(...skipping 20 matching lines...) Expand all
137 uint64 default_galleries_count() { 148 uint64 default_galleries_count() {
138 return default_galleries_count_; 149 return default_galleries_count_;
139 } 150 }
140 151
141 void AddGalleryExpectation(MediaGalleryPrefId id, std::string display_name, 152 void AddGalleryExpectation(MediaGalleryPrefId id, std::string display_name,
142 std::string device_id, FilePath::StringType path, 153 std::string device_id, FilePath::StringType path,
143 MediaGalleryPrefInfo::Type type) { 154 MediaGalleryPrefInfo::Type type) {
144 expected_galleries_[id].pref_id = id; 155 expected_galleries_[id].pref_id = id;
145 expected_galleries_[id].display_name = ASCIIToUTF16(display_name); 156 expected_galleries_[id].display_name = ASCIIToUTF16(display_name);
146 expected_galleries_[id].device_id = device_id; 157 expected_galleries_[id].device_id = device_id;
147 expected_galleries_[id].path = FilePath(path); 158 expected_galleries_[id].path = FilePath(path).NormalizePathSeparators();
148 expected_galleries_[id].type = type; 159 expected_galleries_[id].type = type;
149 160
150 if (type == MediaGalleryPrefInfo::kAutoDetected) 161 if (type == MediaGalleryPrefInfo::kAutoDetected)
151 expected_galleries_for_all.insert(id); 162 expected_galleries_for_all.insert(id);
163
164 expected_device_map[device_id].insert(id);
152 } 165 }
153 166
154 scoped_refptr<extensions::Extension> all_permission_extension; 167 scoped_refptr<extensions::Extension> all_permission_extension;
155 scoped_refptr<extensions::Extension> regular_permission_extension; 168 scoped_refptr<extensions::Extension> regular_permission_extension;
156 scoped_refptr<extensions::Extension> no_permissions_extension; 169 scoped_refptr<extensions::Extension> no_permissions_extension;
157 170
158 std::set<MediaGalleryPrefId> expected_galleries_for_all; 171 std::set<MediaGalleryPrefId> expected_galleries_for_all;
159 std::set<MediaGalleryPrefId> expected_galleries_for_regular; 172 std::set<MediaGalleryPrefId> expected_galleries_for_regular;
160 173
174 DeviceIdPrefIdsMap expected_device_map;
175
161 MediaGalleriesPrefInfoMap expected_galleries_; 176 MediaGalleriesPrefInfoMap expected_galleries_;
162 177
163 private: 178 private:
164 scoped_refptr<extensions::Extension> AddApp( 179 scoped_refptr<extensions::Extension> AddApp(
165 std::string name, 180 std::string name,
166 std::vector<std::string> permissions) { 181 std::vector<std::string> permissions) {
167 scoped_ptr<DictionaryValue> manifest(new DictionaryValue); 182 scoped_ptr<DictionaryValue> manifest(new DictionaryValue);
168 manifest->SetString(extension_manifest_keys::kName, name); 183 manifest->SetString(extension_manifest_keys::kName, name);
169 manifest->SetString(extension_manifest_keys::kVersion, "0.1"); 184 manifest->SetString(extension_manifest_keys::kVersion, "0.1");
170 manifest->SetInteger(extension_manifest_keys::kManifestVersion, 2); 185 manifest->SetInteger(extension_manifest_keys::kManifestVersion, 2);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 301
287 // Remove an auto added gallery (i.e. make it blacklisted). 302 // Remove an auto added gallery (i.e. make it blacklisted).
288 gallery_prefs()->ForgetGalleryById(auto_id); 303 gallery_prefs()->ForgetGalleryById(auto_id);
289 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed; 304 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed;
290 expected_galleries_for_all.erase(auto_id); 305 expected_galleries_for_all.erase(auto_id);
291 Verify(); 306 Verify();
292 307
293 // Remove a user added gallery and it should go away. 308 // Remove a user added gallery and it should go away.
294 gallery_prefs()->ForgetGalleryById(user_added_id); 309 gallery_prefs()->ForgetGalleryById(user_added_id);
295 expected_galleries_.erase(user_added_id); 310 expected_galleries_.erase(user_added_id);
311 expected_device_map[device_id].erase(user_added_id);
296 Verify(); 312 Verify();
297 } 313 }
298 314
299 TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) { 315 TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) {
300 MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, id; 316 MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, id;
301 FilePath path; 317 FilePath path;
302 std::string device_id; 318 std::string device_id;
303 Verify(); 319 Verify();
304 320
305 // Add some galleries to test with. 321 // Add some galleries to test with.
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after
396 // Remove permission for all galleries to the regular permission extension. 412 // Remove permission for all galleries to the regular permission extension.
397 gallery_prefs()->SetGalleryPermissionForExtension( 413 gallery_prefs()->SetGalleryPermissionForExtension(
398 *regular_permission_extension.get(), auto_id, false); 414 *regular_permission_extension.get(), auto_id, false);
399 expected_galleries_for_regular.erase(auto_id); 415 expected_galleries_for_regular.erase(auto_id);
400 Verify(); 416 Verify();
401 417
402 gallery_prefs()->SetGalleryPermissionForExtension( 418 gallery_prefs()->SetGalleryPermissionForExtension(
403 *regular_permission_extension.get(), user_added_id, false); 419 *regular_permission_extension.get(), user_added_id, false);
404 expected_galleries_for_regular.erase(user_added_id); 420 expected_galleries_for_regular.erase(user_added_id);
405 Verify(); 421 Verify();
422
423 // Add permission for an invalid gallery id.
424 gallery_prefs()->SetGalleryPermissionForExtension(
425 *regular_permission_extension.get(), 9999L, true);
426 Verify();
427 }
428
429 TEST_F(MediaGalleriesPreferencesTest, MultipleGalleriesPerDevices) {
430 FilePath path;
431 std::string device_id;
432 Verify();
433
434 // Add a regular gallery
435 path = MakePath("new_user");
436 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(path);
437 MediaGalleryPrefId user_added_id = gallery_prefs()->AddGallery(
438 device_id, ASCIIToUTF16("NewUserGallery"), path, true /*user*/);
439 EXPECT_EQ(default_galleries_count() + 1UL, user_added_id);
440 AddGalleryExpectation(user_added_id, "NewUserGallery", device_id,
441 FILE_PATH_LITERAL("new_user"),
442 MediaGalleryPrefInfo::kUserAdded);
443 Verify();
444
445 // Find it by device id and fail to find something related.
446 MediaGalleryPrefIdSet pref_id_set;
447 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(device_id);
448 EXPECT_EQ(1U, pref_id_set.size());
449 EXPECT_TRUE(pref_id_set.find(user_added_id) != pref_id_set.end());
450
451 device_id = MediaFileSystemRegistry::GetInstance()->GetDeviceIdFromPath(
452 MakePath("new_user/foo"));
453 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(device_id);
454 EXPECT_EQ(0U, pref_id_set.size());
455
456 // Add some galleries on the same device.
457 path = MakePath("path1/on/device1");
458 device_id = "device1";
459 MediaGalleryPrefId dev1_path1_id = gallery_prefs()->AddGallery(
460 device_id, ASCIIToUTF16("Device1Path1"), path, true /*user*/);
461 EXPECT_EQ(default_galleries_count() + 2UL, dev1_path1_id);
462 AddGalleryExpectation(dev1_path1_id, "Device1Path1", device_id,
463 FILE_PATH_LITERAL("path1/on/device1"),
464 MediaGalleryPrefInfo::kUserAdded);
465 Verify();
466
467 path = MakePath("path2/on/device1");
468 MediaGalleryPrefId dev1_path2_id = gallery_prefs()->AddGallery(
469 device_id, ASCIIToUTF16("Device1Path2"), path, true /*user*/);
470 EXPECT_EQ(default_galleries_count() + 3UL, dev1_path2_id);
471 AddGalleryExpectation(dev1_path2_id, "Device1Path2", device_id,
472 FILE_PATH_LITERAL("path2/on/device1"),
473 MediaGalleryPrefInfo::kUserAdded);
474 Verify();
475
476 path = MakePath("path1/on/device2");
477 device_id = "device2";
478 MediaGalleryPrefId dev2_path1_id = gallery_prefs()->AddGallery(
479 device_id, ASCIIToUTF16("Device2Path1"), path, true /*user*/);
480 EXPECT_EQ(default_galleries_count() + 4UL, dev2_path1_id);
481 AddGalleryExpectation(dev2_path1_id, "Device2Path1", device_id,
482 FILE_PATH_LITERAL("path1/on/device2"),
483 MediaGalleryPrefInfo::kUserAdded);
484 Verify();
485
486 path = MakePath("path2/on/device2");
487 MediaGalleryPrefId dev2_path2_id = gallery_prefs()->AddGallery(
488 device_id, ASCIIToUTF16("Device2Path2"), path, true /*user*/);
489 EXPECT_EQ(default_galleries_count() + 5UL, dev2_path2_id);
490 AddGalleryExpectation(dev2_path2_id, "Device2Path2", device_id,
491 FILE_PATH_LITERAL("path2/on/device2"),
492 MediaGalleryPrefInfo::kUserAdded);
493 Verify();
494
495 // Check that adding one of them again works as expected.
496 MediaGalleryPrefId id = gallery_prefs()->AddGallery(
497 device_id, ASCIIToUTF16("Device2Path2"), path, true /*user*/);
498 EXPECT_EQ(dev2_path2_id, id);
499 Verify();
406 } 500 }
407 501
408 } // namespace 502 } // namespace
409 503
410 } // namespace chrome 504 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698