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