| OLD | NEW |
| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 if (device_id) | 42 if (device_id) |
| 43 *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); | 43 *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); |
| 44 if (device_name) | 44 if (device_name) |
| 45 *device_name = path.BaseName().LossyDisplayName(); | 45 *device_name = path.BaseName().LossyDisplayName(); |
| 46 if (relative_path) | 46 if (relative_path) |
| 47 *relative_path = FilePath(); | 47 *relative_path = FilePath(); |
| 48 return true; | 48 return true; |
| 49 } | 49 } |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 class MockGalleryChangeObserver |
| 53 : public MediaGalleriesPreferences::GalleryChangeObserver { |
| 54 public: |
| 55 explicit MockGalleryChangeObserver(MediaGalleriesPreferences* pref) |
| 56 : pref_(pref), |
| 57 notifications_(0) {} |
| 58 virtual ~MockGalleryChangeObserver() {} |
| 59 |
| 60 int notifications() const { return notifications_;} |
| 61 |
| 62 private: |
| 63 // MediaGalleriesPreferences::GalleryChangeObserver implementation. |
| 64 virtual void OnGalleryChanged(MediaGalleriesPreferences* pref) OVERRIDE { |
| 65 EXPECT_EQ(pref_, pref); |
| 66 ++notifications_; |
| 67 } |
| 68 |
| 69 MediaGalleriesPreferences* pref_; |
| 70 int notifications_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver); |
| 73 }; |
| 74 |
| 52 class MediaGalleriesPreferencesTest : public testing::Test { | 75 class MediaGalleriesPreferencesTest : public testing::Test { |
| 53 public: | 76 public: |
| 54 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet> | 77 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet> |
| 55 DeviceIdPrefIdsMap; | 78 DeviceIdPrefIdsMap; |
| 56 | 79 |
| 57 MediaGalleriesPreferencesTest() | 80 MediaGalleriesPreferencesTest() |
| 58 : ui_thread_(content::BrowserThread::UI, &loop_), | 81 : ui_thread_(content::BrowserThread::UI, &loop_), |
| 59 file_thread_(content::BrowserThread::FILE, &loop_), | 82 file_thread_(content::BrowserThread::FILE, &loop_), |
| 60 profile_(new TestingProfile()), | 83 profile_(new TestingProfile()), |
| 61 default_galleries_count_(0) { | 84 default_galleries_count_(0) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 Verify(); | 133 Verify(); |
| 111 } | 134 } |
| 112 | 135 |
| 113 void Verify() { | 136 void Verify() { |
| 114 const MediaGalleriesPrefInfoMap& known_galleries = | 137 const MediaGalleriesPrefInfoMap& known_galleries = |
| 115 gallery_prefs_->known_galleries(); | 138 gallery_prefs_->known_galleries(); |
| 116 EXPECT_EQ(expected_galleries_.size(), known_galleries.size()); | 139 EXPECT_EQ(expected_galleries_.size(), known_galleries.size()); |
| 117 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); | 140 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); |
| 118 it != known_galleries.end(); | 141 it != known_galleries.end(); |
| 119 ++it) { | 142 ++it) { |
| 120 VerifyGalleryInfo(&it->second, it->first); | 143 VerifyGalleryInfo(it->second, it->first); |
| 121 } | 144 } |
| 122 | 145 |
| 123 for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin(); | 146 for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin(); |
| 124 it != expected_device_map.end(); | 147 it != expected_device_map.end(); |
| 125 ++it) { | 148 ++it) { |
| 126 MediaGalleryPrefIdSet actual_id_set = | 149 MediaGalleryPrefIdSet actual_id_set = |
| 127 gallery_prefs_->LookUpGalleriesByDeviceId(it->first); | 150 gallery_prefs_->LookUpGalleriesByDeviceId(it->first); |
| 128 EXPECT_EQ(it->second, actual_id_set); | 151 EXPECT_EQ(it->second, actual_id_set); |
| 129 } | 152 } |
| 130 | 153 |
| 131 std::set<MediaGalleryPrefId> galleries_for_all = | 154 std::set<MediaGalleryPrefId> galleries_for_all = |
| 132 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get()); | 155 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get()); |
| 133 EXPECT_EQ(expected_galleries_for_all, galleries_for_all); | 156 EXPECT_EQ(expected_galleries_for_all, galleries_for_all); |
| 134 | 157 |
| 135 std::set<MediaGalleryPrefId> galleries_for_regular = | 158 std::set<MediaGalleryPrefId> galleries_for_regular = |
| 136 gallery_prefs_->GalleriesForExtension( | 159 gallery_prefs_->GalleriesForExtension( |
| 137 *regular_permission_extension.get()); | 160 *regular_permission_extension.get()); |
| 138 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular); | 161 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular); |
| 139 | 162 |
| 140 std::set<MediaGalleryPrefId> galleries_for_no = | 163 std::set<MediaGalleryPrefId> galleries_for_no = |
| 141 gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get()); | 164 gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get()); |
| 142 EXPECT_EQ(0U, galleries_for_no.size()); | 165 EXPECT_EQ(0U, galleries_for_no.size()); |
| 143 } | 166 } |
| 144 | 167 |
| 145 void VerifyGalleryInfo(const MediaGalleryPrefInfo* actual, | 168 void VerifyGalleryInfo(const MediaGalleryPrefInfo& actual, |
| 146 MediaGalleryPrefId expected_id) const { | 169 MediaGalleryPrefId expected_id) const { |
| 147 MediaGalleriesPrefInfoMap::const_iterator in_expectation = | 170 MediaGalleriesPrefInfoMap::const_iterator in_expectation = |
| 148 expected_galleries_.find(expected_id); | 171 expected_galleries_.find(expected_id); |
| 149 EXPECT_FALSE(in_expectation == expected_galleries_.end()); | 172 ASSERT_FALSE(in_expectation == expected_galleries_.end()) << expected_id; |
| 150 EXPECT_EQ(in_expectation->second.pref_id, actual->pref_id); | 173 EXPECT_EQ(in_expectation->second.pref_id, actual.pref_id); |
| 151 EXPECT_EQ(in_expectation->second.display_name, actual->display_name); | 174 EXPECT_EQ(in_expectation->second.display_name, actual.display_name); |
| 152 EXPECT_EQ(in_expectation->second.device_id, actual->device_id); | 175 EXPECT_EQ(in_expectation->second.device_id, actual.device_id); |
| 153 EXPECT_EQ(in_expectation->second.path.value(), actual->path.value()); | 176 EXPECT_EQ(in_expectation->second.path.value(), actual.path.value()); |
| 154 EXPECT_EQ(in_expectation->second.type, actual->type); | 177 EXPECT_EQ(in_expectation->second.type, actual.type); |
| 155 } | 178 } |
| 156 | 179 |
| 157 MediaGalleriesPreferences* gallery_prefs() { | 180 MediaGalleriesPreferences* gallery_prefs() { |
| 158 return gallery_prefs_.get(); | 181 return gallery_prefs_.get(); |
| 159 } | 182 } |
| 160 | 183 |
| 161 uint64 default_galleries_count() { | 184 uint64 default_galleries_count() { |
| 162 return default_galleries_count_; | 185 return default_galleries_count_; |
| 163 } | 186 } |
| 164 | 187 |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 | 277 |
| 255 // Lookup some galleries. | 278 // Lookup some galleries. |
| 256 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), NULL)); | 279 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), NULL)); |
| 257 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), NULL)); | 280 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), NULL)); |
| 258 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(MakePath("other"), NULL)); | 281 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(MakePath("other"), NULL)); |
| 259 | 282 |
| 260 // Check that we always get the gallery info. | 283 // Check that we always get the gallery info. |
| 261 MediaGalleryPrefInfo gallery_info; | 284 MediaGalleryPrefInfo gallery_info; |
| 262 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), | 285 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), |
| 263 &gallery_info)); | 286 &gallery_info)); |
| 264 VerifyGalleryInfo(&gallery_info, auto_id); | 287 VerifyGalleryInfo(gallery_info, auto_id); |
| 265 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), | 288 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), |
| 266 &gallery_info)); | 289 &gallery_info)); |
| 267 VerifyGalleryInfo(&gallery_info, user_added_id); | 290 VerifyGalleryInfo(gallery_info, user_added_id); |
| 268 | 291 |
| 269 path = MakePath("other"); | 292 path = MakePath("other"); |
| 270 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info)); | 293 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info)); |
| 271 EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id); | 294 EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id); |
| 272 EXPECT_EQ(path.BaseName().LossyDisplayName(), gallery_info.display_name); | 295 EXPECT_EQ(path.BaseName().LossyDisplayName(), gallery_info.display_name); |
| 273 std::string other_device_id; | 296 std::string other_device_id; |
| 274 MediaStorageUtil::GetDeviceInfoFromPath(path, &other_device_id, NULL, | 297 MediaStorageUtil::GetDeviceInfoFromPath(path, &other_device_id, NULL, |
| 275 &relative_path); | 298 &relative_path); |
| 276 EXPECT_EQ(other_device_id, gallery_info.device_id); | 299 EXPECT_EQ(other_device_id, gallery_info.device_id); |
| 277 EXPECT_EQ(relative_path.value(), gallery_info.path.value()); | 300 EXPECT_EQ(relative_path.value(), gallery_info.path.value()); |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 552 MediaGalleryPrefInfo::kUserAdded); | 575 MediaGalleryPrefInfo::kUserAdded); |
| 553 Verify(); | 576 Verify(); |
| 554 | 577 |
| 555 // Check that adding one of them again works as expected. | 578 // Check that adding one of them again works as expected. |
| 556 MediaGalleryPrefId id = gallery_prefs()->AddGallery( | 579 MediaGalleryPrefId id = gallery_prefs()->AddGallery( |
| 557 device_id, device_name, relative_path, true /*user*/); | 580 device_id, device_name, relative_path, true /*user*/); |
| 558 EXPECT_EQ(dev2_path2_id, id); | 581 EXPECT_EQ(dev2_path2_id, id); |
| 559 Verify(); | 582 Verify(); |
| 560 } | 583 } |
| 561 | 584 |
| 585 TEST_F(MediaGalleriesPreferencesTest, GalleryChangeObserver) { |
| 586 // Start with one observer. |
| 587 MockGalleryChangeObserver observer1(gallery_prefs()); |
| 588 gallery_prefs()->AddGalleryChangeObserver(&observer1); |
| 589 |
| 590 // Add a new auto detected gallery. |
| 591 string16 device_name = ASCIIToUTF16("NewAutoGallery"); |
| 592 FilePath path = MakePath("new_auto"); |
| 593 std::string device_id; |
| 594 FilePath relative_path; |
| 595 MediaStorageUtil::GetDeviceInfoFromPath(path, &device_id, NULL, |
| 596 &relative_path); |
| 597 MediaGalleryPrefId auto_id = |
| 598 gallery_prefs()->AddGallery(device_id, device_name, relative_path, |
| 599 false /*auto*/); |
| 600 EXPECT_EQ(default_galleries_count() + 1UL, auto_id); |
| 601 AddGalleryExpectation(auto_id, device_name, device_id, relative_path, |
| 602 MediaGalleryPrefInfo::kAutoDetected); |
| 603 EXPECT_EQ(1, observer1.notifications()); |
| 604 |
| 605 // Add a second observer. |
| 606 MockGalleryChangeObserver observer2(gallery_prefs()); |
| 607 gallery_prefs()->AddGalleryChangeObserver(&observer2); |
| 608 |
| 609 // Add a new user added gallery. |
| 610 path = MakePath("new_user"); |
| 611 MediaStorageUtil::GetDeviceInfoFromPath(path, &device_id, NULL, |
| 612 &relative_path); |
| 613 device_name = ASCIIToUTF16("NewUserGallery"); |
| 614 MediaGalleryPrefId user_added_id = |
| 615 gallery_prefs()->AddGallery(device_id, device_name, relative_path, |
| 616 true /*user*/); |
| 617 AddGalleryExpectation(user_added_id, device_name, device_id, relative_path, |
| 618 MediaGalleryPrefInfo::kUserAdded); |
| 619 EXPECT_EQ(default_galleries_count() + 2UL, user_added_id); |
| 620 EXPECT_EQ(2, observer1.notifications()); |
| 621 EXPECT_EQ(1, observer2.notifications()); |
| 622 |
| 623 // Remove the first observer. |
| 624 gallery_prefs()->RemoveGalleryChangeObserver(&observer1); |
| 625 |
| 626 // Remove an auto added gallery (i.e. make it blacklisted). |
| 627 gallery_prefs()->ForgetGalleryById(auto_id); |
| 628 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed; |
| 629 expected_galleries_for_all.erase(auto_id); |
| 630 |
| 631 EXPECT_EQ(2, observer1.notifications()); |
| 632 EXPECT_EQ(2, observer2.notifications()); |
| 633 |
| 634 // Remove a user added gallery and it should go away. |
| 635 gallery_prefs()->ForgetGalleryById(user_added_id); |
| 636 expected_galleries_.erase(user_added_id); |
| 637 expected_device_map[device_id].erase(user_added_id); |
| 638 |
| 639 EXPECT_EQ(2, observer1.notifications()); |
| 640 EXPECT_EQ(3, observer2.notifications()); |
| 641 } |
| 642 |
| 562 } // namespace | 643 } // namespace |
| 563 | 644 |
| 564 } // namespace chrome | 645 } // namespace chrome |
| OLD | NEW |