Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / storage_monitor / media_transfer_protocol_device_observer_linux_unittest.cc
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.
4 //
5 // MediaTransferProtocolDeviceObserverLinux unit tests.
6
7 #include "components/storage_monitor/media_transfer_protocol_device_observer_linux.h"
8
9 #include <string>
10
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"
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 kStorageUniqueId[] = "VendorModelSerial:COM:MOD2012:283";
28 const char kStorageWithInvalidInfo[] = "usb:2,3,11111";
29 const char kStorageWithValidInfo[] = "usb:2,2,88888";
30
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);
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                     device::MediaTransferProtocolManager* mtp_manager,
40                     std::string* id,
41                     base::string16* label,
42                     std::string* location) {
43   if (storage_name == kStorageWithInvalidInfo)
44     return;  // Do not set any storage details.
45
46   ASSERT_EQ(kStorageWithValidInfo, storage_name);
47
48   *id = GetMtpDeviceId(kStorageUniqueId);
49   *label = base::ASCIIToUTF16(kStorageLabel);
50   *location = kStorageLocation;
51 }
52
53 class TestMediaTransferProtocolDeviceObserverLinux
54     : public MediaTransferProtocolDeviceObserverLinux {
55  public:
56   TestMediaTransferProtocolDeviceObserverLinux(
57       StorageMonitor::Receiver* receiver,
58       device::MediaTransferProtocolManager* mtp_manager)
59       : MediaTransferProtocolDeviceObserverLinux(receiver, mtp_manager,
60                                                  &GetStorageInfo) {
61   }
62
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();
68   }
69
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();
75   }
76
77  private:
78   DISALLOW_COPY_AND_ASSIGN(TestMediaTransferProtocolDeviceObserverLinux);
79 };
80
81 }  // namespace
82
83 // A class to test the functionality of MediaTransferProtocolDeviceObserverLinux
84 // member functions.
85 class MediaTransferProtocolDeviceObserverLinuxTest : public testing::Test {
86  public:
87   MediaTransferProtocolDeviceObserverLinuxTest()
88       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
89
90   virtual ~MediaTransferProtocolDeviceObserverLinuxTest() {}
91
92  protected:
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());
100   }
101
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();
107   }
108
109   // Returns the device changed observer object.
110   MockRemovableStorageObserver& observer() {
111     return *mock_storage_observer_;
112   }
113
114   TestMediaTransferProtocolDeviceObserverLinux* mtp_device_observer() {
115     return mtp_device_observer_.get();
116   }
117
118  private:
119   content::TestBrowserThreadBundle thread_bundle_;
120
121   scoped_ptr<TestMediaTransferProtocolDeviceObserverLinux> mtp_device_observer_;
122   scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
123
124   DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinuxTest);
125 };
126
127 // Test to verify basic mtp storage attach and detach notifications.
128 TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) {
129   std::string device_id = GetMtpDeviceId(kStorageUniqueId);
130
131   // Attach a mtp storage.
132   mtp_device_observer()->MtpStorageAttached(kStorageWithValidInfo);
133
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());
140
141   // Detach the attached storage.
142   mtp_device_observer()->MtpStorageDetached(kStorageWithValidInfo);
143
144   EXPECT_EQ(1, observer().attach_calls());
145   EXPECT_EQ(1, observer().detach_calls());
146   EXPECT_EQ(device_id, observer().last_detached().device_id());
147 }
148
149 // When a mtp storage device with invalid storage label and id is
150 // attached/detached, there should not be any device attach/detach
151 // notifications.
152 TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, StorageWithInvalidInfo) {
153   // Attach the mtp storage with invalid storage info.
154   mtp_device_observer()->MtpStorageAttached(kStorageWithInvalidInfo);
155
156   // Detach the attached storage.
157   mtp_device_observer()->MtpStorageDetached(kStorageWithInvalidInfo);
158
159   EXPECT_EQ(0, observer().attach_calls());
160   EXPECT_EQ(0, observer().detach_calls());
161 }