| 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" |
| (...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 | 481 |
| 482 ProfileState::~ProfileState() { | 482 ProfileState::~ProfileState() { |
| 483 // TestExtensionSystem uses DeleteSoon, so we need to delete the profiles | 483 // TestExtensionSystem uses DeleteSoon, so we need to delete the profiles |
| 484 // and then run the message queue to clean up. But first we have to | 484 // and then run the message queue to clean up. But first we have to |
| 485 // delete everything that references the profile. | 485 // delete everything that references the profile. |
| 486 single_web_contents_.reset(); | 486 single_web_contents_.reset(); |
| 487 shared_web_contents1_.reset(); | 487 shared_web_contents1_.reset(); |
| 488 shared_web_contents2_.reset(); | 488 shared_web_contents2_.reset(); |
| 489 profile_.reset(); | 489 profile_.reset(); |
| 490 | 490 |
| 491 MessageLoop::current()->RunUntilIdle(); | 491 base::MessageLoop::current()->RunUntilIdle(); |
| 492 } | 492 } |
| 493 | 493 |
| 494 MediaGalleriesPreferences* ProfileState::GetMediaGalleriesPrefs() { | 494 MediaGalleriesPreferences* ProfileState::GetMediaGalleriesPrefs() { |
| 495 return MediaGalleriesPreferencesFactory::GetForProfile(profile_.get()); | 495 return MediaGalleriesPreferencesFactory::GetForProfile(profile_.get()); |
| 496 } | 496 } |
| 497 | 497 |
| 498 void ProfileState::CheckGalleries( | 498 void ProfileState::CheckGalleries( |
| 499 const std::string& test, | 499 const std::string& test, |
| 500 const std::vector<MediaFileSystemInfo>& regular_extension_galleries, | 500 const std::vector<MediaFileSystemInfo>& regular_extension_galleries, |
| 501 const std::vector<MediaFileSystemInfo>& all_extension_galleries) { | 501 const std::vector<MediaFileSystemInfo>& all_extension_galleries) { |
| 502 content::RenderViewHost* rvh = single_web_contents_->GetRenderViewHost(); | 502 content::RenderViewHost* rvh = single_web_contents_->GetRenderViewHost(); |
| 503 MediaFileSystemRegistry* registry = | 503 MediaFileSystemRegistry* registry = |
| 504 g_browser_process->media_file_system_registry(); | 504 g_browser_process->media_file_system_registry(); |
| 505 | 505 |
| 506 // No Media Galleries permissions. | 506 // No Media Galleries permissions. |
| 507 std::vector<MediaFileSystemInfo> empty_expectation; | 507 std::vector<MediaFileSystemInfo> empty_expectation; |
| 508 registry->GetMediaFileSystemsForExtension( | 508 registry->GetMediaFileSystemsForExtension( |
| 509 rvh, no_permissions_extension_.get(), | 509 rvh, no_permissions_extension_.get(), |
| 510 base::Bind(&ProfileState::CompareResults, base::Unretained(this), | 510 base::Bind(&ProfileState::CompareResults, base::Unretained(this), |
| 511 base::StringPrintf("%s (no permission)", test.c_str()), | 511 base::StringPrintf("%s (no permission)", test.c_str()), |
| 512 base::ConstRef(empty_expectation))); | 512 base::ConstRef(empty_expectation))); |
| 513 MessageLoop::current()->RunUntilIdle(); | 513 base::MessageLoop::current()->RunUntilIdle(); |
| 514 EXPECT_EQ(1, GetAndClearComparisonCount()); | 514 EXPECT_EQ(1, GetAndClearComparisonCount()); |
| 515 | 515 |
| 516 // Read permission only. | 516 // Read permission only. |
| 517 registry->GetMediaFileSystemsForExtension( | 517 registry->GetMediaFileSystemsForExtension( |
| 518 rvh, regular_permission_extension_.get(), | 518 rvh, regular_permission_extension_.get(), |
| 519 base::Bind(&ProfileState::CompareResults, base::Unretained(this), | 519 base::Bind(&ProfileState::CompareResults, base::Unretained(this), |
| 520 base::StringPrintf("%s (regular permission)", test.c_str()), | 520 base::StringPrintf("%s (regular permission)", test.c_str()), |
| 521 base::ConstRef(regular_extension_galleries))); | 521 base::ConstRef(regular_extension_galleries))); |
| 522 MessageLoop::current()->RunUntilIdle(); | 522 base::MessageLoop::current()->RunUntilIdle(); |
| 523 EXPECT_EQ(1, GetAndClearComparisonCount()); | 523 EXPECT_EQ(1, GetAndClearComparisonCount()); |
| 524 | 524 |
| 525 // All galleries permission. | 525 // All galleries permission. |
| 526 registry->GetMediaFileSystemsForExtension( | 526 registry->GetMediaFileSystemsForExtension( |
| 527 rvh, all_permission_extension_.get(), | 527 rvh, all_permission_extension_.get(), |
| 528 base::Bind(&ProfileState::CompareResults, base::Unretained(this), | 528 base::Bind(&ProfileState::CompareResults, base::Unretained(this), |
| 529 base::StringPrintf("%s (all permission)", test.c_str()), | 529 base::StringPrintf("%s (all permission)", test.c_str()), |
| 530 base::ConstRef(all_extension_galleries))); | 530 base::ConstRef(all_extension_galleries))); |
| 531 MessageLoop::current()->RunUntilIdle(); | 531 base::MessageLoop::current()->RunUntilIdle(); |
| 532 EXPECT_EQ(1, GetAndClearComparisonCount()); | 532 EXPECT_EQ(1, GetAndClearComparisonCount()); |
| 533 } | 533 } |
| 534 | 534 |
| 535 FSInfoMap ProfileState::GetGalleriesInfo(extensions::Extension* extension) { | 535 FSInfoMap ProfileState::GetGalleriesInfo(extensions::Extension* extension) { |
| 536 content::RenderViewHost* rvh = single_web_contents_->GetRenderViewHost(); | 536 content::RenderViewHost* rvh = single_web_contents_->GetRenderViewHost(); |
| 537 FSInfoMap results; | 537 FSInfoMap results; |
| 538 MediaFileSystemRegistry* registry = | 538 MediaFileSystemRegistry* registry = |
| 539 g_browser_process->media_file_system_registry(); | 539 g_browser_process->media_file_system_registry(); |
| 540 registry->GetMediaFileSystemsForExtension( | 540 registry->GetMediaFileSystemsForExtension( |
| 541 rvh, extension, | 541 rvh, extension, |
| 542 base::Bind(&GetGalleryInfoCallback, base::Unretained(&results))); | 542 base::Bind(&GetGalleryInfoCallback, base::Unretained(&results))); |
| 543 MessageLoop::current()->RunUntilIdle(); | 543 base::MessageLoop::current()->RunUntilIdle(); |
| 544 return results; | 544 return results; |
| 545 } | 545 } |
| 546 | 546 |
| 547 extensions::Extension* ProfileState::all_permission_extension() { | 547 extensions::Extension* ProfileState::all_permission_extension() { |
| 548 return all_permission_extension_.get(); | 548 return all_permission_extension_.get(); |
| 549 } | 549 } |
| 550 | 550 |
| 551 extensions::Extension* ProfileState::regular_permission_extension() { | 551 extensions::Extension* ProfileState::regular_permission_extension() { |
| 552 return regular_permission_extension_.get(); | 552 return regular_permission_extension_.get(); |
| 553 } | 553 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 581 return result; | 581 return result; |
| 582 } | 582 } |
| 583 | 583 |
| 584 } // namespace | 584 } // namespace |
| 585 | 585 |
| 586 ///////////////////////////////// | 586 ///////////////////////////////// |
| 587 // MediaFileSystemRegistryTest // | 587 // MediaFileSystemRegistryTest // |
| 588 ///////////////////////////////// | 588 ///////////////////////////////// |
| 589 | 589 |
| 590 MediaFileSystemRegistryTest::MediaFileSystemRegistryTest() | 590 MediaFileSystemRegistryTest::MediaFileSystemRegistryTest() |
| 591 : ui_thread_(content::BrowserThread::UI, MessageLoop::current()), | 591 : ui_thread_(content::BrowserThread::UI, base::MessageLoop::current()), |
| 592 file_thread_(content::BrowserThread::FILE, MessageLoop::current()), | 592 file_thread_(content::BrowserThread::FILE, base::MessageLoop::current()), |
| 593 permissions_info_(extensions::ChromeAPIPermissions()) { | 593 permissions_info_(extensions::ChromeAPIPermissions()) {} |
| 594 } | |
| 595 | 594 |
| 596 void MediaFileSystemRegistryTest::CreateProfileState(size_t profile_count) { | 595 void MediaFileSystemRegistryTest::CreateProfileState(size_t profile_count) { |
| 597 for (size_t i = 0; i < profile_count; ++i) { | 596 for (size_t i = 0; i < profile_count; ++i) { |
| 598 ProfileState* state = new ProfileState(&rph_factory_); | 597 ProfileState* state = new ProfileState(&rph_factory_); |
| 599 profile_states_.push_back(state); | 598 profile_states_.push_back(state); |
| 600 } | 599 } |
| 601 } | 600 } |
| 602 | 601 |
| 603 ProfileState* MediaFileSystemRegistryTest::GetProfileState(size_t i) { | 602 ProfileState* MediaFileSystemRegistryTest::GetProfileState(size_t i) { |
| 604 return profile_states_[i]; | 603 return profile_states_[i]; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 620 } | 619 } |
| 621 | 620 |
| 622 std::string MediaFileSystemRegistryTest::AttachDevice( | 621 std::string MediaFileSystemRegistryTest::AttachDevice( |
| 623 MediaStorageUtil::Type type, | 622 MediaStorageUtil::Type type, |
| 624 const std::string& unique_id, | 623 const std::string& unique_id, |
| 625 const base::FilePath& location) { | 624 const base::FilePath& location) { |
| 626 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | 625 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); |
| 627 DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); | 626 DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); |
| 628 string16 name = location.LossyDisplayName(); | 627 string16 name = location.LossyDisplayName(); |
| 629 ProcessAttach(device_id, name, location.value()); | 628 ProcessAttach(device_id, name, location.value()); |
| 630 MessageLoop::current()->RunUntilIdle(); | 629 base::MessageLoop::current()->RunUntilIdle(); |
| 631 return device_id; | 630 return device_id; |
| 632 } | 631 } |
| 633 | 632 |
| 634 void MediaFileSystemRegistryTest::DetachDevice(const std::string& device_id) { | 633 void MediaFileSystemRegistryTest::DetachDevice(const std::string& device_id) { |
| 635 DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); | 634 DCHECK(MediaStorageUtil::IsRemovableDevice(device_id)); |
| 636 ProcessDetach(device_id); | 635 ProcessDetach(device_id); |
| 637 MessageLoop::current()->RunUntilIdle(); | 636 base::MessageLoop::current()->RunUntilIdle(); |
| 638 } | 637 } |
| 639 | 638 |
| 640 void MediaFileSystemRegistryTest::SetGalleryPermission( | 639 void MediaFileSystemRegistryTest::SetGalleryPermission( |
| 641 ProfileState* profile_state, extensions::Extension* extension, | 640 ProfileState* profile_state, extensions::Extension* extension, |
| 642 const std::string& device_id, bool has_access) { | 641 const std::string& device_id, bool has_access) { |
| 643 MediaGalleriesPreferences* preferences = | 642 MediaGalleriesPreferences* preferences = |
| 644 profile_state->GetMediaGalleriesPrefs(); | 643 profile_state->GetMediaGalleriesPrefs(); |
| 645 MediaGalleryPrefIdSet pref_id = | 644 MediaGalleryPrefIdSet pref_id = |
| 646 preferences->LookUpGalleriesByDeviceId(device_id); | 645 preferences->LookUpGalleriesByDeviceId(device_id); |
| 647 ASSERT_EQ(1U, pref_id.size()); | 646 ASSERT_EQ(1U, pref_id.size()); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 767 ASSERT_TRUE(file_util::CreateDirectory(dcim_dir_.Append(kDCIMDirectoryName))); | 766 ASSERT_TRUE(file_util::CreateDirectory(dcim_dir_.Append(kDCIMDirectoryName))); |
| 768 } | 767 } |
| 769 | 768 |
| 770 void MediaFileSystemRegistryTest::TearDown() { | 769 void MediaFileSystemRegistryTest::TearDown() { |
| 771 profile_states_.clear(); | 770 profile_states_.clear(); |
| 772 ChromeRenderViewHostTestHarness::TearDown(); | 771 ChromeRenderViewHostTestHarness::TearDown(); |
| 773 MediaFileSystemRegistry* registry = | 772 MediaFileSystemRegistry* registry = |
| 774 g_browser_process->media_file_system_registry(); | 773 g_browser_process->media_file_system_registry(); |
| 775 EXPECT_EQ(0U, registry->GetExtensionGalleriesHostCountForTests()); | 774 EXPECT_EQ(0U, registry->GetExtensionGalleriesHostCountForTests()); |
| 776 BrowserThread::GetBlockingPool()->FlushForTesting(); | 775 BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 777 MessageLoop::current()->RunUntilIdle(); | 776 base::MessageLoop::current()->RunUntilIdle(); |
| 778 extensions::ManifestHandler::ClearRegistryForTesting(); | 777 extensions::ManifestHandler::ClearRegistryForTesting(); |
| 779 } | 778 } |
| 780 | 779 |
| 781 /////////// | 780 /////////// |
| 782 // Tests // | 781 // Tests // |
| 783 /////////// | 782 /////////// |
| 784 | 783 |
| 785 TEST_F(MediaFileSystemRegistryTest, Basic) { | 784 TEST_F(MediaFileSystemRegistryTest, Basic) { |
| 786 CreateProfileState(1); | 785 CreateProfileState(1); |
| 787 AssertAllAutoAddedGalleries(); | 786 AssertAllAutoAddedGalleries(); |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 869 GetMediaFileSystemRegistry()->GetPreferences(profile_state->profile()); | 868 GetMediaFileSystemRegistry()->GetPreferences(profile_state->profile()); |
| 870 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); | 869 const MediaGalleriesPrefInfoMap& galleries = prefs->known_galleries(); |
| 871 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); | 870 for (MediaGalleriesPrefInfoMap::const_iterator it = galleries.begin(); |
| 872 it != galleries.end(); ++it) { | 871 it != galleries.end(); ++it) { |
| 873 if (it->second.device_id == device_id) { | 872 if (it->second.device_id == device_id) { |
| 874 prefs->ForgetGalleryById(it->first); | 873 prefs->ForgetGalleryById(it->first); |
| 875 forget_gallery = true; | 874 forget_gallery = true; |
| 876 break; | 875 break; |
| 877 } | 876 } |
| 878 } | 877 } |
| 879 MessageLoop::current()->RunUntilIdle(); | 878 base::MessageLoop::current()->RunUntilIdle(); |
| 880 EXPECT_TRUE(forget_gallery); | 879 EXPECT_TRUE(forget_gallery); |
| 881 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); | 880 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); |
| 882 | 881 |
| 883 // Call GetPreferences() and the gallery count should not change. | 882 // Call GetPreferences() and the gallery count should not change. |
| 884 GetMediaFileSystemRegistry()->GetPreferences(profile_state->profile()); | 883 GetMediaFileSystemRegistry()->GetPreferences(profile_state->profile()); |
| 885 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); | 884 EXPECT_EQ(gallery_count, GetAutoAddedGalleries(profile_state).size()); |
| 886 } | 885 } |
| 887 | 886 |
| 888 TEST_F(MediaFileSystemRegistryTest, GalleryNameDefault) { | 887 TEST_F(MediaFileSystemRegistryTest, GalleryNameDefault) { |
| 889 FSInfoMap galleries_info; | 888 FSInfoMap galleries_info; |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 987 pref_info_with_relpath.device_id = device_id; | 986 pref_info_with_relpath.device_id = device_id; |
| 988 EXPECT_EQ(dcim_dir().Append(pref_info_with_relpath.path).value(), | 987 EXPECT_EQ(dcim_dir().Append(pref_info_with_relpath.path).value(), |
| 989 pref_info_with_relpath.AbsolutePath().value()); | 988 pref_info_with_relpath.AbsolutePath().value()); |
| 990 | 989 |
| 991 DetachDevice(device_id); | 990 DetachDevice(device_id); |
| 992 EXPECT_TRUE(pref_info.AbsolutePath().empty()); | 991 EXPECT_TRUE(pref_info.AbsolutePath().empty()); |
| 993 EXPECT_TRUE(pref_info_with_relpath.AbsolutePath().empty()); | 992 EXPECT_TRUE(pref_info_with_relpath.AbsolutePath().empty()); |
| 994 } | 993 } |
| 995 | 994 |
| 996 } // namespace chrome | 995 } // namespace chrome |
| OLD | NEW |