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 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi
n.h" | 5 #include "chrome/browser/system_monitor/removable_device_notifications_window_wi
n.h" |
6 | 6 |
7 #include <dbt.h> | 7 #include <dbt.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 | 310 |
311 protected: | 311 protected: |
312 // testing::Test: | 312 // testing::Test: |
313 virtual void SetUp() OVERRIDE; | 313 virtual void SetUp() OVERRIDE; |
314 virtual void TearDown() OVERRIDE; | 314 virtual void TearDown() OVERRIDE; |
315 | 315 |
316 void AddMassStorageDeviceAttachExpectation(FilePath drive); | 316 void AddMassStorageDeviceAttachExpectation(FilePath drive); |
317 void PreAttachDevices(); | 317 void PreAttachDevices(); |
318 | 318 |
319 // Runs all the pending tasks on UI thread, FILE thread and blocking thread. | 319 // Runs all the pending tasks on UI thread, FILE thread and blocking thread. |
320 void RunAllPending(); | 320 void RunUntilIdle(); |
321 | 321 |
322 void DoMassStorageDeviceAttachedTest(const DeviceIndices& device_indices); | 322 void DoMassStorageDeviceAttachedTest(const DeviceIndices& device_indices); |
323 void DoMassStorageDevicesDetachedTest(const DeviceIndices& device_indices); | 323 void DoMassStorageDevicesDetachedTest(const DeviceIndices& device_indices); |
324 | 324 |
325 // Injects a device attach or detach change (depending on the value of | 325 // Injects a device attach or detach change (depending on the value of |
326 // |test_attach|) and tests that the appropriate handler is called. | 326 // |test_attach|) and tests that the appropriate handler is called. |
327 void DoMTPDeviceTest(const string16& pnp_device_id, bool test_attach); | 327 void DoMTPDeviceTest(const string16& pnp_device_id, bool test_attach); |
328 | 328 |
329 MessageLoopForUI message_loop_; | 329 MessageLoopForUI message_loop_; |
330 content::TestBrowserThread ui_thread_; | 330 content::TestBrowserThread ui_thread_; |
(...skipping 14 matching lines...) Expand all Loading... |
345 RemovableDeviceNotificationsWindowWinTest:: | 345 RemovableDeviceNotificationsWindowWinTest:: |
346 ~RemovableDeviceNotificationsWindowWinTest() { | 346 ~RemovableDeviceNotificationsWindowWinTest() { |
347 } | 347 } |
348 | 348 |
349 void RemovableDeviceNotificationsWindowWinTest::SetUp() { | 349 void RemovableDeviceNotificationsWindowWinTest::SetUp() { |
350 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 350 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
351 volume_mount_watcher_ = new TestVolumeMountWatcherWin; | 351 volume_mount_watcher_ = new TestVolumeMountWatcherWin; |
352 window_.reset(new TestRemovableDeviceNotificationsWindowWin( | 352 window_.reset(new TestRemovableDeviceNotificationsWindowWin( |
353 volume_mount_watcher_.get(), new TestPortableDeviceWatcherWin)); | 353 volume_mount_watcher_.get(), new TestPortableDeviceWatcherWin)); |
354 window_->InitWithTestData(false); | 354 window_->InitWithTestData(false); |
355 RunAllPending(); | 355 RunUntilIdle(); |
356 system_monitor_.AddDevicesChangedObserver(&observer_); | 356 system_monitor_.AddDevicesChangedObserver(&observer_); |
357 } | 357 } |
358 | 358 |
359 void RemovableDeviceNotificationsWindowWinTest::TearDown() { | 359 void RemovableDeviceNotificationsWindowWinTest::TearDown() { |
360 RunAllPending(); | 360 RunUntilIdle(); |
361 system_monitor_.RemoveDevicesChangedObserver(&observer_); | 361 system_monitor_.RemoveDevicesChangedObserver(&observer_); |
362 } | 362 } |
363 | 363 |
364 void RemovableDeviceNotificationsWindowWinTest:: | 364 void RemovableDeviceNotificationsWindowWinTest:: |
365 AddMassStorageDeviceAttachExpectation(FilePath drive) { | 365 AddMassStorageDeviceAttachExpectation(FilePath drive) { |
366 std::string unique_id; | 366 std::string unique_id; |
367 string16 device_name; | 367 string16 device_name; |
368 bool removable; | 368 bool removable; |
369 ASSERT_TRUE(GetMassStorageDeviceDetails(drive, NULL, &unique_id, | 369 ASSERT_TRUE(GetMassStorageDeviceDetails(drive, NULL, &unique_id, |
370 &device_name, &removable)); | 370 &device_name, &removable)); |
(...skipping 13 matching lines...) Expand all Loading... |
384 testing::InSequence sequence; | 384 testing::InSequence sequence; |
385 AttachedDevices initial_devices = GetTestAttachedDevices(); | 385 AttachedDevices initial_devices = GetTestAttachedDevices(); |
386 for (AttachedDevices::const_iterator it = initial_devices.begin(); | 386 for (AttachedDevices::const_iterator it = initial_devices.begin(); |
387 it != initial_devices.end(); ++it) { | 387 it != initial_devices.end(); ++it) { |
388 AddMassStorageDeviceAttachExpectation(*it); | 388 AddMassStorageDeviceAttachExpectation(*it); |
389 } | 389 } |
390 } | 390 } |
391 window_.reset(new TestRemovableDeviceNotificationsWindowWin( | 391 window_.reset(new TestRemovableDeviceNotificationsWindowWin( |
392 volume_mount_watcher_.get(), new TestPortableDeviceWatcherWin)); | 392 volume_mount_watcher_.get(), new TestPortableDeviceWatcherWin)); |
393 window_->InitWithTestData(true); | 393 window_->InitWithTestData(true); |
394 RunAllPending(); | 394 RunUntilIdle(); |
395 } | 395 } |
396 | 396 |
397 void RemovableDeviceNotificationsWindowWinTest::RunAllPending() { | 397 void RemovableDeviceNotificationsWindowWinTest::RunUntilIdle() { |
398 message_loop_.RunAllPending(); | 398 message_loop_.RunUntilIdle(); |
399 } | 399 } |
400 | 400 |
401 void RemovableDeviceNotificationsWindowWinTest:: | 401 void RemovableDeviceNotificationsWindowWinTest:: |
402 DoMassStorageDeviceAttachedTest(const DeviceIndices& device_indices) { | 402 DoMassStorageDeviceAttachedTest(const DeviceIndices& device_indices) { |
403 DEV_BROADCAST_VOLUME volume_broadcast; | 403 DEV_BROADCAST_VOLUME volume_broadcast; |
404 volume_broadcast.dbcv_size = sizeof(volume_broadcast); | 404 volume_broadcast.dbcv_size = sizeof(volume_broadcast); |
405 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; | 405 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; |
406 volume_broadcast.dbcv_unitmask = 0x0; | 406 volume_broadcast.dbcv_unitmask = 0x0; |
407 volume_broadcast.dbcv_flags = 0x0; | 407 volume_broadcast.dbcv_flags = 0x0; |
408 { | 408 { |
409 testing::InSequence sequence; | 409 testing::InSequence sequence; |
410 for (DeviceIndices::const_iterator it = device_indices.begin(); | 410 for (DeviceIndices::const_iterator it = device_indices.begin(); |
411 it != device_indices.end(); ++it) { | 411 it != device_indices.end(); ++it) { |
412 volume_broadcast.dbcv_unitmask |= 0x1 << *it; | 412 volume_broadcast.dbcv_unitmask |= 0x1 << *it; |
413 AddMassStorageDeviceAttachExpectation(DriveNumberToFilePath(*it)); | 413 AddMassStorageDeviceAttachExpectation(DriveNumberToFilePath(*it)); |
414 } | 414 } |
415 } | 415 } |
416 window_->InjectDeviceChange(DBT_DEVICEARRIVAL, | 416 window_->InjectDeviceChange(DBT_DEVICEARRIVAL, |
417 reinterpret_cast<DWORD>(&volume_broadcast)); | 417 reinterpret_cast<DWORD>(&volume_broadcast)); |
418 RunAllPending(); | 418 RunUntilIdle(); |
419 } | 419 } |
420 | 420 |
421 void RemovableDeviceNotificationsWindowWinTest:: | 421 void RemovableDeviceNotificationsWindowWinTest:: |
422 DoMassStorageDevicesDetachedTest(const DeviceIndices& device_indices) { | 422 DoMassStorageDevicesDetachedTest(const DeviceIndices& device_indices) { |
423 DEV_BROADCAST_VOLUME volume_broadcast; | 423 DEV_BROADCAST_VOLUME volume_broadcast; |
424 volume_broadcast.dbcv_size = sizeof(volume_broadcast); | 424 volume_broadcast.dbcv_size = sizeof(volume_broadcast); |
425 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; | 425 volume_broadcast.dbcv_devicetype = DBT_DEVTYP_VOLUME; |
426 volume_broadcast.dbcv_unitmask = 0x0; | 426 volume_broadcast.dbcv_unitmask = 0x0; |
427 volume_broadcast.dbcv_flags = 0x0; | 427 volume_broadcast.dbcv_flags = 0x0; |
428 { | 428 { |
429 testing::InSequence sequence; | 429 testing::InSequence sequence; |
430 for (DeviceIndices::const_iterator it = device_indices.begin(); | 430 for (DeviceIndices::const_iterator it = device_indices.begin(); |
431 it != device_indices.end(); ++it) { | 431 it != device_indices.end(); ++it) { |
432 volume_broadcast.dbcv_unitmask |= 0x1 << *it; | 432 volume_broadcast.dbcv_unitmask |= 0x1 << *it; |
433 std::string unique_id; | 433 std::string unique_id; |
434 bool removable; | 434 bool removable; |
435 ASSERT_TRUE(GetMassStorageDeviceDetails(DriveNumberToFilePath(*it), NULL, | 435 ASSERT_TRUE(GetMassStorageDeviceDetails(DriveNumberToFilePath(*it), NULL, |
436 &unique_id, NULL, &removable)); | 436 &unique_id, NULL, &removable)); |
437 if (removable) { | 437 if (removable) { |
438 MediaStorageUtil::Type type = | 438 MediaStorageUtil::Type type = |
439 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; | 439 MediaStorageUtil::REMOVABLE_MASS_STORAGE_NO_DCIM; |
440 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); | 440 std::string device_id = MediaStorageUtil::MakeDeviceId(type, unique_id); |
441 EXPECT_CALL(observer_, OnRemovableStorageDetached(device_id)).Times(1); | 441 EXPECT_CALL(observer_, OnRemovableStorageDetached(device_id)).Times(1); |
442 } | 442 } |
443 } | 443 } |
444 } | 444 } |
445 window_->InjectDeviceChange(DBT_DEVICEREMOVECOMPLETE, | 445 window_->InjectDeviceChange(DBT_DEVICEREMOVECOMPLETE, |
446 reinterpret_cast<DWORD>(&volume_broadcast)); | 446 reinterpret_cast<DWORD>(&volume_broadcast)); |
447 RunAllPending(); | 447 RunUntilIdle(); |
448 } | 448 } |
449 | 449 |
450 void RemovableDeviceNotificationsWindowWinTest::DoMTPDeviceTest( | 450 void RemovableDeviceNotificationsWindowWinTest::DoMTPDeviceTest( |
451 const string16& pnp_device_id, bool test_attach) { | 451 const string16& pnp_device_id, bool test_attach) { |
452 GUID guidDevInterface = GUID_NULL; | 452 GUID guidDevInterface = GUID_NULL; |
453 HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &guidDevInterface); | 453 HRESULT hr = CLSIDFromString(kWPDDevInterfaceGUID, &guidDevInterface); |
454 if (FAILED(hr)) | 454 if (FAILED(hr)) |
455 return; | 455 return; |
456 | 456 |
457 size_t device_id_size = pnp_device_id.size() * sizeof(char16); | 457 size_t device_id_size = pnp_device_id.size() * sizeof(char16); |
(...skipping 23 matching lines...) Expand all Loading... |
481 .Times((name.empty() || unique_id.empty()) ? 0 : 1); | 481 .Times((name.empty() || unique_id.empty()) ? 0 : 1); |
482 } else { | 482 } else { |
483 EXPECT_CALL(observer_, OnRemovableStorageDetached(unique_id)) | 483 EXPECT_CALL(observer_, OnRemovableStorageDetached(unique_id)) |
484 .Times((name.empty() || unique_id.empty()) ? 0 : 1); | 484 .Times((name.empty() || unique_id.empty()) ? 0 : 1); |
485 } | 485 } |
486 } | 486 } |
487 } | 487 } |
488 window_->InjectDeviceChange( | 488 window_->InjectDeviceChange( |
489 test_attach ? DBT_DEVICEARRIVAL : DBT_DEVICEREMOVECOMPLETE, | 489 test_attach ? DBT_DEVICEARRIVAL : DBT_DEVICEREMOVECOMPLETE, |
490 reinterpret_cast<DWORD>(dev_interface_broadcast.get())); | 490 reinterpret_cast<DWORD>(dev_interface_broadcast.get())); |
491 RunAllPending(); | 491 RunUntilIdle(); |
492 } | 492 } |
493 | 493 |
494 TEST_F(RemovableDeviceNotificationsWindowWinTest, RandomMessage) { | 494 TEST_F(RemovableDeviceNotificationsWindowWinTest, RandomMessage) { |
495 window_->InjectDeviceChange(DBT_DEVICEQUERYREMOVE, NULL); | 495 window_->InjectDeviceChange(DBT_DEVICEQUERYREMOVE, NULL); |
496 RunAllPending(); | 496 RunUntilIdle(); |
497 } | 497 } |
498 | 498 |
499 TEST_F(RemovableDeviceNotificationsWindowWinTest, DevicesAttached) { | 499 TEST_F(RemovableDeviceNotificationsWindowWinTest, DevicesAttached) { |
500 DeviceIndices device_indices; | 500 DeviceIndices device_indices; |
501 device_indices.push_back(1); | 501 device_indices.push_back(1); |
502 device_indices.push_back(5); | 502 device_indices.push_back(5); |
503 device_indices.push_back(7); | 503 device_indices.push_back(7); |
504 device_indices.push_back(13); | 504 device_indices.push_back(13); |
505 | 505 |
506 DoMassStorageDeviceAttachedTest(device_indices); | 506 DoMassStorageDeviceAttachedTest(device_indices); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
636 | 636 |
637 // Attach a device with two data partitions. Verify that attach/detach | 637 // Attach a device with two data partitions. Verify that attach/detach |
638 // notifications are sent out for each removable storage. | 638 // notifications are sent out for each removable storage. |
639 TEST_F(RemovableDeviceNotificationsWindowWinTest, | 639 TEST_F(RemovableDeviceNotificationsWindowWinTest, |
640 MTPDeviceWithMultipleStorageObjects) { | 640 MTPDeviceWithMultipleStorageObjects) { |
641 DoMTPDeviceTest(kMTPDeviceWithMultipleStorageObjects, true); | 641 DoMTPDeviceTest(kMTPDeviceWithMultipleStorageObjects, true); |
642 DoMTPDeviceTest(kMTPDeviceWithMultipleStorageObjects, false); | 642 DoMTPDeviceTest(kMTPDeviceWithMultipleStorageObjects, false); |
643 } | 643 } |
644 | 644 |
645 } // namespace chrome | 645 } // namespace chrome |
OLD | NEW |