OLD | NEW |
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 // RemovableDeviceNotificationsLinux unit tests. | 5 // RemovableDeviceNotificationsLinux unit tests. |
6 | 6 |
7 #include "chrome/browser/system_monitor/removable_device_notifications_linux.h" | 7 #include "chrome/browser/system_monitor/removable_device_notifications_linux.h" |
8 | 8 |
9 #include <mntent.h> | 9 #include <mntent.h> |
10 #include <stdio.h> | 10 #include <stdio.h> |
11 | 11 |
12 #include <string> | 12 #include <string> |
13 | 13 |
14 #include "base/file_util.h" | 14 #include "base/file_util.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
17 #include "base/message_loop.h" | 17 #include "base/message_loop.h" |
18 #include "base/scoped_temp_dir.h" | 18 #include "base/scoped_temp_dir.h" |
19 #include "base/system_monitor/system_monitor.h" | 19 #include "base/system_monitor/system_monitor.h" |
20 #include "base/test/mock_devices_changed_observer.h" | 20 #include "base/test/mock_devices_changed_observer.h" |
21 #include "base/utf_string_conversions.h" | 21 #include "base/utf_string_conversions.h" |
22 #include "chrome/browser/system_monitor/media_storage_util.h" | 22 #include "chrome/browser/system_monitor/media_storage_util.h" |
23 #include "content/public/test/test_browser_thread.h" | 23 #include "content/public/test/test_browser_thread.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 #include "ui/base/text/bytes_formatting.h" |
25 | 26 |
26 namespace chrome { | 27 namespace chrome { |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 using testing::_; | 31 using testing::_; |
31 | 32 |
32 const char kValidFS[] = "vfat"; | 33 const char kValidFS[] = "vfat"; |
33 const char kInvalidFS[] = "invalidfs"; | 34 const char kInvalidFS[] = "invalidfs"; |
34 | 35 |
(...skipping 11 matching lines...) Expand all Loading... |
46 const char kMountPointB[] = "mnt_b"; | 47 const char kMountPointB[] = "mnt_b"; |
47 const char kMountPointC[] = "mnt_c"; | 48 const char kMountPointC[] = "mnt_c"; |
48 | 49 |
49 const char kDCIM[] = "DCIM"; | 50 const char kDCIM[] = "DCIM"; |
50 | 51 |
51 struct TestDeviceData { | 52 struct TestDeviceData { |
52 const char* device_path; | 53 const char* device_path; |
53 const char* unique_id; | 54 const char* unique_id; |
54 const char* device_name; | 55 const char* device_name; |
55 MediaStorageUtil::Type type; | 56 MediaStorageUtil::Type type; |
| 57 uint64 device_storage_size_in_bytes; |
56 }; | 58 }; |
57 | 59 |
58 const TestDeviceData kTestDeviceData[] = { | 60 const TestDeviceData kTestDeviceData[] = { |
59 {kDeviceDCIM1, "UUID:FFF0-000F", "TEST_USB_MODEL_1", | 61 {kDeviceDCIM1, "UUID:FFF0-000F", "TEST_USB_MODEL_1", |
60 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM}, | 62 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, 88997788}, |
61 {kDeviceDCIM2, "VendorModelSerial:ComName:Model2010:8989", "TEST_USB_MODEL_2", | 63 {kDeviceDCIM2, "VendorModelSerial:ComName:Model2010:8989", "TEST_USB_MODEL_2", |
62 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM}, | 64 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, 8773}, |
63 {kDeviceDCIM3, "VendorModelSerial:::WEM319X792", "TEST_USB_MODEL_3", | 65 {kDeviceDCIM3, "VendorModelSerial:::WEM319X792", "TEST_USB_MODEL_3", |
64 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM}, | 66 MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, 22377892837}, |
65 {kDeviceNoDCIM, "UUID:ABCD-1234", "TEST_USB_MODEL_4", | 67 {kDeviceNoDCIM, "UUID:ABCD-1234", "TEST_USB_MODEL_4", |
66 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM}, | 68 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, 512}, |
67 {kDeviceFixed, "UUID:743A91FD2349", "TEST_USB_MODEL_5", | 69 {kDeviceFixed, "UUID:743A-2349", "743A-2349", |
68 MediaStorageUtil::FIXED_MASS_STORAGE}, | 70 MediaStorageUtil::FIXED_MASS_STORAGE, 172892}, |
69 }; | 71 }; |
70 | 72 |
71 void GetDeviceInfo(const FilePath& device_path, std::string* id, | 73 void GetDeviceInfo(const FilePath& device_path, |
72 string16* name, bool* removable) { | 74 std::string* id, |
| 75 string16* name, |
| 76 bool* removable, |
| 77 string16* device_size) { |
73 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { | 78 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { |
74 if (device_path.value() == kTestDeviceData[i].device_path) { | 79 if (device_path.value() == kTestDeviceData[i].device_path) { |
75 if (id) | 80 if (id) |
76 *id = kTestDeviceData[i].unique_id; | 81 *id = kTestDeviceData[i].unique_id; |
77 if (name) | 82 if (name) |
78 *name = ASCIIToUTF16(kTestDeviceData[i].device_name); | 83 *name = ASCIIToUTF16(kTestDeviceData[i].device_name); |
79 if (removable) { | 84 if (removable) { |
80 MediaStorageUtil::Type type = kTestDeviceData[i].type; | 85 MediaStorageUtil::Type type = kTestDeviceData[i].type; |
81 *removable = | 86 *removable = |
82 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM) || | 87 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM) || |
83 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM); | 88 (type == MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM); |
84 } | 89 } |
| 90 if (device_size) { |
| 91 *device_size = ui::FormatBytes( |
| 92 kTestDeviceData[i].device_storage_size_in_bytes); |
| 93 } |
85 return; | 94 return; |
86 } | 95 } |
87 } | 96 } |
88 NOTREACHED(); | 97 NOTREACHED(); |
89 } | 98 } |
90 | 99 |
91 std::string GetDeviceId(const std::string& device) { | 100 std::string GetDeviceId(const std::string& device) { |
92 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { | 101 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { |
93 if (device == kTestDeviceData[i].device_path) { | 102 if (device == kTestDeviceData[i].device_path) { |
94 return MediaStorageUtil::MakeDeviceId(kTestDeviceData[i].type, | 103 return MediaStorageUtil::MakeDeviceId(kTestDeviceData[i].type, |
95 kTestDeviceData[i].unique_id); | 104 kTestDeviceData[i].unique_id); |
96 } | 105 } |
97 } | 106 } |
98 if (device == kInvalidDevice) { | 107 if (device == kInvalidDevice) { |
99 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::FIXED_MASS_STORAGE, | 108 return MediaStorageUtil::MakeDeviceId(MediaStorageUtil::FIXED_MASS_STORAGE, |
100 kInvalidDevice); | 109 kInvalidDevice); |
101 } | 110 } |
102 return std::string(); | 111 return std::string(); |
103 } | 112 } |
| 113 |
| 114 string16 GetDeviceNameWithSizeDetails(const std::string& device) { |
| 115 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { |
| 116 if (device == kTestDeviceData[i].device_path) { |
| 117 return ui::FormatBytes(kTestDeviceData[i].device_storage_size_in_bytes) + |
| 118 ASCIIToUTF16(std::string(" ") + kTestDeviceData[i].device_name); |
| 119 } |
| 120 } |
| 121 return string16(); |
| 122 } |
| 123 |
104 string16 GetDeviceName(const std::string& device) { | 124 string16 GetDeviceName(const std::string& device) { |
105 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { | 125 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { |
106 if (device == kTestDeviceData[i].device_path) | 126 if (device == kTestDeviceData[i].device_path) |
107 return ASCIIToUTF16(kTestDeviceData[i].device_name); | 127 return ASCIIToUTF16(kTestDeviceData[i].device_name); |
108 } | 128 } |
109 return string16(); | 129 return string16(); |
110 } | 130 } |
111 | 131 |
| 132 string16 GetDeviceStorageSize(const std::string& device) { |
| 133 for (size_t i = 0; i < arraysize(kTestDeviceData); i++) { |
| 134 if (device == kTestDeviceData[i].device_path) |
| 135 return ui::FormatBytes(kTestDeviceData[i].device_storage_size_in_bytes); |
| 136 } |
| 137 return string16(); |
| 138 } |
| 139 |
112 class RemovableDeviceNotificationsLinuxTestWrapper | 140 class RemovableDeviceNotificationsLinuxTestWrapper |
113 : public RemovableDeviceNotificationsLinux { | 141 : public RemovableDeviceNotificationsLinux { |
114 public: | 142 public: |
115 RemovableDeviceNotificationsLinuxTestWrapper(const FilePath& path, | 143 RemovableDeviceNotificationsLinuxTestWrapper(const FilePath& path, |
116 MessageLoop* message_loop) | 144 MessageLoop* message_loop) |
117 : RemovableDeviceNotificationsLinux(path, &GetDeviceInfo), | 145 : RemovableDeviceNotificationsLinux(path, &GetDeviceInfo), |
118 message_loop_(message_loop) { | 146 message_loop_(message_loop) { |
119 } | 147 } |
120 | 148 |
121 private: | 149 private: |
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 testing::Sequence mock_sequence; | 337 testing::Sequence mock_sequence; |
310 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); | 338 FilePath test_path = CreateMountPointWithDCIMDir(kMountPointA); |
311 ASSERT_FALSE(test_path.empty()); | 339 ASSERT_FALSE(test_path.empty()); |
312 MtabTestData test_data[] = { | 340 MtabTestData test_data[] = { |
313 MtabTestData(kDeviceDCIM2, test_path.value(), kValidFS), | 341 MtabTestData(kDeviceDCIM2, test_path.value(), kValidFS), |
314 MtabTestData(kDeviceFixed, kInvalidPath, kValidFS), | 342 MtabTestData(kDeviceFixed, kInvalidPath, kValidFS), |
315 }; | 343 }; |
316 // Only |kDeviceDCIM2| should be attached, since |kDeviceFixed| has a bad | 344 // Only |kDeviceDCIM2| should be attached, since |kDeviceFixed| has a bad |
317 // path. | 345 // path. |
318 EXPECT_CALL(observer(), | 346 EXPECT_CALL(observer(), |
319 OnRemovableStorageAttached(GetDeviceId(kDeviceDCIM2), | 347 OnRemovableStorageAttached( |
320 GetDeviceName(kDeviceDCIM2), | 348 GetDeviceId(kDeviceDCIM2), |
321 test_path.value())) | 349 GetDeviceNameWithSizeDetails(kDeviceDCIM2), |
| 350 test_path.value())) |
322 .InSequence(mock_sequence); | 351 .InSequence(mock_sequence); |
323 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); | 352 AppendToMtabAndRunLoop(test_data, arraysize(test_data)); |
324 | 353 |
325 // |kDeviceDCIM2| should be detached here. | 354 // |kDeviceDCIM2| should be detached here. |
326 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDeviceDCIM2))) | 355 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDeviceDCIM2))) |
327 .InSequence(mock_sequence); | 356 .InSequence(mock_sequence); |
328 WriteEmptyMtabAndRunLoop(); | 357 WriteEmptyMtabAndRunLoop(); |
329 } | 358 } |
330 | 359 |
331 // Only removable devices are recognized. | 360 // Only removable devices are recognized. |
332 TEST_F(RemovableDeviceNotificationLinuxTest, Removable) { | 361 TEST_F(RemovableDeviceNotificationLinuxTest, Removable) { |
333 testing::Sequence mock_sequence; | 362 testing::Sequence mock_sequence; |
334 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); | 363 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
335 ASSERT_FALSE(test_path_a.empty()); | 364 ASSERT_FALSE(test_path_a.empty()); |
336 MtabTestData test_data1[] = { | 365 MtabTestData test_data1[] = { |
337 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS), | 366 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS), |
338 }; | 367 }; |
339 // |kDeviceDCIM1| should be attached as expected. | 368 // |kDeviceDCIM1| should be attached as expected. |
340 EXPECT_CALL(observer(), | 369 EXPECT_CALL(observer(), |
341 OnRemovableStorageAttached(GetDeviceId(kDeviceDCIM1), | 370 OnRemovableStorageAttached( |
342 GetDeviceName(kDeviceDCIM1), | 371 GetDeviceId(kDeviceDCIM1), |
343 test_path_a.value())) | 372 GetDeviceNameWithSizeDetails(kDeviceDCIM1), |
| 373 test_path_a.value())) |
344 .InSequence(mock_sequence); | 374 .InSequence(mock_sequence); |
345 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); | 375 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
346 | 376 |
347 // This should do nothing, since |kDeviceFixed| is not removable. | 377 // This should do nothing, since |kDeviceFixed| is not removable. |
348 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); | 378 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); |
349 ASSERT_FALSE(test_path_b.empty()); | 379 ASSERT_FALSE(test_path_b.empty()); |
350 MtabTestData test_data2[] = { | 380 MtabTestData test_data2[] = { |
351 MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS), | 381 MtabTestData(kDeviceFixed, test_path_b.value(), kValidFS), |
352 }; | 382 }; |
353 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); | 383 AppendToMtabAndRunLoop(test_data2, arraysize(test_data2)); |
354 | 384 |
355 // |kDeviceDCIM1| should be detached as expected. | 385 // |kDeviceDCIM1| should be detached as expected. |
356 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDeviceDCIM1))) | 386 EXPECT_CALL(observer(), OnRemovableStorageDetached(GetDeviceId(kDeviceDCIM1))) |
357 .InSequence(mock_sequence); | 387 .InSequence(mock_sequence); |
358 WriteEmptyMtabAndRunLoop(); | 388 WriteEmptyMtabAndRunLoop(); |
359 | 389 |
360 // |kDeviceNoDCIM| should be attached as expected. | 390 // |kDeviceNoDCIM| should be attached as expected. |
361 EXPECT_CALL(observer(), | 391 EXPECT_CALL(observer(), |
362 OnRemovableStorageAttached(GetDeviceId(kDeviceNoDCIM), | 392 OnRemovableStorageAttached( |
363 GetDeviceName(kDeviceNoDCIM), | 393 GetDeviceId(kDeviceNoDCIM), |
364 test_path_b.value())) | 394 GetDeviceNameWithSizeDetails(kDeviceNoDCIM), |
| 395 test_path_b.value())) |
365 .InSequence(mock_sequence); | 396 .InSequence(mock_sequence); |
366 MtabTestData test_data3[] = { | 397 MtabTestData test_data3[] = { |
367 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS), | 398 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS), |
368 }; | 399 }; |
369 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3)); | 400 AppendToMtabAndRunLoop(test_data3, arraysize(test_data3)); |
370 | 401 |
371 // |kDeviceNoDCIM| should be detached as expected. | 402 // |kDeviceNoDCIM| should be detached as expected. |
372 EXPECT_CALL(observer(), | 403 EXPECT_CALL(observer(), |
373 OnRemovableStorageDetached(GetDeviceId(kDeviceNoDCIM))) | 404 OnRemovableStorageDetached(GetDeviceId(kDeviceNoDCIM))) |
374 .InSequence(mock_sequence); | 405 .InSequence(mock_sequence); |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
627 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_a, &device_info)); | 658 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_a, &device_info)); |
628 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id); | 659 EXPECT_EQ(GetDeviceId(kDeviceDCIM1), device_info.device_id); |
629 | 660 |
630 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_b, &device_info)); | 661 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_b, &device_info)); |
631 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id); | 662 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id); |
632 | 663 |
633 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_c, &device_info)); | 664 EXPECT_TRUE(notifier()->GetDeviceInfoForPath(test_path_c, &device_info)); |
634 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id); | 665 EXPECT_EQ(GetDeviceId(kDeviceFixed), device_info.device_id); |
635 } | 666 } |
636 | 667 |
| 668 TEST_F(RemovableDeviceNotificationLinuxTest, DeviceStorageSizeInfo) { |
| 669 FilePath test_path_a = CreateMountPointWithDCIMDir(kMountPointA); |
| 670 FilePath test_path_b = CreateMountPointWithoutDCIMDir(kMountPointB); |
| 671 ASSERT_FALSE(test_path_a.empty()); |
| 672 ASSERT_FALSE(test_path_b.empty()); |
| 673 |
| 674 MtabTestData test_data1[] = { |
| 675 MtabTestData(kDeviceDCIM1, test_path_a.value(), kValidFS), |
| 676 MtabTestData(kDeviceNoDCIM, test_path_b.value(), kValidFS), |
| 677 MtabTestData(kDeviceFixed, kInvalidPath, kInvalidFS), |
| 678 }; |
| 679 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(2); |
| 680 EXPECT_CALL(observer(), OnRemovableStorageDetached(_)).Times(0); |
| 681 AppendToMtabAndRunLoop(test_data1, arraysize(test_data1)); |
| 682 |
| 683 EXPECT_EQ(GetDeviceStorageSize(kDeviceDCIM1), |
| 684 notifier()->GetStorageSize(test_path_a.value())); |
| 685 EXPECT_EQ(GetDeviceStorageSize(kDeviceNoDCIM), |
| 686 notifier()->GetStorageSize(test_path_b.value())); |
| 687 EXPECT_EQ(string16(), |
| 688 notifier()->GetStorageSize(kInvalidPath)); |
| 689 } |
| 690 |
637 } // namespace | 691 } // namespace |
638 | 692 |
639 } // namespace chrome | 693 } // namespace chrome |
OLD | NEW |