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.
5 // MediaTransferProtocolDeviceObserverLinux unit tests.
7 #include "chrome/browser/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 "chrome/browser/browser_process.h"
15 #include "chrome/browser/storage_monitor/mock_removable_storage_observer.h"
16 #include "chrome/browser/storage_monitor/storage_info.h"
17 #include "chrome/browser/storage_monitor/storage_monitor.h"
18 #include "chrome/browser/storage_monitor/test_storage_monitor.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "device/media_transfer_protocol/media_transfer_protocol_manager.h"
21 #include "testing/gtest/include/gtest/gtest.h"
25 // Sample mtp device storage information.
26 const char kStorageLabel[] = "Camera V1.0";
27 const char kStorageLocation[] = "/usb:2,2,88888";
28 const char kStorageUniqueId[] = "VendorModelSerial:COM:MOD2012:283";
29 const char kStorageWithInvalidInfo[] = "usb:2,3,11111";
30 const char kStorageWithValidInfo[] = "usb:2,2,88888";
32 // Returns the mtp device id given the |unique_id|.
33 std::string GetMtpDeviceId(const std::string& unique_id) {
34 return StorageInfo::MakeDeviceId(StorageInfo::MTP_OR_PTP, unique_id);
37 // Helper function to get the device storage details such as device id, label
38 // and location. On success, fills in |id|, |label| and |location|.
39 void GetStorageInfo(const std::string& storage_name,
40 device::MediaTransferProtocolManager* mtp_manager,
43 std::string* location) {
44 if (storage_name == kStorageWithInvalidInfo)
45 return; // Do not set any storage details.
47 ASSERT_EQ(kStorageWithValidInfo, storage_name);
49 *id = GetMtpDeviceId(kStorageUniqueId);
50 *label = ASCIIToUTF16(kStorageLabel);
51 *location = kStorageLocation;
54 class TestMediaTransferProtocolDeviceObserverLinux
55 : public MediaTransferProtocolDeviceObserverLinux {
57 TestMediaTransferProtocolDeviceObserverLinux(
58 StorageMonitor::Receiver* receiver,
59 device::MediaTransferProtocolManager* mtp_manager)
60 : MediaTransferProtocolDeviceObserverLinux(receiver, mtp_manager,
64 // Notifies MediaTransferProtocolDeviceObserverLinux about the attachment of
65 // mtp storage device given the |storage_name|.
66 void MtpStorageAttached(const std::string& storage_name) {
67 StorageChanged(true, storage_name);
68 base::RunLoop().RunUntilIdle();
71 // Notifies MediaTransferProtocolDeviceObserverLinux about the detachment of
72 // mtp storage device given the |storage_name|.
73 void MtpStorageDetached(const std::string& storage_name) {
74 StorageChanged(false, storage_name);
75 base::RunLoop().RunUntilIdle();
79 DISALLOW_COPY_AND_ASSIGN(TestMediaTransferProtocolDeviceObserverLinux);
84 // A class to test the functionality of MediaTransferProtocolDeviceObserverLinux
86 class MediaTransferProtocolDeviceObserverLinuxTest : public testing::Test {
88 MediaTransferProtocolDeviceObserverLinuxTest()
89 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
91 virtual ~MediaTransferProtocolDeviceObserverLinuxTest() {}
94 virtual void SetUp() OVERRIDE {
95 mock_storage_observer_.reset(new MockRemovableStorageObserver);
96 TestStorageMonitor* monitor = TestStorageMonitor::CreateAndInstall();
97 mtp_device_observer_.reset(
98 new TestMediaTransferProtocolDeviceObserverLinux(
99 monitor->receiver(), monitor->media_transfer_protocol_manager()));
100 monitor->AddObserver(mock_storage_observer_.get());
103 virtual void TearDown() OVERRIDE {
104 StorageMonitor* monitor = g_browser_process->storage_monitor();
105 monitor->RemoveObserver(mock_storage_observer_.get());
106 mtp_device_observer_.reset();
107 TestStorageMonitor::RemoveSingleton();
110 // Returns the device changed observer object.
111 MockRemovableStorageObserver& observer() {
112 return *mock_storage_observer_;
115 TestMediaTransferProtocolDeviceObserverLinux* mtp_device_observer() {
116 return mtp_device_observer_.get();
120 content::TestBrowserThreadBundle thread_bundle_;
122 scoped_ptr<TestMediaTransferProtocolDeviceObserverLinux> mtp_device_observer_;
123 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
125 DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinuxTest);
128 // Test to verify basic mtp storage attach and detach notifications.
129 TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) {
130 std::string device_id = GetMtpDeviceId(kStorageUniqueId);
132 // Attach a mtp storage.
133 mtp_device_observer()->MtpStorageAttached(kStorageWithValidInfo);
135 EXPECT_EQ(1, observer().attach_calls());
136 EXPECT_EQ(0, observer().detach_calls());
137 EXPECT_EQ(device_id, observer().last_attached().device_id());
138 EXPECT_EQ(ASCIIToUTF16(kStorageLabel), 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());