Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome/browser/media_gallery/media_device_notifications_linux_unittest.cc

Issue 10105037: Replace string copy code and scoped_arrays() with const_casts in MediaDeviceNotificationsLinuxTest. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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() design, which is out of our control, the
189 // mtnent struct has several char* fields, even though we do not write to
vandebo (ex-Chrome) 2012/04/18 00:01:50 nit: we -> it
190 // them in the calls below. To make the compiler happy while avoiding making
191 // additional copies of strings, we just const_cast() the strings' c_str()s.
192 // This is safe since addmntent() does not write to the char* fields.
193 // In case there is an addmntent() implementation that does change the
194 // fields in question, it will crash if it tries to wrote to the mnt_opts
vandebo (ex-Chrome) 2012/04/18 00:01:50 nit: wrote -> write
195 // field. If such an implementation writes to the other char* fields, that
196 // is ok because each MtabTestData struct is used only once. All the fields
197 // in the MtabTestData structs are constructed from C strings and not from
198 // other std::strings, so there is no std::string COW issues to worry about
199 // either.
201 mntent entry; 200 mntent entry;
202 scoped_array<char> mount_opts(copy_string("rw")); 201 static const char kMountOpts[] = "rw";
203 entry.mnt_opts = mount_opts.get(); 202 entry.mnt_opts = const_cast<char*>(kMountOpts);
204 entry.mnt_freq = 0; 203 entry.mnt_freq = 0;
205 entry.mnt_passno = 0; 204 entry.mnt_passno = 0;
206 for (size_t i = 0; i < data_size; ++i) { 205 for (size_t i = 0; i < data_size; ++i) {
207 scoped_array<char> mount_device(copy_string(data[i].mount_device)); 206 entry.mnt_fsname = const_cast<char*>(data[i].mount_device.c_str());
208 scoped_array<char> mount_point(copy_string(data[i].mount_point)); 207 entry.mnt_dir = const_cast<char*>(data[i].mount_point.c_str());
209 scoped_array<char> mount_type(copy_string(data[i].mount_type)); 208 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)); 209 ASSERT_EQ(0, addmntent(file, &entry));
214 } 210 }
215 ASSERT_EQ(1, endmntent(file)); 211 ASSERT_EQ(1, endmntent(file));
216 } 212 }
217 213
218 // The message loop and file thread to run tests on. 214 // The message loop and file thread to run tests on.
219 MessageLoop message_loop_; 215 MessageLoop message_loop_;
220 content::TestBrowserThread file_thread_; 216 content::TestBrowserThread file_thread_;
221 217
222 // SystemMonitor and DevicesChangedObserver to hook together to test. 218 // SystemMonitor and DevicesChangedObserver to hook together to test.
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 MtabTestData(kDevice2, test_path_b.value(), kValidFS), 326 MtabTestData(kDevice2, test_path_b.value(), kValidFS),
331 MtabTestData(kDevice1, test_path_b.value(), kValidFS), 327 MtabTestData(kDevice1, test_path_b.value(), kValidFS),
332 }; 328 };
333 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); 329 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
334 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); 330 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1);
335 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4)); 331 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4));
336 332
337 // Detach |kDevice1| from |kMountPointB|. 333 // Detach |kDevice1| from |kMountPointB|.
338 // kDevice1 -> kMountPointA 334 // kDevice1 -> kMountPointA
339 // kDevice2 -> kMountPointB 335 // kDevice2 -> kMountPointB
336 MtabTestData test_data5[] = {
337 MtabTestData(kDevice1, test_path_a.value(), kValidFS),
338 MtabTestData(kDevice2, test_path_b.value(), kValidFS),
339 };
340 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2); 340 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2);
341 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1); 341 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1);
342 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1)); 342 OverwriteMtabAndRunLoop(test_data5, arraysize(test_data5));
343 343
344 // Detach all devices. 344 // Detach all devices.
345 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); 345 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
346 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2); 346 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2);
347 WriteEmptyMtabAndRunLoop(); 347 WriteEmptyMtabAndRunLoop();
348 } 348 }
349 349
350 // More complicated test case with multiple devices on one mount point. 350 // More complicated test case with multiple devices on one mount point.
351 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) { 351 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) {
352 testing::Sequence mock_sequence; 352 testing::Sequence mock_sequence;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 387
388 // Detach all devices. 388 // Detach all devices.
389 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0); 389 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
390 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1); 390 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1);
391 WriteEmptyMtabAndRunLoop(); 391 WriteEmptyMtabAndRunLoop();
392 } 392 }
393 393
394 } // namespace 394 } // namespace
395 395
396 } // namespace chrome 396 } // namespace chrome
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698