Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: chrome/browser/media_galleries/media_file_system_registry_unittest.cc

Issue 14556015: [Media Galleries] Lazily initialize the storage monitor. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Adjustments Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 254 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 285
285 class MediaFileSystemRegistryTest : public ChromeRenderViewHostTestHarness { 286 class MediaFileSystemRegistryTest : public ChromeRenderViewHostTestHarness {
286 public: 287 public:
287 MediaFileSystemRegistryTest(); 288 MediaFileSystemRegistryTest();
288 virtual ~MediaFileSystemRegistryTest() {} 289 virtual ~MediaFileSystemRegistryTest() {}
289 290
290 void CreateProfileState(size_t profile_count); 291 void CreateProfileState(size_t profile_count);
291 292
292 ProfileState* GetProfileState(size_t i); 293 ProfileState* GetProfileState(size_t i);
293 294
295 MediaGalleriesPreferences* GetPreferences(Profile* profile);
296
294 base::FilePath empty_dir() { 297 base::FilePath empty_dir() {
295 return empty_dir_; 298 return empty_dir_;
296 } 299 }
297 300
298 base::FilePath dcim_dir() { 301 base::FilePath dcim_dir() {
299 return dcim_dir_; 302 return dcim_dir_;
300 } 303 }
301 304
302 TestMediaFileSystemContext* test_file_system_context() { 305 TestMediaFileSystemContext* test_file_system_context() {
303 return test_file_system_context_; 306 return test_file_system_context_;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 #if defined OS_CHROMEOS 378 #if defined OS_CHROMEOS
376 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_; 379 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
377 chromeos::ScopedTestCrosSettings test_cros_settings_; 380 chromeos::ScopedTestCrosSettings test_cros_settings_;
378 chromeos::ScopedTestUserManager test_user_manager_; 381 chromeos::ScopedTestUserManager test_user_manager_;
379 #endif 382 #endif
380 383
381 // TODO(gbillock): Eliminate windows-specific code from this test. 384 // TODO(gbillock): Eliminate windows-specific code from this test.
382 #if defined(OS_WIN) 385 #if defined(OS_WIN)
383 scoped_ptr<test::TestStorageMonitorWin> monitor_; 386 scoped_ptr<test::TestStorageMonitorWin> monitor_;
384 #else 387 #else
385 chrome::test::TestStorageMonitor monitor_; 388 scoped_ptr<chrome::test::TestStorageMonitor> monitor_;
386 #endif 389 #endif
387 390
388 MockProfileSharedRenderProcessHostFactory rph_factory_; 391 MockProfileSharedRenderProcessHostFactory rph_factory_;
389 392
390 ScopedVector<ProfileState> profile_states_; 393 ScopedVector<ProfileState> profile_states_;
391 394
392 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistryTest); 395 DISALLOW_COPY_AND_ASSIGN(MediaFileSystemRegistryTest);
393 }; 396 };
394 397
395 namespace { 398 namespace {
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 for (size_t i = 0; i < profile_count; ++i) { 589 for (size_t i = 0; i < profile_count; ++i) {
587 ProfileState* state = new ProfileState(&rph_factory_); 590 ProfileState* state = new ProfileState(&rph_factory_);
588 profile_states_.push_back(state); 591 profile_states_.push_back(state);
589 } 592 }
590 } 593 }
591 594
592 ProfileState* MediaFileSystemRegistryTest::GetProfileState(size_t i) { 595 ProfileState* MediaFileSystemRegistryTest::GetProfileState(size_t i) {
593 return profile_states_[i]; 596 return profile_states_[i];
594 } 597 }
595 598
599 MediaGalleriesPreferences* MediaFileSystemRegistryTest::GetPreferences(
600 Profile* profile) {
601 return GetMediaFileSystemRegistry()->GetPreferences(profile);
vandebo (ex-Chrome) 2013/05/22 21:40:34 Isn't this a recursive call?
Greg Billock 2013/05/23 00:55:59 No, this is in the test. ;-)
602 }
603
596 std::string MediaFileSystemRegistryTest::AddUserGallery( 604 std::string MediaFileSystemRegistryTest::AddUserGallery(
597 StorageInfo::Type type, 605 StorageInfo::Type type,
598 const std::string& unique_id, 606 const std::string& unique_id,
599 const base::FilePath& path) { 607 const base::FilePath& path) {
600 std::string device_id = StorageInfo::MakeDeviceId(type, unique_id); 608 std::string device_id = StorageInfo::MakeDeviceId(type, unique_id);
601 string16 name = path.LossyDisplayName(); 609 string16 name = path.LossyDisplayName();
602 DCHECK(!StorageInfo::IsMediaDevice(device_id)); 610 DCHECK(!StorageInfo::IsMediaDevice(device_id));
603 611
604 for (size_t i = 0; i < profile_states_.size(); ++i) { 612 for (size_t i = 0; i < profile_states_.size(); ++i) {
605 profile_states_[i]->GetMediaGalleriesPrefs()->AddGalleryWithName( 613 profile_states_[i]->GetMediaGalleriesPrefs()->AddGalleryWithName(
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
713 MediaFileSystemInfo info(path.LossyDisplayName(), path, std::string(), 721 MediaFileSystemInfo info(path.LossyDisplayName(), path, std::string(),
714 0, std::string(), false, false); 722 0, std::string(), false, false);
715 result.push_back(info); 723 result.push_back(info);
716 } 724 }
717 } 725 }
718 std::sort(result.begin(), result.end(), MediaFileSystemInfoComparator); 726 std::sort(result.begin(), result.end(), MediaFileSystemInfoComparator);
719 return result; 727 return result;
720 } 728 }
721 729
722 void MediaFileSystemRegistryTest::SetUp() { 730 void MediaFileSystemRegistryTest::SetUp() {
723 #if defined(OS_WIN) 731 #if !defined(OS_WIN)
732 monitor_.reset(new test::TestStorageMonitor());
vandebo (ex-Chrome) 2013/05/22 21:40:34 Looks like the waitable event found it's way back.
Greg Billock 2013/05/23 00:55:59 Grrr. I must have branched without checking in som
733 base::WaitableEvent wait_for_init(false, false);
734 monitor_->Initialize(base::Bind(&base::WaitableEvent::Signal,
735 base::Unretained(&wait_for_init)));
736 monitor_->MarkInitialized();
737 base::RunLoop().RunUntilIdle();
738 DCHECK(wait_for_init.IsSignaled());
739 #else
724 test::TestPortableDeviceWatcherWin* portable_device_watcher = 740 test::TestPortableDeviceWatcherWin* portable_device_watcher =
725 new test::TestPortableDeviceWatcherWin; 741 new test::TestPortableDeviceWatcherWin;
726 test::TestVolumeMountWatcherWin* mount_watcher = 742 test::TestVolumeMountWatcherWin* mount_watcher =
727 new test::TestVolumeMountWatcherWin; 743 new test::TestVolumeMountWatcherWin;
728 portable_device_watcher->set_use_dummy_mtp_storage_info(true); 744 portable_device_watcher->set_use_dummy_mtp_storage_info(true);
729 monitor_.reset(new test::TestStorageMonitorWin( 745 monitor_.reset(new test::TestStorageMonitorWin(
730 mount_watcher, portable_device_watcher)); 746 mount_watcher, portable_device_watcher));
731 monitor_->Init(); 747 base::WaitableEvent wait_for_init(false, false);
748 wait_for_init.Reset();
749 monitor_->Initialize(base::Bind(&base::WaitableEvent::Signal,
750 base::Unretained(&wait_for_init)));
732 // TODO(gbillock): Replace this with the correct event notification 751 // TODO(gbillock): Replace this with the correct event notification
733 // on the storage monitor finishing the startup scan when that exists. 752 // on the storage monitor finishing the startup scan when that exists.
734 base::RunLoop().RunUntilIdle(); 753 base::RunLoop().RunUntilIdle();
735 mount_watcher->FlushWorkerPoolForTesting(); 754 mount_watcher->FlushWorkerPoolForTesting();
736 base::RunLoop().RunUntilIdle(); 755 base::RunLoop().RunUntilIdle();
737 mount_watcher->FlushWorkerPoolForTesting(); 756 mount_watcher->FlushWorkerPoolForTesting();
738 base::RunLoop().RunUntilIdle(); 757 base::RunLoop().RunUntilIdle();
758 DCHECK(wait_for_init.IsSignaled());
739 #endif 759 #endif
740 760
741 ChromeRenderViewHostTestHarness::SetUp(); 761 ChromeRenderViewHostTestHarness::SetUp();
742 DeleteContents(); 762 DeleteContents();
743 SetRenderProcessHostFactory(&rph_factory_); 763 SetRenderProcessHostFactory(&rph_factory_);
744 764
745 test_file_system_context_ = new TestMediaFileSystemContext( 765 test_file_system_context_ = new TestMediaFileSystemContext(
746 g_browser_process->media_file_system_registry()); 766 g_browser_process->media_file_system_registry());
747 767
748 ASSERT_TRUE(galleries_dir_.CreateUniqueTempDir()); 768 ASSERT_TRUE(galleries_dir_.CreateUniqueTempDir());
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 863
844 // Attach a device. 864 // Attach a device.
845 const std::string device_id = AttachDevice( 865 const std::string device_id = AttachDevice(
846 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM, 866 StorageInfo::REMOVABLE_MASS_STORAGE_WITH_DCIM,
847 "removable_dcim_fake_id", 867 "removable_dcim_fake_id",
848 dcim_dir()); 868 dcim_dir());
849 EXPECT_EQ(gallery_count + 1, GetAutoAddedGalleries(profile_state).size()); 869 EXPECT_EQ(gallery_count + 1, GetAutoAddedGalleries(profile_state).size());
850 870
851 // Forget the device. 871 // Forget the device.
852 bool forget_gallery = false; 872 bool forget_gallery = false;
853 MediaGalleriesPreferences* prefs = 873 MediaGalleriesPreferences* prefs = GetPreferences(profile_state->profile());
854 GetMediaFileSystemRegistry()->GetPreferences(profile_state->profile());
855 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); 874 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries();
856 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); 875 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin();
857 it != galleries.end(); ++it) { 876 it != galleries.end(); ++it) {
858 if (it->second.device_id == device_id) { 877 if (it->second.device_id == device_id) {
859 prefs->ForgetGalleryById(it->first); 878 prefs->ForgetGalleryById(it->first);
860 forget_gallery = true; 879 forget_gallery = true;
861 break; 880 break;
862 } 881 }
863 } 882 }
864 MessageLoop::current()->RunUntilIdle(); 883 MessageLoop::current()->RunUntilIdle();
865 EXPECT_TRUE(forget_gallery); 884 EXPECT_TRUE(forget_gallery);
866 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); 885 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size());
867 886
868 // Call GetPreferences() and the gallery count should not change. 887 // Call GetPreferences() and the gallery count should not change.
869 GetMediaFileSystemRegistry()->GetPreferences(profile_state->profile()); 888 prefs = GetPreferences(profile_state->profile());
870 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); 889 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size());
871 } 890 }
872 891
873 TEST_F(MediaFileSystemRegistryTest, GalleryNameDefault) { 892 TEST_F(MediaFileSystemRegistryTest, GalleryNameDefault) {
874 FSInfoMap galleries_info; 893 FSInfoMap galleries_info;
875 InitForGalleriesInfoTest(&galleries_info); 894 InitForGalleriesInfoTest(&galleries_info);
876 895
877 for (FSInfoMap::const_iterator it = galleries_info.begin(); 896 for (FSInfoMap::const_iterator it = galleries_info.begin();
878 it != galleries_info.end(); 897 it != galleries_info.end();
879 ++it) { 898 ++it) {
880 CheckGalleryInfo(it->second, test_file_system_context(), &it->second.name, 899 CheckGalleryInfo(it->second, test_file_system_context(), &it->second.name,
881 it->second.path, false, false); 900 it->second.path, false, false);
882 } 901 }
883 } 902 }
884 903
885 // TODO(gbillock): Put the platform-specific parts of this test in tests 904 // TODO(gbillock): Put the platform-specific parts of this test in tests
886 // for those classes, not here. This test, internally, ends up creating an 905 // for those classes, not here. This test, internally, ends up creating an
887 // MTP delegate. 906 // MTP delegate. (Probably ./win/mtp_device_delegate_impl_win_unittest)
888 #if !defined(OS_MACOSX) 907 #if !defined(OS_MACOSX)
889 TEST_F(MediaFileSystemRegistryTest, GalleryNameMTP) { 908 TEST_F(MediaFileSystemRegistryTest, GalleryNameMTP) {
890 FSInfoMap galleries_info; 909 FSInfoMap galleries_info;
891 InitForGalleriesInfoTest(&galleries_info); 910 InitForGalleriesInfoTest(&galleries_info);
892 911
893 #if defined(OS_WIN) 912 #if defined(OS_WIN)
894 base::FilePath location( 913 base::FilePath location(
895 PortableDeviceWatcherWin::GetStoragePathFromStorageId( 914 PortableDeviceWatcherWin::GetStoragePathFromStorageId(
896 test::TestPortableDeviceWatcherWin::kStorageUniqueIdA)); 915 test::TestPortableDeviceWatcherWin::kStorageUniqueIdA));
897 #else 916 #else
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
970 pref_info_with_relpath.device_id = device_id; 989 pref_info_with_relpath.device_id = device_id;
971 EXPECT_EQ(dcim_dir().Append(pref_info_with_relpath.path).value(), 990 EXPECT_EQ(dcim_dir().Append(pref_info_with_relpath.path).value(),
972 pref_info_with_relpath.AbsolutePath().value()); 991 pref_info_with_relpath.AbsolutePath().value());
973 992
974 DetachDevice(device_id); 993 DetachDevice(device_id);
975 EXPECT_TRUE(pref_info.AbsolutePath().empty()); 994 EXPECT_TRUE(pref_info.AbsolutePath().empty());
976 EXPECT_TRUE(pref_info_with_relpath.AbsolutePath().empty()); 995 EXPECT_TRUE(pref_info_with_relpath.AbsolutePath().empty());
977 } 996 }
978 997
979 } // namespace chrome 998 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698