Chromium Code Reviews| Index: chrome/browser/system_monitor/chromeos/media_transfer_protocol_device_observer_unittest.cc |
| diff --git a/chrome/browser/system_monitor/chromeos/media_transfer_protocol_device_observer_unittest.cc b/chrome/browser/system_monitor/chromeos/media_transfer_protocol_device_observer_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..da5e3b8b7f8c6c654418d2db7673f8dbeec3285e |
| --- /dev/null |
| +++ b/chrome/browser/system_monitor/chromeos/media_transfer_protocol_device_observer_unittest.cc |
| @@ -0,0 +1,133 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +// |
| +// MediaTransferProtocolDeviceObserver unit tests. |
| + |
| +#include "chrome/browser/system_monitor/chromeos/media_transfer_protocol_device_observer.h" |
| + |
| +#include <string> |
| + |
| +#include "base/message_loop.h" |
|
Lei Zhang
2012/09/10 18:18:01
nit: alphabetical order
kmadhusu
2012/09/11 01:50:19
Done.
|
| +#include "base/memory/scoped_ptr.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/media_gallery/media_storage_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace chromeos { |
| +namespace mtp { |
| + |
| +namespace { |
| + |
| +// Sample mtp device storage information. |
| +const char kStorageLabel[] = "Camera V1.0"; |
| +const char kStorageLocation[] = "/usb:2,2,88888"; |
| +const char kStorageName[] = "usb:2,2,88888"; |
| +const char kStorageUniqueId[] = "VendorModelSerial:COM:MOD2012:283"; |
| + |
| +// Returns the mtp device id given the |unique_id|. |
| +std::string GetMtpDeviceId(const std::string& unique_id) { |
| + return chrome::MediaStorageUtil::MakeDeviceId( |
| + chrome::MediaStorageUtil::MTP_OR_PTP, unique_id); |
| +} |
| + |
| +// Helper function to get the device storage details such as device id, label |
| +// and location. On success, fills in |id|, |label| and |location|. |
| +void GetStorageInfo(const std::string& storage_name, |
| + std::string* id, |
| + string16* label, |
| + std::string* location) { |
| + if (storage_name != kStorageName) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + if (id) |
| + *id = GetMtpDeviceId(kStorageUniqueId); |
| + if (label) |
| + *label = ASCIIToUTF16(kStorageLabel); |
| + if (location) |
| + *location = kStorageLocation; |
| +} |
| + |
| +} // namespace |
| + |
| +class MediaTransferProtocolDeviceObserverTest : public testing::Test { |
| + public: |
| + MediaTransferProtocolDeviceObserverTest() {} |
| + virtual ~MediaTransferProtocolDeviceObserverTest() {} |
| + |
| + protected: |
| + virtual void SetUp() OVERRIDE { |
| + mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); |
| + system_monitor_.AddDevicesChangedObserver( |
| + mock_devices_changed_observer_.get()); |
| + |
| + // Initialize the test subject. |
| + mtp_dev_observer_.reset( |
| + new MediaTransferProtocolDeviceObserver(&GetStorageInfo)); |
| + } |
| + |
| + virtual void TearDown() OVERRIDE { |
| + mtp_dev_observer_.reset(); |
| + system_monitor_.RemoveDevicesChangedObserver( |
| + mock_devices_changed_observer_.get()); |
| + } |
| + |
| + // Returns the device changed observer object. |
| + base::MockDevicesChangedObserver& observer() { |
| + return *mock_devices_changed_observer_; |
| + } |
| + |
| + // Notifies |MediaTransferProtocolDeviceObserver| about the attachment of |
| + // mtp storage device given the |storage_name|. |
| + void MtpStorageAttached(const std::string& storage_name) { |
| + mtp_dev_observer_->StorageChanged(true, storage_name); |
| + ui_loop_.RunAllPending(); |
| + } |
| + |
| + // Notifies |MediaTransferProtocolDeviceObserver| about the detachment of |
| + // mtp storage device given the |storage_name|. |
| + void MtpStorageDetached(const std::string& storage_name) { |
| + mtp_dev_observer_->StorageChanged(false, storage_name); |
| + ui_loop_.RunAllPending(); |
| + } |
| + |
| + private: |
| + // Message loop to run the test on. |
| + MessageLoop ui_loop_; |
| + |
| + // SystemMonitor and DevicesChangedObserver to hook together to test. |
| + base::SystemMonitor system_monitor_; |
| + scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; |
| + |
| + // Test subject. |
| + scoped_ptr<MediaTransferProtocolDeviceObserver> mtp_dev_observer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverTest); |
| +}; |
| + |
| +// Test to verify basic mtp storage attach and detach notifications. |
| +TEST_F(MediaTransferProtocolDeviceObserverTest, 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(kStorageName); |
| + |
| + EXPECT_CALL(observer(), OnRemovableStorageDetached(device_id)) |
| + .InSequence(mock_sequence); |
| + |
| + // Detach the attached storage. |
| + MtpStorageDetached(kStorageName); |
| +} |
| + |
| +} // namespace mtp |
| +} // namespace chromeos |