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

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: call init method from ctor 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 message_loop_.RunAllPending();
86 }
87
88 virtual void TearDown() {
89 message_loop_.RunAllPending();
90 notifications_ = NULL;
91 system_monitor_->RemoveDevicesChangedObserver(
92 mock_devices_changed_observer_.get());
93 }
94
95 // Used to run tests. When the mtab file gets modified, the message loop
96 // needs to run in order to react to the file modification.
97 // See WriteToMtab for parameters.
98 void WriteToMtabAndRunLoop(struct MtabTestData* data,
99 size_t data_size,
100 bool overwrite) {
101 WriteToMtab(data, data_size, overwrite);
102 message_loop_.RunAllPending();
103 }
104
105 // Create a directory named |dir| relative to the test directory.
106 // Set |with_dcim_dir| to true if the created directory will have a "DCIM"
107 // subdirectory.
108 // Returns the full path to the created directory on success, or an empty
109 // path on failure.
110 FilePath CreateMountPoint(const char* dir, bool with_dcim_dir) {
111 FilePath return_path(scoped_temp_dir_.path());
112 return_path = return_path.AppendASCII(dir);
113 FilePath path(return_path);
114 if (with_dcim_dir)
115 path = path.AppendASCII("DCIM");
116 if (!file_util::CreateDirectory(path))
117 return FilePath();
118 return return_path;
119 }
120
121 base::MockDevicesChangedObserver& observer() {
122 return *mock_devices_changed_observer_.get();
123 }
124
125 private:
126 // Write the test mtab data to |mtab_file_|.
127 // |data| is an array of mtab entries.
128 // |data_size| is the array size of |data|.
129 // |overwrite| specifies whether to overwrite |mtab_file_|.
130 void WriteToMtab(struct MtabTestData* data,
131 size_t data_size,
132 bool overwrite) {
133 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a");
134 ASSERT_TRUE(file);
135
136 struct mntent entry;
137 entry.mnt_opts = strdup("rw");
138 entry.mnt_freq = 0;
139 entry.mnt_passno = 0;
140 for (size_t i = 0; i < data_size; ++i) {
141 entry.mnt_fsname = strdup(data[i].mount_device);
142 entry.mnt_dir = strdup(data[i].mount_point);
143 entry.mnt_type = strdup(data[i].mount_type);
144 int add_result = addmntent(file, &entry);
145 ASSERT_EQ(0, add_result);
146 free(entry.mnt_fsname);
147 free(entry.mnt_dir);
148 free(entry.mnt_type);
149 }
150 free(entry.mnt_opts);
151 int end_result = endmntent(file);
152 ASSERT_EQ(1, end_result);
153
154 // Need to ensure data reaches disk so the FilePathWatcher fires in time.
155 // Otherwise this will cause MediaDeviceNotificationsLinuxTest to be flaky.
156 int fd = open(mtab_file_.value().c_str(), O_RDONLY);
157 ASSERT_GE(fd, 0);
158
159 int fsync_result = fsync(fd);
160 ASSERT_EQ(0, fsync_result);
161
162 int close_result = close(fd);
163 ASSERT_EQ(0, close_result);
164 }
165
166 // The message loop and file thread to run tests on.
167 MessageLoop message_loop_;
168 BrowserThreadImpl file_thread_;
169
170 // SystemMonitor and DevicesChangedObserver to hook together to test.
171 scoped_ptr<base::SystemMonitor> system_monitor_;
172 scoped_ptr<base::MockDevicesChangedObserver> mock_devices_changed_observer_;
173
174 // Temporary directory for created test data.
175 ScopedTempDir scoped_temp_dir_;
176 // Path to the test mtab file.
177 FilePath mtab_file_;
178
179 scoped_refptr<MediaDeviceNotificationsLinux> notifications_;
180
181 DISALLOW_COPY_AND_ASSIGN(MediaDeviceNotificationsLinuxTest);
182 };
183
184 TEST_F(MediaDeviceNotificationsLinuxTest, BasicAttachDetach) {
185 testing::Sequence mock_sequence;
186 FilePath test_path = CreateMountPoint(kMountPointA, true);
187 ASSERT_FALSE(test_path.empty());
188 struct MtabTestData test_data[] = {
189 MtabTestData(kDevice1, kInvalidPath, kValidFS),
190 MtabTestData(kDevice2, test_path.value().c_str(), kValidFS),
191 };
192 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice2, test_path))
193 .InSequence(mock_sequence);
194 WriteToMtabAndRunLoop(test_data, arraysize(test_data), false);
195
196 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence);
197 WriteToMtabAndRunLoop(NULL, 0, true);
198 }
199
200 // Only mount points with DCIM directories are recognized.
201 TEST_F(MediaDeviceNotificationsLinuxTest, DCIM) {
202 testing::Sequence mock_sequence;
203 FilePath test_pathA = CreateMountPoint(kMountPointA, true);
204 ASSERT_FALSE(test_pathA.empty());
205 struct MtabTestData test_data1[] = {
206 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS),
207 };
208 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathA))
209 .InSequence(mock_sequence);
210 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false);
211
212 FilePath test_pathB = CreateMountPoint(kMountPointB, false);
213 ASSERT_FALSE(test_pathB.empty());
214 struct MtabTestData test_data2[] = {
215 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS),
216 };
217 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false);
218
219 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).InSequence(mock_sequence);
220 WriteToMtabAndRunLoop(NULL, 0, true);
221 }
222
223 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesMultiMountPoints) {
224 FilePath test_pathA = CreateMountPoint(kMountPointA, true);
225 FilePath test_pathB = CreateMountPoint(kMountPointB, true);
226 ASSERT_FALSE(test_pathA.empty());
227 ASSERT_FALSE(test_pathB.empty());
228
229 // Attach two devices.
230 // kDevice1 -> kMountPointA
231 // kDevice2 -> kMountPointB
232 struct MtabTestData test_data1[] = {
233 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS),
234 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS),
235 };
236 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2);
237 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0);
238 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), false);
239
240 // Attach |kDevice1| to |kMountPointB|.
241 // |kDevice2| is inaccessible, so it is detached. |kDevice1| has been
242 // re-attached at |kMountPointB|, so it is 'detached' from kMountPointA.
243 // kDevice1 -> kMountPointA
244 // kDevice2 -> kMountPointB
245 // kDevice1 -> kMountPointB
246 struct MtabTestData test_data2[] = {
247 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS),
248 };
249 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1);
250 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2);
251 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false);
252
253 // Attach |kDevice2| to |kMountPointA|.
254 // kDevice1 -> kMountPointA
255 // kDevice2 -> kMountPointB
256 // kDevice1 -> kMountPointB
257 // kDevice2 -> kMountPointA
258 struct MtabTestData test_data3[] = {
259 MtabTestData(kDevice2, test_pathA.value().c_str(), kValidFS),
260 };
261 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(1);
262 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0);
263 WriteToMtabAndRunLoop(test_data3, arraysize(test_data3), false);
264
265 // Detach |kDevice2| from |kMountPointA|.
266 // kDevice1 -> kMountPointA
267 // kDevice2 -> kMountPointB
268 // kDevice1 -> kMountPointB
269 struct MtabTestData test_data4[] = {
270 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS),
271 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS),
272 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS),
273 };
274 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
275 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1);
276 WriteToMtabAndRunLoop(test_data4, arraysize(test_data4), true);
277
278 // Detach |kDevice1| from |kMountPointB|.
279 // kDevice1 -> kMountPointA
280 // kDevice2 -> kMountPointB
281 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(2);
282 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(1);
283 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true);
284
285 // Detach all devices.
286 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
287 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(2);
288 WriteToMtabAndRunLoop(NULL, 0, true);
289 }
290
291 TEST_F(MediaDeviceNotificationsLinuxTest, MultiDevicesOneMountPoint) {
292 testing::Sequence mock_sequence;
293 FilePath test_pathA = CreateMountPoint(kMountPointA, true);
294 FilePath test_pathB = CreateMountPoint(kMountPointB, true);
295 ASSERT_FALSE(test_pathA.empty());
296 ASSERT_FALSE(test_pathB.empty());
297
298 // |kDevice1| is most recently mounted at |kMountPointB|.
299 // kDevice1 -> kMountPointA
300 // kDevice2 -> kMountPointB
301 // kDevice1 -> kMountPointB
302 struct MtabTestData test_data1[] = {
303 MtabTestData(kDevice1, test_pathA.value().c_str(), kValidFS),
304 MtabTestData(kDevice2, test_pathB.value().c_str(), kValidFS),
305 MtabTestData(kDevice1, test_pathB.value().c_str(), kValidFS),
306 };
307 EXPECT_CALL(observer(), OnMediaDeviceAttached(0, kDevice1, test_pathB))
308 .Times(1);
309 EXPECT_CALL(observer(), OnMediaDeviceDetached(_)).Times(0);
310 WriteToMtabAndRunLoop(test_data1, arraysize(test_data1), true);
311
312 // Attach |kDevice3| to |kMountPointB|.
313 // |kDevice1| is inaccessible at its most recent mount point, so it is
314 // detached and unavailable, even though it is still accessible via
315 // |kMountPointA|.
316 // kDevice1 -> kMountPointA
317 // kDevice2 -> kMountPointB
318 // kDevice1 -> kMountPointB
319 // kDevice3 -> kMountPointB
320 struct MtabTestData test_data2[] = {
321 MtabTestData(kDevice3, test_pathB.value().c_str(), kValidFS),
322 };
323 EXPECT_CALL(observer(), OnMediaDeviceDetached(0)).Times(1);
324 EXPECT_CALL(observer(), OnMediaDeviceAttached(1, kDevice3, test_pathB))
325 .Times(1);
326 WriteToMtabAndRunLoop(test_data2, arraysize(test_data2), false);
327
328 // Detach all devices.
329 EXPECT_CALL(observer(), OnMediaDeviceAttached(_, _, _)).Times(0);
330 EXPECT_CALL(observer(), OnMediaDeviceDetached(1)).Times(1);
331 WriteToMtabAndRunLoop(NULL, 0, true);
332 }
333
334 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698