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_galleries/media_galleries_preferences.h" 8 9 #include "base/command_line.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/run_loop.h" 13 #include "base/strings/utf_string_conversions.h" 14 #include "base/values.h" 15 #include "chrome/browser/extensions/extension_system.h" 16 #include "chrome/browser/extensions/test_extension_system.h" 17 #include "chrome/browser/media_galleries/media_file_system_registry.h" 18 #include "chrome/browser/media_galleries/media_galleries_test_util.h" 19 #include "chrome/browser/storage_monitor/media_storage_util.h" 20 #include "chrome/browser/storage_monitor/storage_monitor.h" 21 #include "chrome/browser/storage_monitor/test_storage_monitor.h" 22 #include "chrome/test/base/testing_profile.h" 23 #include "content/public/test/test_browser_thread_bundle.h" 24 #include "extensions/common/extension.h" 25 #include "extensions/common/manifest_handlers/background_info.h" 26 #include "grit/generated_resources.h" 27 #include "sync/api/string_ordinal.h" 28 #include "testing/gtest/include/gtest/gtest.h" 29 #include "ui/base/l10n/l10n_util.h" 30 31 #if defined(OS_CHROMEOS) 32 #include "chrome/browser/chromeos/login/user_manager.h" 33 #include "chrome/browser/chromeos/settings/cros_settings.h" 34 #include "chrome/browser/chromeos/settings/device_settings_service.h" 35 #endif 36 37 namespace { 38 39 class MockGalleryChangeObserver 40 : public MediaGalleriesPreferences::GalleryChangeObserver { 41 public: 42 explicit MockGalleryChangeObserver(MediaGalleriesPreferences* pref) 43 : pref_(pref), 44 notifications_(0) {} 45 virtual ~MockGalleryChangeObserver() {} 46 47 int notifications() const { return notifications_;} 48 49 private: 50 // MediaGalleriesPreferences::GalleryChangeObserver implementation. 51 virtual void OnPermissionAdded(MediaGalleriesPreferences* pref, 52 const std::string& extension_id, 53 MediaGalleryPrefId pref_id) OVERRIDE { 54 EXPECT_EQ(pref_, pref); 55 ++notifications_; 56 } 57 58 virtual void OnPermissionRemoved(MediaGalleriesPreferences* pref, 59 const std::string& extension_id, 60 MediaGalleryPrefId pref_id) OVERRIDE { 61 EXPECT_EQ(pref_, pref); 62 ++notifications_; 63 } 64 65 virtual void OnGalleryAdded(MediaGalleriesPreferences* pref, 66 MediaGalleryPrefId pref_id) OVERRIDE { 67 EXPECT_EQ(pref_, pref); 68 ++notifications_; 69 } 70 71 virtual void OnGalleryRemoved(MediaGalleriesPreferences* pref, 72 MediaGalleryPrefId pref_id) OVERRIDE { 73 EXPECT_EQ(pref_, pref); 74 ++notifications_; 75 } 76 77 virtual void OnGalleryInfoUpdated(MediaGalleriesPreferences* pref, 78 MediaGalleryPrefId pref_id) OVERRIDE { 79 EXPECT_EQ(pref_, pref); 80 ++notifications_; 81 } 82 83 MediaGalleriesPreferences* pref_; 84 int notifications_; 85 86 DISALLOW_COPY_AND_ASSIGN(MockGalleryChangeObserver); 87 }; 88 89 } // namespace 90 91 class MediaGalleriesPreferencesTest : public testing::Test { 92 public: 93 typedef std::map<std::string /*device id*/, MediaGalleryPrefIdSet> 94 DeviceIdPrefIdsMap; 95 96 MediaGalleriesPreferencesTest() 97 : profile_(new TestingProfile()), 98 default_galleries_count_(0) { 99 } 100 101 virtual ~MediaGalleriesPreferencesTest() { 102 } 103 104 virtual void SetUp() OVERRIDE { 105 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall()); 106 107 extensions::TestExtensionSystem* extension_system( 108 static_cast<extensions::TestExtensionSystem*>( 109 extensions::ExtensionSystem::Get(profile_.get()))); 110 extension_system->CreateExtensionService( 111 CommandLine::ForCurrentProcess(), base::FilePath(), false); 112 113 gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get())); 114 base::RunLoop loop; 115 gallery_prefs_->EnsureInitialized(loop.QuitClosure()); 116 loop.Run(); 117 118 // Load the default galleries into the expectations. 119 const MediaGalleriesPrefInfoMap& known_galleries = 120 gallery_prefs_->known_galleries(); 121 if (known_galleries.size()) { 122 ASSERT_EQ(3U, known_galleries.size()); 123 default_galleries_count_ = 3; 124 MediaGalleriesPrefInfoMap::const_iterator it; 125 for (it = known_galleries.begin(); it != known_galleries.end(); ++it) { 126 expected_galleries_[it->first] = it->second; 127 if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) 128 expected_galleries_for_all.insert(it->first); 129 } 130 } 131 132 std::vector<std::string> all_permissions; 133 all_permissions.push_back("allAutoDetected"); 134 all_permissions.push_back("read"); 135 std::vector<std::string> read_permissions; 136 read_permissions.push_back("read"); 137 138 all_permission_extension = 139 AddMediaGalleriesApp("all", all_permissions, profile_.get()); 140 regular_permission_extension = 141 AddMediaGalleriesApp("regular", read_permissions, profile_.get()); 142 no_permissions_extension = 143 AddMediaGalleriesApp("no", read_permissions, profile_.get()); 144 } 145 146 virtual void TearDown() OVERRIDE { 147 Verify(); 148 TestStorageMonitor::RemoveSingleton(); 149 } 150 151 void Verify() { 152 const MediaGalleriesPrefInfoMap& known_galleries = 153 gallery_prefs_->known_galleries(); 154 EXPECT_EQ(expected_galleries_.size(), known_galleries.size()); 155 for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin(); 156 it != known_galleries.end(); 157 ++it) { 158 VerifyGalleryInfo(it->second, it->first); 159 } 160 161 for (DeviceIdPrefIdsMap::const_iterator it = expected_device_map.begin(); 162 it != expected_device_map.end(); 163 ++it) { 164 MediaGalleryPrefIdSet actual_id_set = 165 gallery_prefs_->LookUpGalleriesByDeviceId(it->first); 166 EXPECT_EQ(it->second, actual_id_set); 167 } 168 169 std::set<MediaGalleryPrefId> galleries_for_all = 170 gallery_prefs_->GalleriesForExtension(*all_permission_extension.get()); 171 EXPECT_EQ(expected_galleries_for_all, galleries_for_all); 172 173 std::set<MediaGalleryPrefId> galleries_for_regular = 174 gallery_prefs_->GalleriesForExtension( 175 *regular_permission_extension.get()); 176 EXPECT_EQ(expected_galleries_for_regular, galleries_for_regular); 177 178 std::set<MediaGalleryPrefId> galleries_for_no = 179 gallery_prefs_->GalleriesForExtension(*no_permissions_extension.get()); 180 EXPECT_EQ(0U, galleries_for_no.size()); 181 } 182 183 void VerifyGalleryInfo(const MediaGalleryPrefInfo& actual, 184 MediaGalleryPrefId expected_id) const { 185 MediaGalleriesPrefInfoMap::const_iterator in_expectation = 186 expected_galleries_.find(expected_id); 187 ASSERT_FALSE(in_expectation == expected_galleries_.end()) << expected_id; 188 EXPECT_EQ(in_expectation->second.pref_id, actual.pref_id); 189 EXPECT_EQ(in_expectation->second.display_name, actual.display_name); 190 EXPECT_EQ(in_expectation->second.device_id, actual.device_id); 191 EXPECT_EQ(in_expectation->second.path.value(), actual.path.value()); 192 EXPECT_EQ(in_expectation->second.type, actual.type); 193 } 194 195 MediaGalleriesPreferences* gallery_prefs() { 196 return gallery_prefs_.get(); 197 } 198 199 uint64 default_galleries_count() { 200 return default_galleries_count_; 201 } 202 203 void AddGalleryExpectation(MediaGalleryPrefId id, base::string16 display_name, 204 std::string device_id, 205 base::FilePath relative_path, 206 MediaGalleryPrefInfo::Type type) { 207 expected_galleries_[id].pref_id = id; 208 expected_galleries_[id].display_name = display_name; 209 expected_galleries_[id].device_id = device_id; 210 expected_galleries_[id].path = relative_path.NormalizePathSeparators(); 211 expected_galleries_[id].type = type; 212 213 if (type == MediaGalleryPrefInfo::kAutoDetected) 214 expected_galleries_for_all.insert(id); 215 216 expected_device_map[device_id].insert(id); 217 } 218 219 MediaGalleryPrefId AddGalleryWithNameV0(const std::string& device_id, 220 const base::string16& display_name, 221 const base::FilePath& relative_path, 222 bool user_added) { 223 return gallery_prefs()->AddGalleryInternal( 224 device_id, display_name, relative_path, user_added, 225 base::string16(), base::string16(), base::string16(), 0, base::Time(), 226 false, 0); 227 } 228 229 MediaGalleryPrefId AddGalleryWithNameV1(const std::string& device_id, 230 const base::string16& display_name, 231 const base::FilePath& relative_path, 232 bool user_added) { 233 return gallery_prefs()->AddGalleryInternal( 234 device_id, display_name, relative_path, user_added, 235 base::string16(), base::string16(), base::string16(), 0, base::Time(), 236 false, 1); 237 } 238 239 MediaGalleryPrefId AddGalleryWithNameV2(const std::string& device_id, 240 const base::string16& display_name, 241 const base::FilePath& relative_path, 242 bool user_added) { 243 return gallery_prefs()->AddGalleryInternal( 244 device_id, display_name, relative_path, user_added, 245 base::string16(), base::string16(), base::string16(), 0, base::Time(), 246 false, 2); 247 } 248 249 bool UpdateDeviceIDForSingletonType(const std::string& device_id) { 250 return gallery_prefs()->UpdateDeviceIDForSingletonType(device_id); 251 } 252 253 scoped_refptr<extensions::Extension> all_permission_extension; 254 scoped_refptr<extensions::Extension> regular_permission_extension; 255 scoped_refptr<extensions::Extension> no_permissions_extension; 256 257 std::set<MediaGalleryPrefId> expected_galleries_for_all; 258 std::set<MediaGalleryPrefId> expected_galleries_for_regular; 259 260 DeviceIdPrefIdsMap expected_device_map; 261 262 MediaGalleriesPrefInfoMap expected_galleries_; 263 264 private: 265 // Needed for extension service & friends to work. 266 content::TestBrowserThreadBundle thread_bundle_; 267 268 EnsureMediaDirectoriesExists mock_gallery_locations_; 269 270 #if defined OS_CHROMEOS 271 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 272 chromeos::ScopedTestCrosSettings test_cros_settings_; 273 chromeos::ScopedTestUserManager test_user_manager_; 274 #endif 275 276 TestStorageMonitor monitor_; 277 scoped_ptr<TestingProfile> profile_; 278 scoped_ptr<MediaGalleriesPreferences> gallery_prefs_; 279 280 uint64 default_galleries_count_; 281 282 DISALLOW_COPY_AND_ASSIGN(MediaGalleriesPreferencesTest); 283 }; 284 285 base::FilePath MakePath(std::string dir) { 286 #if defined(OS_WIN) 287 return base::FilePath(FILE_PATH_LITERAL("C:\\")).Append(UTF8ToWide(dir)); 288 #elif defined(OS_POSIX) 289 return base::FilePath(FILE_PATH_LITERAL("/")).Append(dir); 290 #else 291 NOTREACHED(); 292 #endif 293 } 294 295 TEST_F(MediaGalleriesPreferencesTest, GalleryManagement) { 296 MediaGalleryPrefId auto_id, user_added_id, id; 297 base::FilePath path; 298 base::FilePath relative_path; 299 Verify(); 300 301 // Add a new auto detected gallery. 302 path = MakePath("new_auto"); 303 StorageInfo info; 304 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 305 info.set_name(ASCIIToUTF16("NewAutoGallery")); 306 id = AddGalleryWithNameV1(info.device_id(), info.name(), 307 relative_path, false /*auto*/); 308 EXPECT_EQ(default_galleries_count() + 1UL, id); 309 auto_id = id; 310 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 311 MediaGalleryPrefInfo::kAutoDetected); 312 Verify(); 313 314 // Add it again (as user), nothing should happen. 315 id = AddGalleryWithNameV1(info.device_id(), info.name(), 316 relative_path, true /*auto*/); 317 EXPECT_EQ(auto_id, id); 318 Verify(); 319 320 // Add a new user added gallery. 321 path = MakePath("new_user"); 322 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 323 info.set_name(ASCIIToUTF16("NewUserGallery")); 324 id = AddGalleryWithNameV1(info.device_id(), info.name(), 325 relative_path, true /*user*/); 326 EXPECT_EQ(default_galleries_count() + 2UL, id); 327 user_added_id = id; 328 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 329 MediaGalleryPrefInfo::kUserAdded); 330 Verify(); 331 332 // Lookup some galleries. 333 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), NULL)); 334 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), NULL)); 335 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(MakePath("other"), NULL)); 336 337 // Check that we always get the gallery info. 338 MediaGalleryPrefInfo gallery_info; 339 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), 340 &gallery_info)); 341 VerifyGalleryInfo(gallery_info, auto_id); 342 EXPECT_FALSE(gallery_info.volume_metadata_valid); 343 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_user"), 344 &gallery_info)); 345 VerifyGalleryInfo(gallery_info, user_added_id); 346 EXPECT_FALSE(gallery_info.volume_metadata_valid); 347 348 path = MakePath("other"); 349 EXPECT_FALSE(gallery_prefs()->LookUpGalleryByPath(path, &gallery_info)); 350 EXPECT_EQ(kInvalidMediaGalleryPrefId, gallery_info.pref_id); 351 352 StorageInfo other_info; 353 MediaStorageUtil::GetDeviceInfoFromPath(path, &other_info, &relative_path); 354 EXPECT_EQ(other_info.device_id(), gallery_info.device_id); 355 EXPECT_EQ(relative_path.value(), gallery_info.path.value()); 356 357 // Remove an auto added gallery (i.e. make it blacklisted). 358 gallery_prefs()->ForgetGalleryById(auto_id); 359 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed; 360 expected_galleries_for_all.erase(auto_id); 361 Verify(); 362 363 // Remove a user added gallery and it should go away. 364 gallery_prefs()->ForgetGalleryById(user_added_id); 365 expected_galleries_.erase(user_added_id); 366 expected_device_map[info.device_id()].erase(user_added_id); 367 Verify(); 368 } 369 370 TEST_F(MediaGalleriesPreferencesTest, AddGalleryWithVolumeMetadata) { 371 MediaGalleryPrefId id; 372 StorageInfo info; 373 base::FilePath path; 374 base::FilePath relative_path; 375 base::Time now = base::Time::Now(); 376 Verify(); 377 378 // Add a new auto detected gallery. 379 path = MakePath("new_auto"); 380 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 381 id = gallery_prefs()->AddGallery(info.device_id(), relative_path, 382 false /*auto*/, 383 ASCIIToUTF16("volume label"), 384 ASCIIToUTF16("vendor name"), 385 ASCIIToUTF16("model name"), 386 1000000ULL, now); 387 EXPECT_EQ(default_galleries_count() + 1UL, id); 388 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path, 389 MediaGalleryPrefInfo::kAutoDetected); 390 Verify(); 391 392 MediaGalleryPrefInfo gallery_info; 393 EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(MakePath("new_auto"), 394 &gallery_info)); 395 EXPECT_TRUE(gallery_info.volume_metadata_valid); 396 EXPECT_EQ(ASCIIToUTF16("volume label"), gallery_info.volume_label); 397 EXPECT_EQ(ASCIIToUTF16("vendor name"), gallery_info.vendor_name); 398 EXPECT_EQ(ASCIIToUTF16("model name"), gallery_info.model_name); 399 EXPECT_EQ(1000000ULL, gallery_info.total_size_in_bytes); 400 // Note: we put the microseconds time into a double, so there'll 401 // be some possible rounding errors. If it's less than 100, we don't 402 // care. 403 EXPECT_LE(abs(now.ToInternalValue() - 404 gallery_info.last_attach_time.ToInternalValue()), 100); 405 } 406 407 TEST_F(MediaGalleriesPreferencesTest, ReplaceGalleryWithVolumeMetadata) { 408 MediaGalleryPrefId id, metadata_id; 409 base::FilePath path; 410 StorageInfo info; 411 base::FilePath relative_path; 412 base::Time now = base::Time::Now(); 413 Verify(); 414 415 // Add an auto detected gallery in the prefs version 0 format. 416 path = MakePath("new_auto"); 417 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 418 info.set_name(ASCIIToUTF16("NewAutoGallery")); 419 id = AddGalleryWithNameV0(info.device_id(), info.name(), 420 relative_path, false /*auto*/); 421 EXPECT_EQ(default_galleries_count() + 1UL, id); 422 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 423 MediaGalleryPrefInfo::kAutoDetected); 424 Verify(); 425 426 metadata_id = gallery_prefs()->AddGallery(info.device_id(), 427 relative_path, 428 false /*auto*/, 429 ASCIIToUTF16("volume label"), 430 ASCIIToUTF16("vendor name"), 431 ASCIIToUTF16("model name"), 432 1000000ULL, now); 433 EXPECT_EQ(id, metadata_id); 434 AddGalleryExpectation(id, base::string16(), info.device_id(), relative_path, 435 MediaGalleryPrefInfo::kAutoDetected); 436 437 // Make sure the display_name is set to empty now, as the metadata 438 // upgrade should set the manual override name empty. 439 Verify(); 440 } 441 442 443 // Whenever a gallery is added, its type is either set to "AutoDetected" or 444 // "UserAdded". When the gallery is removed, user added galleries are actually 445 // deleted and the auto detected galleries are moved to black listed state. 446 // When the gallery is added again, the black listed state is updated back to 447 // "AutoDetected" type. 448 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryType) { 449 MediaGalleryPrefId auto_id, id; 450 base::FilePath path; 451 StorageInfo info; 452 base::FilePath relative_path; 453 Verify(); 454 455 // Add a new auto detect gallery to test with. 456 path = MakePath("new_auto"); 457 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 458 info.set_name(ASCIIToUTF16("NewAutoGallery")); 459 id = AddGalleryWithNameV1(info.device_id(), info.name(), 460 relative_path, false /*auto*/); 461 EXPECT_EQ(default_galleries_count() + 1UL, id); 462 auto_id = id; 463 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 464 MediaGalleryPrefInfo::kAutoDetected); 465 Verify(); 466 467 // Remove an auto added gallery (i.e. make it blacklisted). 468 gallery_prefs()->ForgetGalleryById(auto_id); 469 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed; 470 expected_galleries_for_all.erase(auto_id); 471 Verify(); 472 473 // Add the gallery again as a user action. 474 id = gallery_prefs()->AddGalleryByPath(path); 475 EXPECT_EQ(auto_id, id); 476 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 477 MediaGalleryPrefInfo::kAutoDetected); 478 Verify(); 479 480 // Remove an auto added gallery (i.e. make it blacklisted). 481 gallery_prefs()->ForgetGalleryById(auto_id); 482 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed; 483 expected_galleries_for_all.erase(auto_id); 484 Verify(); 485 486 // Try adding the gallery again automatically and it should be a no-op. 487 id = AddGalleryWithNameV1(info.device_id(), info.name(), 488 relative_path, false /*auto*/); 489 EXPECT_EQ(auto_id, id); 490 Verify(); 491 } 492 493 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryNameV2) { 494 // Add a new auto detect gallery to test with. 495 base::FilePath path = MakePath("new_auto"); 496 StorageInfo info; 497 base::FilePath relative_path; 498 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 499 info.set_name(ASCIIToUTF16("NewAutoGallery")); 500 MediaGalleryPrefId id = 501 AddGalleryWithNameV2(info.device_id(), info.name(), 502 relative_path, false /*auto*/); 503 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 504 MediaGalleryPrefInfo::kAutoDetected); 505 Verify(); 506 507 // Won't override the name -- don't change any expectation. 508 info.set_name(base::string16()); 509 AddGalleryWithNameV2(info.device_id(), info.name(), relative_path, false); 510 Verify(); 511 512 info.set_name(ASCIIToUTF16("NewName")); 513 id = AddGalleryWithNameV2(info.device_id(), info.name(), 514 relative_path, false); 515 // Note: will really just update the existing expectation. 516 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 517 MediaGalleryPrefInfo::kAutoDetected); 518 Verify(); 519 } 520 521 TEST_F(MediaGalleriesPreferencesTest, GalleryPermissions) { 522 MediaGalleryPrefId auto_id, user_added_id, to_blacklist_id, id; 523 base::FilePath path; 524 StorageInfo info; 525 base::FilePath relative_path; 526 Verify(); 527 528 // Add some galleries to test with. 529 path = MakePath("new_user"); 530 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 531 info.set_name(ASCIIToUTF16("NewUserGallery")); 532 id = AddGalleryWithNameV1(info.device_id(), info.name(), 533 relative_path, true /*user*/); 534 EXPECT_EQ(default_galleries_count() + 1UL, id); 535 user_added_id = id; 536 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 537 MediaGalleryPrefInfo::kUserAdded); 538 Verify(); 539 540 path = MakePath("new_auto"); 541 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 542 info.set_name(ASCIIToUTF16("NewAutoGallery")); 543 id = AddGalleryWithNameV1(info.device_id(), info.name(), 544 relative_path, false /*auto*/); 545 EXPECT_EQ(default_galleries_count() + 2UL, id); 546 auto_id = id; 547 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 548 MediaGalleryPrefInfo::kAutoDetected); 549 Verify(); 550 551 path = MakePath("to_blacklist"); 552 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 553 info.set_name(ASCIIToUTF16("ToBlacklistGallery")); 554 id = AddGalleryWithNameV1(info.device_id(), info.name(), 555 relative_path, false /*auto*/); 556 EXPECT_EQ(default_galleries_count() + 3UL, id); 557 to_blacklist_id = id; 558 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 559 MediaGalleryPrefInfo::kAutoDetected); 560 Verify(); 561 562 // Remove permission for all galleries from the all-permission extension. 563 gallery_prefs()->SetGalleryPermissionForExtension( 564 *all_permission_extension.get(), auto_id, false); 565 expected_galleries_for_all.erase(auto_id); 566 Verify(); 567 568 gallery_prefs()->SetGalleryPermissionForExtension( 569 *all_permission_extension.get(), user_added_id, false); 570 expected_galleries_for_all.erase(user_added_id); 571 Verify(); 572 573 gallery_prefs()->SetGalleryPermissionForExtension( 574 *all_permission_extension.get(), to_blacklist_id, false); 575 expected_galleries_for_all.erase(to_blacklist_id); 576 Verify(); 577 578 // Add permission back for all galleries to the all-permission extension. 579 gallery_prefs()->SetGalleryPermissionForExtension( 580 *all_permission_extension.get(), auto_id, true); 581 expected_galleries_for_all.insert(auto_id); 582 Verify(); 583 584 gallery_prefs()->SetGalleryPermissionForExtension( 585 *all_permission_extension.get(), user_added_id, true); 586 expected_galleries_for_all.insert(user_added_id); 587 Verify(); 588 589 gallery_prefs()->SetGalleryPermissionForExtension( 590 *all_permission_extension.get(), to_blacklist_id, true); 591 expected_galleries_for_all.insert(to_blacklist_id); 592 Verify(); 593 594 // Add permission for all galleries to the regular permission extension. 595 gallery_prefs()->SetGalleryPermissionForExtension( 596 *regular_permission_extension.get(), auto_id, true); 597 expected_galleries_for_regular.insert(auto_id); 598 Verify(); 599 600 gallery_prefs()->SetGalleryPermissionForExtension( 601 *regular_permission_extension.get(), user_added_id, true); 602 expected_galleries_for_regular.insert(user_added_id); 603 Verify(); 604 605 gallery_prefs()->SetGalleryPermissionForExtension( 606 *regular_permission_extension.get(), to_blacklist_id, true); 607 expected_galleries_for_regular.insert(to_blacklist_id); 608 Verify(); 609 610 // Blacklist the to be black listed gallery 611 gallery_prefs()->ForgetGalleryById(to_blacklist_id); 612 expected_galleries_[to_blacklist_id].type = 613 MediaGalleryPrefInfo::kBlackListed; 614 expected_galleries_for_all.erase(to_blacklist_id); 615 expected_galleries_for_regular.erase(to_blacklist_id); 616 Verify(); 617 618 // Remove permission for all galleries to the regular permission extension. 619 gallery_prefs()->SetGalleryPermissionForExtension( 620 *regular_permission_extension.get(), auto_id, false); 621 expected_galleries_for_regular.erase(auto_id); 622 Verify(); 623 624 gallery_prefs()->SetGalleryPermissionForExtension( 625 *regular_permission_extension.get(), user_added_id, false); 626 expected_galleries_for_regular.erase(user_added_id); 627 Verify(); 628 629 // Add permission for an invalid gallery id. 630 gallery_prefs()->SetGalleryPermissionForExtension( 631 *regular_permission_extension.get(), 9999L, true); 632 Verify(); 633 } 634 635 // Whenever a gallery is added, check to see if there is any gallery info exists 636 // for the added gallery. If so, verify the existing gallery information with 637 // the new details. If there is a mismatch, update the gallery information 638 // accordingly. 639 TEST_F(MediaGalleriesPreferencesTest, UpdateGalleryDetails) { 640 MediaGalleryPrefId auto_id, id; 641 base::FilePath path; 642 StorageInfo info; 643 base::FilePath relative_path; 644 Verify(); 645 646 // Add a new auto detect gallery to test with. 647 path = MakePath("new_auto"); 648 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 649 info.set_name(ASCIIToUTF16("NewAutoGallery")); 650 id = AddGalleryWithNameV1(info.device_id(), info.name(), 651 relative_path, false /*auto*/); 652 EXPECT_EQ(default_galleries_count() + 1UL, id); 653 auto_id = id; 654 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 655 MediaGalleryPrefInfo::kAutoDetected); 656 Verify(); 657 658 // Update the device name and add the gallery again. 659 info.set_name(ASCIIToUTF16("AutoGallery2")); 660 id = AddGalleryWithNameV1(info.device_id(), info.name(), 661 relative_path, false /*auto*/); 662 EXPECT_EQ(auto_id, id); 663 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 664 MediaGalleryPrefInfo::kAutoDetected); 665 Verify(); 666 } 667 668 TEST_F(MediaGalleriesPreferencesTest, MultipleGalleriesPerDevices) { 669 base::FilePath path; 670 StorageInfo info; 671 base::FilePath relative_path; 672 Verify(); 673 674 // Add a regular gallery 675 path = MakePath("new_user"); 676 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 677 info.set_name(ASCIIToUTF16("NewUserGallery")); 678 MediaGalleryPrefId user_added_id = 679 AddGalleryWithNameV1(info.device_id(), info.name(), 680 relative_path, true /*user*/); 681 EXPECT_EQ(default_galleries_count() + 1UL, user_added_id); 682 AddGalleryExpectation(user_added_id, info.name(), info.device_id(), 683 relative_path, MediaGalleryPrefInfo::kUserAdded); 684 Verify(); 685 686 // Find it by device id and fail to find something related. 687 MediaGalleryPrefIdSet pref_id_set; 688 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id()); 689 EXPECT_EQ(1U, pref_id_set.size()); 690 EXPECT_TRUE(pref_id_set.find(user_added_id) != pref_id_set.end()); 691 692 MediaStorageUtil::GetDeviceInfoFromPath(MakePath("new_user/foo"), &info, 693 &relative_path); 694 pref_id_set = gallery_prefs()->LookUpGalleriesByDeviceId(info.device_id()); 695 EXPECT_EQ(0U, pref_id_set.size()); 696 697 // Add some galleries on the same device. 698 relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device1")); 699 info.set_name(ASCIIToUTF16("Device1Path1")); 700 std::string device_id = "path:device1"; 701 MediaGalleryPrefId dev1_path1_id = AddGalleryWithNameV1( 702 device_id, info.name(), relative_path, true /*user*/); 703 EXPECT_EQ(default_galleries_count() + 2UL, dev1_path1_id); 704 AddGalleryExpectation(dev1_path1_id, info.name(), device_id, relative_path, 705 MediaGalleryPrefInfo::kUserAdded); 706 Verify(); 707 708 relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device1")); 709 info.set_name(ASCIIToUTF16("Device1Path2")); 710 MediaGalleryPrefId dev1_path2_id = AddGalleryWithNameV1( 711 device_id, info.name(), relative_path, true /*user*/); 712 EXPECT_EQ(default_galleries_count() + 3UL, dev1_path2_id); 713 AddGalleryExpectation(dev1_path2_id, info.name(), device_id, relative_path, 714 MediaGalleryPrefInfo::kUserAdded); 715 Verify(); 716 717 relative_path = base::FilePath(FILE_PATH_LITERAL("path1/on/device2")); 718 info.set_name(ASCIIToUTF16("Device2Path1")); 719 device_id = "path:device2"; 720 MediaGalleryPrefId dev2_path1_id = AddGalleryWithNameV1( 721 device_id, info.name(), relative_path, true /*user*/); 722 EXPECT_EQ(default_galleries_count() + 4UL, dev2_path1_id); 723 AddGalleryExpectation(dev2_path1_id, info.name(), device_id, relative_path, 724 MediaGalleryPrefInfo::kUserAdded); 725 Verify(); 726 727 relative_path = base::FilePath(FILE_PATH_LITERAL("path2/on/device2")); 728 info.set_name(ASCIIToUTF16("Device2Path2")); 729 MediaGalleryPrefId dev2_path2_id = AddGalleryWithNameV1( 730 device_id, info.name(), relative_path, true /*user*/); 731 EXPECT_EQ(default_galleries_count() + 5UL, dev2_path2_id); 732 AddGalleryExpectation(dev2_path2_id, info.name(), device_id, relative_path, 733 MediaGalleryPrefInfo::kUserAdded); 734 Verify(); 735 736 // Check that adding one of them again works as expected. 737 MediaGalleryPrefId id = AddGalleryWithNameV1( 738 device_id, info.name(), relative_path, true /*user*/); 739 EXPECT_EQ(dev2_path2_id, id); 740 Verify(); 741 } 742 743 TEST_F(MediaGalleriesPreferencesTest, GalleryChangeObserver) { 744 // Start with one observer. 745 MockGalleryChangeObserver observer1(gallery_prefs()); 746 gallery_prefs()->AddGalleryChangeObserver(&observer1); 747 748 // Add a new auto detected gallery. 749 base::FilePath path = MakePath("new_auto"); 750 StorageInfo info; 751 base::FilePath relative_path; 752 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 753 info.set_name(ASCIIToUTF16("NewAutoGallery")); 754 MediaGalleryPrefId auto_id = AddGalleryWithNameV1( 755 info.device_id(), info.name(), relative_path, false /*auto*/); 756 EXPECT_EQ(default_galleries_count() + 1UL, auto_id); 757 AddGalleryExpectation(auto_id, info.name(), info.device_id(), 758 relative_path, MediaGalleryPrefInfo::kAutoDetected); 759 EXPECT_EQ(1, observer1.notifications()); 760 761 // Add a second observer. 762 MockGalleryChangeObserver observer2(gallery_prefs()); 763 gallery_prefs()->AddGalleryChangeObserver(&observer2); 764 765 // Add a new user added gallery. 766 path = MakePath("new_user"); 767 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 768 info.set_name(ASCIIToUTF16("NewUserGallery")); 769 MediaGalleryPrefId user_added_id = 770 AddGalleryWithNameV1(info.device_id(), info.name(), 771 relative_path, true /*user*/); 772 AddGalleryExpectation(user_added_id, info.name(), info.device_id(), 773 relative_path, MediaGalleryPrefInfo::kUserAdded); 774 EXPECT_EQ(default_galleries_count() + 2UL, user_added_id); 775 EXPECT_EQ(2, observer1.notifications()); 776 EXPECT_EQ(1, observer2.notifications()); 777 778 // Remove the first observer. 779 gallery_prefs()->RemoveGalleryChangeObserver(&observer1); 780 781 // Remove an auto added gallery (i.e. make it blacklisted). 782 gallery_prefs()->ForgetGalleryById(auto_id); 783 expected_galleries_[auto_id].type = MediaGalleryPrefInfo::kBlackListed; 784 expected_galleries_for_all.erase(auto_id); 785 786 EXPECT_EQ(2, observer1.notifications()); 787 EXPECT_EQ(2, observer2.notifications()); 788 789 // Remove a user added gallery and it should go away. 790 gallery_prefs()->ForgetGalleryById(user_added_id); 791 expected_galleries_.erase(user_added_id); 792 expected_device_map[info.device_id()].erase(user_added_id); 793 794 EXPECT_EQ(2, observer1.notifications()); 795 EXPECT_EQ(3, observer2.notifications()); 796 } 797 798 TEST_F(MediaGalleriesPreferencesTest, UpdateSingletonDeviceIdType) { 799 MediaGalleryPrefId id; 800 base::FilePath path; 801 StorageInfo info; 802 base::FilePath relative_path; 803 Verify(); 804 805 // Add a new auto detect gallery to test with. 806 path = MakePath("new_auto"); 807 MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path); 808 info.set_name(ASCIIToUTF16("NewAutoGallery")); 809 info.set_device_id(StorageInfo::MakeDeviceId(StorageInfo::ITUNES, 810 path.AsUTF8Unsafe())); 811 id = AddGalleryWithNameV2(info.device_id(), info.name(), relative_path, 812 false /*auto*/); 813 EXPECT_EQ(default_galleries_count() + 1UL, id); 814 AddGalleryExpectation(id, info.name(), info.device_id(), relative_path, 815 MediaGalleryPrefInfo::kAutoDetected); 816 Verify(); 817 818 // Update the device id. 819 MockGalleryChangeObserver observer(gallery_prefs()); 820 gallery_prefs()->AddGalleryChangeObserver(&observer); 821 822 path = MakePath("updated_path"); 823 std::string updated_device_id = 824 StorageInfo::MakeDeviceId(StorageInfo::ITUNES, path.AsUTF8Unsafe()); 825 EXPECT_TRUE(UpdateDeviceIDForSingletonType(updated_device_id)); 826 AddGalleryExpectation(id, info.name(), updated_device_id, relative_path, 827 MediaGalleryPrefInfo::kAutoDetected); 828 expected_device_map[info.device_id()].erase(id); 829 expected_device_map[updated_device_id].insert(id); 830 Verify(); 831 EXPECT_EQ(1, observer.notifications()); 832 833 // No gallery for type. 834 std::string new_device_id = 835 StorageInfo::MakeDeviceId(StorageInfo::PICASA, path.AsUTF8Unsafe()); 836 EXPECT_FALSE(UpdateDeviceIDForSingletonType(new_device_id)); 837 } 838 839 TEST(MediaGalleryPrefInfoTest, NameGeneration) { 840 ASSERT_TRUE(TestStorageMonitor::CreateAndInstall()); 841 842 MediaGalleryPrefInfo info; 843 info.pref_id = 1; 844 info.display_name = ASCIIToUTF16("override"); 845 info.device_id = StorageInfo::MakeDeviceId( 846 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, "unique"); 847 848 EXPECT_EQ(ASCIIToUTF16("override"), info.GetGalleryDisplayName()); 849 850 info.display_name = ASCIIToUTF16("o2"); 851 EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName()); 852 853 EXPECT_EQ(l10n_util::GetStringUTF16( 854 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED), 855 info.GetGalleryAdditionalDetails()); 856 857 info.last_attach_time = base::Time::Now(); 858 EXPECT_NE(l10n_util::GetStringUTF16( 859 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED), 860 info.GetGalleryAdditionalDetails()); 861 EXPECT_NE(l10n_util::GetStringUTF16( 862 IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED), 863 info.GetGalleryAdditionalDetails()); 864 865 info.volume_label = ASCIIToUTF16("vol"); 866 info.vendor_name = ASCIIToUTF16("vendor"); 867 info.model_name = ASCIIToUTF16("model"); 868 EXPECT_EQ(ASCIIToUTF16("o2"), info.GetGalleryDisplayName()); 869 870 info.display_name = base::string16(); 871 EXPECT_EQ(ASCIIToUTF16("vol"), info.GetGalleryDisplayName()); 872 info.volume_label = base::string16(); 873 EXPECT_EQ(ASCIIToUTF16("vendor, model"), info.GetGalleryDisplayName()); 874 875 info.device_id = StorageInfo::MakeDeviceId( 876 StorageInfo::FIXED_MASS_STORAGE, "unique"); 877 EXPECT_EQ(base::FilePath(FILE_PATH_LITERAL("unique")).AsUTF8Unsafe(), 878 UTF16ToUTF8(info.GetGalleryTooltip())); 879 880 TestStorageMonitor::RemoveSingleton(); 881 } 882