- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / storage_monitor / media_transfer_protocol_device_observer_linux_unittest.cc
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 // MediaTransferProtocolDeviceObserverLinux unit tests.
6
7 #include "chrome/browser/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 "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"
22
23 namespace {
24
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";
31
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);
35 }
36
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,
41                     std::string* id,
42                     string16* label,
43                     std::string* location) {
44   if (storage_name == kStorageWithInvalidInfo)
45     return;  // Do not set any storage details.
46
47   ASSERT_EQ(kStorageWithValidInfo, storage_name);
48
49   *id = GetMtpDeviceId(kStorageUniqueId);
50   *label = ASCIIToUTF16(kStorageLabel);
51   *location = kStorageLocation;
52 }
53
54 class TestMediaTransferProtocolDeviceObserverLinux
55     : public MediaTransferProtocolDeviceObserverLinux {
56  public:
57   TestMediaTransferProtocolDeviceObserverLinux(
58       StorageMonitor::Receiver* receiver,
59       device::MediaTransferProtocolManager* mtp_manager)
60       : MediaTransferProtocolDeviceObserverLinux(receiver, mtp_manager,
61                                                  &GetStorageInfo) {
62   }
63
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();
69   }
70
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();
76   }
77
78  private:
79   DISALLOW_COPY_AND_ASSIGN(TestMediaTransferProtocolDeviceObserverLinux);
80 };
81
82 }  // namespace
83
84 // A class to test the functionality of MediaTransferProtocolDeviceObserverLinux
85 // member functions.
86 class MediaTransferProtocolDeviceObserverLinuxTest : public testing::Test {
87  public:
88   MediaTransferProtocolDeviceObserverLinuxTest()
89       : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {}
90
91   virtual ~MediaTransferProtocolDeviceObserverLinuxTest() {}
92
93  protected:
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());
101   }
102
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();
108   }
109
110   // Returns the device changed observer object.
111   MockRemovableStorageObserver& observer() {
112     return *mock_storage_observer_;
113   }
114
115   TestMediaTransferProtocolDeviceObserverLinux* mtp_device_observer() {
116     return mtp_device_observer_.get();
117   }
118
119  private:
120   content::TestBrowserThreadBundle thread_bundle_;
121
122   scoped_ptr<TestMediaTransferProtocolDeviceObserverLinux> mtp_device_observer_;
123   scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
124
125   DISALLOW_COPY_AND_ASSIGN(MediaTransferProtocolDeviceObserverLinuxTest);
126 };
127
128 // Test to verify basic mtp storage attach and detach notifications.
129 TEST_F(MediaTransferProtocolDeviceObserverLinuxTest, BasicAttachDetach) {
130   std::string device_id = GetMtpDeviceId(kStorageUniqueId);
131
132   // Attach a mtp storage.
133   mtp_device_observer()->MtpStorageAttached(kStorageWithValidInfo);
134
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());
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 }