| 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 // chromeos::RemovableDeviceNotificationsCros unit tests. | 5 // chromeos::RemovableDeviceNotificationsCros unit tests. |
| 6 | 6 |
| 7 #include "chrome/browser/system_monitor/removable_device_notifications_chromeos.
h" | 7 #include "chrome/browser/system_monitor/removable_device_notifications_chromeos.
h" |
| 8 | 8 |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
| 13 #include "base/scoped_temp_dir.h" | 13 #include "base/scoped_temp_dir.h" |
| 14 #include "base/system_monitor/system_monitor.h" | 14 #include "base/system_monitor/system_monitor.h" |
| 15 #include "base/test/mock_devices_changed_observer.h" | 15 #include "base/test/mock_devices_changed_observer.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "chrome/browser/system_monitor/media_storage_util.h" | 17 #include "chrome/browser/system_monitor/media_storage_util.h" |
| 18 #include "chrome/browser/system_monitor/removable_device_constants.h" |
| 18 #include "chromeos/disks/mock_disk_mount_manager.h" | 19 #include "chromeos/disks/mock_disk_mount_manager.h" |
| 19 #include "content/public/test/test_browser_thread.h" | 20 #include "content/public/test/test_browser_thread.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 22 |
| 22 namespace chromeos { | 23 namespace chromeos { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 using content::BrowserThread; | 27 using content::BrowserThread; |
| 27 using disks::DiskMountManager; | 28 using disks::DiskMountManager; |
| 28 using testing::_; | 29 using testing::_; |
| 29 | 30 |
| 30 const char kDevice1[] = "/dev/d1"; | 31 const char kDevice1[] = "/dev/d1"; |
| 31 const char kDevice2[] = "/dev/disk/d2"; | 32 const char kDevice2[] = "/dev/disk/d2"; |
| 32 const char kDevice1Name[] = "d1"; | 33 const char kDevice1Name[] = "d1"; |
| 33 const char kDevice2Name[] = "d2"; | 34 const char kDevice2Name[] = "d2"; |
| 34 const char kMountPointA[] = "mnt_a"; | 35 const char kMountPointA[] = "mnt_a"; |
| 35 const char kMountPointB[] = "mnt_b"; | 36 const char kMountPointB[] = "mnt_b"; |
| 36 | 37 |
| 37 std::string GetDCIMDeviceId(const std::string& unique_id) { | 38 std::string GetDCIMDeviceId(const std::string& unique_id) { |
| 38 return chrome::MediaStorageUtil::MakeDeviceId( | 39 return chrome::MediaStorageUtil::MakeDeviceId( |
| 39 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id); | 40 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, |
| 41 chrome::kFSUniqueIdPrefix + unique_id); |
| 40 } | 42 } |
| 41 | 43 |
| 42 class RemovableDeviceNotificationsCrosTest : public testing::Test { | 44 class RemovableDeviceNotificationsCrosTest : public testing::Test { |
| 43 public: | 45 public: |
| 44 RemovableDeviceNotificationsCrosTest() | 46 RemovableDeviceNotificationsCrosTest() |
| 45 : ui_thread_(BrowserThread::UI, &ui_loop_), | 47 : ui_thread_(BrowserThread::UI, &ui_loop_), |
| 46 file_thread_(BrowserThread::FILE) { | 48 file_thread_(BrowserThread::FILE) { |
| 47 } | 49 } |
| 48 virtual ~RemovableDeviceNotificationsCrosTest() {} | 50 virtual ~RemovableDeviceNotificationsCrosTest() {} |
| 49 | 51 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 73 mock_devices_changed_observer_.get()); | 75 mock_devices_changed_observer_.get()); |
| 74 WaitForFileThread(); | 76 WaitForFileThread(); |
| 75 } | 77 } |
| 76 | 78 |
| 77 base::MockDevicesChangedObserver& observer() { | 79 base::MockDevicesChangedObserver& observer() { |
| 78 return *mock_devices_changed_observer_; | 80 return *mock_devices_changed_observer_; |
| 79 } | 81 } |
| 80 | 82 |
| 81 void MountDevice(MountError error_code, | 83 void MountDevice(MountError error_code, |
| 82 const DiskMountManager::MountPointInfo& mount_info, | 84 const DiskMountManager::MountPointInfo& mount_info, |
| 83 const std::string& unique_id) { | 85 const std::string& unique_id, |
| 86 const std::string& device_label) { |
| 84 if (error_code == MOUNT_ERROR_NONE) { | 87 if (error_code == MOUNT_ERROR_NONE) { |
| 85 disk_mount_manager_mock_->CreateDiskEntryForMountDevice( | 88 disk_mount_manager_mock_->CreateDiskEntryForMountDevice( |
| 86 mount_info, unique_id); | 89 mount_info, unique_id, device_label); |
| 87 } | 90 } |
| 88 notifications_->MountCompleted(disks::DiskMountManager::MOUNTING, | 91 notifications_->MountCompleted(disks::DiskMountManager::MOUNTING, |
| 89 error_code, | 92 error_code, |
| 90 mount_info); | 93 mount_info); |
| 91 WaitForFileThread(); | 94 WaitForFileThread(); |
| 92 } | 95 } |
| 93 | 96 |
| 94 void UnmountDevice(MountError error_code, | 97 void UnmountDevice(MountError error_code, |
| 95 const DiskMountManager::MountPointInfo& mount_info) { | 98 const DiskMountManager::MountPointInfo& mount_info) { |
| 96 notifications_->MountCompleted(disks::DiskMountManager::UNMOUNTING, | 99 notifications_->MountCompleted(disks::DiskMountManager::UNMOUNTING, |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 DiskMountManager::MountPointInfo mount_info(kDevice1, | 163 DiskMountManager::MountPointInfo mount_info(kDevice1, |
| 161 mount_path1.value(), | 164 mount_path1.value(), |
| 162 MOUNT_TYPE_DEVICE, | 165 MOUNT_TYPE_DEVICE, |
| 163 disks::MOUNT_CONDITION_NONE); | 166 disks::MOUNT_CONDITION_NONE); |
| 164 const std::string kUniqueId0 = "FFFF-FFFF"; | 167 const std::string kUniqueId0 = "FFFF-FFFF"; |
| 165 EXPECT_CALL(observer(), | 168 EXPECT_CALL(observer(), |
| 166 OnRemovableStorageAttached(GetDCIMDeviceId(kUniqueId0), | 169 OnRemovableStorageAttached(GetDCIMDeviceId(kUniqueId0), |
| 167 ASCIIToUTF16(kDevice1Name), | 170 ASCIIToUTF16(kDevice1Name), |
| 168 mount_path1.value())) | 171 mount_path1.value())) |
| 169 .InSequence(mock_sequence); | 172 .InSequence(mock_sequence); |
| 170 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId0); | 173 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId0, kDevice1Name); |
| 171 | 174 |
| 172 EXPECT_CALL(observer(), | 175 EXPECT_CALL(observer(), |
| 173 OnRemovableStorageDetached(GetDCIMDeviceId(kUniqueId0))) | 176 OnRemovableStorageDetached(GetDCIMDeviceId(kUniqueId0))) |
| 174 .InSequence(mock_sequence); | 177 .InSequence(mock_sequence); |
| 175 UnmountDevice(MOUNT_ERROR_NONE, mount_info); | 178 UnmountDevice(MOUNT_ERROR_NONE, mount_info); |
| 176 | 179 |
| 177 FilePath mount_path2 = CreateMountPoint(kMountPointB, true); | 180 FilePath mount_path2 = CreateMountPoint(kMountPointB, true); |
| 178 ASSERT_FALSE(mount_path2.empty()); | 181 ASSERT_FALSE(mount_path2.empty()); |
| 179 DiskMountManager::MountPointInfo mount_info2(kDevice2, | 182 DiskMountManager::MountPointInfo mount_info2(kDevice2, |
| 180 mount_path2.value(), | 183 mount_path2.value(), |
| 181 MOUNT_TYPE_DEVICE, | 184 MOUNT_TYPE_DEVICE, |
| 182 disks::MOUNT_CONDITION_NONE); | 185 disks::MOUNT_CONDITION_NONE); |
| 183 const std::string kUniqueId1 = "FFF0-FFF0"; | 186 const std::string kUniqueId1 = "FFF0-FFF0"; |
| 184 | 187 |
| 185 EXPECT_CALL(observer(), | 188 EXPECT_CALL(observer(), |
| 186 OnRemovableStorageAttached(GetDCIMDeviceId(kUniqueId1), | 189 OnRemovableStorageAttached(GetDCIMDeviceId(kUniqueId1), |
| 187 ASCIIToUTF16(kDevice2Name), | 190 ASCIIToUTF16(kDevice2Name), |
| 188 mount_path2.value())) | 191 mount_path2.value())) |
| 189 .InSequence(mock_sequence); | 192 .InSequence(mock_sequence); |
| 190 MountDevice(MOUNT_ERROR_NONE, mount_info2, kUniqueId1); | 193 MountDevice(MOUNT_ERROR_NONE, mount_info2, kUniqueId1, kDevice2Name); |
| 191 | 194 |
| 192 EXPECT_CALL(observer(), | 195 EXPECT_CALL(observer(), |
| 193 OnRemovableStorageDetached(GetDCIMDeviceId(kUniqueId1))) | 196 OnRemovableStorageDetached(GetDCIMDeviceId(kUniqueId1))) |
| 194 .InSequence(mock_sequence); | 197 .InSequence(mock_sequence); |
| 195 UnmountDevice(MOUNT_ERROR_NONE, mount_info2); | 198 UnmountDevice(MOUNT_ERROR_NONE, mount_info2); |
| 196 } | 199 } |
| 197 | 200 |
| 198 // Removable mass storage devices with no dcim folder are also recognized. | 201 // Removable mass storage devices with no dcim folder are also recognized. |
| 199 TEST_F(RemovableDeviceNotificationsCrosTest, NoDCIM) { | 202 TEST_F(RemovableDeviceNotificationsCrosTest, NoDCIM) { |
| 200 testing::Sequence mock_sequence; | 203 testing::Sequence mock_sequence; |
| 201 FilePath mount_path = CreateMountPoint(kMountPointA, false); | 204 FilePath mount_path = CreateMountPoint(kMountPointA, false); |
| 202 const std::string kUniqueId = "FFFF-FFFF"; | 205 const std::string kUniqueId = "FFFF-FFFF"; |
| 203 ASSERT_FALSE(mount_path.empty()); | 206 ASSERT_FALSE(mount_path.empty()); |
| 204 DiskMountManager::MountPointInfo mount_info(kDevice1, | 207 DiskMountManager::MountPointInfo mount_info(kDevice1, |
| 205 mount_path.value(), | 208 mount_path.value(), |
| 206 MOUNT_TYPE_DEVICE, | 209 MOUNT_TYPE_DEVICE, |
| 207 disks::MOUNT_CONDITION_NONE); | 210 disks::MOUNT_CONDITION_NONE); |
| 208 const std::string device_id = chrome::MediaStorageUtil::MakeDeviceId( | 211 const std::string device_id = chrome::MediaStorageUtil::MakeDeviceId( |
| 209 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, kUniqueId); | 212 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM, |
| 213 chrome::kFSUniqueIdPrefix + kUniqueId); |
| 210 EXPECT_CALL(observer(), | 214 EXPECT_CALL(observer(), |
| 211 OnRemovableStorageAttached(device_id, ASCIIToUTF16(kDevice1Name), | 215 OnRemovableStorageAttached(device_id, ASCIIToUTF16(kDevice1Name), |
| 212 mount_path.value())).Times(1); | 216 mount_path.value())).Times(1); |
| 213 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId); | 217 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId, kDevice1Name); |
| 214 } | 218 } |
| 215 | 219 |
| 216 // Non device mounts and mount errors are ignored. | 220 // Non device mounts and mount errors are ignored. |
| 217 TEST_F(RemovableDeviceNotificationsCrosTest, Ignore) { | 221 TEST_F(RemovableDeviceNotificationsCrosTest, Ignore) { |
| 218 testing::Sequence mock_sequence; | 222 testing::Sequence mock_sequence; |
| 219 FilePath mount_path = CreateMountPoint(kMountPointA, true); | 223 FilePath mount_path = CreateMountPoint(kMountPointA, true); |
| 220 const std::string kUniqueId = "FFFF-FFFF"; | 224 const std::string kUniqueId = "FFFF-FFFF"; |
| 221 ASSERT_FALSE(mount_path.empty()); | 225 ASSERT_FALSE(mount_path.empty()); |
| 222 | 226 |
| 223 // Mount error. | 227 // Mount error. |
| 224 DiskMountManager::MountPointInfo mount_info(kDevice1, | 228 DiskMountManager::MountPointInfo mount_info(kDevice1, |
| 225 mount_path.value(), | 229 mount_path.value(), |
| 226 MOUNT_TYPE_DEVICE, | 230 MOUNT_TYPE_DEVICE, |
| 227 disks::MOUNT_CONDITION_NONE); | 231 disks::MOUNT_CONDITION_NONE); |
| 228 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); | 232 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); |
| 229 MountDevice(MOUNT_ERROR_UNKNOWN, mount_info, kUniqueId); | 233 MountDevice(MOUNT_ERROR_UNKNOWN, mount_info, kUniqueId, kDevice1Name); |
| 230 | 234 |
| 231 // Not a device | 235 // Not a device |
| 232 mount_info.mount_type = MOUNT_TYPE_ARCHIVE; | 236 mount_info.mount_type = MOUNT_TYPE_ARCHIVE; |
| 233 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); | 237 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); |
| 234 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId); | 238 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId, kDevice1Name); |
| 235 | 239 |
| 236 // Unsupported file system. | 240 // Unsupported file system. |
| 237 mount_info.mount_type = MOUNT_TYPE_DEVICE; | 241 mount_info.mount_type = MOUNT_TYPE_DEVICE; |
| 238 mount_info.mount_condition = disks::MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM; | 242 mount_info.mount_condition = disks::MOUNT_CONDITION_UNSUPPORTED_FILESYSTEM; |
| 239 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); | 243 EXPECT_CALL(observer(), OnRemovableStorageAttached(_, _, _)).Times(0); |
| 240 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId); | 244 MountDevice(MOUNT_ERROR_NONE, mount_info, kUniqueId, kDevice1Name); |
| 241 } | 245 } |
| 242 | 246 |
| 243 } // namespace | 247 } // namespace |
| 244 | 248 |
| 245 } // namespace chrome | 249 } // namespace chrome |
| OLD | NEW |