| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include <windows.h> | 5 #include <windows.h> |
| 6 #include <dbt.h> | 6 #include <dbt.h> |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 for (DeviceIndices::const_iterator it = device_indices.begin(); | 158 for (DeviceIndices::const_iterator it = device_indices.begin(); |
| 159 it != device_indices.end(); ++it) { | 159 it != device_indices.end(); ++it) { |
| 160 volume_broadcast.dbcv_unitmask |= 0x1 << *it; | 160 volume_broadcast.dbcv_unitmask |= 0x1 << *it; |
| 161 bool removable; | 161 bool removable; |
| 162 ASSERT_TRUE(volume_mount_watcher_->GetDeviceRemovable( | 162 ASSERT_TRUE(volume_mount_watcher_->GetDeviceRemovable( |
| 163 VolumeMountWatcherWin::DriveNumberToFilePath(*it), &removable)); | 163 VolumeMountWatcherWin::DriveNumberToFilePath(*it), &removable)); |
| 164 if (removable) | 164 if (removable) |
| 165 expect_attach_calls++; | 165 expect_attach_calls++; |
| 166 } | 166 } |
| 167 monitor_->InjectDeviceChange(DBT_DEVICEARRIVAL, | 167 monitor_->InjectDeviceChange(DBT_DEVICEARRIVAL, |
| 168 reinterpret_cast<DWORD>(&volume_broadcast)); | 168 reinterpret_cast<LPARAM>(&volume_broadcast)); |
| 169 | 169 |
| 170 RunUntilIdle(); | 170 RunUntilIdle(); |
| 171 volume_mount_watcher_->FlushWorkerPoolForTesting(); | 171 volume_mount_watcher_->FlushWorkerPoolForTesting(); |
| 172 RunUntilIdle(); | 172 RunUntilIdle(); |
| 173 | 173 |
| 174 EXPECT_EQ(expect_attach_calls, observer_.attach_calls()); | 174 EXPECT_EQ(expect_attach_calls, observer_.attach_calls()); |
| 175 EXPECT_EQ(0, observer_.detach_calls()); | 175 EXPECT_EQ(0, observer_.detach_calls()); |
| 176 } | 176 } |
| 177 | 177 |
| 178 void StorageMonitorWinTest::DoMassStorageDevicesDetachedTest( | 178 void StorageMonitorWinTest::DoMassStorageDevicesDetachedTest( |
| 179 const DeviceIndices& device_indices) { | 179 const DeviceIndices& device_indices) { |
| 180 DEV_BROADCAST_VOLUME volume_broadcast; | 180 DEV_BROADCAST_VOLUME volume_broadcast; |
| 181 volume_broadcast.dbcv_size = sizeof(volume_broadcast); | 181 volume_broadcast.dbcv_size = sizeof(volume_broadcast); |
| 182 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; | 182 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; |
| 183 volume_broadcast.dbcv_unitmask = 0x0; | 183 volume_broadcast.dbcv_unitmask = 0x0; |
| 184 volume_broadcast.dbcv_flags = 0x0; | 184 volume_broadcast.dbcv_flags = 0x0; |
| 185 | 185 |
| 186 int pre_attach_calls = observer_.attach_calls(); | 186 int pre_attach_calls = observer_.attach_calls(); |
| 187 int expect_detach_calls = 0; | 187 int expect_detach_calls = 0; |
| 188 for (DeviceIndices::const_iterator it = device_indices.begin(); | 188 for (DeviceIndices::const_iterator it = device_indices.begin(); |
| 189 it != device_indices.end(); ++it) { | 189 it != device_indices.end(); ++it) { |
| 190 volume_broadcast.dbcv_unitmask |= 0x1 << *it; | 190 volume_broadcast.dbcv_unitmask |= 0x1 << *it; |
| 191 StorageInfo info; | 191 StorageInfo info; |
| 192 ASSERT_TRUE(volume_mount_watcher_->GetDeviceInfo( | 192 ASSERT_TRUE(volume_mount_watcher_->GetDeviceInfo( |
| 193 VolumeMountWatcherWin::DriveNumberToFilePath(*it), &info)); | 193 VolumeMountWatcherWin::DriveNumberToFilePath(*it), &info)); |
| 194 if (StorageInfo::IsRemovableDevice(info.device_id())) | 194 if (StorageInfo::IsRemovableDevice(info.device_id())) |
| 195 ++expect_detach_calls; | 195 ++expect_detach_calls; |
| 196 } | 196 } |
| 197 monitor_->InjectDeviceChange(DBT_DEVICEREMOVECOMPLETE, | 197 monitor_->InjectDeviceChange(DBT_DEVICEREMOVECOMPLETE, |
| 198 reinterpret_cast<DWORD>(&volume_broadcast)); | 198 reinterpret_cast<LPARAM>(&volume_broadcast)); |
| 199 RunUntilIdle(); | 199 RunUntilIdle(); |
| 200 EXPECT_EQ(pre_attach_calls, observer_.attach_calls()); | 200 EXPECT_EQ(pre_attach_calls, observer_.attach_calls()); |
| 201 EXPECT_EQ(expect_detach_calls, observer_.detach_calls()); | 201 EXPECT_EQ(expect_detach_calls, observer_.detach_calls()); |
| 202 } | 202 } |
| 203 | 203 |
| 204 void StorageMonitorWinTest::DoMTPDeviceTest(const base::string16& pnp_device_id, | 204 void StorageMonitorWinTest::DoMTPDeviceTest(const base::string16& pnp_device_id, |
| 205 bool test_attach) { | 205 bool test_attach) { |
| 206 GUID guidDevInterface = GUID_NULL; | 206 GUID guidDevInterface = GUID_NULL; |
| 207 HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &guidDevInterface); | 207 HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &guidDevInterface); |
| 208 if (FAILED(hr)) | 208 if (FAILED(hr)) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 234 &location, &unique_id, | 234 &location, &unique_id, |
| 235 &name); | 235 &name); |
| 236 if (test_attach && !name.empty() && !unique_id.empty()) | 236 if (test_attach && !name.empty() && !unique_id.empty()) |
| 237 expect_attach_calls++; | 237 expect_attach_calls++; |
| 238 else if (!name.empty() && !unique_id.empty()) | 238 else if (!name.empty() && !unique_id.empty()) |
| 239 expect_detach_calls++; | 239 expect_detach_calls++; |
| 240 } | 240 } |
| 241 | 241 |
| 242 monitor_->InjectDeviceChange( | 242 monitor_->InjectDeviceChange( |
| 243 test_attach ? DBT_DEVICEARRIVAL : DBT_DEVICEREMOVECOMPLETE, | 243 test_attach ? DBT_DEVICEARRIVAL : DBT_DEVICEREMOVECOMPLETE, |
| 244 reinterpret_cast<DWORD>(dev_interface_broadcast.get())); | 244 reinterpret_cast<LPARAM>(dev_interface_broadcast.get())); |
| 245 | 245 |
| 246 RunUntilIdle(); | 246 RunUntilIdle(); |
| 247 EXPECT_EQ(expect_attach_calls, observer_.attach_calls()); | 247 EXPECT_EQ(expect_attach_calls, observer_.attach_calls()); |
| 248 EXPECT_EQ(expect_detach_calls, observer_.detach_calls()); | 248 EXPECT_EQ(expect_detach_calls, observer_.detach_calls()); |
| 249 } | 249 } |
| 250 | 250 |
| 251 bool StorageMonitorWinTest::GetMTPStorageInfo( | 251 bool StorageMonitorWinTest::GetMTPStorageInfo( |
| 252 const std::string& storage_device_id, | 252 const std::string& storage_device_id, |
| 253 base::string16* pnp_device_id, | 253 base::string16* pnp_device_id, |
| 254 base::string16* storage_object_id) { | 254 base::string16* storage_object_id) { |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 volume_mount_watcher_->BlockDeviceCheckForTesting(); | 409 volume_mount_watcher_->BlockDeviceCheckForTesting(); |
| 410 base::FilePath kAttachedDevicePath = | 410 base::FilePath kAttachedDevicePath = |
| 411 VolumeMountWatcherWin::DriveNumberToFilePath(8); // I: | 411 VolumeMountWatcherWin::DriveNumberToFilePath(8); // I: |
| 412 | 412 |
| 413 DEV_BROADCAST_VOLUME volume_broadcast; | 413 DEV_BROADCAST_VOLUME volume_broadcast; |
| 414 volume_broadcast.dbcv_size = sizeof(volume_broadcast); | 414 volume_broadcast.dbcv_size = sizeof(volume_broadcast); |
| 415 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; | 415 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; |
| 416 volume_broadcast.dbcv_flags = 0x0; | 416 volume_broadcast.dbcv_flags = 0x0; |
| 417 volume_broadcast.dbcv_unitmask = 0x100; // I: drive | 417 volume_broadcast.dbcv_unitmask = 0x100; // I: drive |
| 418 monitor_->InjectDeviceChange(DBT_DEVICEARRIVAL, | 418 monitor_->InjectDeviceChange(DBT_DEVICEARRIVAL, |
| 419 reinterpret_cast<DWORD>(&volume_broadcast)); | 419 reinterpret_cast<LPARAM>(&volume_broadcast)); |
| 420 | 420 |
| 421 EXPECT_EQ(0u, volume_mount_watcher_->devices_checked().size()); | 421 EXPECT_EQ(0u, volume_mount_watcher_->devices_checked().size()); |
| 422 | 422 |
| 423 // Re-attach the same volume. We haven't released the mock device check | 423 // Re-attach the same volume. We haven't released the mock device check |
| 424 // event, so there'll be pending calls in the UI thread to finish the | 424 // event, so there'll be pending calls in the UI thread to finish the |
| 425 // device check notification, blocking the duplicate device injection. | 425 // device check notification, blocking the duplicate device injection. |
| 426 monitor_->InjectDeviceChange(DBT_DEVICEARRIVAL, | 426 monitor_->InjectDeviceChange(DBT_DEVICEARRIVAL, |
| 427 reinterpret_cast<DWORD>(&volume_broadcast)); | 427 reinterpret_cast<LPARAM>(&volume_broadcast)); |
| 428 | 428 |
| 429 EXPECT_EQ(0u, volume_mount_watcher_->devices_checked().size()); | 429 EXPECT_EQ(0u, volume_mount_watcher_->devices_checked().size()); |
| 430 volume_mount_watcher_->ReleaseDeviceCheck(); | 430 volume_mount_watcher_->ReleaseDeviceCheck(); |
| 431 RunUntilIdle(); | 431 RunUntilIdle(); |
| 432 volume_mount_watcher_->ReleaseDeviceCheck(); | 432 volume_mount_watcher_->ReleaseDeviceCheck(); |
| 433 | 433 |
| 434 // Now let all attach notifications finish running. We'll only get one | 434 // Now let all attach notifications finish running. We'll only get one |
| 435 // finish-attach call. | 435 // finish-attach call. |
| 436 volume_mount_watcher_->FlushWorkerPoolForTesting(); | 436 volume_mount_watcher_->FlushWorkerPoolForTesting(); |
| 437 RunUntilIdle(); | 437 RunUntilIdle(); |
| 438 | 438 |
| 439 const std::vector<base::FilePath>& checked_devices = | 439 const std::vector<base::FilePath>& checked_devices = |
| 440 volume_mount_watcher_->devices_checked(); | 440 volume_mount_watcher_->devices_checked(); |
| 441 ASSERT_EQ(1u, checked_devices.size()); | 441 ASSERT_EQ(1u, checked_devices.size()); |
| 442 EXPECT_EQ(kAttachedDevicePath, checked_devices[0]); | 442 EXPECT_EQ(kAttachedDevicePath, checked_devices[0]); |
| 443 | 443 |
| 444 // We'll receive a duplicate check now that the first check has fully cleared. | 444 // We'll receive a duplicate check now that the first check has fully cleared. |
| 445 monitor_->InjectDeviceChange(DBT_DEVICEARRIVAL, | 445 monitor_->InjectDeviceChange(DBT_DEVICEARRIVAL, |
| 446 reinterpret_cast<DWORD>(&volume_broadcast)); | 446 reinterpret_cast<LPARAM>(&volume_broadcast)); |
| 447 volume_mount_watcher_->FlushWorkerPoolForTesting(); | 447 volume_mount_watcher_->FlushWorkerPoolForTesting(); |
| 448 volume_mount_watcher_->ReleaseDeviceCheck(); | 448 volume_mount_watcher_->ReleaseDeviceCheck(); |
| 449 RunUntilIdle(); | 449 RunUntilIdle(); |
| 450 | 450 |
| 451 ASSERT_EQ(2u, checked_devices.size()); | 451 ASSERT_EQ(2u, checked_devices.size()); |
| 452 EXPECT_EQ(kAttachedDevicePath, checked_devices[0]); | 452 EXPECT_EQ(kAttachedDevicePath, checked_devices[0]); |
| 453 EXPECT_EQ(kAttachedDevicePath, checked_devices[1]); | 453 EXPECT_EQ(kAttachedDevicePath, checked_devices[1]); |
| 454 } | 454 } |
| 455 | 455 |
| 456 TEST_F(StorageMonitorWinTest, DeviceInfoForPath) { | 456 TEST_F(StorageMonitorWinTest, DeviceInfoForPath) { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 TestPortableDeviceWatcherWin::kMTPDeviceWithValidInfo); | 539 TestPortableDeviceWatcherWin::kMTPDeviceWithValidInfo); |
| 540 EXPECT_EQ(expected, pnp_device_id); | 540 EXPECT_EQ(expected, pnp_device_id); |
| 541 EXPECT_EQ(it->object_persistent_id, | 541 EXPECT_EQ(it->object_persistent_id, |
| 542 TestPortableDeviceWatcherWin::GetMTPStorageUniqueId( | 542 TestPortableDeviceWatcherWin::GetMTPStorageUniqueId( |
| 543 pnp_device_id, storage_object_id)); | 543 pnp_device_id, storage_object_id)); |
| 544 } | 544 } |
| 545 DoMTPDeviceTest(TestPortableDeviceWatcherWin::kMTPDeviceWithValidInfo, false); | 545 DoMTPDeviceTest(TestPortableDeviceWatcherWin::kMTPDeviceWithValidInfo, false); |
| 546 } | 546 } |
| 547 | 547 |
| 548 } // namespace storage_monitor | 548 } // namespace storage_monitor |
| OLD | NEW |