OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 // MediaTransferProtocolDeviceObserverCros unit tests. |
| 6 |
| 7 #include "chrome/browser/system_monitor/media_transfer_protocol_device_observer_
chromeos.h" |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop.h" |
| 13 #include "base/system_monitor/system_monitor.h" |
| 14 #include "base/test/mock_devices_changed_observer.h" |
| 15 #include "base/utf_string_conversions.h" |
| 16 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 namespace chromeos { |
| 20 namespace mtp { |
| 21 |
| 22 namespace { |
| 23 |
| 24 // Sample mtp device storage information. |
| 25 const char kStorageLabel[] = "Camera V1.0"; |
| 26 const char kStorageLocation[] = "/usb:2,2,88888"; |
| 27 const char kStorageName[] = "usb:2,2,88888"; |
| 28 const char kStorageUniqueId[] = "VendorModelSerial:COM:MOD2012:283"; |
| 29 |
| 30 // Returns the mtp device id given the |unique_id|. |
| 31 std::string GetMtpDeviceId(const std::string& unique_id) { |
| 32 return chrome::MediaStorageUtil::MakeDeviceId( |
| 33 chrome::MediaStorageUtil::MTP_OR_PTP, unique_id); |
| 34 } |
| 35 |
| 36 // Helper function to get the device storage details such as device id, label |
| 37 // and location. On success, fills in |id|, |label| and |location|. |
| 38 void GetStorageInfo(const std::string& storage_name, |
| 39 std::string* id, |
| 40 string16* label, |
| 41 std::string* location) { |
| 42 if (storage_name != kStorageName) { |
| 43 NOTREACHED(); |
| 44 return; |
| 45 } |
| 46 if (id) |
| 47 *id = GetMtpDeviceId(kStorageUniqueId); |
| 48 if (label) |
| 49 *label = ASCIIToUTF16(kStorageLabel); |
| 50 if (location) |
| 51 *location = kStorageLocation; |
| 52 } |
| 53 |
| 54 } // namespace |
| 55 |
| 56 // A class to test the functionality of MediaTransferProtocolDeviceObserverCros |
| 57 // member functions. |
| 58 class MediaTransferProtocolDeviceObserverCrosTest |
| 59 : public testing::Test, |
| 60 public MediaTransferProtocolDeviceObserverCros { |
| 61 public: |
| 62 MediaTransferProtocolDeviceObserverCrosTest() |
| 63 : MediaTransferProtocolDeviceObserverCros(&GetStorageInfo) { |
| 64 } |
| 65 |
| 66 virtual ~MediaTransferProtocolDeviceObserverCrosTest() {} |
| 67 |
| 68 protected: |
| 69 virtual void SetUp() OVERRIDE { |
| 70 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); |
| 71 system_monitor_.AddDevicesChangedObserver( |
| 72 mock_devices_changed_observer_.get()); |
| 73 } |
| 74 |
| 75 virtual void TearDown() OVERRIDE { |
| 76 system_monitor_.RemoveDevicesChangedObserver( |
| 77 mock_devices_changed_observer_.get()); |
| 78 } |
| 79 |
| 80 // Returns the device changed observer object. |
| 81 base::MockDevicesChangedObserver& observer() { |
| 82 return *mock_devices_changed_observer_; |
| 83 } |
| 84 |
| 85 // Notifies MediaTransferProtocolDeviceObserverCros about the attachment of |
| 86 // mtp storage device given the |storage_name|. |
| 87 void MtpStorageAttached(const std::string& storage_name) { |
| 88 MediaTransferProtocolDeviceObserverCros::StorageChanged(true, storage_name); |
| 89 ui_loop_.RunAllPending(); |
| 90 } |
| 91 |
| 92 // Notifies MediaTransferProtocolDeviceObserverCros about the detachment of |
| 93 // mtp storage device given the |storage_name|. |
| 94 void MtpStorageDetached(const std::string& storage_name) { |
| 95 MediaTransferProtocolDeviceObserverCros::StorageChanged(false, |
| 96 storage_name); |
| 97 ui_loop_.RunAllPending(); |
| 98 } |
| 99 |
| 100 private: |
| 101 // Message loop to run the test on. |
| 102 MessageLoop ui_loop_; |
| 103 |
| 104 // SystemMonitor and DevicesChangedObserver to hook together to test. |
| 105 base::SystemMonitor system_monitor_; |
| 106 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverCrosTest); |
| 109 }; |
| 110 |
| 111 // Test to verify basic mtp storage attach and detach notifications. |
| 112 TEST_F(MediaTransferProtocolDeviceObserverCrosTest, BasicAttachDetach) { |
| 113 testing::Sequence mock_sequence; |
| 114 std::string device_id = GetMtpDeviceId(kStorageUniqueId); |
| 115 |
| 116 EXPECT_CALL(observer(), |
| 117 OnRemovableStorageAttached(device_id, |
| 118 ASCIIToUTF16(kStorageLabel), |
| 119 kStorageLocation)) |
| 120 .InSequence(mock_sequence); |
| 121 |
| 122 // Attach a mtp storage. |
| 123 MtpStorageAttached(kStorageName); |
| 124 |
| 125 EXPECT_CALL(observer(), OnRemovableStorageDetached(device_id)) |
| 126 .InSequence(mock_sequence); |
| 127 |
| 128 // Detach the attached storage. |
| 129 MtpStorageDetached(kStorageName); |
| 130 } |
| 131 |
| 132 } // namespace mtp |
| 133 } // namespace chromeos |
OLD | NEW |