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

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

Powered by Google App Engine
This is Rietveld 408576698