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 TestBluetoothEventRouter : public ExtensionBluetoothEventRouter { |
| 28 public: |
| 29 explicit TestBluetoothEventRouter(device::BluetoothAdapter* test_adapter) |
| 30 : ExtensionBluetoothEventRouter(&test_profile_), |
| 31 test_adapter_(test_adapter) { |
| 32 } |
| 33 |
| 34 protected: |
| 35 virtual scoped_refptr<device::BluetoothAdapter> GetDefaultAdapter() const { |
| 36 return scoped_refptr<device::BluetoothAdapter>(test_adapter_); |
| 37 } |
| 38 |
| 39 private: |
| 40 TestingProfile test_profile_; |
| 41 device::BluetoothAdapter* test_adapter_; |
| 42 }; |
| 43 |
| 44 class ExtensionBluetoothEventRouterTest : public testing::Test { |
| 45 public: |
| 46 ExtensionBluetoothEventRouterTest() |
| 47 : mock_adapter_(new testing::StrictMock<device::MockBluetoothAdapter>( |
| 48 kAdapterAddress, kName)), |
| 49 mock_socket_(new testing::NiceMock<device::MockBluetoothSocket>()), |
| 50 mock_another_socket_( |
| 51 new testing::NiceMock<device::MockBluetoothSocket>()), |
| 52 router_(mock_adapter_) { |
| 53 } |
| 54 |
| 55 protected: |
| 56 testing::StrictMock<device::MockBluetoothAdapter>* mock_adapter_; |
| 57 testing::NiceMock<device::MockBluetoothSocket>* mock_socket_; |
| 58 testing::NiceMock<device::MockBluetoothSocket>* mock_another_socket_; |
| 59 TestBluetoothEventRouter router_; |
| 60 }; |
| 61 |
| 62 TEST_F(ExtensionBluetoothEventRouterTest, GetAdapter) { |
| 63 scoped_refptr<const device::BluetoothAdapter> adapter = router_.adapter(); |
| 64 |
| 65 EXPECT_EQ(mock_adapter_, adapter); |
| 66 } |
| 67 |
| 68 TEST_F(ExtensionBluetoothEventRouterTest, GetMutableAdapter) { |
| 69 scoped_refptr<device::BluetoothAdapter> adapter = router_.GetMutableAdapter(); |
| 70 |
| 71 EXPECT_EQ(mock_adapter_, adapter); |
| 72 } |
| 73 |
| 74 TEST_F(ExtensionBluetoothEventRouterTest, BluetoothEventListener) { |
| 75 EXPECT_CALL(*mock_adapter_, AddObserver(testing::_)).Times(1); |
| 76 router_.OnEventListenerAdded( |
| 77 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 78 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 79 router_.OnEventListenerRemoved( |
| 80 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 81 } |
| 82 |
| 83 TEST_F(ExtensionBluetoothEventRouterTest, MultipleBluetoothEventListeners) { |
| 84 EXPECT_CALL(*mock_adapter_, AddObserver(testing::_)).Times(1); |
| 85 router_.OnEventListenerAdded( |
| 86 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 87 router_.OnEventListenerAdded( |
| 88 extensions::event_names::kBluetoothOnDiscoveringChanged); |
| 89 router_.OnEventListenerAdded( |
| 90 extensions::event_names::kBluetoothOnPowerChanged); |
| 91 router_.OnEventListenerRemoved( |
| 92 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 93 router_.OnEventListenerRemoved( |
| 94 extensions::event_names::kBluetoothOnDiscoveringChanged); |
| 95 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 96 router_.OnEventListenerRemoved( |
| 97 extensions::event_names::kBluetoothOnPowerChanged); |
| 98 } |
| 99 |
| 100 TEST_F(ExtensionBluetoothEventRouterTest, NonBluetoothEventListeners) { |
| 101 router_.OnEventListenerAdded(kNonBluetoothEventName); |
| 102 router_.OnEventListenerRemoved(kNonBluetoothEventName); |
| 103 } |
| 104 |
| 105 TEST_F(ExtensionBluetoothEventRouterTest, RegisterSocket) { |
| 106 EXPECT_CALL(*mock_adapter_, AddObserver(testing::_)).Times(1); |
| 107 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 108 int socket_id = router_.RegisterSocket(mock_socket_); |
| 109 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 110 router_.ReleaseSocket(socket_id); |
| 111 } |
| 112 |
| 113 TEST_F(ExtensionBluetoothEventRouterTest, RegisterMultipleSockets) { |
| 114 EXPECT_CALL(*mock_adapter_, AddObserver(testing::_)).Times(1); |
| 115 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 116 int socket_id = router_.RegisterSocket(mock_socket_); |
| 117 EXPECT_CALL(*mock_another_socket_, fd()) |
| 118 .WillRepeatedly(testing::Return(kAnotherSocketId)); |
| 119 int another_socket_id = router_.RegisterSocket(mock_another_socket_); |
| 120 router_.ReleaseSocket(another_socket_id); |
| 121 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 122 router_.ReleaseSocket(socket_id); |
| 123 } |
| 124 |
| 125 TEST_F(ExtensionBluetoothEventRouterTest, RegisterSameSockets) { |
| 126 EXPECT_CALL(*mock_adapter_, AddObserver(testing::_)).Times(1); |
| 127 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 128 int socket_id = router_.RegisterSocket(mock_socket_); |
| 129 socket_id = router_.RegisterSocket(mock_socket_); |
| 130 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 131 router_.ReleaseSocket(socket_id); |
| 132 } |
| 133 |
| 134 TEST_F(ExtensionBluetoothEventRouterTest, AddEventListerAndSocket) { |
| 135 EXPECT_CALL(*mock_adapter_, AddObserver(testing::_)).Times(1); |
| 136 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 137 int socket_id = router_.RegisterSocket(mock_socket_); |
| 138 router_.OnEventListenerAdded( |
| 139 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 140 router_.ReleaseSocket(socket_id); |
| 141 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 142 router_.OnEventListenerRemoved( |
| 143 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 144 } |
| 145 |
| 146 TEST_F(ExtensionBluetoothEventRouterTest, AddSocketAndEventListener) { |
| 147 EXPECT_CALL(*mock_adapter_, AddObserver(testing::_)).Times(1); |
| 148 EXPECT_CALL(*mock_socket_, fd()).WillRepeatedly(testing::Return(kSocketId)); |
| 149 router_.OnEventListenerAdded( |
| 150 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 151 int socket_id = router_.RegisterSocket(mock_socket_); |
| 152 router_.OnEventListenerRemoved( |
| 153 extensions::event_names::kBluetoothOnAvailabilityChanged); |
| 154 EXPECT_CALL(*mock_adapter_, RemoveObserver(testing::_)).Times(1); |
| 155 router_.ReleaseSocket(socket_id); |
| 156 } |
| 157 |
| 158 } // namespace extensions |
OLD | NEW |