Chromium Code Reviews| Index: chrome/browser/system_monitor/media_transfer_protocol_device_observer_linux_unittest.cc |
| diff --git a/chrome/browser/system_monitor/media_transfer_protocol_device_observer_linux_unittest.cc b/chrome/browser/system_monitor/media_transfer_protocol_device_observer_linux_unittest.cc |
| index b4db1261e86baf5f209493412e926bd620d170a6..e64f6776701c21fc321608139d206de152889fed 100644 |
| --- a/chrome/browser/system_monitor/media_transfer_protocol_device_observer_linux_unittest.cc |
| +++ b/chrome/browser/system_monitor/media_transfer_protocol_device_observer_linux_unittest.cc |
| @@ -10,10 +10,13 @@ |
| #include "base/memory/scoped_ptr.h" |
| #include "base/message_loop.h" |
| -#include "base/system_monitor/system_monitor.h" |
| -#include "base/test/mock_devices_changed_observer.h" |
| #include "base/utf_string_conversions.h" |
| #include "chrome/browser/system_monitor/media_storage_util.h" |
| +#include "chrome/browser/system_monitor/removable_device_notifications_linux.h" |
| +#include "chrome/browser/system_monitor/removable_storage_notifications.h" |
| +#include "chrome/browser/system_monitor/removable_storage_notifications_test_util.h" |
| +#include "content/public/test/test_browser_thread.h" |
| +#include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| namespace chrome { |
| @@ -54,6 +57,28 @@ void GetStorageInfo(const std::string& storage_name, |
| *location = kStorageLocation; |
| } |
| +class RemovableDeviceNotificationsLinuxTestWrapper |
| + : public RemovableDeviceNotificationsLinux { |
| + public: |
| + RemovableDeviceNotificationsLinuxTestWrapper(const FilePath& path, |
| + MessageLoop* message_loop) |
| + : RemovableDeviceNotificationsLinux(path), |
| + message_loop_(message_loop) { |
| + } |
| + |
| + // Avoids code deleting the object while there are references to it. |
| + // Aside from the base::RefCountedThreadSafe friend class, any attempts to |
| + // call this dtor will result in a compile-time error. |
| + ~RemovableDeviceNotificationsLinuxTestWrapper() {} |
| + |
| + virtual void OnFilePathChanged(const FilePath& path, bool error) OVERRIDE { |
| + RemovableDeviceNotificationsLinux::OnFilePathChanged(path, error); |
| + message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| + } |
| + |
| + MessageLoop* message_loop_; |
| +}; |
| + |
| } // namespace |
| // A class to test the functionality of MediaTransferProtocolDeviceObserverLinux |
| @@ -63,26 +88,27 @@ class MediaTransferProtocolDeviceObserverLinuxTest |
| public MediaTransferProtocolDeviceObserverLinux { |
| public: |
| MediaTransferProtocolDeviceObserverLinuxTest() |
| - : MediaTransferProtocolDeviceObserverLinux(&GetStorageInfo) { |
| - } |
| + : MediaTransferProtocolDeviceObserverLinux(&GetStorageInfo), |
| + message_loop_(MessageLoop::TYPE_IO), |
| + file_thread_(content::BrowserThread::FILE, &message_loop_), |
| + notifications_(new RemovableDeviceNotificationsLinuxTestWrapper( |
| + FilePath("/etc/mtab"), &message_loop_)) {} |
|
vandebo (ex-Chrome)
2013/01/11 22:06:50
Should this point to a temp/testing path?
Greg Billock
2013/01/17 22:10:45
I've now fixed this up with a proper fake.
|
| virtual ~MediaTransferProtocolDeviceObserverLinuxTest() {} |
| protected: |
| virtual void SetUp() OVERRIDE { |
| - mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); |
| - system_monitor_.AddDevicesChangedObserver( |
| - mock_devices_changed_observer_.get()); |
| + mock_storage_observer_.reset(new MockRemovableStorageObserver); |
| + notifications_->AddObserver(mock_storage_observer_.get()); |
| } |
| virtual void TearDown() OVERRIDE { |
| - system_monitor_.RemoveDevicesChangedObserver( |
| - mock_devices_changed_observer_.get()); |
| + notifications_->RemoveObserver(mock_storage_observer_.get()); |
| } |
| // Returns the device changed observer object. |
| - base::MockDevicesChangedObserver& observer() { |
| - return *mock_devices_changed_observer_; |
| + MockRemovableStorageObserver& observer() { |
| + return *mock_storage_observer_; |
| } |
| // Notifies MediaTransferProtocolDeviceObserverLinux about the attachment of |
| @@ -90,7 +116,7 @@ class MediaTransferProtocolDeviceObserverLinuxTest |
| void MtpStorageAttached(const std::string& storage_name) { |
| MediaTransferProtocolDeviceObserverLinux::StorageChanged(true, |
| storage_name); |
| - ui_loop_.RunUntilIdle(); |
| + message_loop_.RunUntilIdle(); |
| } |
| // Notifies MediaTransferProtocolDeviceObserverLinux about the detachment of |
| @@ -98,54 +124,50 @@ class MediaTransferProtocolDeviceObserverLinuxTest |
| void MtpStorageDetached(const std::string& storage_name) { |
| MediaTransferProtocolDeviceObserverLinux::StorageChanged(false, |
| storage_name); |
| - ui_loop_.RunUntilIdle(); |
| + message_loop_.RunUntilIdle(); |
| } |
| private: |
| - // Message loop to run the test on. |
| - MessageLoop ui_loop_; |
| + MessageLoop message_loop_; |
| + content::TestBrowserThread file_thread_; |
| - // SystemMonitor and DevicesChangedObserver to hook together to test. |
| - base::SystemMonitor system_monitor_; |
| - scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; |
| + scoped_refptr<RemovableDeviceNotificationsLinuxTestWrapper> notifications_; |
| + scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_; |
| DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinuxTest); |
| }; |
| // Test to verify basic mtp storage attach and detach notifications. |
| TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) { |
| - testing::Sequence mock_sequence; |
| std::string device_id = GetMtpDeviceId(kStorageUniqueId); |
| - EXPECT_CALL(observer(), |
| - OnRemovableStorageAttached(device_id, |
| - ASCIIToUTF16(kStorageLabel), |
| - kStorageLocation)) |
| - .InSequence(mock_sequence); |
| - |
| // Attach a mtp storage. |
| MtpStorageAttached(kStorageWithValidInfo); |
| - EXPECT_CALL(observer(), OnRemovableStorageDetached(device_id)) |
| - .InSequence(mock_sequence); |
| + EXPECT_EQ(1, observer().attach_calls()); |
| + EXPECT_EQ(device_id, observer().last_attached().device_id); |
| + EXPECT_EQ(ASCIIToUTF16(kStorageLabel), observer().last_attached().name); |
| + EXPECT_EQ(kStorageLocation, observer().last_attached().location); |
| // Detach the attached storage. |
| MtpStorageDetached(kStorageWithValidInfo); |
| + |
| + EXPECT_EQ(1, observer().detach_calls()); |
| + EXPECT_EQ(device_id, observer().last_detached().device_id); |
| } |
| // When a mtp storage device with invalid storage label and id is |
| // attached/detached, there should not be any device attach/detach |
| // notifications. |
| TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, StorageWithInvalidInfo) { |
| - EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); |
| - |
| // Attach the mtp storage with invalid storage info. |
| MtpStorageAttached(kStorageWithInvalidInfo); |
| - EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0); |
| - |
| // Detach the attached storage. |
| MtpStorageDetached(kStorageWithInvalidInfo); |
| + |
| + EXPECT_EQ(0, observer().attach_calls()); |
| + EXPECT_EQ(0, observer().detach_calls()); |
| } |
| } // namespace chrome |