1 // Copyright 2014 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.
5 // MediaTransferProtocolDeviceObserverLinux unit tests.
7 #include "components/storage_monitor/media_transfer_protocol_device_observer_linux.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/run_loop.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "components/storage_monitor/mock_removable_storage_observer.h"
15 #include "components/storage_monitor/storage_info.h"
16 #include "components/storage_monitor/storage_monitor.h"
17 #include "components/storage_monitor/test_storage_monitor.h"
18 #include "content/public/test/test_browser_thread_bundle.h"
19 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
20 #include "testing/gtest/include/gtest/gtest.h"
24 // Sample mtp device storage information.
25 const char kStorageLabel[] = "Camera V1.0";
26 const char kStorageLocation[] = "/usb:2,2,88888";
27 const char kStorageUniqueId[] = "VendorModelSerial:COM:MOD2012:283";
28 const char kStorageWithInvalidInfo[] = "usb:2,3,11111";
29 const char kStorageWithValidInfo[] = "usb:2,2,88888";
31 // Returns the mtp device id given the |unique_id|.
32 std::string GetMtpDeviceId(const std::string& unique_id) {
33 return StorageInfo::MakeDeviceId(StorageInfo::MTP_OR_PTP, unique_id);
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 device::MediaTransferProtocolManager* mtp_manager,
41 base::string16* label,
42 std::string* location) {
43 if (storage_name == kStorageWithInvalidInfo)
44 return; // Do not set any storage details.
46 ASSERT_EQ(kStorageWithValidInfo, storage_name);
48 *id = GetMtpDeviceId(kStorageUniqueId);
49 *label = base::ASCIIToUTF16(kStorageLabel);
50 *location = kStorageLocation;
53 class TestMediaTransferProtocolDeviceObserverLinux
54 : public MediaTransferProtocolDeviceObserverLinux {
56 TestMediaTransferProtocolDeviceObserverLinux(
57 StorageMonitor::Receiver* receiver,
58 device::MediaTransferProtocolManager* mtp_manager)
59 : MediaTransferProtocolDeviceObserverLinux(receiver, mtp_manager,
63 // Notifies MediaTransferProtocolDeviceObserverLinux about the attachment of
64 // mtp storage device given the |storage_name|.
65 void MtpStorageAttached(const std::string& storage_name) {
66 StorageChanged(true, storage_name);
67 base::RunLoop().RunUntilIdle();
70 // Notifies MediaTransferProtocolDeviceObserverLinux about the detachment of
71 // mtp storage device given the |storage_name|.
72 void MtpStorageDetached(const std::string& storage_name) {
73 StorageChanged(false, storage_name);
74 base::RunLoop().RunUntilIdle();
78 DISALLOW_COPY_AND_ASSIGN(TestMediaTransferProtocolDeviceObserverLinux);
83 // A class to test the functionality of MediaTransferProtocolDeviceObserverLinux
85 class MediaTransferProtocolDeviceObserverLinuxTest : public testing::Test {
87 MediaTransferProtocolDeviceObserverLinuxTest()
88 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
90 virtual ~MediaTransferProtocolDeviceObserverLinuxTest() {}
93 virtual void SetUp() OVERRIDE {
94 mock_storage_observer_.reset(new MockRemovableStorageObserver);
95 TestStorageMonitor* monitor = TestStorageMonitor::CreateAndInstall();
96 mtp_device_observer_.reset(
97 new TestMediaTransferProtocolDeviceObserverLinux(
98 monitor->receiver(), monitor->media_transfer_protocol_manager()));
99 monitor->AddObserver(mock_storage_observer_.get());
102 virtual void TearDown() OVERRIDE {
103 StorageMonitor* monitor = StorageMonitor::GetInstance();
104 monitor->RemoveObserver(mock_storage_observer_.get());
105 mtp_device_observer_.reset();
106 TestStorageMonitor::Destroy();
109 // Returns the device changed observer object.
110 MockRemovableStorageObserver& observer() {
111 return *mock_storage_observer_;
114 TestMediaTransferProtocolDeviceObserverLinux* mtp_device_observer() {
115 return mtp_device_observer_.get();
119 content::TestBrowserThreadBundle thread_bundle_;
121 scoped_ptr<TestMediaTransferProtocolDeviceObserverLinux> mtp_device_observer_;
122 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
124 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinuxTest);
127 // Test to verify basic mtp storage attach and detach notifications.
128 TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) {
129 std::string device_id = GetMtpDeviceId(kStorageUniqueId);
131 // Attach a mtp storage.
132 mtp_device_observer()->MtpStorageAttached(kStorageWithValidInfo);
134 EXPECT_EQ(1, observer().attach_calls());
135 EXPECT_EQ(0, observer().detach_calls());
136 EXPECT_EQ(device_id, observer().last_attached().device_id());
137 EXPECT_EQ(base::ASCIIToUTF16(kStorageLabel),
138 observer().last_attached().name());
139 EXPECT_EQ(kStorageLocation, observer().last_attached().location());
141 // Detach the attached storage.
142 mtp_device_observer()->MtpStorageDetached(kStorageWithValidInfo);
144 EXPECT_EQ(1, observer().attach_calls());
145 EXPECT_EQ(1, observer().detach_calls());
146 EXPECT_EQ(device_id, observer().last_detached().device_id());
149 // When a mtp storage device with invalid storage label and id is
150 // attached/detached, there should not be any device attach/detach
152 TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, StorageWithInvalidInfo) {
153 // Attach the mtp storage with invalid storage info.
154 mtp_device_observer()->MtpStorageAttached(kStorageWithInvalidInfo);
156 // Detach the attached storage.
157 mtp_device_observer()->MtpStorageDetached(kStorageWithInvalidInfo);
159 EXPECT_EQ(0, observer().attach_calls());
160 EXPECT_EQ(0, observer().detach_calls());