Chromium Code Reviews| Index: chrome/browser/media_gallery/media_file_system_registry_unittest.cc |
| =================================================================== |
| --- chrome/browser/media_gallery/media_file_system_registry_unittest.cc (revision 173083) |
| +++ chrome/browser/media_gallery/media_file_system_registry_unittest.cc (working copy) |
| @@ -5,10 +5,12 @@ |
| // MediaFileSystemRegistry unit tests. |
| #include <algorithm> |
| +#include <set> |
| #include "base/command_line.h" |
| #include "base/file_util.h" |
| #include "base/files/scoped_temp_dir.h" |
| +#include "base/json/json_reader.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "base/memory/scoped_vector.h" |
| @@ -151,6 +153,43 @@ |
| namespace { |
| +void GetGalleryNamesCallback( |
| + std::set<std::string>* results, |
| + const std::vector<MediaFileSystemInfo>& file_systems) { |
| + for (size_t i = 0; i < file_systems.size(); ++i) { |
| + std::set<std::string>::const_iterator it = |
| + results->find(file_systems[i].name); |
| + ASSERT_EQ(it, results->end()); |
| + results->insert(file_systems[i].name); |
| + } |
| +} |
| + |
| +void CheckGalleryJSONName(const std::string& name, bool removable) { |
| + scoped_ptr<DictionaryValue> dict(static_cast<DictionaryValue*>( |
| + base::JSONReader::Read(name))); |
| + ASSERT_TRUE(dict); |
| + |
| + // Check deviceId. |
| + EXPECT_EQ(removable, |
| + dict->HasKey(MediaFileSystemRegistry::kDeviceIdKey)) << name; |
| + if (removable) { |
| + std::string device_id; |
| + EXPECT_TRUE(dict->GetString(MediaFileSystemRegistry::kDeviceIdKey, |
| + &device_id)) << name; |
| + EXPECT_FALSE(device_id.empty()) << name; |
| + } |
| + |
| + // Check galleryId. |
| + EXPECT_TRUE(dict->HasKey(MediaFileSystemRegistry::kGalleryIdKey)) << name; |
| + |
| + // Check name. |
| + EXPECT_TRUE(dict->HasKey(MediaFileSystemRegistry::kNameKey)) << name; |
| + std::string gallery_name; |
| + EXPECT_TRUE(dict->GetString(MediaFileSystemRegistry::kNameKey, |
| + &gallery_name)) << name; |
| + EXPECT_FALSE(gallery_name.empty()) << name; |
| +} |
| + |
| class TestMediaStorageUtil : public MediaStorageUtil { |
| public: |
| static void SetTestingMode(); |
| @@ -196,6 +235,8 @@ |
| const std::vector<MediaFileSystemInfo>& regular_extension_galleries, |
| const std::vector<MediaFileSystemInfo>& all_extension_galleries); |
| + std::set<std::string> GetGalleryNames(extensions::Extension* extension); |
| + |
| extensions::Extension* all_permission_extension(); |
| extensions::Extension* regular_permission_extension(); |
| @@ -263,6 +304,12 @@ |
| void AssertAllAutoAddedGalleries(); |
| + void InitForGalleryNamesTest(std::set<std::string>* gallery_names); |
| + |
| + void CheckNewGallery(ProfileState* profile_state, |
| + const std::set<std::string>& gallery_names, |
| + bool removable); |
| + |
| std::vector<MediaFileSystemInfo> GetAutoAddedGalleries( |
| ProfileState* profile_state); |
| @@ -453,6 +500,19 @@ |
| EXPECT_EQ(1, GetAndClearComparisonCount()); |
| } |
| +std::set<std::string> ProfileState::GetGalleryNames( |
| + extensions::Extension* extension) { |
| + content::RenderViewHost* rvh = single_web_contents_->GetRenderViewHost(); |
| + std::set<std::string> results; |
| + MediaFileSystemRegistry* registry = |
| + g_browser_process->media_file_system_registry(); |
| + registry->GetMediaFileSystemsForExtension( |
| + rvh, extension, |
| + base::Bind(&GetGalleryNamesCallback, base::Unretained(&results))); |
| + MessageLoop::current()->RunUntilIdle(); |
| + return results; |
| +} |
| + |
| extensions::Extension* ProfileState::all_permission_extension() { |
| return all_permission_extension_.get(); |
| } |
| @@ -472,7 +532,7 @@ |
| num_comparisons_++; |
| ASSERT_EQ(expected.size(), actual.size()) << test; |
| - for (size_t i = 0; i < expected.size() && i < actual.size(); i++) { |
| + for (size_t i = 0; i < expected.size() && i < actual.size(); ++i) { |
| EXPECT_EQ(expected[i].path.value(), actual[i].path.value()) << test; |
| EXPECT_FALSE(actual[i].fsid.empty()) << test; |
| if (!expected[i].fsid.empty()) |
| @@ -496,7 +556,7 @@ |
| } |
| void MediaFileSystemRegistryTest::CreateProfileState(size_t profile_count) { |
| - for (size_t i = 0; i < profile_count; i++) { |
| + for (size_t i = 0; i < profile_count; ++i) { |
| ProfileState* state = new ProfileState(&rph_factory_); |
| profile_states_.push_back(state); |
| } |
| @@ -514,7 +574,7 @@ |
| string16 name = path.LossyDisplayName(); |
| DCHECK(!MediaStorageUtil::IsMediaDevice(device_id)); |
| - for (size_t i = 0; i < profile_states_.size(); i++) { |
| + for (size_t i = 0; i < profile_states_.size(); ++i) { |
| profile_states_[i]->GetMediaGalleriesPrefs()->AddGallery( |
| device_id, name, FilePath(), true /*user_added*/); |
| } |
| @@ -530,6 +590,11 @@ |
| string16 name = location.LossyDisplayName(); |
| base::SystemMonitor::Get()->ProcessRemovableStorageAttached(device_id, name, |
| location.value()); |
| + bool user_added = (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM); |
| + for (size_t i = 0; i < profile_states_.size(); ++i) { |
| + profile_states_[i]->GetMediaGalleriesPrefs()->AddGallery( |
| + device_id, name, FilePath(), user_added); |
| + } |
| MessageLoop::current()->RunUntilIdle(); |
| return device_id; |
| } |
| @@ -547,13 +612,13 @@ |
| profile_state->GetMediaGalleriesPrefs(); |
| MediaGalleryPrefIdSet pref_id = |
| preferences->LookUpGalleriesByDeviceId(device_id); |
| - DCHECK_EQ(1U, pref_id.size()); |
| + ASSERT_EQ(1U, pref_id.size()); |
| preferences->SetGalleryPermissionForExtension(*extension, *pref_id.begin(), |
| has_access); |
| } |
| void MediaFileSystemRegistryTest::AssertAllAutoAddedGalleries() { |
| - for (size_t i = 0; i < profile_states_.size(); i++) { |
| + for (size_t i = 0; i < profile_states_.size(); ++i) { |
| MediaGalleriesPreferences* prefs = |
| profile_states_[0]->GetMediaGalleriesPrefs(); |
| @@ -571,6 +636,37 @@ |
| } |
| } |
| +void MediaFileSystemRegistryTest::InitForGalleryNamesTest( |
| + std::set<std::string>* gallery_names) { |
| + CreateProfileState(1); |
| + AssertAllAutoAddedGalleries(); |
| + |
| + // Get all existing gallery names. |
| + ProfileState* profile_state = GetProfileState(0U); |
| + *gallery_names = |
| + profile_state->GetGalleryNames(profile_state->all_permission_extension()); |
| + ASSERT_EQ(3U, gallery_names->size()); |
| +} |
| + |
| +void MediaFileSystemRegistryTest::CheckNewGallery( |
| + ProfileState* profile_state, |
| + const std::set<std::string>& gallery_names, |
| + bool removable) { |
| + // Get new galleries. |
| + std::set<std::string> new_gallery_names = |
| + profile_state->GetGalleryNames(profile_state->all_permission_extension()); |
| + ASSERT_EQ(4U, new_gallery_names.size()); |
| + |
| + // Find the new one and check it. |
| + std::vector<std::string> difference; |
| + std::set_symmetric_difference( |
| + gallery_names.begin(), gallery_names.end(), |
| + new_gallery_names.begin(), new_gallery_names.end(), |
| + std::back_inserter(difference)); |
| + ASSERT_EQ(1U, difference.size()); |
| + CheckGalleryJSONName(difference[0], removable); |
| +} |
| + |
| std::vector<MediaFileSystemInfo> |
| MediaFileSystemRegistryTest::GetAutoAddedGalleries( |
| ProfileState* profile_state) { |
| @@ -670,6 +766,75 @@ |
| auto_galleries); |
| } |
| +TEST_F(MediaFileSystemRegistryTest, GalleryNameDefault) { |
| + std::set<std::string> gallery_names; |
| + InitForGalleryNamesTest(&gallery_names); |
| + |
| + for (std::set<std::string>::const_iterator it = gallery_names.begin(); |
| + it != gallery_names.end(); |
| + ++it) { |
| + CheckGalleryJSONName(*it, false /*not removable*/); |
| + } |
| +} |
| + |
| +TEST_F(MediaFileSystemRegistryTest, GalleryNameMTP) { |
| + std::set<std::string> gallery_names; |
| + InitForGalleryNamesTest(&gallery_names); |
| + |
| + FilePath location(FILE_PATH_LITERAL("/mtp_bogus")); |
| + AttachDevice(MediaStorageUtil::MTP_OR_PTP, "mtp_fake_id", location); |
| + CheckNewGallery(GetProfileState(0U), gallery_names, true /*removable*/); |
| +} |
| + |
| +TEST_F(MediaFileSystemRegistryTest, GalleryNameDCIM) { |
| + std::set<std::string> gallery_names; |
| + InitForGalleryNamesTest(&gallery_names); |
| + |
| + FilePath location(FILE_PATH_LITERAL("/removable_dcim_bogus")); |
| + AttachDevice(MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, |
| + "removable_dcim_fake_id", |
| + location); |
| + CheckNewGallery(GetProfileState(0U), gallery_names, true /*removable*/); |
| +} |
| + |
| +TEST_F(MediaFileSystemRegistryTest, GalleryNameNoDCIM) { |
| + std::set<std::string> gallery_names; |
| + InitForGalleryNamesTest(&gallery_names); |
| + |
| + std::string device_id = |
| + AttachDevice(MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, |
|
kmadhusu
2012/12/15 02:46:06
nit: indent 4 spaces.
Lei Zhang
2012/12/15 04:23:24
Done.
|
| + empty_dir().AsUTF8Unsafe(), |
| + empty_dir()); |
| + std::string device_id2 = |
| + AddUserGallery(MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, |
|
kmadhusu
2012/12/15 02:46:06
nit: As per http://google-styleguide.googlecode.co
Lei Zhang
2012/12/15 04:23:24
This is acceptable style and is consistent with th
|
| + empty_dir().AsUTF8Unsafe(), |
| + empty_dir()); |
| + ASSERT_EQ(device_id, device_id2); |
| + // Add permission for new non-default gallery. |
| + ProfileState* profile_state = GetProfileState(0U); |
| + SetGalleryPermission(profile_state, |
| + profile_state->all_permission_extension(), |
| + device_id, |
| + true /*has access*/); |
| + CheckNewGallery(profile_state, gallery_names, true /*removable*/); |
| +} |
| + |
| +TEST_F(MediaFileSystemRegistryTest, GalleryNameUserAddedPath) { |
| + std::set<std::string> gallery_names; |
| + InitForGalleryNamesTest(&gallery_names); |
| + |
| + std::string device_id = AddUserGallery(MediaStorageUtil::FIXED_MASS_STORAGE, |
| + empty_dir().AsUTF8Unsafe(), |
| + empty_dir()); |
| + // Add permission for new non-default gallery. |
| + ProfileState* profile_state = GetProfileState(0U); |
| + SetGalleryPermission(profile_state, |
| + profile_state->all_permission_extension(), |
| + device_id, |
| + true /*has access*/); |
| + CheckNewGallery(profile_state, gallery_names, false /*not removable*/); |
| +} |
| + |
| } // namespace |
| } // namespace chrome |