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

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

Issue 9560008: Implement Linux media notifier. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 #include <fcntl.h>
6 #include <mntent.h>
7 #include <stdio.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10
11 #include <string>
12
13 #include "base/file_util.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/message_loop.h"
17 #include "base/scoped_temp_dir.h"
18 #include "base/system_monitor/system_monitor.h"
19 #include "base/test/mock_devices_changed_observer.h"
20 #include "content/browser/browser_thread_impl.h"
21 #include "content/browser/media_device_notifications_linux.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23
24 using testing::_;
25
26 namespace {
27
28 const char* kValidFS = "vfat";
29 const char* kInvalidFS = "invalidfs";
30
31 const char* kInvalidPath = "invalid path does not exist";
32
33 const char* kDevice1 = "d1";
34 const char* kDevice2 = "d2";
35 const char* kDevice3 = "d3";
36
37 const char* kMountPointA = "mnt_a";
38 const char* kMountPointB = "mnt_b";
39
40 } // namespace
41
42 namespace content {
43
44 class MediaDeviceNotificationsLinuxTest : public testing::Test {
45 public:
46 struct MtabTestData {
47 MtabTestData(const char* mount_device,
48 const char* mount_point,
49 const char* mount_type)
50 : mount_device(mount_device),
51 mount_point(mount_point),
52 mount_type(mount_type) {
53 }
54
55 const char* mount_device;
56 const char* mount_point;
57 const char* mount_type;
58 };
59
60 MediaDeviceNotificationsLinuxTest()
61 : message_loop_(MessageLoop::TYPE_IO),
62 file_thread_(BrowserThread::FILE, &message_loop_) {
63 system_monitor_.reset(new base::SystemMonitor());
64 }
65 virtual ~MediaDeviceNotificationsLinuxTest() {}
66
67 protected:
68 virtual void SetUp() {
69 mock_devices_changed_observer_.reset(new base::MockDevicesChangedObserver);
70 system_monitor_->AddDevicesChangedObserver(
71 mock_devices_changed_observer_.get());
72
73 // Create and set up a temp dir with files for the test.
74 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
75 FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc");
76 ASSERT_TRUE(file_util::CreateDirectory(test_dir));
77 mtab_file_ = test_dir.AppendASCII("test_mtab");
78 struct MtabTestData initial_test_data[] = {
79 MtabTestData("dummydevice", "dummydir", kInvalidFS),
80 };
81 WriteToMtab(initial_test_data, arraysize(initial_test_data), true);
82
83 // Initialize the test subject.
84 notifications_ = new MediaDeviceNotificationsLinux(mtab_file_);
85 BrowserThread::PostTask(
86 BrowserThread::FILE, FROM_HERE,
87 base::Bind(&MediaDeviceNotificationsLinux::InitOnFileThread,
88 notifications_.get()));
89 message_loop_.RunAllPending();
90 }
91
92 virtual void TearDown() {
93 message_loop_.RunAllPending();
94 notifications_ = NULL;
95 system_monitor_->RemoveDevicesChangedObserver(
96 mock_devices_changed_observer_.get());
97 }
98
99 // Used to run tests. When the mtab file gets modified, the message loop
100 // needs to run in order to react to the file modification.
101 // See WriteToMtab for parameters.
102 void WriteToMtabAndRunLoop(struct MtabTestData* data,
103 size_t data_size,
104 bool overwrite) {
105 WriteToMtab(data, data_size, overwrite);
106 message_loop_.RunAllPending();
107 }
108
109 // Create a directory named |dir| relative to the test directory.
110 // Set |with_dcim_dir| to true if the created directory will have a "DCIM"
111 // subdirectory.
112 // Returns the full path to the created directory on success, or an empty
113 // path on failure.
114 FilePath CreateMountPoint(const char* dir, bool with_dcim_dir) {
115 FilePath return_path(scoped_temp_dir_.path());
116 return_path = return_path.AppendASCII(dir);
117 FilePath path(return_path);
118 if (with_dcim_dir)
119 path = path.AppendASCII("DCIM");
120 if (!file_util::CreateDirectory(path))
121 return FilePath();
122 return return_path;
123 }
124
125 base::MockDevicesChangedObserver& observer() {
126 return *mock_devices_changed_observer_.get();
127 }
128
129 private:
130 // Write the test mtab data to |mtab_file_|.
131 // |data| is an array of mtab entries.
132 // |data_size| is the array size of |data|.
133 // |overwrite| specifies whether to overwrite |mtab_file_|.
134 void WriteToMtab(struct MtabTestData* data,
135 size_t data_size,
136 bool overwrite) {
137 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a");
138 ASSERT_TRUE(file);
139
140 struct mntent entry;
141 entry.mnt_opts = strdup("rw");
142 entry.mnt_freq = 0;
143 entry.mnt_passno = 0;
144 for (size_t i = 0; i < data_size; ++i) {
145 entry.mnt_fsname = strdup(data[i].mount_device);
146 entry.mnt_dir = strdup(data[i].mount_point);
147 entry.mnt_type = strdup(data[i].mount_type);
148 int add_result = addmntent(file, &entry);
149 ASSERT_EQ(0, add_result);
150 free(entry.mnt_fsname);
151 free(entry.mnt_dir);
152 free(entry.mnt_type);
153 }
154 free(entry.mnt_opts);
155 int end_result = endmntent(file);
156 ASSERT_EQ(1, end_result);
157
158 // Need to ensure data reaches disk so the FilePathWatcher fires in time.
159 // Otherwise this will cause MediaDeviceNotificationsLinuxTest to be flaky.
160 int fd = open(mtab_file_.value().c_str(), O_RDONLY);
161 ASSERT_GE(fd, 0);
162
163 int fsync_result = fsync(fd);
164 ASSERT_EQ(0, fsync_result);
165
166 int close_result = close(fd);
167 ASSERT_EQ(0, close_result);
168 }
169
170 // The message loop and file thread to run tests on.
171 MessageLoop message_loop_;
172 BrowserThreadImpl file_thread_;
173
174 // SystemMonitor and DevicesChangedObserver to hook together to test.
175 scoped_ptr<base::SystemMonitor> system_monitor_;
176 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_;
177
178 // Temporary directory for created test data.
179 ScopedTempDir scoped_temp_dir_;
180 // Path to the test mtab file.
181 FilePath mtab_file_;
182
183 scoped_refptr<MediaDeviceNotificationsLinux> notifications_;
184
185 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest);
186 };
187
188 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) {
189 testing::Sequence mock_sequence;
190 FilePath test_path = CreateMountPoint(kMountPointA, true);
191 ASSERT_FALSE(test_path.empty());
192 struct MtabTestData test_data[] = {
193 MtabTestData(kDevice1, kInvalidPath, kValidFS),
194 MtabTestData(kDevice2, test_path.value().c_str(), kValidFS),
195 };
196 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice2, test_path))
197 .InSequence(mock_sequence);
198 WriteToMtabAndRunLoop(test_data, arraysize(test_data), false);
199
200 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence);
201 WriteToMtabAndRunLoop(NULL, 0, true);
202 }
203
204 // Only mount points with DCIM directories are recognized.
205 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) {
206 testing::Sequence mock_sequence;
207 FilePath test_pathA = CreateMountPoint(kMountPointA, true);
208 ASSERT_FALSE(test_pathA.empty());
209 struct MtabTestData test_data1[] = {
210 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS),
211 };
212 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathA))
213 .InSequence(mock_sequence);
214 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false);
215
216 FilePath test_pathB = CreateMountPoint(kMountPointB, false);
217 ASSERT_FALSE(test_pathB.empty());
218 struct MtabTestData test_data2[] = {
219 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS),
220 };
221 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false);
222
223 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence);
224 WriteToMtabAndRunLoop(NULL, 0, true);
225 }
226
227 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) {
228 FilePath test_pathA = CreateMountPoint(kMountPointA, true);
229 FilePath test_pathB = CreateMountPoint(kMountPointB, true);
230 ASSERT_FALSE(test_pathA.empty());
231 ASSERT_FALSE(test_pathB.empty());
232
233 // Attach two devices.
234 // kDevice1 -> kMountPointA
235 // kDevice2 -> kMountPointB
236 struct MtabTestData test_data1[] = {
237 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS),
238 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS),
239 };
240 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2);
241 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0);
242 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false);
243
244 // Attach |kDevice1| to |kMountPointB|.
245 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been
246 // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA.
247 // kDevice1 -> kMountPointA
248 // kDevice2 -> kMountPointB
249 // kDevice1 -> kMountPointB
250 struct MtabTestData test_data2[] = {
251 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS),
252 };
253 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1);
254 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2);
255 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false);
256
257 // Attach |kDevice2| to |kMountPointA|.
258 // kDevice1 -> kMountPointA
259 // kDevice2 -> kMountPointB
260 // kDevice1 -> kMountPointB
261 // kDevice2 -> kMountPointA
262 struct MtabTestData test_data3[] = {
263 MtabTestData(kDevice2, test_pathA.value().c_str(), kValidFS),
264 };
265 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1);
266 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0);
267 WriteToMtabAndRunLoop(test_data3, arraysize(test_data3), false);
268
269 // Detach |kDevice2| from |kMountPointA|.
270 // kDevice1 -> kMountPointA
271 // kDevice2 -> kMountPointB
272 // kDevice1 -> kMountPointB
273 struct MtabTestData test_data4[] = {
274 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS),
275 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS),
276 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS),
277 };
278 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
279 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1);
280 WriteToMtabAndRunLoop(test_data4, arraysize(test_data4), true);
281
282 // Detach |kDevice1| from |kMountPointB|.
283 // kDevice1 -> kMountPointA
284 // kDevice2 -> kMountPointB
285 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2);
286 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1);
287 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true);
288
289 // Detach all devices.
290 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
291 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2);
292 WriteToMtabAndRunLoop(NULL, 0, true);
293 }
294
295 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) {
296 testing::Sequence mock_sequence;
297 FilePath test_pathA = CreateMountPoint(kMountPointA, true);
298 FilePath test_pathB = CreateMountPoint(kMountPointB, true);
299 ASSERT_FALSE(test_pathA.empty());
300 ASSERT_FALSE(test_pathB.empty());
301
302 // |kDevice1| is most recently mounted at |kMountPointB|.
303 // kDevice1 -> kMountPointA
304 // kDevice2 -> kMountPointB
305 // kDevice1 -> kMountPointB
306 struct MtabTestData test_data1[] = {
307 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS),
308 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS),
309 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS),
310 };
311 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathB))
312 .Times(1);
313 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0);
314 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true);
315
316 // Attach |kDevice3| to |kMountPointB|.
317 // |kDevice1| is inaccessible at its most recent mount point, so it is
318 // detached and unavailable, even though it is still accessible via
319 // |kMountPointA|.
320 // kDevice1 -> kMountPointA
321 // kDevice2 -> kMountPointB
322 // kDevice1 -> kMountPointB
323 // kDevice3 -> kMountPointB
324 struct MtabTestData test_data2[] = {
325 MtabTestData(kDevice3, test_pathB.value().c_str(), kValidFS),
326 };
327 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).Times(1);
328 EXPECT_CALL(observer(), OnMediaDeviceAttached(1, kDevice3, test_pathB))
329 .Times(1);
330 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false);
331
332 // Detach all devices.
333 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
334 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1);
335 WriteToMtabAndRunLoop(NULL, 0, true);
336 }
337
338 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698