Chromium Code Reviews| Index: chrome/browser/storage_monitor/media_storage_util_unittest.cc |
| diff --git a/chrome/browser/storage_monitor/media_storage_util_unittest.cc b/chrome/browser/storage_monitor/media_storage_util_unittest.cc |
| index f5d8ebaff6126ce7a24c349c124abcddcc24d36d..df1b21dc0313c8370779e817fba08e3fd00a08e9 100644 |
| --- a/chrome/browser/storage_monitor/media_storage_util_unittest.cc |
| +++ b/chrome/browser/storage_monitor/media_storage_util_unittest.cc |
| @@ -4,10 +4,13 @@ |
| #include <string> |
| +#include "base/file_util.h" |
| +#include "base/files/scoped_temp_dir.h" |
| #include "base/message_loop.h" |
| #include "base/synchronization/waitable_event.h" |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/storage_monitor/media_storage_util.h" |
| +#include "chrome/browser/storage_monitor/removable_device_constants.h" |
| #include "chrome/browser/storage_monitor/storage_monitor.h" |
| #include "chrome/browser/storage_monitor/test_storage_monitor.h" |
| #include "content/public/browser/browser_thread.h" |
| @@ -25,18 +28,96 @@ const char kImageCaptureDeviceId[] = "ic:xyz"; |
| } // namespace |
| +using content::BrowserThread; |
| + |
| class MediaStorageUtilTest : public testing::Test { |
| public: |
| + MediaStorageUtilTest() |
| + : ui_thread_(BrowserThread::UI, &message_loop_), |
| + file_thread_(BrowserThread::FILE) {} |
| + virtual ~MediaStorageUtilTest() { } |
| + |
| + // Verify mounted device type. |
| + void CheckDeviceType(const base::FilePath::StringType& mount_point, |
| + bool expected_val) { |
| + if (expected_val) |
| + EXPECT_TRUE(MediaStorageUtil::HasDcim(mount_point)); |
| + else |
| + EXPECT_FALSE(MediaStorageUtil::HasDcim(mount_point)); |
| + } |
| + |
| void ProcessAttach(const std::string& id, |
| const string16& name, |
| const base::FilePath::StringType& location) { |
| monitor_.receiver()->ProcessAttach(StorageInfo(id, name, location)); |
| } |
| + protected: |
| + // Create mount point for the test device. |
| + base::FilePath CreateMountPoint(bool create_dcim_dir) { |
| + base::FilePath path(scoped_temp_dir_.path()); |
| + if (create_dcim_dir) |
| + path = path.Append(kDCIMDirectoryName); |
| + if (!file_util::CreateDirectory(path)) |
| + return base::FilePath(); |
| + return scoped_temp_dir_.path(); |
| + } |
| + |
| + virtual void SetUp() OVERRIDE { |
| + ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
| + file_thread_.Start(); |
| + } |
| + |
| + virtual void TearDown() { |
| + WaitForFileThread(); |
| + } |
| + |
| + static void PostQuitToUIThread() { |
| + BrowserThread::PostTask(BrowserThread::UI, |
| + FROM_HERE, |
| + MessageLoop::QuitClosure()); |
| + } |
| + |
| + static void WaitForFileThread() { |
| + BrowserThread::PostTask(BrowserThread::FILE, |
| + FROM_HERE, |
| + base::Bind(&PostQuitToUIThread)); |
| + MessageLoop::current()->Run(); |
| + } |
| + |
| + MessageLoop message_loop_; |
| + |
| private: |
| chrome::test::TestStorageMonitor monitor_; |
| + content::TestBrowserThread ui_thread_; |
| + content::TestBrowserThread file_thread_; |
| + base::ScopedTempDir scoped_temp_dir_; |
| }; |
| +// Test to verify that HasDcim() function returns true for the given media |
| +// device mount point. |
| +TEST_F(MediaStorageUtilTest, MediaDeviceAttached) { |
| + // Create a dummy mount point with DCIM Directory. |
| + base::FilePath mount_point(CreateMountPoint(true)); |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&MediaStorageUtilTest::CheckDeviceType, |
| + base::Unretained(this), mount_point.value(), true)); |
| + message_loop_.RunUntilIdle(); |
| +} |
| + |
| +// Test to verify that HasDcim() function returns false for a given non-media |
| +// device mount point. |
| +TEST_F(MediaStorageUtilTest, NonMediaDeviceAttached) { |
| + // Create a dummy mount point without DCIM Directory. |
| + base::FilePath mount_point(CreateMountPoint(false)); |
| + BrowserThread::PostTask( |
| + BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&MediaStorageUtilTest::CheckDeviceType, |
| + base::Unretained(this), mount_point.value(), false)); |
| + message_loop_.RunUntilIdle(); |
| +} |
| + |
| // Test to verify |MediaStorageUtil::MakeDeviceId| functionality using a sample |
| // mtp device unique id. |
| TEST_F(MediaStorageUtilTest, MakeMtpDeviceId) { |
| @@ -74,16 +155,18 @@ TEST_F(MediaStorageUtilTest, CanCreateFileSystemForImageCapture) { |
| } |
| TEST_F(MediaStorageUtilTest, DetectDeviceFiltered) { |
| - MessageLoop loop; |
| - content::TestBrowserThread file_thread(content::BrowserThread::FILE, &loop); |
| - |
| MediaStorageUtil::DeviceIdSet devices; |
| devices.insert(kImageCaptureDeviceId); |
| base::WaitableEvent event(true, false); |
|
tommycli
2013/03/11 17:31:01
Run FilterAttachedDevices on the FILE thread.
Greg Billock
2013/03/11 18:07:43
This is fine. FilterAttachedDevices bounces to the
vandebo (ex-Chrome)
2013/03/11 22:27:03
FYI - I think another way that would have worked t
|
| - MediaStorageUtil::FilterAttachedDevices(&devices, |
| - base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); |
| - loop.RunUntilIdle(); |
| + base::Closure signal_event = |
| + base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event)); |
| + |
| + // We need signal_event to be executed on the FILE thread, as the test thread |
| + // is blocked. Therefore, we invoke FilterAttachedDevices on the FILE thread. |
| + BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&MediaStorageUtil::FilterAttachedDevices, |
| + base::Unretained(&devices), signal_event)); |
| event.Wait(); |
| EXPECT_FALSE(devices.find(kImageCaptureDeviceId) != devices.end()); |
| @@ -91,9 +174,9 @@ TEST_F(MediaStorageUtilTest, DetectDeviceFiltered) { |
| FILE_PATH_LITERAL("/location")); |
| devices.insert(kImageCaptureDeviceId); |
| event.Reset(); |
| - MediaStorageUtil::FilterAttachedDevices(&devices, |
| - base::Bind(&base::WaitableEvent::Signal, base::Unretained(&event))); |
| - loop.RunUntilIdle(); |
| + BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&MediaStorageUtil::FilterAttachedDevices, |
| + base::Unretained(&devices), signal_event)); |
| event.Wait(); |
| EXPECT_TRUE(devices.find(kImageCaptureDeviceId) != devices.end()); |