OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // MediaDeviceNotificationsLinux unit tests. | |
6 | |
7 #include "content/browser/media_device_notifications_linux.h" | |
8 | |
5 #include <mntent.h> | 9 #include <mntent.h> |
6 #include <stdio.h> | 10 #include <stdio.h> |
7 #include <string.h> | 11 #include <string> |
8 | 12 |
9 #include "base/file_util.h" | 13 #include "base/file_util.h" |
10 #include "base/logging.h" | 14 #include "base/logging.h" |
11 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
12 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
13 #include "base/scoped_temp_dir.h" | 17 #include "base/scoped_temp_dir.h" |
14 #include "base/system_monitor/system_monitor.h" | 18 #include "base/system_monitor/system_monitor.h" |
15 #include "base/test/mock_devices_changed_observer.h" | 19 #include "base/test/mock_devices_changed_observer.h" |
16 #include "content/browser/browser_thread_impl.h" | 20 #include "content/browser/browser_thread_impl.h" |
17 #include "content/browser/media_device_notifications_linux.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
19 | 22 |
23 namespace content { | |
24 | |
25 namespace { | |
26 | |
20 using testing::_; | 27 using testing::_; |
21 | 28 |
22 namespace { | 29 const char kValidFS[] = "vfat"; |
30 const char kInvalidFS[] = "invalidfs"; | |
23 | 31 |
24 const char* kValidFS = "vfat"; | 32 const char kInvalidPath[] = "invalid path does not exist"; |
25 const char* kInvalidFS = "invalidfs"; | |
26 | 33 |
27 const char* kInvalidPath = "invalid path does not exist"; | 34 const char kDevice1[] = "d1"; |
35 const char kDevice2[] = "d2"; | |
36 const char kDevice3[] = "d3"; | |
28 | 37 |
29 const char* kDevice1 = "d1"; | 38 const char kMountPointA[] = "mnt_a"; |
30 const char* kDevice2 = "d2"; | 39 const char kMountPointB[] = "mnt_b"; |
31 const char* kDevice3 = "d3"; | |
32 | 40 |
33 const char* kMountPointA = "mnt_a"; | 41 // TODO(thestig) Move this into base/string_util.h, or replace this with |
34 const char* kMountPointB = "mnt_b"; | 42 // strndup_with_new() if we find more uses for it. |
35 | 43 // Duplicate the content of |str| into a new char array. Caller takes ownership |
36 } // namespace | 44 // of the allocated array. Unlike std::string::c_str(), this returns a char* |
37 | 45 // instead of a const char*. |
38 namespace content { | 46 char* copy_string(const std::string& str) { |
47 const size_t len = str.length(); | |
48 char* ret = new char[len + 1]; | |
49 str.copy(ret, len, 0); | |
50 ret[len] = '\0'; | |
51 return ret; | |
52 } | |
39 | 53 |
40 class MediaDeviceNotificationsLinuxTestWrapper | 54 class MediaDeviceNotificationsLinuxTestWrapper |
41 : public MediaDeviceNotificationsLinux { | 55 : public MediaDeviceNotificationsLinux { |
42 public: | 56 public: |
43 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, | 57 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, |
44 MessageLoop* message_loop) | 58 MessageLoop* message_loop) |
45 : MediaDeviceNotificationsLinux(path), | 59 : MediaDeviceNotificationsLinux(path), |
46 message_loop_(message_loop) { | 60 message_loop_(message_loop) { |
47 } | 61 } |
48 | 62 |
49 protected: | 63 private: |
64 // Avoids code deleting the object while there are references to it. | |
65 // Aside from the base::RefCountedThreadSafe friend class, any attempts to | |
66 // call this dtor will result in a compile-time error. | |
50 ~MediaDeviceNotificationsLinuxTestWrapper() {} | 67 ~MediaDeviceNotificationsLinuxTestWrapper() {} |
51 | 68 |
52 virtual void OnFilePathChanged(const FilePath& path) { | 69 virtual void OnFilePathChanged(const FilePath& path) { |
53 MediaDeviceNotificationsLinux::OnFilePathChanged(path); | 70 MediaDeviceNotificationsLinux::OnFilePathChanged(path); |
54 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 71 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
55 } | 72 } |
56 | 73 |
57 private: | |
58 MessageLoop* message_loop_; | 74 MessageLoop* message_loop_; |
59 | 75 |
60 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTestWrapper); | 76 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTestWrapper); |
61 }; | 77 }; |
62 | 78 |
63 class MediaDeviceNotificationsLinuxTest : public testing::Test { | 79 class MediaDeviceNotificationsLinuxTest : public testing::Test { |
64 public: | 80 public: |
65 struct MtabTestData { | 81 struct MtabTestData { |
66 MtabTestData(const char* mount_device, | 82 MtabTestData(const std::string& mount_device, |
67 const char* mount_point, | 83 const std::string& mount_point, |
68 const char* mount_type) | 84 const std::string& mount_type) |
69 : mount_device(mount_device), | 85 : mount_device(mount_device), |
70 mount_point(mount_point), | 86 mount_point(mount_point), |
71 mount_type(mount_type) { | 87 mount_type(mount_type) { |
72 } | 88 } |
73 | 89 |
74 const char* mount_device; | 90 const std::string mount_device; |
75 const char* mount_point; | 91 const std::string mount_point; |
76 const char* mount_type; | 92 const std::string mount_type; |
77 }; | 93 }; |
78 | 94 |
79 MediaDeviceNotificationsLinuxTest() | 95 MediaDeviceNotificationsLinuxTest() |
80 : message_loop_(MessageLoop::TYPE_IO), | 96 : message_loop_(MessageLoop::TYPE_IO), |
81 file_thread_(BrowserThread::FILE, &message_loop_) { | 97 file_thread_(BrowserThread::FILE, &message_loop_) { |
82 system_monitor_.reset(new base::SystemMonitor()); | |
83 } | 98 } |
84 virtual ~MediaDeviceNotificationsLinuxTest() {} | 99 virtual ~MediaDeviceNotificationsLinuxTest() {} |
85 | 100 |
86 protected: | 101 protected: |
87 virtual void SetUp() { | 102 virtual void SetUp() { |
88 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); | 103 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver); |
89 system_monitor_->AddDevicesChangedObserver( | 104 system_monitor_.AddDevicesChangedObserver( |
90 mock_devices_changed_observer_.get()); | 105 mock_devices_changed_observer_.get()); |
91 | 106 |
92 // Create and set up a temp dir with files for the test. | 107 // Create and set up a temp dir with files for the test. |
93 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); | 108 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir()); |
94 FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc"); | 109 FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc"); |
95 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); | 110 ASSERT_TRUE(file_util::CreateDirectory(test_dir)); |
96 mtab_file_ = test_dir.AppendASCII("test_mtab"); | 111 mtab_file_ = test_dir.AppendASCII("test_mtab"); |
97 struct MtabTestData initial_test_data[] = { | 112 MtabTestData initial_test_data[] = { |
98 MtabTestData("dummydevice", "dummydir", kInvalidFS), | 113 MtabTestData("dummydevice", "dummydir", kInvalidFS), |
99 }; | 114 }; |
100 WriteToMtab(initial_test_data, arraysize(initial_test_data), true); | 115 WriteToMtab(initial_test_data, |
116 arraysize(initial_test_data), | |
117 true /* overwrite */); | |
101 | 118 |
102 // Initialize the test subject. | 119 // Initialize the test subject. |
103 notifications_ = | 120 notifications_ = |
104 new MediaDeviceNotificationsLinuxTestWrapper(mtab_file_, | 121 new MediaDeviceNotificationsLinuxTestWrapper(mtab_file_, |
105 &message_loop_); | 122 &message_loop_); |
106 notifications_->Init(); | 123 notifications_->Init(); |
107 message_loop_.RunAllPending(); | 124 message_loop_.RunAllPending(); |
108 } | 125 } |
109 | 126 |
110 virtual void TearDown() { | 127 virtual void TearDown() { |
111 message_loop_.RunAllPending(); | 128 message_loop_.RunAllPending(); |
112 notifications_ = NULL; | 129 notifications_ = NULL; |
113 system_monitor_->RemoveDevicesChangedObserver( | 130 system_monitor_.RemoveDevicesChangedObserver( |
114 mock_devices_changed_observer_.get()); | 131 mock_devices_changed_observer_.get()); |
115 } | 132 } |
116 | 133 |
117 // Used to run tests. When the mtab file gets modified, the message loop | 134 // Append to the mtab with |data| of |data_size| and run the message loop. |
ajl
2012/04/10 01:28:27
Grammar for this comment is kind of awkward. Some
Lei Zhang
2012/04/10 21:02:47
Done.
| |
118 // needs to run in order to react to the file modification. | 135 void AppendToMtabAndRunLoop(const MtabTestData* data, size_t data_size) { |
119 // See WriteToMtab for parameters. | 136 WriteToMtab(data, data_size, false /* do not overwrite */); |
120 void WriteToMtabAndRunLoop(struct MtabTestData* data, | |
121 size_t data_size, | |
122 bool overwrite) { | |
123 WriteToMtab(data, data_size, overwrite); | |
124 message_loop_.Run(); | 137 message_loop_.Run(); |
125 } | 138 } |
126 | 139 |
140 // Overwrite the mtab with |data| of |data_size| and run the message loop. | |
141 void OverwriteMtabAndRunLoop(const MtabTestData* data, size_t data_size) { | |
142 WriteToMtab(data, data_size, true /* overwrite */); | |
143 message_loop_.Run(); | |
144 } | |
145 | |
146 // Simplied version of WriteToMtabAndRunLoop() that just deletes all the | |
147 // entries in the mtab file. | |
148 void WriteEmptyMtabAndRunLoop() { | |
149 OverwriteMtabAndRunLoop(NULL, // No data. | |
150 0); // No data length. | |
151 } | |
152 | |
153 // Create a directory named |dir| relative to the test directory. | |
154 // It has a DCIM directory, so MediaDeviceNotificationsLinux recognizes it as | |
155 // a media directory. | |
156 FilePath CreateMountPointWithDCIMDir(const std::string& dir) { | |
157 return CreateMountPoint(dir, true /* create DCIM dir */); | |
158 } | |
159 | |
160 // Create a directory named |dir| relative to the test directory. | |
161 // It does not have a DCIM directory, so MediaDeviceNotificationsLinux does | |
162 // not recognizes it as a media directory. | |
163 FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) { | |
164 return CreateMountPoint(dir, false /* do not create DCIM dir */); | |
165 } | |
166 | |
167 base::MockDevicesChangedObserver& observer() { | |
168 return *mock_devices_changed_observer_; | |
169 } | |
170 | |
171 private: | |
127 // Create a directory named |dir| relative to the test directory. | 172 // Create a directory named |dir| relative to the test directory. |
128 // Set |with_dcim_dir| to true if the created directory will have a "DCIM" | 173 // Set |with_dcim_dir| to true if the created directory will have a "DCIM" |
129 // subdirectory. | 174 // subdirectory. |
130 // Returns the full path to the created directory on success, or an empty | 175 // Returns the full path to the created directory on success, or an empty |
131 // path on failure. | 176 // path on failure. |
132 FilePath CreateMountPoint(const char* dir, bool with_dcim_dir) { | 177 FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) { |
133 FilePath return_path(scoped_temp_dir_.path()); | 178 FilePath return_path(scoped_temp_dir_.path()); |
134 return_path = return_path.AppendASCII(dir); | 179 return_path = return_path.AppendASCII(dir); |
135 FilePath path(return_path); | 180 FilePath path(return_path); |
136 if (with_dcim_dir) | 181 if (with_dcim_dir) |
137 path = path.AppendASCII("DCIM"); | 182 path = path.AppendASCII("DCIM"); |
138 if (!file_util::CreateDirectory(path)) | 183 if (!file_util::CreateDirectory(path)) |
139 return FilePath(); | 184 return FilePath(); |
140 return return_path; | 185 return return_path; |
141 } | 186 } |
142 | 187 |
143 base::MockDevicesChangedObserver& observer() { | |
144 return *mock_devices_changed_observer_; | |
145 } | |
146 | |
147 private: | |
148 // Write the test mtab data to |mtab_file_|. | 188 // Write the test mtab data to |mtab_file_|. |
149 // |data| is an array of mtab entries. | 189 // |data| is an array of mtab entries. |
150 // |data_size| is the array size of |data|. | 190 // |data_size| is the array size of |data|. |
151 // |overwrite| specifies whether to overwrite |mtab_file_|. | 191 // |overwrite| specifies whether to overwrite |mtab_file_|. |
152 void WriteToMtab(struct MtabTestData* data, | 192 void WriteToMtab(const MtabTestData* data, |
153 size_t data_size, | 193 size_t data_size, |
154 bool overwrite) { | 194 bool overwrite) { |
155 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a"); | 195 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a"); |
156 ASSERT_TRUE(file); | 196 ASSERT_TRUE(file); |
157 | 197 |
158 struct mntent entry; | 198 mntent entry; |
159 entry.mnt_opts = strdup("rw"); | 199 scoped_array<char> mount_opts(copy_string("rw")); |
200 entry.mnt_opts = mount_opts.get(); | |
160 entry.mnt_freq = 0; | 201 entry.mnt_freq = 0; |
161 entry.mnt_passno = 0; | 202 entry.mnt_passno = 0; |
162 for (size_t i = 0; i < data_size; ++i) { | 203 for (size_t i = 0; i < data_size; ++i) { |
163 entry.mnt_fsname = strdup(data[i].mount_device); | 204 scoped_array<char> mount_device(copy_string(data[i].mount_device)); |
164 entry.mnt_dir = strdup(data[i].mount_point); | 205 scoped_array<char> mount_point(copy_string(data[i].mount_point)); |
165 entry.mnt_type = strdup(data[i].mount_type); | 206 scoped_array<char> mount_type(copy_string(data[i].mount_type)); |
166 int add_result = addmntent(file, &entry); | 207 entry.mnt_fsname = mount_device.get(); |
167 ASSERT_EQ(0, add_result); | 208 entry.mnt_dir = mount_point.get(); |
168 free(entry.mnt_fsname); | 209 entry.mnt_type = mount_type.get(); |
169 free(entry.mnt_dir); | 210 ASSERT_EQ(0, addmntent(file, &entry)); |
170 free(entry.mnt_type); | |
171 } | 211 } |
172 free(entry.mnt_opts); | 212 ASSERT_EQ(1, endmntent(file)); |
173 int end_result = endmntent(file); | |
174 ASSERT_EQ(1, end_result); | |
175 } | 213 } |
176 | 214 |
177 // The message loop and file thread to run tests on. | 215 // The message loop and file thread to run tests on. |
178 MessageLoop message_loop_; | 216 MessageLoop message_loop_; |
179 BrowserThreadImpl file_thread_; | 217 BrowserThreadImpl file_thread_; |
180 | 218 |
181 // SystemMonitor and DevicesChangedObserver to hook together to test. | 219 // SystemMonitor and DevicesChangedObserver to hook together to test. |
182 scoped_ptr<base::SystemMonitor> system_monitor_; | 220 base::SystemMonitor system_monitor_; |
183 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; | 221 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_; |
184 | 222 |
185 // Temporary directory for created test data. | 223 // Temporary directory for created test data. |
186 ScopedTempDir scoped_temp_dir_; | 224 ScopedTempDir scoped_temp_dir_; |
187 // Path to the test mtab file. | 225 // Path to the test mtab file. |
188 FilePath mtab_file_; | 226 FilePath mtab_file_; |
189 | 227 |
190 scoped_refptr<MediaDeviceNotificationsLinuxTestWrapper> notifications_; | 228 scoped_refptr<MediaDeviceNotificationsLinuxTestWrapper> notifications_; |
191 | 229 |
192 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest); | 230 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest); |
193 }; | 231 }; |
194 | 232 |
233 // Simple test case where we attach and detach a media device. | |
195 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { | 234 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) { |
196 testing::Sequence mock_sequence; | 235 testing::Sequence mock_sequence; |
197 FilePath test_path = CreateMountPoint(kMountPointA, true); | 236 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); |
198 ASSERT_FALSE(test_path.empty()); | 237 ASSERT_FALSE(test_path.empty()); |
199 struct MtabTestData test_data[] = { | 238 MtabTestData test_data[] = { |
200 MtabTestData(kDevice1, kInvalidPath, kValidFS), | 239 MtabTestData(kDevice1, kInvalidPath, kValidFS), |
201 MtabTestData(kDevice2, test_path.value().c_str(), kValidFS), | 240 MtabTestData(kDevice2, test_path.value().c_str(), kValidFS), |
202 }; | 241 }; |
242 // Only |kDevice2| should be attached, since |kDevice1| has a bad path. | |
203 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice2, test_path)) | 243 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice2, test_path)) |
204 .InSequence(mock_sequence); | 244 .InSequence(mock_sequence); |
205 WriteToMtabAndRunLoop(test_data, arraysize(test_data), false); | 245 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); |
206 | 246 |
247 // |kDevice2| should be detached here. | |
207 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); | 248 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); |
208 WriteToMtabAndRunLoop(NULL, 0, true); | 249 WriteEmptyMtabAndRunLoop(); |
209 } | 250 } |
210 | 251 |
211 // Only mount points with DCIM directories are recognized. | 252 // Only mount points with DCIM directories are recognized. |
212 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { | 253 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) { |
213 testing::Sequence mock_sequence; | 254 testing::Sequence mock_sequence; |
214 FilePath test_pathA = CreateMountPoint(kMountPointA, true); | 255 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
215 ASSERT_FALSE(test_pathA.empty()); | 256 ASSERT_FALSE(test_path_a.empty()); |
216 struct MtabTestData test_data1[] = { | 257 MtabTestData test_data1[] = { |
217 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 258 MtabTestData(kDevice1, test_path_a.value().c_str(), kValidFS), |
218 }; | 259 }; |
219 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathA)) | 260 // |kDevice1| should be attached as expected. |
261 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_path_a)) | |
220 .InSequence(mock_sequence); | 262 .InSequence(mock_sequence); |
221 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false); | 263 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
222 | 264 |
223 FilePath test_pathB = CreateMountPoint(kMountPointB, false); | 265 // This should do nothing, since |kMountPointB| does not have a DCIM dir. |
224 ASSERT_FALSE(test_pathB.empty()); | 266 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); |
225 struct MtabTestData test_data2[] = { | 267 ASSERT_FALSE(test_path_b.empty()); |
226 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 268 MtabTestData test_data2[] = { |
269 MtabTestData(kDevice2, test_path_b.value().c_str(), kValidFS), | |
227 }; | 270 }; |
228 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); | 271 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
229 | 272 |
273 // |kDevice1| should be detached as expected. | |
230 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); | 274 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence); |
231 WriteToMtabAndRunLoop(NULL, 0, true); | 275 WriteEmptyMtabAndRunLoop(); |
232 } | 276 } |
233 | 277 |
278 // More complicated test case with multiple devices on multiple mount points. | |
234 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) { | 279 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) { |
235 FilePath test_pathA = CreateMountPoint(kMountPointA, true); | 280 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
236 FilePath test_pathB = CreateMountPoint(kMountPointB, true); | 281 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); |
237 ASSERT_FALSE(test_pathA.empty()); | 282 ASSERT_FALSE(test_path_a.empty()); |
238 ASSERT_FALSE(test_pathB.empty()); | 283 ASSERT_FALSE(test_path_b.empty()); |
239 | 284 |
240 // Attach two devices. | 285 // Attach two devices. |
241 // kDevice1 -> kMountPointA | 286 // kDevice1 -> kMountPointA |
242 // kDevice2 -> kMountPointB | 287 // kDevice2 -> kMountPointB |
243 struct MtabTestData test_data1[] = { | 288 MtabTestData test_data1[] = { |
244 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 289 MtabTestData(kDevice1, test_path_a.value().c_str(), kValidFS), |
ajl
2012/04/10 01:28:27
This c_str() here is unnecessary now, right? Same
Lei Zhang
2012/04/10 21:02:47
Done.
| |
245 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 290 MtabTestData(kDevice2, test_path_b.value().c_str(), kValidFS), |
246 }; | 291 }; |
247 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); | 292 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); |
248 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 293 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
249 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false); | 294 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
250 | 295 |
251 // Attach |kDevice1| to |kMountPointB|. | 296 // Attach |kDevice1| to |kMountPointB|. |
252 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been | 297 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been |
253 // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA. | 298 // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA. |
254 // kDevice1 -> kMountPointA | 299 // kDevice1 -> kMountPointA |
255 // kDevice2 -> kMountPointB | 300 // kDevice2 -> kMountPointB |
256 // kDevice1 -> kMountPointB | 301 // kDevice1 -> kMountPointB |
257 struct MtabTestData test_data2[] = { | 302 MtabTestData test_data2[] = { |
258 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS), | 303 MtabTestData(kDevice1, test_path_b.value().c_str(), kValidFS), |
259 }; | 304 }; |
260 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); | 305 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); |
261 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); | 306 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); |
262 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); | 307 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
263 | 308 |
264 // Attach |kDevice2| to |kMountPointA|. | 309 // Attach |kDevice2| to |kMountPointA|. |
265 // kDevice1 -> kMountPointA | 310 // kDevice1 -> kMountPointA |
266 // kDevice2 -> kMountPointB | 311 // kDevice2 -> kMountPointB |
267 // kDevice1 -> kMountPointB | 312 // kDevice1 -> kMountPointB |
268 // kDevice2 -> kMountPointA | 313 // kDevice2 -> kMountPointA |
269 struct MtabTestData test_data3[] = { | 314 MtabTestData test_data3[] = { |
270 MtabTestData(kDevice2, test_pathA.value().c_str(), kValidFS), | 315 MtabTestData(kDevice2, test_path_a.value().c_str(), kValidFS), |
271 }; | 316 }; |
272 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); | 317 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1); |
273 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 318 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
274 WriteToMtabAndRunLoop(test_data3, arraysize(test_data3), false); | 319 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3)); |
275 | 320 |
276 // Detach |kDevice2| from |kMountPointA|. | 321 // Detach |kDevice2| from |kMountPointA|. |
277 // kDevice1 -> kMountPointA | 322 // kDevice1 -> kMountPointA |
278 // kDevice2 -> kMountPointB | 323 // kDevice2 -> kMountPointB |
279 // kDevice1 -> kMountPointB | 324 // kDevice1 -> kMountPointB |
280 struct MtabTestData test_data4[] = { | 325 MtabTestData test_data4[] = { |
281 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 326 MtabTestData(kDevice1, test_path_a.value().c_str(), kValidFS), |
282 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 327 MtabTestData(kDevice2, test_path_b.value().c_str(), kValidFS), |
283 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS), | 328 MtabTestData(kDevice1, test_path_b.value().c_str(), kValidFS), |
284 }; | 329 }; |
285 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 330 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
286 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); | 331 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); |
287 WriteToMtabAndRunLoop(test_data4, arraysize(test_data4), true); | 332 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4)); |
288 | 333 |
289 // Detach |kDevice1| from |kMountPointB|. | 334 // Detach |kDevice1| from |kMountPointB|. |
290 // kDevice1 -> kMountPointA | 335 // kDevice1 -> kMountPointA |
291 // kDevice2 -> kMountPointB | 336 // kDevice2 -> kMountPointB |
292 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); | 337 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); |
293 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); | 338 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); |
294 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true); | 339 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); |
295 | 340 |
296 // Detach all devices. | 341 // Detach all devices. |
297 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 342 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
298 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); | 343 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); |
299 WriteToMtabAndRunLoop(NULL, 0, true); | 344 WriteEmptyMtabAndRunLoop(); |
300 } | 345 } |
301 | 346 |
347 // More complicated test case with multiple devices on one mount point. | |
302 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { | 348 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { |
303 testing::Sequence mock_sequence; | 349 testing::Sequence mock_sequence; |
304 FilePath test_pathA = CreateMountPoint(kMountPointA, true); | 350 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
305 FilePath test_pathB = CreateMountPoint(kMountPointB, true); | 351 FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB); |
306 ASSERT_FALSE(test_pathA.empty()); | 352 ASSERT_FALSE(test_path_a.empty()); |
307 ASSERT_FALSE(test_pathB.empty()); | 353 ASSERT_FALSE(test_path_b.empty()); |
308 | 354 |
309 // |kDevice1| is most recently mounted at |kMountPointB|. | 355 // |kDevice1| is most recently mounted at |kMountPointB|. |
310 // kDevice1 -> kMountPointA | 356 // kDevice1 -> kMountPointA |
311 // kDevice2 -> kMountPointB | 357 // kDevice2 -> kMountPointB |
312 // kDevice1 -> kMountPointB | 358 // kDevice1 -> kMountPointB |
313 struct MtabTestData test_data1[] = { | 359 MtabTestData test_data1[] = { |
314 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS), | 360 MtabTestData(kDevice1, test_path_a.value().c_str(), kValidFS), |
315 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS), | 361 MtabTestData(kDevice2, test_path_b.value().c_str(), kValidFS), |
316 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS), | 362 MtabTestData(kDevice1, test_path_b.value().c_str(), kValidFS), |
317 }; | 363 }; |
318 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathB)) | 364 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_path_b)) |
319 .Times(1); | 365 .Times(1); |
320 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); | 366 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0); |
321 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true); | 367 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); |
322 | 368 |
323 // Attach |kDevice3| to |kMountPointB|. | 369 // Attach |kDevice3| to |kMountPointB|. |
324 // |kDevice1| is inaccessible at its most recent mount point, so it is | 370 // |kDevice1| is inaccessible at its most recent mount point, so it is |
325 // detached and unavailable, even though it is still accessible via | 371 // detached and unavailable, even though it is still accessible via |
326 // |kMountPointA|. | 372 // |kMountPointA|. |
327 // kDevice1 -> kMountPointA | 373 // kDevice1 -> kMountPointA |
328 // kDevice2 -> kMountPointB | 374 // kDevice2 -> kMountPointB |
329 // kDevice1 -> kMountPointB | 375 // kDevice1 -> kMountPointB |
330 // kDevice3 -> kMountPointB | 376 // kDevice3 -> kMountPointB |
331 struct MtabTestData test_data2[] = { | 377 MtabTestData test_data2[] = { |
332 MtabTestData(kDevice3, test_pathB.value().c_str(), kValidFS), | 378 MtabTestData(kDevice3, test_path_b.value().c_str(), kValidFS), |
333 }; | 379 }; |
334 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).Times(1); | 380 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).Times(1); |
335 EXPECT_CALL(observer(), OnMediaDeviceAttached(1, kDevice3, test_pathB)) | 381 EXPECT_CALL(observer(), OnMediaDeviceAttached(1, kDevice3, test_path_b)) |
336 .Times(1); | 382 .Times(1); |
337 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false); | 383 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
338 | 384 |
339 // Detach all devices. | 385 // Detach all devices. |
340 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 386 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
341 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1); | 387 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1); |
342 WriteToMtabAndRunLoop(NULL, 0, true); | 388 WriteEmptyMtabAndRunLoop(); |
343 } | 389 } |
344 | 390 |
391 } // namespace | |
392 | |
345 } // namespace content | 393 } // namespace content |
OLD | NEW |