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