| 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. | 5 // MediaDeviceNotificationsLinux unit tests. |
| 6 | 6 |
| 7 #include "chrome/browser/media_gallery/media_device_notifications_linux.h" | 7 #include "chrome/browser/media_gallery/media_device_notifications_linux.h" |
| 8 | 8 |
| 9 #include <mntent.h> | 9 #include <mntent.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 const char kInvalidPath[] = "invalid path does not exist"; | 33 const char kInvalidPath[] = "invalid path does not exist"; |
| 34 | 34 |
| 35 const char kDevice1[] = "d1"; | 35 const char kDevice1[] = "d1"; |
| 36 const char kDevice2[] = "d2"; | 36 const char kDevice2[] = "d2"; |
| 37 const char kDevice3[] = "d3"; | 37 const char kDevice3[] = "d3"; |
| 38 | 38 |
| 39 const char kMountPointA[] = "mnt_a"; | 39 const char kMountPointA[] = "mnt_a"; |
| 40 const char kMountPointB[] = "mnt_b"; | 40 const char kMountPointB[] = "mnt_b"; |
| 41 | 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 | 42 class MediaDeviceNotificationsLinuxTestWrapper |
| 56 : public MediaDeviceNotificationsLinux { | 43 : public MediaDeviceNotificationsLinux { |
| 57 public: | 44 public: |
| 58 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, | 45 MediaDeviceNotificationsLinuxTestWrapper(const FilePath& path, |
| 59 MessageLoop* message_loop) | 46 MessageLoop* message_loop) |
| 60 : MediaDeviceNotificationsLinux(path), | 47 : MediaDeviceNotificationsLinux(path), |
| 61 message_loop_(message_loop) { | 48 message_loop_(message_loop) { |
| 62 } | 49 } |
| 63 | 50 |
| 64 private: | 51 private: |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 // Write the test mtab data to |mtab_file_|. | 178 // Write the test mtab data to |mtab_file_|. |
| 192 // |data| is an array of mtab entries. | 179 // |data| is an array of mtab entries. |
| 193 // |data_size| is the array size of |data|. | 180 // |data_size| is the array size of |data|. |
| 194 // |overwrite| specifies whether to overwrite |mtab_file_|. | 181 // |overwrite| specifies whether to overwrite |mtab_file_|. |
| 195 void WriteToMtab(const MtabTestData* data, | 182 void WriteToMtab(const MtabTestData* data, |
| 196 size_t data_size, | 183 size_t data_size, |
| 197 bool overwrite) { | 184 bool overwrite) { |
| 198 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a"); | 185 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a"); |
| 199 ASSERT_TRUE(file); | 186 ASSERT_TRUE(file); |
| 200 | 187 |
| 188 // Due to the glibc *mntent() interface design, which is out of our |
| 189 // control, the mtnent struct has several char* fields, even though |
| 190 // addmntent() does not write to them in the calls below. To make the |
| 191 // compiler happy while avoiding making additional copies of strings, |
| 192 // we just const_cast() the strings' c_str()s. |
| 193 // Assuming addmntent() does not write to the char* fields, this is safe. |
| 194 // It is unlikely the platforms this test suite runs on will have an |
| 195 // addmntent() implementation that does change the char* fields. If that |
| 196 // was ever the case, the test suite will start crashing or failing. |
| 201 mntent entry; | 197 mntent entry; |
| 202 scoped_array<char> mount_opts(copy_string("rw")); | 198 static const char kMountOpts[] = "rw"; |
| 203 entry.mnt_opts = mount_opts.get(); | 199 entry.mnt_opts = const_cast<char*>(kMountOpts); |
| 204 entry.mnt_freq = 0; | 200 entry.mnt_freq = 0; |
| 205 entry.mnt_passno = 0; | 201 entry.mnt_passno = 0; |
| 206 for (size_t i = 0; i < data_size; ++i) { | 202 for (size_t i = 0; i < data_size; ++i) { |
| 207 scoped_array<char> mount_device(copy_string(data[i].mount_device)); | 203 entry.mnt_fsname = const_cast<char*>(data[i].mount_device.c_str()); |
| 208 scoped_array<char> mount_point(copy_string(data[i].mount_point)); | 204 entry.mnt_dir = const_cast<char*>(data[i].mount_point.c_str()); |
| 209 scoped_array<char> mount_type(copy_string(data[i].mount_type)); | 205 entry.mnt_type = const_cast<char*>(data[i].mount_type.c_str()); |
| 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)); | 206 ASSERT_EQ(0, addmntent(file, &entry)); |
| 214 } | 207 } |
| 215 ASSERT_EQ(1, endmntent(file)); | 208 ASSERT_EQ(1, endmntent(file)); |
| 216 } | 209 } |
| 217 | 210 |
| 218 // The message loop and file thread to run tests on. | 211 // The message loop and file thread to run tests on. |
| 219 MessageLoop message_loop_; | 212 MessageLoop message_loop_; |
| 220 content::TestBrowserThread file_thread_; | 213 content::TestBrowserThread file_thread_; |
| 221 | 214 |
| 222 // SystemMonitor and DevicesChangedObserver to hook together to test. | 215 // SystemMonitor and DevicesChangedObserver to hook together to test. |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 | 380 |
| 388 // Detach all devices. | 381 // Detach all devices. |
| 389 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); | 382 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); |
| 390 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1); | 383 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1); |
| 391 WriteEmptyMtabAndRunLoop(); | 384 WriteEmptyMtabAndRunLoop(); |
| 392 } | 385 } |
| 393 | 386 |
| 394 } // namespace | 387 } // namespace |
| 395 | 388 |
| 396 } // namespace chrome | 389 } // namespace chrome |
| OLD | NEW |