Index: chrome/browser/media_gallery/media_file_system_registry_unittest.cc |
diff --git a/chrome/browser/media_gallery/media_file_system_registry_unittest.cc b/chrome/browser/media_gallery/media_file_system_registry_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..06a9c7cdd444711e1a5967e58afcffdce566939f |
--- /dev/null |
+++ b/chrome/browser/media_gallery/media_file_system_registry_unittest.cc |
@@ -0,0 +1,674 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// MediaFileSystemRegistry unit tests. |
+ |
+#include "base/command_line.h" |
+#include "base/file_util.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/memory/scoped_vector.h" |
+#include "base/message_loop.h" |
+#include "base/scoped_temp_dir.h" |
+#include "base/stringprintf.h" |
+#include "base/utf_string_conversions.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/extension_service.h" |
+#include "chrome/browser/extensions/extension_system.h" |
+#include "chrome/browser/extensions/test_extension_system.h" |
+#include "chrome/browser/media_gallery/media_file_system_registry.h" |
+#include "chrome/browser/media_gallery/media_galleries_preferences_factory.h" |
+#include "chrome/browser/media_gallery/media_galleries_test_util.h" |
+#include "chrome/browser/system_monitor/media_storage_util.h" |
+#include "chrome/common/extensions/extension.h" |
+#include "chrome/common/extensions/extension_manifest_constants.h" |
+#include "chrome/test/base/chrome_render_view_host_test_harness.h" |
+#include "chrome/test/base/testing_profile.h" |
+#include "content/public/browser/render_process_host_factory.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "content/public/browser/render_view_host.h" |
+#include "content/public/browser/web_contents.h" |
+#include "content/public/test/mock_render_process_host.h" |
+#include "content/public/test/test_browser_thread.h" |
+#include "sync/api/string_ordinal.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace chrome { |
+ |
+class TestMediaFileSystemContext : public MediaFileSystemContext { |
+ public: |
+ struct FSInfo { |
+ FSInfo() {}; |
Lei Zhang
2012/11/09 09:14:53
nit: no ;
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ FSInfo(const std::string& device_id, const FilePath& path, |
+ const std::string& fsid); |
+ |
+ bool operator<(const FSInfo& other) const; |
+ |
+ std::string device_id; |
+ FilePath path; |
+ std::string fsid; |
+ }; |
+ |
+ explicit TestMediaFileSystemContext(MediaFileSystemRegistry* registry); |
+ virtual ~TestMediaFileSystemContext() {} |
+ |
+ // MediaFileSystemContext implementation. |
+ virtual std::string RegisterFileSystemForMassStorage( |
+ const std::string& device_id, const FilePath& path) OVERRIDE; |
+ |
+#if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
+ virtual std::string RegisterFileSystemForMTPDevice( |
+ const std::string& device_id, const FilePath& path, |
+ scoped_refptr<ScopedMTPDeviceMapEntry>* entry) OVERRIDE; |
+#endif |
+ |
+ virtual void RevokeFileSystem(const std::string& fsid) OVERRIDE; |
+ |
+ private: |
+ std::string AddFSEntry(const std::string& device_id, const FilePath& path); |
+ |
+ MediaFileSystemRegistry* registry_; |
+ int fsid_; |
Lei Zhang
2012/11/09 09:14:53
Looks a bit weird when fsid_ is an int but fsid is
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ std::map<std::string /*fsid*/, FSInfo> file_systems_by_id_; |
+}; |
+ |
+TestMediaFileSystemContext::FSInfo::FSInfo(const std::string& device_id, |
+ const FilePath& path, |
+ const std::string& fsid) |
+ : device_id(device_id), |
+ path(path), |
+ fsid(fsid) { |
+} |
+ |
+bool TestMediaFileSystemContext::FSInfo::operator<(const FSInfo& other) const { |
+ if (device_id != other.device_id) |
+ return device_id < other.device_id; |
+ if (path.value() != other.path.value()) |
+ return path.value() < other.path.value(); |
+ return fsid < other.fsid; |
+} |
+ |
+TestMediaFileSystemContext::TestMediaFileSystemContext( |
+ MediaFileSystemRegistry* registry) |
+ : registry_(registry), |
+ fsid_(0) { |
+ registry_->file_system_context_.reset(this); |
+} |
+ |
+std::string TestMediaFileSystemContext::RegisterFileSystemForMassStorage( |
+ const std::string& device_id, const FilePath& path) { |
+ CHECK(MediaStorageUtil::IsMassStorageDevice(device_id)); |
+ return AddFSEntry(device_id, path); |
+} |
+ |
+#if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
+std::string TestMediaFileSystemContext::RegisterFileSystemForMTPDevice( |
+ const std::string& device_id, const FilePath& path, |
+ scoped_refptr<ScopedMTPDeviceMapEntry>* entry) { |
+ CHECK(!MediaStorageUtil::IsMassStorageDevice(device_id)); |
+ DCHECK(entry); |
Lei Zhang
2012/11/09 09:14:53
No need for this. If we crash on the next line, it
vandebo (ex-Chrome)
2012/11/13 06:50:18
The segfault might end up being somewhat mysteriou
|
+ *entry = registry_->GetOrCreateScopedMTPDeviceMapEntry(path.value()); |
+ return AddFSEntry(device_id, path); |
+} |
+#endif |
+ |
+void TestMediaFileSystemContext::RevokeFileSystem(const std::string& fsid) { |
+ if (!ContainsKey(file_systems_by_id_, fsid)) |
+ return; |
+ EXPECT_EQ(1U, file_systems_by_id_.erase(fsid)); |
+} |
+ |
+std::string TestMediaFileSystemContext::AddFSEntry(const std::string& device_id, |
+ const FilePath& path) { |
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
Lei Zhang
2012/11/09 09:14:53
we usually dcheck these.
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ CHECK(path.IsAbsolute()); |
+ CHECK(!path.ReferencesParent()); |
+ |
+ std::string fsid = base::StringPrintf("FSID:%d", ++fsid_); |
+ FSInfo info(device_id, path, fsid); |
+ file_systems_by_id_[fsid] = info; |
+ return fsid; |
+} |
+ |
+namespace { |
+ |
+class MediaStorageUtilTest : public MediaStorageUtil { |
+ public: |
+ static void SetTestingMode(); |
+ |
+ static bool GetDeviceInfoFromPathTestFunction(const FilePath& path, |
+ std::string* device_id, |
+ string16* device_name, |
+ FilePath* relative_path); |
+}; |
+ |
+class MockProfileSharedRenderProcessHostFactory |
+ : public content::RenderProcessHostFactory { |
+ public: |
+ MockProfileSharedRenderProcessHostFactory() {} |
+ virtual ~MockProfileSharedRenderProcessHostFactory(); |
+ |
+ // RPH created with this factory are owned by it. If the RPH is destroyed |
+ // for testing purposes, it must be removed from the factory first. |
+ content::MockRenderProcessHost* ReleaseRPH( |
+ content::BrowserContext* browser_context); |
+ |
+ virtual content::RenderProcessHost* CreateRenderProcessHost( |
+ content::BrowserContext* browser_context) const OVERRIDE; |
+ |
+ private: |
+ typedef std::map<content::BrowserContext*, content::MockRenderProcessHost*> |
+ ProfileRPHMap; |
+ mutable ProfileRPHMap rph_map_; |
Lei Zhang
2012/11/09 09:14:53
why mutable?
vandebo (ex-Chrome)
2012/11/13 06:50:18
Because RenderProcessHostFactory::CreateRenderProc
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(MockProfileSharedRenderProcessHostFactory); |
+}; |
+ |
+class ProfileState { |
+ public: |
+ ProfileState(); |
+ ~ProfileState(); |
+ |
+ MediaGalleriesPreferences* GetPrefs(); |
Lei Zhang
2012/11/09 09:14:53
GetMediaGalleryPrefs? I usually think GetPrefs wou
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ |
+ scoped_ptr<TestingProfile> profile; |
+ |
+ scoped_ptr<content::WebContents> single_web_contents; |
+ scoped_ptr<content::WebContents> shared_web_contents1; |
+ scoped_ptr<content::WebContents> shared_web_contents2; |
+ |
+ // The RenderProcessHosts are freed when their respective WebContents / |
+ // RenderViewHosts go away. |
+ content::MockRenderProcessHost* single_rph; |
+ content::MockRenderProcessHost* shared_rph; |
+ |
+ scoped_refptr<extensions::Extension> all_permission_extension; |
+ scoped_refptr<extensions::Extension> regular_permission_extension; |
+ scoped_refptr<extensions::Extension> no_permissions_extension; |
+ |
+ private: |
+ scoped_refptr<extensions::Extension> AddApp( |
+ std::string name, |
Lei Zhang
2012/11/09 09:14:53
const ref both parameters?
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ std::vector<std::string> media_galleries_permissions); |
+ |
+ ExtensionService* extension_service_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ProfileState); |
+}; |
+ |
+class MediaFileSystemRegistryTest : public ChromeRenderViewHostTestHarness { |
+ public: |
+ MediaFileSystemRegistryTest(); |
+ virtual ~MediaFileSystemRegistryTest() {} |
+ |
+ void CreateProfileState(int profile_count); |
+ |
+ ProfileState* GetProfileState(int i); |
+ |
+ FilePath empty_dir() { |
+ return empty_dir_; |
+ } |
+ |
+ FilePath dcim_dir() { |
+ return dcim_dir_; |
+ } |
+ |
+ // Create a user added gallery based on the information passed and add it to |
+ // |profiles|. Returns the device id. |
+ std::string AddUserGallery(MediaStorageUtil::Type type, |
+ const std::string& unique_id, |
+ const FilePath& path); |
+ |
+ void AttachDevice(MediaStorageUtil::Type type, const std::string& unique_id, |
+ const FilePath& location); |
+ |
+ void DetachDevice(const std::string& device_id); |
+ |
+ void SetGalleryPermission(size_t profile, extensions::Extension* extension, |
+ const std::string& device_id, bool has_access); |
+ |
+ void AssertAllAutoAddedGalleries(); |
+ |
+ std::vector<MediaFileSystemInfo> GetAutoAddedGalleries(size_t profile); |
+ |
+ void CompareResults(const std::string& test, |
+ const std::vector<MediaFileSystemInfo>& expected, |
+ const std::vector<MediaFileSystemInfo>& actual); |
+ |
+ int GetAndClearComparisonCount(); |
+ |
+ void CheckGalleriesForProfile(size_t profile, const std::string& test, |
+ const std::vector<MediaFileSystemInfo>& regular_extension_galleries, |
+ const std::vector<MediaFileSystemInfo>& all_extension_galleries); |
+ |
+ protected: |
+ void SetUp(); |
+ void TearDown(); |
+ |
+ private: |
+ // This makes sure that at least one default gallery exists on the file |
+ // system. |
+ EnsureMediaDirectoriesExists media_directories_; |
+ |
+ // Some test gallery directories. |
+ ScopedTempDir galleries_dir_; |
+ FilePath empty_dir_; |
Lei Zhang
2012/11/09 09:14:53
Can you mention these are inside |galleries_dir_|
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ FilePath dcim_dir_; |
+ |
+ // MediaFileSystemRegistry owns this. |
+ TestMediaFileSystemContext* test_file_system_context_; |
+ |
+ // Needed for extension service & friends to work. |
+ content::TestBrowserThread ui_thread_; |
+ content::TestBrowserThread file_thread_; |
+ |
+ MockProfileSharedRenderProcessHostFactory rph_factory_; |
+ |
+ ScopedVector<ProfileState> profile_state_; |
Lei Zhang
2012/11/09 09:14:53
nit: profile_states_
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ |
+ int num_comparisons_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistryTest); |
+}; |
+ |
+bool MediaFileSystemInfoComparator(const MediaFileSystemInfo& a, |
+ const MediaFileSystemInfo& b) { |
+ if (a.name != b.name) |
+ return a.name < b.name; |
+ if (a.path.value() != b.path.value()) |
+ return a.path.value() < b.path.value(); |
+ return a.fsid < b.fsid; |
+} |
+ |
+////////////////////////// |
+// MediaStorageUtilTest // |
+////////////////////////// |
+ |
+// static |
+void MediaStorageUtilTest::SetTestingMode() { |
+ SetGetDeviceInfoFromPathFunctionForTesting( |
+ &GetDeviceInfoFromPathTestFunction); |
+} |
+ |
+// static |
+bool MediaStorageUtilTest::GetDeviceInfoFromPathTestFunction( |
+ const FilePath& path, std::string* device_id, string16* device_name, |
+ FilePath* relative_path) { |
+ if (device_id) |
+ *device_id = MakeDeviceId(FIXED_MASS_STORAGE, path.AsUTF8Unsafe()); |
+ if (device_name) |
+ *device_name = path.BaseName().LossyDisplayName(); |
+ if (relative_path) |
+ *relative_path = FilePath(); |
+ return true; |
+} |
+ |
+/////////////////////////////////////////////// |
+// MockProfileSharedRenderProcessHostFactory // |
+/////////////////////////////////////////////// |
+ |
+MockProfileSharedRenderProcessHostFactory:: |
+ ~MockProfileSharedRenderProcessHostFactory() { |
+ for (ProfileRPHMap::const_iterator it = rph_map_.begin(); |
Lei Zhang
2012/11/09 09:14:53
This entire dtor is just a call to STLDeleteValues
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ it != rph_map_.end(); |
+ ++it) { |
+ delete it->second; |
+ } |
+ rph_map_.clear(); |
+} |
+ |
+content::MockRenderProcessHost* |
+MockProfileSharedRenderProcessHostFactory::ReleaseRPH( |
+ content::BrowserContext* browser_context) { |
+ ProfileRPHMap::iterator existing = rph_map_.find(browser_context); |
+ if (existing == rph_map_.end()) |
+ return NULL; |
+ content::MockRenderProcessHost* result = existing->second; |
+ rph_map_.erase(existing); |
+ return result; |
+} |
+ |
+content::RenderProcessHost* |
+MockProfileSharedRenderProcessHostFactory::CreateRenderProcessHost( |
+ content::BrowserContext* browser_context) const { |
+ ProfileRPHMap::const_iterator existing = rph_map_.find(browser_context); |
+ if (existing != rph_map_.end()) |
+ return existing->second; |
+ rph_map_[browser_context] = |
+ new content::MockRenderProcessHost(browser_context); |
+ return rph_map_[browser_context]; |
+} |
+ |
+////////////////// |
+// ProfileState // |
+////////////////// |
+ |
+ProfileState::ProfileState() |
+ : profile(new TestingProfile()), |
+ extension_service_(NULL) { |
+ extensions::TestExtensionSystem* extension_system( |
+ static_cast<extensions::TestExtensionSystem*>( |
+ extensions::ExtensionSystem::Get(profile.get()))); |
+ extension_service_ = extension_system->CreateExtensionService( |
+ CommandLine::ForCurrentProcess(), FilePath(), false); |
+ |
+ std::vector<std::string> all_permissions; |
+ all_permissions.push_back("allAutoDetected"); |
+ all_permissions.push_back("read"); |
+ std::vector<std::string> read_permissions; |
+ read_permissions.push_back("read"); |
+ |
+ all_permission_extension = AddApp("all", all_permissions); |
+ regular_permission_extension = AddApp("regular", read_permissions); |
+ no_permissions_extension = AddApp("no", read_permissions); |
+} |
+ |
+ProfileState::~ProfileState() { |
+ // TestExtensionSystem uses DeleteSoon, so we need to delete the profiles |
+ // and then run the message queue to clean up. But first we have to |
+ // delete everything that references the profile. |
+ single_web_contents.reset(); |
+ shared_web_contents1.reset(); |
+ shared_web_contents2.reset(); |
+ profile.reset(); |
+ |
+ MessageLoop::current()->RunUntilIdle(); |
+} |
+ |
+MediaGalleriesPreferences* ProfileState::GetPrefs() { |
+ return MediaGalleriesPreferencesFactory::GetForProfile(profile.get()); |
+} |
+ |
+scoped_refptr<extensions::Extension> ProfileState::AddApp( |
Lei Zhang
2012/11/09 09:14:53
Don't we have something similar to this in chrome/
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ std::string name, |
+ std::vector<std::string> media_galleries_permissions) { |
+ scoped_ptr<DictionaryValue> manifest(new DictionaryValue); |
+ manifest->SetString(extension_manifest_keys::kName, name); |
+ manifest->SetString(extension_manifest_keys::kVersion, "0.1"); |
+ manifest->SetInteger(extension_manifest_keys::kManifestVersion, 2); |
+ ListValue* background_script_list = new ListValue; |
+ background_script_list->Append(Value::CreateStringValue("background.js")); |
+ manifest->Set(extension_manifest_keys::kPlatformAppBackgroundScripts, |
+ background_script_list); |
+ |
+ ListValue* permission_detail_list = new ListValue; |
+ for (size_t i = 0; i < media_galleries_permissions.size(); i++) |
+ permission_detail_list->Append( |
+ Value::CreateStringValue(media_galleries_permissions[i])); |
+ DictionaryValue* media_galleries_permission = new DictionaryValue(); |
+ media_galleries_permission->Set("mediaGalleries", permission_detail_list); |
+ ListValue* permission_list = new ListValue; |
+ permission_list->Append(media_galleries_permission); |
+ manifest->Set(extension_manifest_keys::kPermissions, permission_list); |
+ |
+ FilePath path = profile->GetPath().Append(name); |
+ std::string errors; |
+ scoped_refptr<extensions::Extension> extension = |
+ extensions::Extension::Create(path, extensions::Extension::INTERNAL, |
+ *manifest.get(), |
+ extensions::Extension::NO_FLAGS, &errors); |
+ EXPECT_TRUE(extension.get() != NULL) << errors; |
+ EXPECT_TRUE(extensions::Extension::IdIsValid(extension->id())); |
+ if (!extension.get() || !extensions::Extension::IdIsValid(extension->id())) |
+ return NULL; |
+ |
+ extension_service_->extension_prefs()->OnExtensionInstalled( |
+ extension.get(), extensions::Extension::ENABLED, |
+ syncer::StringOrdinal::CreateInitialOrdinal()); |
+ |
+ extension_service_->AddExtension(extension); |
+ extension_service_->EnableExtension(extension->id()); |
+ |
+ return extension; |
+} |
+ |
+///////////////////////////////// |
+// MediaFileSystemRegistryTest // |
+///////////////////////////////// |
+ |
+MediaFileSystemRegistryTest::MediaFileSystemRegistryTest() |
+ : ui_thread_(content::BrowserThread::UI, MessageLoop::current()), |
+ file_thread_(content::BrowserThread::FILE, MessageLoop::current()), |
+ num_comparisons_(0) { |
+} |
+ |
+void MediaFileSystemRegistryTest::CreateProfileState(int profile_count) { |
+ for (int i = 0; i < profile_count; i++) { |
+ ProfileState * state = new ProfileState; |
+ profile_state_.push_back(state); |
+ |
+ // Temporarily set the profile while we use it. |
+ content::BrowserContext* profile = state->profile.get(); |
+ browser_context_.reset(profile); |
Lei Zhang
2012/11/09 09:14:53
Do you have to do this? Can you use WebContentsTes
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ |
+ state->single_web_contents.reset(CreateTestWebContents()); |
+ state->single_rph = rph_factory_.ReleaseRPH(profile); |
+ |
+ state->shared_web_contents1.reset(CreateTestWebContents()); |
+ state->shared_web_contents2.reset(CreateTestWebContents()); |
+ state->shared_rph = rph_factory_.ReleaseRPH(profile); |
+ |
+ // And release it when we're done, assign it to appease the compile. |
+ profile = browser_context_.release(); |
+ } |
+} |
+ |
+ProfileState* MediaFileSystemRegistryTest::GetProfileState(int i) { |
+ return profile_state_[i]; |
+} |
+ |
+// Create a user added gallery based on the information passed and add it to |
Lei Zhang
2012/11/09 09:14:53
no need for a second copy of comments.
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+// |profiles|. Returns the device id. |
+std::string MediaFileSystemRegistryTest::AddUserGallery( |
+ MediaStorageUtil::Type type, |
+ const std::string& unique_id, |
+ const FilePath& path) { |
+ std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); |
+ string16 name = path.LossyDisplayName(); |
+ DCHECK(!MediaStorageUtil::IsMediaDevice(device_id)); |
+ |
+ for (size_t i = 0; i < profile_state_.size(); i++) { |
+ profile_state_[i]->GetPrefs()->AddGallery(device_id, name, FilePath(), |
+ true /*user_added*/); |
+ } |
+ return device_id; |
+} |
+ |
+void MediaFileSystemRegistryTest::AttachDevice(MediaStorageUtil::Type type, |
+ const std::string& unique_id, |
+ const FilePath& location) { |
+ std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); |
+ DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); |
+ string16 name = location.LossyDisplayName(); |
+ base::SystemMonitor::Get()->ProcessRemovableStorageAttached(device_id, name, |
+ location.value()); |
Lei Zhang
2012/11/09 09:14:53
indentation
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+} |
+ |
+void MediaFileSystemRegistryTest::DetachDevice(const std::string& device_id) { |
+ DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); |
+ base::SystemMonitor::Get()->ProcessRemovableStorageDetached(device_id); |
+} |
+ |
+void MediaFileSystemRegistryTest::SetGalleryPermission( |
+ size_t profile, extensions::Extension* extension, |
+ const std::string& device_id, bool has_access) { |
+ MediaGalleriesPreferences* preferences = GetProfileState(profile)->GetPrefs(); |
+ MediaGalleryPrefIdSet pref_id = |
+ preferences->LookUpGalleriesByDeviceId(device_id); |
+ DCHECK_EQ(1U, pref_id.size()); |
+ preferences->SetGalleryPermissionForExtension(*extension, *pref_id.begin(), |
+ has_access); |
+} |
+ |
+void MediaFileSystemRegistryTest::AssertAllAutoAddedGalleries() { |
+ for (size_t i = 0; i < profile_state_.size(); i++) { |
+ MediaGalleriesPreferences* prefs = profile_state_[0]->GetPrefs(); |
+ |
+ // Make sure that we have at least one gallery and that they are all |
+ // auto added galleries. |
+ const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); |
+ ASSERT_LT(0U, galleries.size()); |
Lei Zhang
2012/11/09 09:14:53
Unlike _EQ, there's not "expected" value here. So
vandebo (ex-Chrome)
2012/11/13 06:50:18
Done.
|
+ for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); |
+ it != galleries.end(); |
+ ++it) { |
+ ASSERT_EQ(MediaGalleryPrefInfo::kAutoDetected, it->second.type); |
+ } |
+ } |
+} |
+ |
+std::vector<MediaFileSystemInfo> |
+MediaFileSystemRegistryTest::GetAutoAddedGalleries(size_t profile) { |
+ DCHECK_LT(profile, profile_state_.size()); |
+ const MediaGalleriesPrefInfoMap& galleries = |
+ GetProfileState(profile)->GetPrefs()->known_galleries(); |
+ std::vector<MediaFileSystemInfo> result; |
+ for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); |
+ it != galleries.end(); |
+ ++it) { |
+ if (it->second.type == MediaGalleryPrefInfo::kAutoDetected) { |
+ FilePath path = it->second.AbsolutePath(); |
+ MediaFileSystemInfo info(std::string(), path, std::string()); |
+ result.push_back(info); |
+ } |
+ } |
+ std::sort(result.begin(), result.end(), MediaFileSystemInfoComparator); |
+ return result; |
+} |
+ |
+void MediaFileSystemRegistryTest::CompareResults( |
+ const std::string& test, |
+ const std::vector<MediaFileSystemInfo>& expected, |
+ const std::vector<MediaFileSystemInfo>& actual) { |
+ // Order isn't important, so sort the results. Assume that expected |
+ // is already sorted. |
+ std::vector<MediaFileSystemInfo> sorted(actual); |
+ std::sort(sorted.begin(), sorted.end(), MediaFileSystemInfoComparator); |
+ |
+ num_comparisons_++; |
+ EXPECT_EQ(expected.size(), actual.size()) << test; |
+ for (size_t i = 0; i < expected.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()) |
+ EXPECT_EQ(expected[i].fsid, actual[i].fsid) << test; |
+ } |
+} |
+ |
+int MediaFileSystemRegistryTest::GetAndClearComparisonCount() { |
+ int result = num_comparisons_; |
+ num_comparisons_ = 0; |
+ return result; |
+} |
+ |
+void MediaFileSystemRegistryTest::CheckGalleriesForProfile( |
+ size_t profile, |
+ const std::string& test, |
+ const std::vector<MediaFileSystemInfo>& regular_extension_galleries, |
+ const std::vector<MediaFileSystemInfo>& all_extension_galleries) { |
+ content::RenderViewHost* rvh = |
+ GetProfileState(profile)->single_web_contents->GetRenderViewHost(); |
+ |
+ // No Media Galleries permissions. |
+ std::vector<MediaFileSystemInfo> empty_expectation; |
+ MediaFileSystemRegistry::GetInstance()->GetMediaFileSystemsForExtension( |
+ rvh, GetProfileState(profile)->no_permissions_extension.get(), |
+ base::Bind(&MediaFileSystemRegistryTest::CompareResults, |
+ base::Unretained(this), |
+ StringPrintf("%s (no permission)", test.c_str()), |
+ base::ConstRef(empty_expectation))); |
+ MessageLoop::current()->RunUntilIdle(); |
+ EXPECT_EQ(1, GetAndClearComparisonCount()); |
+ |
+ // Read permission only. |
+ MediaFileSystemRegistry::GetInstance()->GetMediaFileSystemsForExtension( |
+ rvh, GetProfileState(profile)->regular_permission_extension.get(), |
+ base::Bind(&MediaFileSystemRegistryTest::CompareResults, |
+ base::Unretained(this), |
+ StringPrintf("%s (regular permission)", test.c_str()), |
+ base::ConstRef(regular_extension_galleries))); |
+ MessageLoop::current()->RunUntilIdle(); |
+ EXPECT_EQ(1, GetAndClearComparisonCount()); |
+ |
+ // All galleries permission. |
+ MediaFileSystemRegistry::GetInstance()->GetMediaFileSystemsForExtension( |
+ rvh, GetProfileState(profile)->all_permission_extension.get(), |
+ base::Bind(&MediaFileSystemRegistryTest::CompareResults, |
+ base::Unretained(this), |
+ StringPrintf("%s (all permission)", test.c_str()), |
+ base::ConstRef(all_extension_galleries))); |
+ MessageLoop::current()->RunUntilIdle(); |
+ EXPECT_EQ(1, GetAndClearComparisonCount()); |
+} |
+ |
+void MediaFileSystemRegistryTest::SetUp() { |
+ ChromeRenderViewHostTestHarness::SetUp(); |
+ DeleteContents(); |
+ SetRenderProcessHostFactory(&rph_factory_); |
+ |
+ MediaStorageUtilTest::SetTestingMode(); |
+ test_file_system_context_ = |
+ new TestMediaFileSystemContext(MediaFileSystemRegistry::GetInstance()); |
+ |
+ ASSERT_TRUE(galleries_dir_.CreateUniqueTempDir()); |
+ empty_dir_ = galleries_dir_.path().Append(FILE_PATH_LITERAL("empty")); |
+ ASSERT_TRUE(file_util::CreateDirectory(empty_dir_)); |
+ dcim_dir_ = galleries_dir_.path().Append(FILE_PATH_LITERAL("with_dcim")); |
+ ASSERT_TRUE(file_util::CreateDirectory(dcim_dir_)); |
+ ASSERT_TRUE(file_util::CreateDirectory(dcim_dir_.Append("DCIM"))); |
+} |
+ |
+void MediaFileSystemRegistryTest::TearDown() { |
+ profile_state_.clear(); |
+ ChromeRenderViewHostTestHarness::TearDown(); |
+ MediaFileSystemRegistry* registry = MediaFileSystemRegistry::GetInstance(); |
+ EXPECT_EQ(0U, registry->GetExtensionHostCountForTests()); |
+} |
+ |
+/////////// |
+// Tests // |
+/////////// |
+ |
+TEST_F(MediaFileSystemRegistryTest, Basic) { |
+ CreateProfileState(1); |
+ AssertAllAutoAddedGalleries(); |
+ |
+ std::vector<MediaFileSystemInfo> empty_expectation; |
+ std::vector<MediaFileSystemInfo> auto_galleries = GetAutoAddedGalleries(0); |
+ CheckGalleriesForProfile(0, "basic", empty_expectation, auto_galleries); |
+} |
+ |
+TEST_F(MediaFileSystemRegistryTest, UserAddedGallery) { |
+ CreateProfileState(1); |
+ AssertAllAutoAddedGalleries(); |
+ |
+ std::vector<MediaFileSystemInfo> added_galleries; |
+ std::vector<MediaFileSystemInfo> auto_galleries = GetAutoAddedGalleries(0); |
+ CheckGalleriesForProfile(0, "user added init", added_galleries, |
+ auto_galleries); |
+ |
+ // Add a user gallery to the regular permission extension. |
+ std::string device_id = AddUserGallery(MediaStorageUtil::FIXED_MASS_STORAGE, |
+ empty_dir().AsUTF8Unsafe(), |
+ empty_dir()); |
+ SetGalleryPermission(0, |
+ GetProfileState(0)->regular_permission_extension.get(), |
+ device_id, |
+ true /*has access*/); |
+ MediaFileSystemInfo added_info(empty_dir().AsUTF8Unsafe(), empty_dir(), |
+ std::string()); |
+ added_galleries.push_back(added_info); |
+ CheckGalleriesForProfile(0, "user added regular", added_galleries, |
+ auto_galleries); |
+ |
+ // Add it to the all galleries extension. |
+ SetGalleryPermission(0, GetProfileState(0)->all_permission_extension.get(), |
+ device_id, true /*has access*/); |
+ auto_galleries.push_back(added_info); |
+ CheckGalleriesForProfile(0, "user added all", added_galleries, |
+ auto_galleries); |
+} |
+ |
+} // namespace |
+ |
+} // namespace chrome |