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

Side by Side Diff: content/browser/media_device_notifications_linux_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698