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

Side by Side Diff: chrome/browser/storage_monitor/removable_device_notifications_linux_unittest.cc

Issue 12382005: Rename RemovableDeviceNotifications=>StorageMonitor (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 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
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 // RemovableDeviceNotificationsLinux unit tests.
6
7 #include "chrome/browser/storage_monitor/removable_device_notifications_linux.h"
8
9 #include <mntent.h>
10 #include <stdio.h>
11
12 #include <string>
13
14 #include "base/file_util.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/message_loop.h"
19 #include "base/utf_string_conversions.h"
20 #include "chrome/browser/storage_monitor/media_device_notifications_utils.h"
21 #include "chrome/browser/storage_monitor/media_storage_util.h"
22 #include "chrome/browser/storage_monitor/mock_removable_storage_observer.h"
23 #include "chrome/browser/storage_monitor/removable_device_constants.h"
24 #include "chrome/browser/storage_monitor/storage_monitor.h"
25 #include "content/public/test/test_browser_thread.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 namespace chrome {
29
30 namespace {
31
32 const char kValidFS[] = "vfat";
33 const char kInvalidFS[] = "invalidfs";
34
35 const char kInvalidPath[] = "invalid path does not exist";
36
37 const char kDeviceDCIM1[] = "d1";
38 const char kDeviceDCIM2[] = "d2";
39 const char kDeviceDCIM3[] = "d3";
40 const char kDeviceNoDCIM[] = "d4";
41 const char kDeviceFixed[] = "d5";
42
43 const char kInvalidDevice[] = "invalid_device";
44
45 const char kMountPointA[] = "mnt_a";
46 const char kMountPointB[] = "mnt_b";
47 const char kMountPointC[] = "mnt_c";
48
49 struct TestDeviceData {
50 const char* device_path;
51 const char* unique_id;
52 const char* device_name;
53 MediaStorageUtil::Type type;
54 uint64 partition_size_in_bytes;
55 };
56
57 const TestDeviceData kTestDeviceData[] = {
58 { kDeviceDCIM1, "UUID:FFF0-000F", "TEST_USB_MODEL_1",
59 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, 88788 },
60 { kDeviceDCIM2, "VendorModelSerial:ComName:Model2010:8989",
61 "TEST_USB_MODEL_2", MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM,
62 8773 },
63 { kDeviceDCIM3, "VendorModelSerial:::WEM319X792", "TEST_USB_MODEL_3",
64 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, 22837 },
65 { kDeviceNoDCIM, "UUID:ABCD-1234", "TEST_USB_MODEL_4",
66 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, 512 },
67 { kDeviceFixed, "UUID:743A-2349", "743A-2349",
68 MediaStorageUtil::FIXED_MASS_STORAGE, 17282 },
69 };
70
71 void GetDeviceInfo(const base::FilePath& device_path,
72 std::string* id,
73 string16* name,
74 bool* removable,
75 uint64* partition_size_in_bytes) {
76 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) {
77 if (device_path.value() == kTestDeviceData[i].device_path) {
78 if (id)
79 *id = kTestDeviceData[i].unique_id;
80 if (name)
81 *name = ASCIIToUTF16(kTestDeviceData[i].device_name);
82 if (removable) {
83 MediaStorageUtil::Type type = kTestDeviceData[i].type;
84 *removable =
85 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM) ||
86 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM);
87 }
88 if (partition_size_in_bytes)
89 *partition_size_in_bytes = kTestDeviceData[i].partition_size_in_bytes;
90 return;
91 }
92 }
93 NOTREACHED();
94 }
95
96 uint64 GetDevicePartitionSize(const std::string& device) {
97 for (size_t i = 0; i < arraysize(kTestDeviceData); ++i) {
98 if (device == kTestDeviceData[i].device_path)
99 return kTestDeviceData[i].partition_size_in_bytes;
100 }
101 return 0;
102 }
103
104 std::string GetDeviceId(const std::string& device) {
105 for (size_t i = 0; i < arraysize(kTestDeviceData); ++i) {
106 if (device == kTestDeviceData[i].device_path) {
107 return MediaStorageUtil::MakeDeviceId(kTestDeviceData[i].type,
108 kTestDeviceData[i].unique_id);
109 }
110 }
111 if (device == kInvalidDevice) {
112 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::FIXED_MASS_STORAGE,
113 kInvalidDevice);
114 }
115 return std::string();
116 }
117
118 string16 GetDeviceNameWithSizeDetails(const std::string& device) {
119 for (size_t i = 0; i < arraysize(kTestDeviceData); ++i) {
120 if (device == kTestDeviceData[i].device_path) {
121 return GetDisplayNameForDevice(
122 kTestDeviceData[i].partition_size_in_bytes,
123 ASCIIToUTF16(kTestDeviceData[i].device_name));
124 }
125 }
126 return string16();
127 }
128
129 string16 GetDeviceName(const std::string& device) {
130 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) {
131 if (device == kTestDeviceData[i].device_path)
132 return ASCIIToUTF16(kTestDeviceData[i].device_name);
133 }
134 return string16();
135 }
136
137 class RemovableDeviceNotificationsLinuxTestWrapper
138 : public RemovableDeviceNotificationsLinux {
139 public:
140 RemovableDeviceNotificationsLinuxTestWrapper(const base::FilePath& path,
141 MessageLoop* message_loop)
142 : RemovableDeviceNotificationsLinux(path, &GetDeviceInfo),
143 message_loop_(message_loop) {
144 }
145
146 private:
147 // Avoids code deleting the object while there are references to it.
148 // Aside from the base::RefCountedThreadSafe friend class, any attempts to
149 // call this dtor will result in a compile-time error.
150 virtual ~RemovableDeviceNotificationsLinuxTestWrapper() {}
151
152 virtual void OnFilePathChanged(const base::FilePath& path,
153 bool error) OVERRIDE {
154 RemovableDeviceNotificationsLinux::OnFilePathChanged(path, error);
155 message_loop_->PostTask(FROM_HERE, MessageLoop::QuitClosure());
156 }
157
158 MessageLoop* message_loop_;
159
160 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationsLinuxTestWrapper);
161 };
162
163 class RemovableDeviceNotificationLinuxTest : public testing::Test {
164 public:
165 struct MtabTestData {
166 MtabTestData(const std::string& mount_device,
167 const std::string& mount_point,
168 const std::string& mount_type)
169 : mount_device(mount_device),
170 mount_point(mount_point),
171 mount_type(mount_type) {
172 }
173
174 const std::string mount_device;
175 const std::string mount_point;
176 const std::string mount_type;
177 };
178
179 RemovableDeviceNotificationLinuxTest()
180 : message_loop_(MessageLoop::TYPE_IO),
181 file_thread_(content::BrowserThread::FILE, &message_loop_) {
182 }
183 virtual ~RemovableDeviceNotificationLinuxTest() {}
184
185 protected:
186 virtual void SetUp() OVERRIDE {
187 // Create and set up a temp dir with files for the test.
188 ASSERT_TRUE(scoped_temp_dir_.CreateUniqueTempDir());
189 base::FilePath test_dir = scoped_temp_dir_.path().AppendASCII("test_etc");
190 ASSERT_TRUE(file_util::CreateDirectory(test_dir));
191 mtab_file_ = test_dir.AppendASCII("test_mtab");
192 MtabTestData initial_test_data[] = {
193 MtabTestData("dummydevice", "dummydir", kInvalidFS),
194 };
195 WriteToMtab(initial_test_data,
196 arraysize(initial_test_data),
197 true /* overwrite */);
198
199 // Initialize the test subject.
200 notifications_ = new RemovableDeviceNotificationsLinuxTestWrapper(
201 mtab_file_, &message_loop_);
202 mock_storage_observer_.reset(new MockRemovableStorageObserver);
203 notifications_->AddObserver(mock_storage_observer_.get());
204
205 notifications_->Init();
206 message_loop_.RunUntilIdle();
207 }
208
209 virtual void TearDown() OVERRIDE {
210 message_loop_.RunUntilIdle();
211 notifications_->RemoveObserver(mock_storage_observer_.get());
212 notifications_ = NULL;
213 }
214
215 // Append mtab entries from the |data| array of size |data_size| to the mtab
216 // file, and run the message loop.
217 void AppendToMtabAndRunLoop(const MtabTestData* data, size_t data_size) {
218 WriteToMtab(data, data_size, false /* do not overwrite */);
219 message_loop_.Run();
220 }
221
222 // Overwrite the mtab file with mtab entries from the |data| array of size
223 // |data_size|, and run the message loop.
224 void OverwriteMtabAndRunLoop(const MtabTestData* data, size_t data_size) {
225 WriteToMtab(data, data_size, true /* overwrite */);
226 message_loop_.Run();
227 }
228
229 // Simplied version of OverwriteMtabAndRunLoop() that just deletes all the
230 // entries in the mtab file.
231 void WriteEmptyMtabAndRunLoop() {
232 OverwriteMtabAndRunLoop(NULL, // No data.
233 0); // No data length.
234 }
235
236 // Create a directory named |dir| relative to the test directory.
237 // It has a DCIM directory, so RemovableDeviceNotificationsLinux recognizes it
238 // as a media directory.
239 base::FilePath CreateMountPointWithDCIMDir(const std::string& dir) {
240 return CreateMountPoint(dir, true /* create DCIM dir */);
241 }
242
243 // Create a directory named |dir| relative to the test directory.
244 // It does not have a DCIM directory, so RemovableDeviceNotificationsLinux
245 // does not recognizes it as a media directory.
246 base::FilePath CreateMountPointWithoutDCIMDir(const std::string& dir) {
247 return CreateMountPoint(dir, false /* do not create DCIM dir */);
248 }
249
250 void RemoveDCIMDirFromMountPoint(const std::string& dir) {
251 base::FilePath dcim =
252 scoped_temp_dir_.path().AppendASCII(dir).Append(kDCIMDirectoryName);
253 file_util::Delete(dcim, false);
254 }
255
256 MockRemovableStorageObserver& observer() {
257 return *mock_storage_observer_;
258 }
259
260 RemovableDeviceNotificationsLinux* notifier() {
261 return notifications_.get();
262 }
263
264 private:
265 // Create a directory named |dir| relative to the test directory.
266 // Set |with_dcim_dir| to true if the created directory will have a "DCIM"
267 // subdirectory.
268 // Returns the full path to the created directory on success, or an empty
269 // path on failure.
270 base::FilePath CreateMountPoint(const std::string& dir, bool with_dcim_dir) {
271 base::FilePath return_path(scoped_temp_dir_.path());
272 return_path = return_path.AppendASCII(dir);
273 base::FilePath path(return_path);
274 if (with_dcim_dir)
275 path = path.Append(kDCIMDirectoryName);
276 if (!file_util::CreateDirectory(path))
277 return base::FilePath();
278 return return_path;
279 }
280
281 // Write the test mtab data to |mtab_file_|.
282 // |data| is an array of mtab entries.
283 // |data_size| is the array size of |data|.
284 // |overwrite| specifies whether to overwrite |mtab_file_|.
285 void WriteToMtab(const MtabTestData* data,
286 size_t data_size,
287 bool overwrite) {
288 FILE* file = setmntent(mtab_file_.value().c_str(), overwrite ? "w" : "a");
289 ASSERT_TRUE(file);
290
291 // Due to the glibc *mntent() interface design, which is out of our
292 // control, the mtnent struct has several char* fields, even though
293 // addmntent() does not write to them in the calls below. To make the
294 // compiler happy while avoiding making additional copies of strings,
295 // we just const_cast() the strings' c_str()s.
296 // Assuming addmntent() does not write to the char* fields, this is safe.
297 // It is unlikely the platforms this test suite runs on will have an
298 // addmntent() implementation that does change the char* fields. If that
299 // was ever the case, the test suite will start crashing or failing.
300 mntent entry;
301 static const char kMountOpts[] = "rw";
302 entry.mnt_opts = const_cast<char*>(kMountOpts);
303 entry.mnt_freq = 0;
304 entry.mnt_passno = 0;
305 for (size_t i = 0; i < data_size; ++i) {
306 entry.mnt_fsname = const_cast<char*>(data[i].mount_device.c_str());
307 entry.mnt_dir = const_cast<char*>(data[i].mount_point.c_str());
308 entry.mnt_type = const_cast<char*>(data[i].mount_type.c_str());
309 ASSERT_EQ(0, addmntent(file, &entry));
310 }
311 ASSERT_EQ(1, endmntent(file));
312 }
313
314 // The message loop and file thread to run tests on.
315 MessageLoop message_loop_;
316 content::TestBrowserThread file_thread_;
317
318 scoped_ptr<MockRemovableStorageObserver> mock_storage_observer_;
319
320 // Temporary directory for created test data.
321 base::ScopedTempDir scoped_temp_dir_;
322 // Path to the test mtab file.
323 base::FilePath mtab_file_;
324
325 scoped_refptr<RemovableDeviceNotificationsLinuxTestWrapper> notifications_;
326
327 DISALLOW_COPY_AND_ASSIGN(RemovableDeviceNotificationLinuxTest);
328 };
329
330 // Simple test case where we attach and detach a media device.
331 TEST_F(RemovableDeviceNotificationLinuxTest, BasicAttachDetach) {
332 base::FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA);
333 ASSERT_FALSE(test_path.empty());
334 MtabTestData test_data[] = {
335 MtabTestData(kDeviceDCIM2, test_path.value(), kValidFS),
336 MtabTestData(kDeviceFixed, kInvalidPath, kValidFS),
337 };
338 // Only |kDeviceDCIM2| should be attached, since |kDeviceFixed| has a bad
339 // path.
340 AppendToMtabAndRunLoop(test_data, arraysize(test_data));
341
342 EXPECT_EQ(1, observer().attach_calls());
343 EXPECT_EQ(0, observer().detach_calls());
344 EXPECT_EQ(GetDeviceId(kDeviceDCIM2), observer().last_attached().device_id);
345 EXPECT_EQ(GetDeviceNameWithSizeDetails(kDeviceDCIM2),
346 observer().last_attached().name);
347 EXPECT_EQ(test_path.value(),
348 observer().last_attached().location);
349
350 // |kDeviceDCIM2| should be detached here.
351 WriteEmptyMtabAndRunLoop();
352 EXPECT_EQ(1, observer().attach_calls());
353 EXPECT_EQ(1, observer().detach_calls());
354 EXPECT_EQ(GetDeviceId(kDeviceDCIM2), observer().last_detached().device_id);
355 }
356
357 // Only removable devices are recognized.
358 TEST_F(RemovableDeviceNotificationLinuxTest, Removable) {
359 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
360 ASSERT_FALSE(test_path_a.empty());
361 MtabTestData test_data1[] = {
362 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
363 };
364 // |kDeviceDCIM1| should be attached as expected.
365 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
366
367 EXPECT_EQ(1, observer().attach_calls());
368 EXPECT_EQ(0, observer().detach_calls());
369 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), observer().last_attached().device_id);
370 EXPECT_EQ(GetDeviceNameWithSizeDetails(kDeviceDCIM1),
371 observer().last_attached().name);
372 EXPECT_EQ(test_path_a.value(),
373 observer().last_attached().location);
374
375 // This should do nothing, since |kDeviceFixed| is not removable.
376 base::FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
377 ASSERT_FALSE(test_path_b.empty());
378 MtabTestData test_data2[] = {
379 MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS),
380 };
381 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
382 EXPECT_EQ(1, observer().attach_calls());
383 EXPECT_EQ(0, observer().detach_calls());
384
385 // |kDeviceDCIM1| should be detached as expected.
386 WriteEmptyMtabAndRunLoop();
387 EXPECT_EQ(1, observer().attach_calls());
388 EXPECT_EQ(1, observer().detach_calls());
389 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), observer().last_detached().device_id);
390
391 // |kDeviceNoDCIM| should be attached as expected.
392 MtabTestData test_data3[] = {
393 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
394 };
395 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3));
396 EXPECT_EQ(2, observer().attach_calls());
397 EXPECT_EQ(1, observer().detach_calls());
398 EXPECT_EQ(GetDeviceId(kDeviceNoDCIM), observer().last_attached().device_id);
399 EXPECT_EQ(GetDeviceNameWithSizeDetails(kDeviceNoDCIM),
400 observer().last_attached().name);
401 EXPECT_EQ(test_path_b.value(),
402 observer().last_attached().location);
403
404 // |kDeviceNoDCIM| should be detached as expected.
405 WriteEmptyMtabAndRunLoop();
406 EXPECT_EQ(2, observer().attach_calls());
407 EXPECT_EQ(2, observer().detach_calls());
408 EXPECT_EQ(GetDeviceId(kDeviceNoDCIM), observer().last_detached().device_id);
409 }
410
411 // More complicated test case with multiple devices on multiple mount points.
412 TEST_F(RemovableDeviceNotificationLinuxTest, SwapMountPoints) {
413 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
414 base::FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
415 ASSERT_FALSE(test_path_a.empty());
416 ASSERT_FALSE(test_path_b.empty());
417
418 // Attach two devices.
419 // (*'d mounts are those RemovableStorageNotifications knows about.)
420 // kDeviceDCIM1 -> kMountPointA *
421 // kDeviceDCIM2 -> kMountPointB *
422 MtabTestData test_data1[] = {
423 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
424 MtabTestData(kDeviceDCIM2, test_path_b.value(), kValidFS),
425 };
426 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
427 EXPECT_EQ(2, observer().attach_calls());
428 EXPECT_EQ(0, observer().detach_calls());
429
430 // Detach two devices from old mount points and attach the devices at new
431 // mount points.
432 // kDeviceDCIM1 -> kMountPointB *
433 // kDeviceDCIM2 -> kMountPointA *
434 MtabTestData test_data2[] = {
435 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
436 MtabTestData(kDeviceDCIM2, test_path_a.value(), kValidFS),
437 };
438 OverwriteMtabAndRunLoop(test_data2, arraysize(test_data2));
439 EXPECT_EQ(4, observer().attach_calls());
440 EXPECT_EQ(2, observer().detach_calls());
441
442 // Detach all devices.
443 WriteEmptyMtabAndRunLoop();
444 EXPECT_EQ(4, observer().attach_calls());
445 EXPECT_EQ(4, observer().detach_calls());
446 }
447
448 // More complicated test case with multiple devices on multiple mount points.
449 TEST_F(RemovableDeviceNotificationLinuxTest, MultiDevicesMultiMountPoints) {
450 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
451 base::FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
452 ASSERT_FALSE(test_path_a.empty());
453 ASSERT_FALSE(test_path_b.empty());
454
455 // Attach two devices.
456 // (*'d mounts are those RemovableStorageNotifications knows about.)
457 // kDeviceDCIM1 -> kMountPointA *
458 // kDeviceDCIM2 -> kMountPointB *
459 MtabTestData test_data1[] = {
460 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
461 MtabTestData(kDeviceDCIM2, test_path_b.value(), kValidFS),
462 };
463 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
464 EXPECT_EQ(2, observer().attach_calls());
465 EXPECT_EQ(0, observer().detach_calls());
466
467 // Attach |kDeviceDCIM1| to |kMountPointB|.
468 // |kDeviceDCIM2| is inaccessible, so it is detached. |kDeviceDCIM1| has been
469 // attached at |kMountPointB|, but is still accessible from |kMountPointA|.
470 // kDeviceDCIM1 -> kMountPointA *
471 // kDeviceDCIM2 -> kMountPointB
472 // kDeviceDCIM1 -> kMountPointB
473 MtabTestData test_data2[] = {
474 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
475 };
476 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
477 EXPECT_EQ(2, observer().attach_calls());
478 EXPECT_EQ(1, observer().detach_calls());
479
480 // Detach |kDeviceDCIM1| from |kMountPointA|, causing a detach and attach
481 // event.
482 // kDeviceDCIM2 -> kMountPointB
483 // kDeviceDCIM1 -> kMountPointB *
484 MtabTestData test_data3[] = {
485 MtabTestData(kDeviceDCIM2, test_path_b.value(), kValidFS),
486 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
487 };
488 OverwriteMtabAndRunLoop(test_data3, arraysize(test_data3));
489 EXPECT_EQ(3, observer().attach_calls());
490 EXPECT_EQ(2, observer().detach_calls());
491
492 // Attach |kDeviceDCIM1| to |kMountPointA|.
493 // kDeviceDCIM2 -> kMountPointB
494 // kDeviceDCIM1 -> kMountPointB *
495 // kDeviceDCIM1 -> kMountPointA
496 MtabTestData test_data4[] = {
497 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
498 };
499 AppendToMtabAndRunLoop(test_data4, arraysize(test_data4));
500 EXPECT_EQ(3, observer().attach_calls());
501 EXPECT_EQ(2, observer().detach_calls());
502
503 // Detach |kDeviceDCIM1| from |kMountPointB|.
504 // kDeviceDCIM1 -> kMountPointA *
505 // kDeviceDCIM2 -> kMountPointB *
506 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1));
507 EXPECT_EQ(5, observer().attach_calls());
508 EXPECT_EQ(3, observer().detach_calls());
509
510 // Detach all devices.
511 WriteEmptyMtabAndRunLoop();
512 EXPECT_EQ(5, observer().attach_calls());
513 EXPECT_EQ(5, observer().detach_calls());
514 }
515
516 TEST_F(RemovableDeviceNotificationLinuxTest,
517 MultipleMountPointsWithNonDCIMDevices) {
518 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
519 base::FilePath test_path_b = CreateMountPointWithDCIMDir(kMountPointB);
520 ASSERT_FALSE(test_path_a.empty());
521 ASSERT_FALSE(test_path_b.empty());
522
523 // Attach to one first.
524 // (*'d mounts are those RemovableStorageNotifications knows about.)
525 // kDeviceDCIM1 -> kMountPointA *
526 MtabTestData test_data1[] = {
527 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
528 };
529 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
530 EXPECT_EQ(1, observer().attach_calls());
531 EXPECT_EQ(0, observer().detach_calls());
532
533 // Attach |kDeviceDCIM1| to |kMountPointB|.
534 // kDeviceDCIM1 -> kMountPointA *
535 // kDeviceDCIM1 -> kMountPointB
536 MtabTestData test_data2[] = {
537 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
538 };
539 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
540 EXPECT_EQ(1, observer().attach_calls());
541 EXPECT_EQ(0, observer().detach_calls());
542
543 // Attach |kDeviceFixed| (a non-removable device) to |kMountPointA|.
544 // kDeviceDCIM1 -> kMountPointA
545 // kDeviceDCIM1 -> kMountPointB *
546 // kDeviceFixed -> kMountPointA
547 MtabTestData test_data3[] = {
548 MtabTestData(kDeviceFixed, test_path_a.value(), kValidFS),
549 };
550 RemoveDCIMDirFromMountPoint(kMountPointA);
551 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3));
552 EXPECT_EQ(2, observer().attach_calls());
553 EXPECT_EQ(1, observer().detach_calls());
554
555 // Detach |kDeviceFixed|.
556 // kDeviceDCIM1 -> kMountPointA
557 // kDeviceDCIM1 -> kMountPointB *
558 MtabTestData test_data4[] = {
559 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
560 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
561 };
562 CreateMountPointWithDCIMDir(kMountPointA);
563 OverwriteMtabAndRunLoop(test_data4, arraysize(test_data4));
564 EXPECT_EQ(2, observer().attach_calls());
565 EXPECT_EQ(1, observer().detach_calls());
566
567 // Attach |kDeviceNoDCIM| (a non-DCIM device) to |kMountPointB|.
568 // kDeviceDCIM1 -> kMountPointA *
569 // kDeviceDCIM1 -> kMountPointB
570 // kDeviceNoDCIM -> kMountPointB *
571 MtabTestData test_data5[] = {
572 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
573 };
574 file_util::Delete(test_path_b.Append(kDCIMDirectoryName), false);
575 AppendToMtabAndRunLoop(test_data5, arraysize(test_data5));
576 EXPECT_EQ(4, observer().attach_calls());
577 EXPECT_EQ(2, observer().detach_calls());
578
579 // Detach |kDeviceNoDCIM|.
580 // kDeviceDCIM1 -> kMountPointA *
581 // kDeviceDCIM1 -> kMountPointB
582 MtabTestData test_data6[] = {
583 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
584 MtabTestData(kDeviceDCIM1, test_path_b.value(), kValidFS),
585 };
586 CreateMountPointWithDCIMDir(kMountPointB);
587 OverwriteMtabAndRunLoop(test_data6, arraysize(test_data6));
588 EXPECT_EQ(4, observer().attach_calls());
589 EXPECT_EQ(3, observer().detach_calls());
590
591 // Detach |kDeviceDCIM1| from |kMountPointB|.
592 // kDeviceDCIM1 -> kMountPointA *
593 OverwriteMtabAndRunLoop(test_data1, arraysize(test_data1));
594 EXPECT_EQ(4, observer().attach_calls());
595 EXPECT_EQ(3, observer().detach_calls());
596
597 // Detach all devices.
598 WriteEmptyMtabAndRunLoop();
599 EXPECT_EQ(4, observer().attach_calls());
600 EXPECT_EQ(4, observer().detach_calls());
601 }
602
603 TEST_F(RemovableDeviceNotificationLinuxTest, DeviceLookUp) {
604 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
605 base::FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
606 base::FilePath test_path_c = CreateMountPointWithoutDCIMDir(kMountPointC);
607 ASSERT_FALSE(test_path_a.empty());
608 ASSERT_FALSE(test_path_b.empty());
609 ASSERT_FALSE(test_path_c.empty());
610
611 // Attach to one first.
612 // (*'d mounts are those RemovableStorageNotifications knows about.)
613 // kDeviceDCIM1 -> kMountPointA *
614 // kDeviceNoDCIM -> kMountPointB *
615 // kDeviceFixed -> kMountPointC
616 MtabTestData test_data1[] = {
617 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
618 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
619 MtabTestData(kDeviceFixed, test_path_c.value(), kValidFS),
620 };
621 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
622 EXPECT_EQ(2, observer().attach_calls());
623 EXPECT_EQ(0, observer().detach_calls());
624
625 StorageMonitor::StorageInfo device_info;
626 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_a, &device_info));
627 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id);
628 EXPECT_EQ(test_path_a.value(), device_info.location);
629 EXPECT_EQ(GetDeviceName(kDeviceDCIM1), device_info.name);
630
631 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_b, &device_info));
632 EXPECT_EQ(GetDeviceId(kDeviceNoDCIM), device_info.device_id);
633 EXPECT_EQ(test_path_b.value(), device_info.location);
634 EXPECT_EQ(GetDeviceName(kDeviceNoDCIM), device_info.name);
635
636 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_c, &device_info));
637 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id);
638 EXPECT_EQ(test_path_c.value(), device_info.location);
639 EXPECT_EQ(GetDeviceName(kDeviceFixed), device_info.name);
640
641 // An invalid path.
642 EXPECT_FALSE(
643 notifier()->GetStorageInfoForPath(base::FilePath(kInvalidPath), NULL));
644
645 // Test filling in of the mount point.
646 EXPECT_TRUE(
647 notifier()->GetStorageInfoForPath(test_path_a.Append("some/other/path"),
648 &device_info));
649 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id);
650 EXPECT_EQ(test_path_a.value(), device_info.location);
651 EXPECT_EQ(GetDeviceName(kDeviceDCIM1), device_info.name);
652
653 // One device attached at multiple points.
654 // kDeviceDCIM1 -> kMountPointA *
655 // kDeviceFixed -> kMountPointB
656 // kDeviceFixed -> kMountPointC
657 MtabTestData test_data2[] = {
658 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
659 MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS),
660 MtabTestData(kDeviceFixed, test_path_c.value(), kValidFS),
661 };
662 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2));
663
664 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_a, &device_info));
665 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id);
666
667 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_b, &device_info));
668 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id);
669
670 EXPECT_TRUE(notifier()->GetStorageInfoForPath(test_path_c, &device_info));
671 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id);
672
673 EXPECT_EQ(2, observer().attach_calls());
674 EXPECT_EQ(1, observer().detach_calls());
675 }
676
677 TEST_F(RemovableDeviceNotificationLinuxTest, DevicePartitionSize) {
678 base::FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA);
679 base::FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB);
680 ASSERT_FALSE(test_path_a.empty());
681 ASSERT_FALSE(test_path_b.empty());
682
683 MtabTestData test_data1[] = {
684 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS),
685 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS),
686 MtabTestData(kDeviceFixed, kInvalidPath, kInvalidFS),
687 };
688 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1));
689 EXPECT_EQ(2, observer().attach_calls());
690 EXPECT_EQ(0, observer().detach_calls());
691
692 EXPECT_EQ(GetDevicePartitionSize(kDeviceDCIM1),
693 notifier()->GetStorageSize(test_path_a.value()));
694 EXPECT_EQ(GetDevicePartitionSize(kDeviceNoDCIM),
695 notifier()->GetStorageSize(test_path_b.value()));
696 EXPECT_EQ(GetDevicePartitionSize(kInvalidPath),
697 notifier()->GetStorageSize(kInvalidPath));
698 }
699
700 } // namespace
701
702 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698