Chromium Code Reviews| 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 // MediaFileSystemRegistry unit tests. | 5 // MediaFileSystemRegistry unit tests. |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/scoped_temp_dir.h" | 12 #include "base/files/scoped_temp_dir.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/stl_util.h" | 18 #include "base/stl_util.h" |
| 19 #include "base/stringprintf.h" | 19 #include "base/stringprintf.h" |
| 20 #include "base/synchronization/waitable_event.h" | |
| 20 #include "base/threading/sequenced_worker_pool.h" | 21 #include "base/threading/sequenced_worker_pool.h" |
| 21 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
| 22 #include "base/values.h" | 23 #include "base/values.h" |
| 23 #include "chrome/browser/extensions/extension_service.h" | 24 #include "chrome/browser/extensions/extension_service.h" |
| 24 #include "chrome/browser/extensions/extension_system.h" | 25 #include "chrome/browser/extensions/extension_system.h" |
| 25 #include "chrome/browser/extensions/test_extension_system.h" | 26 #include "chrome/browser/extensions/test_extension_system.h" |
| 26 #include "chrome/browser/media_galleries/media_file_system_context.h" | 27 #include "chrome/browser/media_galleries/media_file_system_context.h" |
| 27 #include "chrome/browser/media_galleries/media_file_system_registry.h" | 28 #include "chrome/browser/media_galleries/media_file_system_registry.h" |
| 28 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h" | 29 #include "chrome/browser/media_galleries/media_galleries_preferences_factory.h" |
| 29 #include "chrome/browser/media_galleries/media_galleries_test_util.h" | 30 #include "chrome/browser/media_galleries/media_galleries_test_util.h" |
| (...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 | 289 |
| 289 class MediaFileSystemRegistryTest : public ChromeRenderViewHostTestHarness { | 290 class MediaFileSystemRegistryTest : public ChromeRenderViewHostTestHarness { |
| 290 public: | 291 public: |
| 291 MediaFileSystemRegistryTest(); | 292 MediaFileSystemRegistryTest(); |
| 292 virtual ~MediaFileSystemRegistryTest() {} | 293 virtual ~MediaFileSystemRegistryTest() {} |
| 293 | 294 |
| 294 void CreateProfileState(size_t profile_count); | 295 void CreateProfileState(size_t profile_count); |
| 295 | 296 |
| 296 ProfileState* GetProfileState(size_t i); | 297 ProfileState* GetProfileState(size_t i); |
| 297 | 298 |
| 299 MediaGalleriesPreferences* GetPreferences(Profile* profile); | |
| 300 | |
| 298 base::FilePath empty_dir() { | 301 base::FilePath empty_dir() { |
| 299 return empty_dir_; | 302 return empty_dir_; |
| 300 } | 303 } |
| 301 | 304 |
| 302 base::FilePath dcim_dir() { | 305 base::FilePath dcim_dir() { |
| 303 return dcim_dir_; | 306 return dcim_dir_; |
| 304 } | 307 } |
| 305 | 308 |
| 306 TestMediaFileSystemContext* test_file_system_context() { | 309 TestMediaFileSystemContext* test_file_system_context() { |
| 307 return test_file_system_context_; | 310 return test_file_system_context_; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 #if defined OS_CHROMEOS | 382 #if defined OS_CHROMEOS |
| 380 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; | 383 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; |
| 381 chromeos::ScopedTestCrosSettings test_cros_settings_; | 384 chromeos::ScopedTestCrosSettings test_cros_settings_; |
| 382 chromeos::ScopedTestUserManager test_user_manager_; | 385 chromeos::ScopedTestUserManager test_user_manager_; |
| 383 #endif | 386 #endif |
| 384 | 387 |
| 385 // TODO(gbillock): Eliminate windows-specific code from this test. | 388 // TODO(gbillock): Eliminate windows-specific code from this test. |
| 386 #if defined(OS_WIN) | 389 #if defined(OS_WIN) |
| 387 scoped_ptr<test::TestStorageMonitorWin> monitor_; | 390 scoped_ptr<test::TestStorageMonitorWin> monitor_; |
| 388 #else | 391 #else |
| 389 chrome::test::TestStorageMonitor monitor_; | 392 scoped_ptr<chrome::test::TestStorageMonitor> monitor_; |
| 390 #endif | 393 #endif |
| 391 | 394 |
| 392 MockProfileSharedRenderProcessHostFactory rph_factory_; | 395 MockProfileSharedRenderProcessHostFactory rph_factory_; |
| 393 | 396 |
| 394 ScopedVector<ProfileState> profile_states_; | 397 ScopedVector<ProfileState> profile_states_; |
| 395 | 398 |
| 396 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistryTest); | 399 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistryTest); |
| 397 }; | 400 }; |
| 398 | 401 |
| 399 namespace { | 402 namespace { |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 590 for (size_t i = 0; i < profile_count; ++i) { | 593 for (size_t i = 0; i < profile_count; ++i) { |
| 591 ProfileState* state = new ProfileState(&rph_factory_); | 594 ProfileState* state = new ProfileState(&rph_factory_); |
| 592 profile_states_.push_back(state); | 595 profile_states_.push_back(state); |
| 593 } | 596 } |
| 594 } | 597 } |
| 595 | 598 |
| 596 ProfileState* MediaFileSystemRegistryTest::GetProfileState(size_t i) { | 599 ProfileState* MediaFileSystemRegistryTest::GetProfileState(size_t i) { |
| 597 return profile_states_[i]; | 600 return profile_states_[i]; |
| 598 } | 601 } |
| 599 | 602 |
| 603 void GetPreferencesForwarder(base::WaitableEvent* event, | |
|
vandebo (ex-Chrome)
2013/05/16 18:56:27
As far as I can tell, you don't need a waitable ev
Greg Billock
2013/05/16 23:27:55
I was thinking I'd have to run this in another thr
vandebo (ex-Chrome)
2013/05/17 22:19:58
Yes, please remove the waitable events, it makes t
Greg Billock
2013/05/18 00:02:26
Done.
| |
| 604 MediaGalleriesPreferences** prefs_ptr, | |
| 605 MediaGalleriesPreferences* prefs) { | |
| 606 *prefs_ptr = prefs; | |
| 607 event->Signal(); | |
| 608 } | |
| 609 | |
| 610 MediaGalleriesPreferences* MediaFileSystemRegistryTest::GetPreferences( | |
| 611 Profile* profile) { | |
| 612 base::WaitableEvent pref_event(false, false); | |
| 613 MediaGalleriesPreferences* prefs; | |
| 614 GetMediaFileSystemRegistry()->GetPreferencesAsync( | |
| 615 profile, base::Bind(&GetPreferencesForwarder, &pref_event, &prefs)); | |
| 616 base::RunLoop().RunUntilIdle(); | |
| 617 EXPECT_TRUE(pref_event.IsSignaled()); | |
| 618 return prefs; | |
| 619 } | |
| 620 | |
| 600 std::string MediaFileSystemRegistryTest::AddUserGallery( | 621 std::string MediaFileSystemRegistryTest::AddUserGallery( |
| 601 MediaStorageUtil::Type type, | 622 MediaStorageUtil::Type type, |
| 602 const std::string& unique_id, | 623 const std::string& unique_id, |
| 603 const base::FilePath& path) { | 624 const base::FilePath& path) { |
| 604 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | 625 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); |
| 605 string16 name = path.LossyDisplayName(); | 626 string16 name = path.LossyDisplayName(); |
| 606 DCHECK(!MediaStorageUtil::IsMediaDevice(device_id)); | 627 DCHECK(!MediaStorageUtil::IsMediaDevice(device_id)); |
| 607 | 628 |
| 608 for (size_t i = 0; i < profile_states_.size(); ++i) { | 629 for (size_t i = 0; i < profile_states_.size(); ++i) { |
| 609 profile_states_[i]->GetMediaGalleriesPrefs()->AddGalleryWithName( | 630 profile_states_[i]->GetMediaGalleriesPrefs()->AddGalleryWithName( |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 717 MediaFileSystemInfo info(path.LossyDisplayName(), path, std::string(), | 738 MediaFileSystemInfo info(path.LossyDisplayName(), path, std::string(), |
| 718 0, std::string(), false, false); | 739 0, std::string(), false, false); |
| 719 result.push_back(info); | 740 result.push_back(info); |
| 720 } | 741 } |
| 721 } | 742 } |
| 722 std::sort(result.begin(), result.end(), MediaFileSystemInfoComparator); | 743 std::sort(result.begin(), result.end(), MediaFileSystemInfoComparator); |
| 723 return result; | 744 return result; |
| 724 } | 745 } |
| 725 | 746 |
| 726 void MediaFileSystemRegistryTest::SetUp() { | 747 void MediaFileSystemRegistryTest::SetUp() { |
| 727 #if defined(OS_WIN) | 748 #if !defined(OS_WIN) |
| 749 monitor_.reset(new test::TestStorageMonitor()); | |
| 750 base::WaitableEvent wait_for_init(false, false); | |
| 751 monitor_->Initialize(base::Bind(&base::WaitableEvent::Signal, | |
| 752 base::Unretained(&wait_for_init))); | |
| 753 monitor_->MarkInitialized(); | |
| 754 base::RunLoop().RunUntilIdle(); | |
| 755 DCHECK(wait_for_init.IsSignaled()); | |
| 756 #else | |
| 728 test::TestPortableDeviceWatcherWin* portable_device_watcher = | 757 test::TestPortableDeviceWatcherWin* portable_device_watcher = |
| 729 new test::TestPortableDeviceWatcherWin; | 758 new test::TestPortableDeviceWatcherWin; |
| 730 test::TestVolumeMountWatcherWin* mount_watcher = | 759 test::TestVolumeMountWatcherWin* mount_watcher = |
| 731 new test::TestVolumeMountWatcherWin; | 760 new test::TestVolumeMountWatcherWin; |
| 732 portable_device_watcher->set_use_dummy_mtp_storage_info(true); | 761 portable_device_watcher->set_use_dummy_mtp_storage_info(true); |
| 733 monitor_.reset(new test::TestStorageMonitorWin( | 762 monitor_.reset(new test::TestStorageMonitorWin( |
| 734 mount_watcher, portable_device_watcher)); | 763 mount_watcher, portable_device_watcher)); |
| 735 monitor_->Init(); | 764 base::WaitableEvent wait_for_init(false, false); |
| 765 wait_for_init.Reset(); | |
| 766 monitor_->Initialize(base::Bind(&base::WaitableEvent::Signal, | |
| 767 base::Unretained(&wait_for_init))); | |
| 736 // TODO(gbillock): Replace this with the correct event notification | 768 // TODO(gbillock): Replace this with the correct event notification |
| 737 // on the storage monitor finishing the startup scan when that exists. | 769 // on the storage monitor finishing the startup scan when that exists. |
| 738 base::RunLoop().RunUntilIdle(); | 770 base::RunLoop().RunUntilIdle(); |
| 739 mount_watcher->FlushWorkerPoolForTesting(); | 771 mount_watcher->FlushWorkerPoolForTesting(); |
| 740 base::RunLoop().RunUntilIdle(); | 772 base::RunLoop().RunUntilIdle(); |
| 741 mount_watcher->FlushWorkerPoolForTesting(); | 773 mount_watcher->FlushWorkerPoolForTesting(); |
| 742 base::RunLoop().RunUntilIdle(); | 774 base::RunLoop().RunUntilIdle(); |
| 775 DCHECK(wait_for_init.IsSignaled()); | |
| 743 #endif | 776 #endif |
| 744 | 777 |
| 745 ChromeRenderViewHostTestHarness::SetUp(); | 778 ChromeRenderViewHostTestHarness::SetUp(); |
| 746 DeleteContents(); | 779 DeleteContents(); |
| 747 SetRenderProcessHostFactory(&rph_factory_); | 780 SetRenderProcessHostFactory(&rph_factory_); |
| 748 | 781 |
| 749 test_file_system_context_ = new TestMediaFileSystemContext( | 782 test_file_system_context_ = new TestMediaFileSystemContext( |
| 750 g_browser_process->media_file_system_registry()); | 783 g_browser_process->media_file_system_registry()); |
| 751 | 784 |
| 752 ASSERT_TRUE(galleries_dir_.CreateUniqueTempDir()); | 785 ASSERT_TRUE(galleries_dir_.CreateUniqueTempDir()); |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 847 | 880 |
| 848 // Attach a device. | 881 // Attach a device. |
| 849 const std::string device_id = AttachDevice( | 882 const std::string device_id = AttachDevice( |
| 850 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, | 883 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, |
| 851 "removable_dcim_fake_id", | 884 "removable_dcim_fake_id", |
| 852 dcim_dir()); | 885 dcim_dir()); |
| 853 EXPECT_EQ(gallery_count + 1, GetAutoAddedGalleries(profile_state).size()); | 886 EXPECT_EQ(gallery_count + 1, GetAutoAddedGalleries(profile_state).size()); |
| 854 | 887 |
| 855 // Forget the device. | 888 // Forget the device. |
| 856 bool forget_gallery = false; | 889 bool forget_gallery = false; |
| 857 MediaGalleriesPreferences* prefs = | 890 MediaGalleriesPreferences* prefs = GetPreferences(profile_state->profile()); |
| 858 GetMediaFileSystemRegistry()->GetPreferences(profile_state->profile()); | |
| 859 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); | 891 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); |
| 860 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); | 892 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); |
| 861 it != galleries.end(); ++it) { | 893 it != galleries.end(); ++it) { |
| 862 if (it->second.device_id == device_id) { | 894 if (it->second.device_id == device_id) { |
| 863 prefs->ForgetGalleryById(it->first); | 895 prefs->ForgetGalleryById(it->first); |
| 864 forget_gallery = true; | 896 forget_gallery = true; |
| 865 break; | 897 break; |
| 866 } | 898 } |
| 867 } | 899 } |
| 868 MessageLoop::current()->RunUntilIdle(); | 900 MessageLoop::current()->RunUntilIdle(); |
| 869 EXPECT_TRUE(forget_gallery); | 901 EXPECT_TRUE(forget_gallery); |
| 870 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); | 902 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); |
| 871 | 903 |
| 872 // Call GetPreferences() and the gallery count should not change. | 904 // Call GetPreferences() and the gallery count should not change. |
| 873 GetMediaFileSystemRegistry()->GetPreferences(profile_state->profile()); | 905 prefs = GetPreferences(profile_state->profile()); |
| 874 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); | 906 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); |
| 875 } | 907 } |
| 876 | 908 |
| 877 TEST_F(MediaFileSystemRegistryTest, GalleryNameDefault) { | 909 TEST_F(MediaFileSystemRegistryTest, GalleryNameDefault) { |
| 878 FSInfoMap galleries_info; | 910 FSInfoMap galleries_info; |
| 879 InitForGalleriesInfoTest(&galleries_info); | 911 InitForGalleriesInfoTest(&galleries_info); |
| 880 | 912 |
| 881 for (FSInfoMap::const_iterator it = galleries_info.begin(); | 913 for (FSInfoMap::const_iterator it = galleries_info.begin(); |
| 882 it != galleries_info.end(); | 914 it != galleries_info.end(); |
| 883 ++it) { | 915 ++it) { |
| 884 CheckGalleryInfo(it->second, test_file_system_context(), &it->second.name, | 916 CheckGalleryInfo(it->second, test_file_system_context(), &it->second.name, |
| 885 it->second.path, false, false); | 917 it->second.path, false, false); |
| 886 } | 918 } |
| 887 } | 919 } |
| 888 | 920 |
| 889 // TODO(gbillock): Put the platform-specific parts of this test in tests | 921 // TODO(gbillock): Put the platform-specific parts of this test in tests |
| 890 // for those classes, not here. This test, internally, ends up creating an | 922 // for those classes, not here. This test, internally, ends up creating an |
| 891 // MTP delegate. | 923 // MTP delegate. (Probably ./win/mtp_device_delegate_impl_win_unittest) |
| 892 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) | 924 #if defined(SUPPORT_MTP_DEVICE_FILESYSTEM) |
| 893 #if !defined(OS_MACOSX) | 925 #if !defined(OS_MACOSX) |
| 894 TEST_F(MediaFileSystemRegistryTest, GalleryNameMTP) { | 926 TEST_F(MediaFileSystemRegistryTest, GalleryNameMTP) { |
| 895 FSInfoMap galleries_info; | 927 FSInfoMap galleries_info; |
| 896 InitForGalleriesInfoTest(&galleries_info); | 928 InitForGalleriesInfoTest(&galleries_info); |
| 897 | 929 |
| 898 #if defined(OS_WIN) | 930 #if defined(OS_WIN) |
| 899 base::FilePath location( | 931 base::FilePath location( |
| 900 PortableDeviceWatcherWin::GetStoragePathFromStorageId( | 932 PortableDeviceWatcherWin::GetStoragePathFromStorageId( |
| 901 test::TestPortableDeviceWatcherWin::kStorageUniqueIdA)); | 933 test::TestPortableDeviceWatcherWin::kStorageUniqueIdA)); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 976 pref_info_with_relpath.device_id = device_id; | 1008 pref_info_with_relpath.device_id = device_id; |
| 977 EXPECT_EQ(dcim_dir().Append(pref_info_with_relpath.path).value(), | 1009 EXPECT_EQ(dcim_dir().Append(pref_info_with_relpath.path).value(), |
| 978 pref_info_with_relpath.AbsolutePath().value()); | 1010 pref_info_with_relpath.AbsolutePath().value()); |
| 979 | 1011 |
| 980 DetachDevice(device_id); | 1012 DetachDevice(device_id); |
| 981 EXPECT_TRUE(pref_info.AbsolutePath().empty()); | 1013 EXPECT_TRUE(pref_info.AbsolutePath().empty()); |
| 982 EXPECT_TRUE(pref_info_with_relpath.AbsolutePath().empty()); | 1014 EXPECT_TRUE(pref_info_with_relpath.AbsolutePath().empty()); |
| 983 } | 1015 } |
| 984 | 1016 |
| 985 } // namespace chrome | 1017 } // namespace chrome |
| OLD | NEW |