OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "device/usb/mojo/device_impl.h" | 5 #include "device/usb/mojo/device_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
(...skipping 428 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 mock_device().NotifyDeviceRemoved(); | 439 mock_device().NotifyDeviceRemoved(); |
440 loop.Run(); | 440 loop.Run(); |
441 } | 441 } |
442 | 442 |
443 TEST_F(USBDeviceImplTest, Open) { | 443 TEST_F(USBDeviceImplTest, Open) { |
444 UsbDevicePtr device = GetMockDeviceProxy(); | 444 UsbDevicePtr device = GetMockDeviceProxy(); |
445 | 445 |
446 EXPECT_FALSE(is_device_open()); | 446 EXPECT_FALSE(is_device_open()); |
447 | 447 |
448 EXPECT_CALL(mock_device(), Open(_)); | 448 EXPECT_CALL(mock_device(), Open(_)); |
| 449 EXPECT_CALL(permission_provider(), IncrementConnectionCount()); |
449 | 450 |
450 { | 451 { |
451 base::RunLoop loop; | 452 base::RunLoop loop; |
452 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, | 453 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
453 loop.QuitClosure())); | 454 loop.QuitClosure())); |
454 loop.Run(); | 455 loop.Run(); |
455 } | 456 } |
456 | 457 |
457 { | 458 { |
458 base::RunLoop loop; | 459 base::RunLoop loop; |
459 device->Open(base::Bind(&ExpectOpenAndThen, | 460 device->Open(base::Bind(&ExpectOpenAndThen, |
460 mojom::UsbOpenDeviceError::ALREADY_OPEN, | 461 mojom::UsbOpenDeviceError::ALREADY_OPEN, |
461 loop.QuitClosure())); | 462 loop.QuitClosure())); |
462 loop.Run(); | 463 loop.Run(); |
463 } | 464 } |
464 | 465 |
465 EXPECT_CALL(mock_handle(), Close()); | 466 EXPECT_CALL(mock_handle(), Close()); |
| 467 EXPECT_CALL(permission_provider(), DecrementConnectionCount()); |
466 } | 468 } |
467 | 469 |
468 TEST_F(USBDeviceImplTest, Close) { | 470 TEST_F(USBDeviceImplTest, Close) { |
469 UsbDevicePtr device = GetMockDeviceProxy(); | 471 UsbDevicePtr device = GetMockDeviceProxy(); |
470 | 472 |
471 EXPECT_FALSE(is_device_open()); | 473 EXPECT_FALSE(is_device_open()); |
472 | 474 |
473 EXPECT_CALL(mock_device(), Open(_)); | 475 EXPECT_CALL(mock_device(), Open(_)); |
474 | 476 |
475 { | 477 { |
(...skipping 20 matching lines...) Expand all Loading... |
496 EXPECT_CALL(mock_device(), Open(_)); | 498 EXPECT_CALL(mock_device(), Open(_)); |
497 | 499 |
498 { | 500 { |
499 base::RunLoop loop; | 501 base::RunLoop loop; |
500 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, | 502 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
501 loop.QuitClosure())); | 503 loop.QuitClosure())); |
502 loop.Run(); | 504 loop.Run(); |
503 } | 505 } |
504 | 506 |
505 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); | 507 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); |
506 EXPECT_CALL(permission_provider(), HasConfigurationPermission(42, _)); | |
507 | 508 |
508 { | 509 { |
509 // SetConfiguration should fail because 42 is not a valid mock | 510 // SetConfiguration should fail because 42 is not a valid mock |
510 // configuration. | 511 // configuration. |
511 base::RunLoop loop; | 512 base::RunLoop loop; |
512 device->SetConfiguration( | 513 device->SetConfiguration( |
513 42, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); | 514 42, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); |
514 loop.Run(); | 515 loop.Run(); |
515 } | 516 } |
516 | 517 |
517 EXPECT_CALL(mock_handle(), Close()); | 518 EXPECT_CALL(mock_handle(), Close()); |
518 } | 519 } |
519 | 520 |
520 TEST_F(USBDeviceImplTest, SetValidConfiguration) { | 521 TEST_F(USBDeviceImplTest, SetValidConfiguration) { |
521 UsbDevicePtr device = GetMockDeviceProxy(); | 522 UsbDevicePtr device = GetMockDeviceProxy(); |
522 | 523 |
523 EXPECT_CALL(mock_device(), Open(_)); | 524 EXPECT_CALL(mock_device(), Open(_)); |
524 | 525 |
525 { | 526 { |
526 base::RunLoop loop; | 527 base::RunLoop loop; |
527 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, | 528 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
528 loop.QuitClosure())); | 529 loop.QuitClosure())); |
529 loop.Run(); | 530 loop.Run(); |
530 } | 531 } |
531 | 532 |
532 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); | 533 EXPECT_CALL(mock_handle(), SetConfiguration(42, _)); |
533 EXPECT_CALL(permission_provider(), HasConfigurationPermission(42, _)); | |
534 | 534 |
535 AddMockConfig(ConfigBuilder(42)); | 535 AddMockConfig(ConfigBuilder(42)); |
536 | 536 |
537 { | 537 { |
538 // SetConfiguration should succeed because 42 is a valid mock configuration. | 538 // SetConfiguration should succeed because 42 is a valid mock configuration. |
539 base::RunLoop loop; | 539 base::RunLoop loop; |
540 device->SetConfiguration( | 540 device->SetConfiguration( |
541 42, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 541 42, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
542 loop.Run(); | 542 loop.Run(); |
543 } | 543 } |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
591 base::RunLoop loop; | 591 base::RunLoop loop; |
592 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, | 592 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
593 loop.QuitClosure())); | 593 loop.QuitClosure())); |
594 loop.Run(); | 594 loop.Run(); |
595 } | 595 } |
596 | 596 |
597 // Now add a mock interface #1. | 597 // Now add a mock interface #1. |
598 AddMockConfig(ConfigBuilder(1).AddInterface(1, 0, 1, 2, 3)); | 598 AddMockConfig(ConfigBuilder(1).AddInterface(1, 0, 1, 2, 3)); |
599 | 599 |
600 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); | 600 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); |
601 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); | |
602 | 601 |
603 { | 602 { |
604 base::RunLoop loop; | 603 base::RunLoop loop; |
605 device->SetConfiguration( | 604 device->SetConfiguration( |
606 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 605 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
607 loop.Run(); | 606 loop.Run(); |
608 } | 607 } |
609 | 608 |
610 { | 609 { |
611 // Try to claim an invalid interface and expect failure. | 610 // Try to claim an invalid interface and expect failure. |
612 base::RunLoop loop; | 611 base::RunLoop loop; |
613 device->ClaimInterface( | 612 device->ClaimInterface( |
614 2, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); | 613 2, base::Bind(&ExpectResultAndThen, false, loop.QuitClosure())); |
615 loop.Run(); | 614 loop.Run(); |
616 } | 615 } |
617 | 616 |
618 EXPECT_CALL(mock_handle(), ClaimInterface(1, _)); | 617 EXPECT_CALL(mock_handle(), ClaimInterface(1, _)); |
619 EXPECT_CALL(permission_provider(), HasFunctionPermission(1, 1, _)); | |
620 | 618 |
621 { | 619 { |
622 base::RunLoop loop; | 620 base::RunLoop loop; |
623 device->ClaimInterface( | 621 device->ClaimInterface( |
624 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 622 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
625 loop.Run(); | 623 loop.Run(); |
626 } | 624 } |
627 | 625 |
628 EXPECT_CALL(mock_handle(), ReleaseInterface(2, _)); | 626 EXPECT_CALL(mock_handle(), ReleaseInterface(2, _)); |
629 | 627 |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
694 { | 692 { |
695 base::RunLoop loop; | 693 base::RunLoop loop; |
696 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, | 694 device->Open(base::Bind(&ExpectOpenAndThen, mojom::UsbOpenDeviceError::OK, |
697 loop.QuitClosure())); | 695 loop.QuitClosure())); |
698 loop.Run(); | 696 loop.Run(); |
699 } | 697 } |
700 | 698 |
701 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); | 699 AddMockConfig(ConfigBuilder(1).AddInterface(7, 0, 1, 2, 3)); |
702 | 700 |
703 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); | 701 EXPECT_CALL(mock_handle(), SetConfiguration(1, _)); |
704 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); | |
705 | 702 |
706 { | 703 { |
707 base::RunLoop loop; | 704 base::RunLoop loop; |
708 device->SetConfiguration( | 705 device->SetConfiguration( |
709 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); | 706 1, base::Bind(&ExpectResultAndThen, true, loop.QuitClosure())); |
710 loop.Run(); | 707 loop.Run(); |
711 } | 708 } |
712 | 709 |
713 std::vector<uint8_t> fake_data; | 710 std::vector<uint8_t> fake_data; |
714 fake_data.push_back(41); | 711 fake_data.push_back(41); |
715 fake_data.push_back(42); | 712 fake_data.push_back(42); |
716 fake_data.push_back(43); | 713 fake_data.push_back(43); |
717 | 714 |
718 AddMockInboundData(fake_data); | 715 AddMockInboundData(fake_data); |
719 | 716 |
720 EXPECT_CALL(mock_handle(), | 717 EXPECT_CALL(mock_handle(), |
721 ControlTransfer(UsbTransferDirection::INBOUND, | 718 ControlTransfer(UsbTransferDirection::INBOUND, |
722 UsbControlTransferType::STANDARD, | 719 UsbControlTransferType::STANDARD, |
723 UsbControlTransferRecipient::DEVICE, 5, 6, 7, _, | 720 UsbControlTransferRecipient::DEVICE, 5, 6, 7, _, |
724 _, 0, _)); | 721 _, 0, _)); |
725 EXPECT_CALL(permission_provider(), HasConfigurationPermission(1, _)); | |
726 | 722 |
727 { | 723 { |
728 auto params = mojom::UsbControlTransferParams::New(); | 724 auto params = mojom::UsbControlTransferParams::New(); |
729 params->type = UsbControlTransferType::STANDARD; | 725 params->type = UsbControlTransferType::STANDARD; |
730 params->recipient = UsbControlTransferRecipient::DEVICE; | 726 params->recipient = UsbControlTransferRecipient::DEVICE; |
731 params->request = 5; | 727 params->request = 5; |
732 params->value = 6; | 728 params->value = 6; |
733 params->index = 7; | 729 params->index = 7; |
734 base::RunLoop loop; | 730 base::RunLoop loop; |
735 device->ControlTransferIn(std::move(params), | 731 device->ControlTransferIn(std::move(params), |
736 static_cast<uint32_t>(fake_data.size()), 0, | 732 static_cast<uint32_t>(fake_data.size()), 0, |
737 base::Bind(&ExpectTransferInAndThen, | 733 base::Bind(&ExpectTransferInAndThen, |
738 mojom::UsbTransferStatus::COMPLETED, | 734 mojom::UsbTransferStatus::COMPLETED, |
739 fake_data, loop.QuitClosure())); | 735 fake_data, loop.QuitClosure())); |
740 loop.Run(); | 736 loop.Run(); |
741 } | 737 } |
742 | 738 |
743 AddMockOutboundData(fake_data); | 739 AddMockOutboundData(fake_data); |
744 | 740 |
745 EXPECT_CALL(mock_handle(), | 741 EXPECT_CALL(mock_handle(), |
746 ControlTransfer(UsbTransferDirection::OUTBOUND, | 742 ControlTransfer(UsbTransferDirection::OUTBOUND, |
747 UsbControlTransferType::STANDARD, | 743 UsbControlTransferType::STANDARD, |
748 UsbControlTransferRecipient::INTERFACE, 5, 6, 7, | 744 UsbControlTransferRecipient::INTERFACE, 5, 6, 7, |
749 _, _, 0, _)); | 745 _, _, 0, _)); |
750 EXPECT_CALL(permission_provider(), HasFunctionPermission(7, 1, _)); | |
751 | 746 |
752 { | 747 { |
753 auto params = mojom::UsbControlTransferParams::New(); | 748 auto params = mojom::UsbControlTransferParams::New(); |
754 params->type = UsbControlTransferType::STANDARD; | 749 params->type = UsbControlTransferType::STANDARD; |
755 params->recipient = UsbControlTransferRecipient::INTERFACE; | 750 params->recipient = UsbControlTransferRecipient::INTERFACE; |
756 params->request = 5; | 751 params->request = 5; |
757 params->value = 6; | 752 params->value = 6; |
758 params->index = 7; | 753 params->index = 7; |
759 base::RunLoop loop; | 754 base::RunLoop loop; |
760 device->ControlTransferOut( | 755 device->ControlTransferOut( |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
879 base::Bind(&ExpectPacketsInAndThen, fake_inbound_data, | 874 base::Bind(&ExpectPacketsInAndThen, fake_inbound_data, |
880 expected_transferred_lengths, loop.QuitClosure())); | 875 expected_transferred_lengths, loop.QuitClosure())); |
881 loop.Run(); | 876 loop.Run(); |
882 } | 877 } |
883 | 878 |
884 EXPECT_CALL(mock_handle(), Close()); | 879 EXPECT_CALL(mock_handle(), Close()); |
885 } | 880 } |
886 | 881 |
887 } // namespace usb | 882 } // namespace usb |
888 } // namespace device | 883 } // namespace device |
OLD | NEW |