OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/memory/ref_counted.h" |
| 6 #include "chrome/browser/extensions/bluetooth_event_router.h" |
| 7 #include "chrome/browser/extensions/event_names.h" |
| 8 #include "chrome/test/base/testing_profile.h" |
| 9 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 10 #include "device/bluetooth/test/mock_bluetooth_socket.h" |
| 11 #include "testing/gmock/include/gmock/gmock.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 const char* kAdapterAddress = "Mock Adapter address for testing"; |
| 17 const char* kName = "Mock Adapter name for testing"; |
| 18 const char* kNonBluetoothEventName = "Non bluetooth event name"; |
| 19 |
| 20 const int kSocketId = 1; |
| 21 const int kAnotherSocketId = 2; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 namespace extensions { |
| 26 |
| 27 class ExtensionBluetoothEventRouterTest : public testing::Test { |
| 28 public: |
| 29 ExtensionBluetoothEventRouterTest() |
| 30 : mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>( |
| 31 kAdapterAddress, kName)), |
| 32 mock_socket_(new testing::NiceMock<device::MockBluetoothSocket>()), |
| 33 mock_another_socket_( |
| 34 new testing::NiceMock<device::MockBluetoothSocket>()), |
| 35 router_(&test_profile_) { |
| 36 router_.SetAdapterForTest(mock_adapter_); |
| 37 } |
| 38 |
| 39 protected: |
| 40 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; |
| 41 testing::NiceMock<device::MockBluetoothSocket>* mock_socket_; |
| 42 testing::NiceMock<device::MockBluetoothSocket>* mock_another_socket_; |
| 43 TestingProfile test_profile_; |
| 44 ExtensionBluetoothEventRouter router_; |
| 45 }; |
| 46 |
| 47 TEST_F(ExtensionBluetoothEventRouterTest, BluetoothEventListener) { |
| 48 router_.OnEventListenerAdded( |
| 49 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 50 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 51 router_.OnEventListenerRemoved( |
| 52 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 53 } |
| 54 |
| 55 TEST_F(ExtensionBluetoothEventRouterTest, MultipleBluetoothEventListeners) { |
| 56 router_.OnEventListenerAdded( |
| 57 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 58 router_.OnEventListenerAdded( |
| 59 extensions::event_names::kBluetoothOnDiscoveringChanged); |
| 60 router_.OnEventListenerAdded( |
| 61 extensions::event_names::kBluetoothOnPowerChanged); |
| 62 router_.OnEventListenerRemoved( |
| 63 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 64 router_.OnEventListenerRemoved( |
| 65 extensions::event_names::kBluetoothOnDiscoveringChanged); |
| 66 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 67 router_.OnEventListenerRemoved( |
| 68 extensions::event_names::kBluetoothOnPowerChanged); |
| 69 } |
| 70 |
| 71 TEST_F(ExtensionBluetoothEventRouterTest, NonBluetoothEventListeners) { |
| 72 router_.OnEventListenerAdded(kNonBluetoothEventName); |
| 73 router_.OnEventListenerRemoved(kNonBluetoothEventName); |
| 74 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 75 } |
| 76 |
| 77 TEST_F(ExtensionBluetoothEventRouterTest, RegisterSocket) { |
| 78 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 79 int socket_id = router_.RegisterSocket(mock_socket_); |
| 80 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 81 router_.ReleaseSocket(socket_id); |
| 82 } |
| 83 |
| 84 TEST_F(ExtensionBluetoothEventRouterTest, RegisterMultipleSockets) { |
| 85 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 86 int socket_id = router_.RegisterSocket(mock_socket_); |
| 87 EXPECT_CALL(*mock_another_socket_, fd()) |
| 88 .WillRepeatedly(testing::Return(kAnotherSocketId)); |
| 89 int another_socket_id = router_.RegisterSocket(mock_another_socket_); |
| 90 router_.ReleaseSocket(another_socket_id); |
| 91 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 92 router_.ReleaseSocket(socket_id); |
| 93 } |
| 94 |
| 95 TEST_F(ExtensionBluetoothEventRouterTest, RegisterSameSockets) { |
| 96 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 97 int socket_id = router_.RegisterSocket(mock_socket_); |
| 98 socket_id = router_.RegisterSocket(mock_socket_); |
| 99 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 100 router_.ReleaseSocket(socket_id); |
| 101 } |
| 102 |
| 103 TEST_F(ExtensionBluetoothEventRouterTest, AddEventListerAndSocket) { |
| 104 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 105 int socket_id = router_.RegisterSocket(mock_socket_); |
| 106 router_.OnEventListenerAdded( |
| 107 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 108 router_.ReleaseSocket(socket_id); |
| 109 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 110 router_.OnEventListenerRemoved( |
| 111 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 112 } |
| 113 |
| 114 TEST_F(ExtensionBluetoothEventRouterTest, AddSocketAndEventListener) { |
| 115 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 116 router_.OnEventListenerAdded( |
| 117 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 118 int socket_id = router_.RegisterSocket(mock_socket_); |
| 119 router_.OnEventListenerRemoved( |
| 120 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 121 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 122 router_.ReleaseSocket(socket_id); |
| 123 } |
| 124 |
| 125 } // namespace extensions |
OLD | NEW |