#include "chrome/browser/media_galleries/media_galleries_preferences.h"
+#include <string>
+#include <vector>
+
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
+#include "base/path_service.h"
+#include "base/prefs/scoped_user_pref_update.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/media_galleries/media_file_system_registry.h"
#include "chrome/browser/media_galleries/media_galleries_test_util.h"
+#include "chrome/common/chrome_paths.h"
+#include "chrome/common/pref_names.h"
#include "chrome/test/base/testing_profile.h"
#include "components/storage_monitor/media_storage_util.h"
#include "components/storage_monitor/storage_monitor.h"
#include "ui/base/l10n/l10n_util.h"
#if defined(OS_CHROMEOS)
-#include "chrome/browser/chromeos/login/user_manager.h"
+#include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
#include "chrome/browser/chromeos/settings/cros_settings.h"
#include "chrome/browser/chromeos/settings/device_settings_service.h"
#endif
extension_system->CreateExtensionService(
CommandLine::ForCurrentProcess(), base::FilePath(), false);
- gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
- base::RunLoop loop;
- gallery_prefs_->EnsureInitialized(loop.QuitClosure());
- loop.Run();
+ ReinitPrefsAndExpectations();
- // Load the default galleries into the expectations.
const MediaGalleriesPrefInfoMap& known_galleries =
gallery_prefs_->known_galleries();
- if (known_galleries.size()) {
+ if (!known_galleries.empty()) {
ASSERT_EQ(3U, known_galleries.size());
- default_galleries_count_ = 3;
- MediaGalleriesPrefInfoMap::const_iterator it;
- for (it = known_galleries.begin(); it != known_galleries.end(); ++it) {
- expected_galleries_[it->first] = it->second;
- if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
- expected_galleries_for_all.insert(it->first);
- }
}
std::vector<std::string> all_permissions;
TestStorageMonitor::Destroy();
}
+ void ChangeMediaPathOverrides() {
+ mock_gallery_locations_.ChangeMediaPathOverrides();
+ }
+
+ void ReinitPrefsAndExpectations() {
+ gallery_prefs_.reset(new MediaGalleriesPreferences(profile_.get()));
+ base::RunLoop loop;
+ gallery_prefs_->EnsureInitialized(loop.QuitClosure());
+ loop.Run();
+
+ // Load the default galleries into the expectations.
+ const MediaGalleriesPrefInfoMap& known_galleries =
+ gallery_prefs_->known_galleries();
+ if (!known_galleries.empty()) {
+ default_galleries_count_ = 3;
+ MediaGalleriesPrefInfoMap::const_iterator it;
+ for (it = known_galleries.begin(); it != known_galleries.end(); ++it) {
+ expected_galleries_[it->first] = it->second;
+ if (it->second.type == MediaGalleryPrefInfo::kAutoDetected)
+ expected_galleries_for_all.insert(it->first);
+ }
+ }
+ }
+
+ void RemovePersistedDefaultGalleryValues() {
+ PrefService* prefs = profile_->GetPrefs();
+ scoped_ptr<ListPrefUpdate> update(new ListPrefUpdate(
+ prefs, prefs::kMediaGalleriesRememberedGalleries));
+ base::ListValue* list = update->Get();
+
+ for (base::ListValue::iterator iter = list->begin();
+ iter != list->end();
+ ++iter) {
+ base::DictionaryValue* dict;
+
+ if ((*iter)->GetAsDictionary(&dict)) {
+ // Setting the prefs version to 2 which is the version before
+ // default_gallery_type was added.
+ dict->SetInteger(kMediaGalleriesPrefsVersionKey, 2);
+ dict->Remove(kMediaGalleriesDefaultGalleryTypeKey, NULL);
+ }
+ }
+ update.reset();
+ }
+
void Verify() {
const MediaGalleriesPrefInfoMap& known_galleries =
gallery_prefs_->known_galleries();
EXPECT_EQ(in_expectation->second.audio_count, actual.audio_count);
EXPECT_EQ(in_expectation->second.image_count, actual.image_count);
EXPECT_EQ(in_expectation->second.video_count, actual.video_count);
+ EXPECT_EQ(
+ in_expectation->second.default_gallery_type,
+ actual.default_gallery_type);
}
MediaGalleriesPreferences* gallery_prefs() {
MediaGalleryPrefInfo::Type type =
user_added ? MediaGalleryPrefInfo::kUserAdded
: MediaGalleryPrefInfo::kAutoDetected;
- return gallery_prefs()->AddGalleryInternal(
+ return gallery_prefs()->AddOrUpdateGalleryInternal(
device_id, display_name, relative_path, type,
base::string16(), base::string16(), base::string16(), 0, base::Time(),
- false, 0, 0, 0, 0);
+ false, 0, 0, 0, 0, MediaGalleryPrefInfo::kNotDefault);
}
MediaGalleryPrefId AddGalleryWithNameV1(const std::string& device_id,
MediaGalleryPrefInfo::Type type =
user_added ? MediaGalleryPrefInfo::kUserAdded
: MediaGalleryPrefInfo::kAutoDetected;
- return gallery_prefs()->AddGalleryInternal(
+ return gallery_prefs()->AddOrUpdateGalleryInternal(
device_id, display_name, relative_path, type,
base::string16(), base::string16(), base::string16(), 0, base::Time(),
- false, 0, 0, 0, 1);
+ false, 0, 0, 0, 1, MediaGalleryPrefInfo::kNotDefault);
}
MediaGalleryPrefId AddGalleryWithNameV2(const std::string& device_id,
const base::string16& display_name,
const base::FilePath& relative_path,
MediaGalleryPrefInfo::Type type) {
- return gallery_prefs()->AddGalleryInternal(
+ return gallery_prefs()->AddOrUpdateGalleryInternal(
device_id, display_name, relative_path, type,
base::string16(), base::string16(), base::string16(), 0, base::Time(),
- false, 0, 0, 0, 2);
+ false, 0, 0, 0, 2, MediaGalleryPrefInfo::kNotDefault);
}
MediaGalleryPrefId AddFixedGalleryWithExepectation(
TEST_F(MediaGalleriesPreferencesTest, ForgetAndErase) {
MediaGalleryPrefId user_erase =
AddFixedGalleryWithExepectation("user_erase", "UserErase",
- MediaGalleryPrefInfo::kUserAdded);
+ MediaGalleryPrefInfo::kUserAdded);
EXPECT_EQ(default_galleries_count() + 1UL, user_erase);
MediaGalleryPrefId user_forget =
AddFixedGalleryWithExepectation("user_forget", "UserForget",
TEST_F(MediaGalleriesPreferencesTest, UpdateSingletonDeviceIdType) {
MediaGalleryPrefId id;
base::FilePath path;
- StorageInfo info;
- base::FilePath relative_path;
Verify();
// Add a new auto detect gallery to test with.
path = MakeMediaGalleriesTestingPath("new_auto");
- MediaStorageUtil::GetDeviceInfoFromPath(path, &info, &relative_path);
base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
- info.set_device_id(StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
- path.AsUTF8Unsafe()));
- id = AddGalleryWithNameV2(info.device_id(), gallery_name, relative_path,
+ std::string device_id = StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
+ path.AsUTF8Unsafe());
+ id = AddGalleryWithNameV2(device_id, gallery_name, base::FilePath(),
MediaGalleryPrefInfo::kAutoDetected);
EXPECT_EQ(default_galleries_count() + 1UL, id);
- AddGalleryExpectation(id, gallery_name, info.device_id(), relative_path,
+ AddGalleryExpectation(id, gallery_name, device_id, base::FilePath(),
MediaGalleryPrefInfo::kAutoDetected);
Verify();
std::string updated_device_id =
StorageInfo::MakeDeviceId(StorageInfo::ITUNES, path.AsUTF8Unsafe());
EXPECT_TRUE(UpdateDeviceIDForSingletonType(updated_device_id));
- AddGalleryExpectation(id, gallery_name, updated_device_id, relative_path,
+ AddGalleryExpectation(id, gallery_name, updated_device_id, base::FilePath(),
MediaGalleryPrefInfo::kAutoDetected);
- expected_device_map[info.device_id()].erase(id);
+ expected_device_map[device_id].erase(id);
expected_device_map[updated_device_id].insert(id);
Verify();
EXPECT_EQ(1, observer.notifications());
EXPECT_FALSE(UpdateDeviceIDForSingletonType(new_device_id));
}
+TEST_F(MediaGalleriesPreferencesTest, LookupImportedGalleryByPath) {
+ MediaGalleryPrefId id;
+ base::FilePath path;
+ Verify();
+
+ // iTunes device path points to an XML file in the library directory.
+ path = MakeMediaGalleriesTestingPath("new_auto").AppendASCII("library.xml");
+ base::string16 gallery_name = base::ASCIIToUTF16("NewAutoGallery");
+ std::string device_id = StorageInfo::MakeDeviceId(StorageInfo::ITUNES,
+ path.AsUTF8Unsafe());
+ id = AddGalleryWithNameV2(device_id, gallery_name, base::FilePath(),
+ MediaGalleryPrefInfo::kAutoDetected);
+ EXPECT_EQ(default_galleries_count() + 1UL, id);
+ AddGalleryExpectation(id, gallery_name, device_id, base::FilePath(),
+ MediaGalleryPrefInfo::kAutoDetected);
+ Verify();
+
+ // Verify we can look up the imported gallery by its path.
+ MediaGalleryPrefInfo gallery_info;
+ EXPECT_TRUE(gallery_prefs()->LookUpGalleryByPath(path.DirName(),
+ &gallery_info));
+ EXPECT_EQ(id, gallery_info.pref_id);
+}
+
TEST_F(MediaGalleriesPreferencesTest, ScanResults) {
MediaGalleryPrefId id;
base::FilePath path;
TestStorageMonitor::Destroy();
}
+
+TEST_F(MediaGalleriesPreferencesTest, SetsDefaultGalleryTypeField) {
+ // Tests that default galleries (Music, Pictures, Video) have the correct
+ // default_gallery field set.
+
+ // No default galleries exist on CrOS so this test isn't relevant there.
+#if defined(OS_CHROMEOS)
+ return;
+#endif
+
+ base::FilePath music_path;
+ base::FilePath pictures_path;
+ base::FilePath videos_path;
+ bool got_music_path = PathService::Get(chrome::DIR_USER_MUSIC, &music_path);
+ bool got_pictures_path =
+ PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path);
+ bool got_videos_path =
+ PathService::Get(chrome::DIR_USER_VIDEOS, &videos_path);
+
+ int num_default_galleries = 0;
+
+ const MediaGalleriesPrefInfoMap& known_galleries =
+ gallery_prefs()->known_galleries();
+ for (MediaGalleriesPrefInfoMap::const_iterator it =
+ known_galleries.begin();
+ it != known_galleries.end();
+ ++it) {
+ if (it->second.type != MediaGalleryPrefInfo::kAutoDetected)
+ continue;
+
+ std::string unique_id;
+ if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
+ continue;
+
+ if (got_music_path && unique_id == music_path.AsUTF8Unsafe()) {
+ EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault,
+ it->second.default_gallery_type);
+ num_default_galleries++;
+ } else if (got_pictures_path && unique_id == pictures_path.AsUTF8Unsafe()) {
+ EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault,
+ it->second.default_gallery_type);
+ num_default_galleries++;
+ } else if (got_videos_path && unique_id == videos_path.AsUTF8Unsafe()) {
+ EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault,
+ it->second.default_gallery_type);
+ num_default_galleries++;
+ } else {
+ EXPECT_EQ(MediaGalleryPrefInfo::DefaultGalleryType::kNotDefault,
+ it->second.default_gallery_type);
+ }
+ }
+
+ EXPECT_EQ(3, num_default_galleries);
+}
+
+TEST_F(MediaGalleriesPreferencesTest, UpdatesDefaultGalleryType) {
+ // Tests that if the path of a default gallery changed since last init,
+ // then when the MediaGalleriesPreferences is initialized, it will
+ // rewrite the device ID in prefs to include the new path.
+
+ // No default galleries exist on CrOS so this test isn't relevant there.
+#if defined(OS_CHROMEOS)
+ return;
+#endif
+
+ base::FilePath old_music_path;
+ base::FilePath old_pictures_path;
+ base::FilePath old_videos_path;
+ bool got_old_music_path =
+ PathService::Get(chrome::DIR_USER_MUSIC, &old_music_path);
+ bool got_old_pictures_path =
+ PathService::Get(chrome::DIR_USER_PICTURES, &old_pictures_path);
+ bool got_old_videos_path =
+ PathService::Get(chrome::DIR_USER_VIDEOS, &old_videos_path);
+
+ bool found_music = false;
+ bool found_pictures = false;
+ bool found_videos = false;
+
+ const MediaGalleriesPrefInfoMap& old_known_galleries =
+ gallery_prefs()->known_galleries();
+ for (MediaGalleriesPrefInfoMap::const_iterator it =
+ old_known_galleries.begin();
+ it != old_known_galleries.end();
+ ++it) {
+ if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) {
+ std::string unique_id;
+ if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
+ continue;
+
+ if (got_old_music_path &&
+ it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault) {
+ EXPECT_EQ(old_music_path.AsUTF8Unsafe(), unique_id);
+ found_music = true;
+ } else if (got_old_pictures_path &&
+ it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault) {
+ EXPECT_EQ(old_pictures_path.AsUTF8Unsafe(), unique_id);
+ found_pictures = true;
+ } else if (got_old_videos_path &&
+ it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault) {
+ EXPECT_EQ(old_videos_path.AsUTF8Unsafe(), unique_id);
+ found_videos = true;
+ }
+ }
+ }
+
+ EXPECT_TRUE(found_music);
+ EXPECT_TRUE(found_pictures);
+ EXPECT_TRUE(found_videos);
+
+ ChangeMediaPathOverrides();
+ ReinitPrefsAndExpectations();
+
+ base::FilePath new_music_path;
+ base::FilePath new_pictures_path;
+ base::FilePath new_videos_path;
+ bool got_new_music_path =
+ PathService::Get(chrome::DIR_USER_MUSIC, &new_music_path);
+ bool got_new_pictures_path =
+ PathService::Get(chrome::DIR_USER_PICTURES, &new_pictures_path);
+ bool got_new_videos_path =
+ PathService::Get(chrome::DIR_USER_VIDEOS, &new_videos_path);
+
+ EXPECT_NE(new_music_path, old_music_path);
+ EXPECT_NE(new_pictures_path, old_pictures_path);
+ EXPECT_NE(new_videos_path, old_videos_path);
+
+ found_music = false;
+ found_pictures = false;
+ found_videos = false;
+
+ const MediaGalleriesPrefInfoMap& known_galleries =
+ gallery_prefs()->known_galleries();
+ for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
+ it != known_galleries.end();
+ ++it) {
+ if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) {
+ std::string unique_id;
+ if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
+ continue;
+
+ if (got_new_music_path &&
+ it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault) {
+ EXPECT_EQ(new_music_path.AsUTF8Unsafe(), unique_id);
+ found_music = true;
+ } else if (got_new_pictures_path &&
+ it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault) {
+ EXPECT_EQ(new_pictures_path.AsUTF8Unsafe(), unique_id);
+ found_pictures = true;
+ } else if (got_new_videos_path &&
+ it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault) {
+ EXPECT_EQ(new_videos_path.AsUTF8Unsafe(), unique_id);
+ found_videos = true;
+ }
+ }
+ }
+
+ EXPECT_TRUE(found_music);
+ EXPECT_TRUE(found_pictures);
+ EXPECT_TRUE(found_videos);
+}
+
+TEST_F(MediaGalleriesPreferencesTest, UpdateAddsDefaultGalleryTypeIfMissing) {
+ // Tests that if no default_gallery_type was specified for an existing prefs
+ // info object corresponding to a particular gallery, then when the
+ // MediaGalleriesPreferences is initialized, it assigns the proper one.
+
+ // No default galleries exist on CrOS so this test isn't relevant there.
+#if defined(OS_CHROMEOS)
+ return;
+#endif
+
+ // Add a new user added gallery.
+ AddFixedGalleryWithExepectation("user_added", "UserAdded",
+ MediaGalleryPrefInfo::kUserAdded);
+
+ // Remove the "default_gallery_type" field completely from the persisted data
+ // for the prefs info object. This simulates the case where a user updated
+ // Chrome from a version without that field to one with it.
+ RemovePersistedDefaultGalleryValues();
+
+ // Reinitializing the MediaGalleriesPreferences should populate the
+ // default_gallery_type field with the correct value for each gallery.
+ ReinitPrefsAndExpectations();
+
+ base::FilePath music_path;
+ base::FilePath pictures_path;
+ base::FilePath videos_path;
+ bool got_music_path = PathService::Get(chrome::DIR_USER_MUSIC, &music_path);
+ bool got_pictures_path =
+ PathService::Get(chrome::DIR_USER_PICTURES, &pictures_path);
+ bool got_videos_path =
+ PathService::Get(chrome::DIR_USER_VIDEOS, &videos_path);
+
+ bool found_music = false;
+ bool found_pictures = false;
+ bool found_videos = false;
+ bool found_user_added = false;
+
+ const MediaGalleriesPrefInfoMap& known_galleries =
+ gallery_prefs()->known_galleries();
+ for (MediaGalleriesPrefInfoMap::const_iterator it = known_galleries.begin();
+ it != known_galleries.end();
+ ++it) {
+ std::string unique_id;
+ if (!StorageInfo::CrackDeviceId(it->second.device_id, NULL, &unique_id))
+ continue;
+
+ if (got_music_path &&
+ it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kMusicDefault) {
+ EXPECT_EQ(music_path.AsUTF8Unsafe(), unique_id);
+ found_music = true;
+ } else if (got_pictures_path &&
+ it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kPicturesDefault) {
+ EXPECT_EQ(pictures_path.AsUTF8Unsafe(), unique_id);
+ found_pictures = true;
+ } else if (got_videos_path &&
+ it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kVideosDefault) {
+ EXPECT_EQ(videos_path.AsUTF8Unsafe(), unique_id);
+ found_videos = true;
+ } else if (it->second.default_gallery_type ==
+ MediaGalleryPrefInfo::DefaultGalleryType::kNotDefault) {
+ found_user_added = true;
+ }
+ }
+
+ EXPECT_TRUE(found_music);
+ EXPECT_TRUE(found_pictures);
+ EXPECT_TRUE(found_videos);
+ EXPECT_TRUE(found_user_added);
+}