| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "content/browser/bluetooth/frame_connected_bluetooth_devices.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/memory/ref_counted.h" |
| 9 #include "content/test/test_render_view_host.h" |
| 10 #include "content/test/test_web_contents.h" |
| 11 #include "device/bluetooth/bluetooth_gatt_connection.h" |
| 12 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 13 #include "device/bluetooth/test/mock_bluetooth_device.h" |
| 14 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace content { |
| 19 |
| 20 typedef testing::NiceMock<device::MockBluetoothAdapter> |
| 21 NiceMockBluetoothAdapter; |
| 22 typedef testing::NiceMock<device::MockBluetoothDevice> NiceMockBluetoothDevice; |
| 23 typedef testing::NiceMock<device::MockBluetoothGattConnection> |
| 24 NiceMockBluetoothGattConnection; |
| 25 |
| 26 using testing::Return; |
| 27 using testing::StrEq; |
| 28 using testing::_; |
| 29 |
| 30 namespace { |
| 31 |
| 32 constexpr char kDeviceId0[] = "0"; |
| 33 constexpr char kDeviceAddress0[] = "0"; |
| 34 constexpr char kDeviceName0[] = "Device0"; |
| 35 |
| 36 constexpr char kDeviceId1[] = "1"; |
| 37 constexpr char kDeviceAddress1[] = "1"; |
| 38 constexpr char kDeviceName1[] = "Device1"; |
| 39 |
| 40 class FrameConnectedBluetoothDevicesTest |
| 41 : public RenderViewHostImplTestHarness { |
| 42 public: |
| 43 FrameConnectedBluetoothDevicesTest() |
| 44 : adapter_(new NiceMockBluetoothAdapter()), |
| 45 device0_(adapter_.get(), |
| 46 0 /* class */, |
| 47 kDeviceName0, |
| 48 kDeviceAddress0, |
| 49 false /* paired */, |
| 50 false /* connected */), |
| 51 device1_(adapter_.get(), |
| 52 0 /* class */, |
| 53 kDeviceName1, |
| 54 kDeviceAddress1, |
| 55 false /* paired */, |
| 56 false /* connected */) { |
| 57 ON_CALL(*adapter_, GetDevice(_)).WillByDefault(Return(nullptr)); |
| 58 ON_CALL(*adapter_, GetDevice(StrEq(kDeviceAddress0))) |
| 59 .WillByDefault(Return(&device0_)); |
| 60 ON_CALL(*adapter_, GetDevice(StrEq(kDeviceAddress1))) |
| 61 .WillByDefault(Return(&device1_)); |
| 62 } |
| 63 |
| 64 ~FrameConnectedBluetoothDevicesTest() override {} |
| 65 |
| 66 void SetUp() override { |
| 67 RenderViewHostImplTestHarness::SetUp(); |
| 68 map0_.reset(new FrameConnectedBluetoothDevices(contents()->GetMainFrame())); |
| 69 map1_.reset(new FrameConnectedBluetoothDevices(contents()->GetMainFrame())); |
| 70 } |
| 71 |
| 72 void TearDown() override { |
| 73 map1_.reset(); |
| 74 map0_.reset(); |
| 75 RenderViewHostImplTestHarness::TearDown(); |
| 76 } |
| 77 |
| 78 std::unique_ptr<NiceMockBluetoothGattConnection> GetConnection( |
| 79 const std::string& address) { |
| 80 return base::WrapUnique( |
| 81 new NiceMockBluetoothGattConnection(adapter_.get(), address)); |
| 82 } |
| 83 |
| 84 protected: |
| 85 std::unique_ptr<FrameConnectedBluetoothDevices> map0_; |
| 86 std::unique_ptr<FrameConnectedBluetoothDevices> map1_; |
| 87 |
| 88 private: |
| 89 scoped_refptr<NiceMockBluetoothAdapter> adapter_; |
| 90 NiceMockBluetoothDevice device0_; |
| 91 NiceMockBluetoothDevice device1_; |
| 92 }; |
| 93 |
| 94 } // namespace |
| 95 |
| 96 TEST_F(FrameConnectedBluetoothDevicesTest, Insert_Once) { |
| 97 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 98 |
| 99 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 100 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 101 } |
| 102 |
| 103 TEST_F(FrameConnectedBluetoothDevicesTest, Insert_Twice) { |
| 104 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 105 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 106 |
| 107 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 108 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 109 } |
| 110 |
| 111 TEST_F(FrameConnectedBluetoothDevicesTest, Insert_TwoDevices) { |
| 112 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 113 map0_->Insert(kDeviceId1, GetConnection(kDeviceAddress1)); |
| 114 |
| 115 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 116 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 117 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 118 } |
| 119 |
| 120 TEST_F(FrameConnectedBluetoothDevicesTest, Insert_TwoMaps) { |
| 121 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 122 map1_->Insert(kDeviceId1, GetConnection(kDeviceAddress1)); |
| 123 |
| 124 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 125 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 126 EXPECT_TRUE(map1_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 127 } |
| 128 |
| 129 TEST_F(FrameConnectedBluetoothDevicesTest, |
| 130 CloseConnectionId_OneDevice_AddOnce_RemoveOnce) { |
| 131 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 132 |
| 133 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 134 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 135 |
| 136 map0_->CloseConnectionToDeviceWithId(kDeviceId0); |
| 137 |
| 138 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 139 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 140 } |
| 141 |
| 142 TEST_F(FrameConnectedBluetoothDevicesTest, |
| 143 CloseConnectionId_OneDevice_AddOnce_RemoveTwice) { |
| 144 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 145 |
| 146 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 147 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 148 |
| 149 map0_->CloseConnectionToDeviceWithId(kDeviceId0); |
| 150 map0_->CloseConnectionToDeviceWithId(kDeviceId0); |
| 151 |
| 152 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 153 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 154 } |
| 155 |
| 156 TEST_F(FrameConnectedBluetoothDevicesTest, |
| 157 CloseConnectionId_OneDevice_AddTwice_RemoveOnce) { |
| 158 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 159 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 160 |
| 161 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 162 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 163 |
| 164 map0_->CloseConnectionToDeviceWithId(kDeviceId0); |
| 165 |
| 166 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 167 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 168 } |
| 169 |
| 170 TEST_F(FrameConnectedBluetoothDevicesTest, |
| 171 CloseConnectionId_OneDevice_AddTwice_RemoveTwice) { |
| 172 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 173 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 174 |
| 175 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 176 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 177 |
| 178 map0_->CloseConnectionToDeviceWithId(kDeviceId0); |
| 179 map0_->CloseConnectionToDeviceWithId(kDeviceId0); |
| 180 |
| 181 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 182 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 183 } |
| 184 |
| 185 TEST_F(FrameConnectedBluetoothDevicesTest, CloseConnectionId_TwoDevices) { |
| 186 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 187 map0_->Insert(kDeviceId1, GetConnection(kDeviceAddress1)); |
| 188 |
| 189 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 190 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 191 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 192 |
| 193 map0_->CloseConnectionToDeviceWithId(kDeviceId0); |
| 194 |
| 195 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 196 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 197 |
| 198 map0_->CloseConnectionToDeviceWithId(kDeviceId1); |
| 199 |
| 200 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 201 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 202 } |
| 203 |
| 204 TEST_F(FrameConnectedBluetoothDevicesTest, CloseConnectionId_TwoMaps) { |
| 205 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 206 map1_->Insert(kDeviceId1, GetConnection(kDeviceAddress1)); |
| 207 |
| 208 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 209 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 210 EXPECT_TRUE(map1_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 211 |
| 212 map0_->CloseConnectionToDeviceWithId(kDeviceId0); |
| 213 |
| 214 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 215 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 216 |
| 217 map1_->CloseConnectionToDeviceWithId(kDeviceId1); |
| 218 |
| 219 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 220 EXPECT_FALSE(map1_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 221 } |
| 222 |
| 223 TEST_F(FrameConnectedBluetoothDevicesTest, |
| 224 CloseConnectionAddress_OneDevice_AddOnce_RemoveOnce) { |
| 225 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 226 |
| 227 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 228 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 229 |
| 230 EXPECT_EQ(map0_->CloseConnectionToDeviceWithAddress(kDeviceAddress0), |
| 231 kDeviceId0); |
| 232 |
| 233 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 234 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 235 } |
| 236 |
| 237 TEST_F(FrameConnectedBluetoothDevicesTest, |
| 238 CloseConnectionAddress_OneDevice_AddOnce_RemoveTwice) { |
| 239 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 240 |
| 241 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 242 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 243 |
| 244 EXPECT_EQ(map0_->CloseConnectionToDeviceWithAddress(kDeviceAddress0), |
| 245 kDeviceId0); |
| 246 EXPECT_EQ(map0_->CloseConnectionToDeviceWithAddress(kDeviceAddress0), ""); |
| 247 |
| 248 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 249 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 250 } |
| 251 |
| 252 TEST_F(FrameConnectedBluetoothDevicesTest, |
| 253 CloseConnectionAddress_OneDevice_AddTwice_RemoveOnce) { |
| 254 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 255 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 256 |
| 257 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 258 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 259 |
| 260 EXPECT_EQ(map0_->CloseConnectionToDeviceWithAddress(kDeviceAddress0), |
| 261 kDeviceId0); |
| 262 |
| 263 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 264 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 265 } |
| 266 |
| 267 TEST_F(FrameConnectedBluetoothDevicesTest, |
| 268 CloseConnectionAddress_OneDevice_AddTwice_RemoveTwice) { |
| 269 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 270 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 271 |
| 272 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 273 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 274 |
| 275 EXPECT_EQ(map0_->CloseConnectionToDeviceWithAddress(kDeviceAddress0), |
| 276 kDeviceId0); |
| 277 EXPECT_EQ(map0_->CloseConnectionToDeviceWithAddress(kDeviceAddress0), ""); |
| 278 |
| 279 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 280 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 281 } |
| 282 |
| 283 TEST_F(FrameConnectedBluetoothDevicesTest, CloseConnectionAddress_TwoDevices) { |
| 284 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 285 map0_->Insert(kDeviceId1, GetConnection(kDeviceAddress1)); |
| 286 |
| 287 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 288 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 289 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 290 |
| 291 EXPECT_EQ(map0_->CloseConnectionToDeviceWithAddress(kDeviceAddress0), |
| 292 kDeviceId0); |
| 293 |
| 294 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 295 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 296 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 297 |
| 298 EXPECT_EQ(map0_->CloseConnectionToDeviceWithAddress(kDeviceAddress1), |
| 299 kDeviceId1); |
| 300 |
| 301 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 302 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 303 } |
| 304 |
| 305 TEST_F(FrameConnectedBluetoothDevicesTest, CloseConnectionAddress_TwoMaps) { |
| 306 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 307 map1_->Insert(kDeviceId1, GetConnection(kDeviceAddress1)); |
| 308 |
| 309 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 310 EXPECT_TRUE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 311 EXPECT_TRUE(map1_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 312 |
| 313 EXPECT_EQ(map0_->CloseConnectionToDeviceWithAddress(kDeviceAddress0), |
| 314 kDeviceId0); |
| 315 |
| 316 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 317 EXPECT_FALSE(map0_->IsConnectedToDeviceWithId(kDeviceId0)); |
| 318 EXPECT_TRUE(map1_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 319 |
| 320 EXPECT_EQ(map1_->CloseConnectionToDeviceWithAddress(kDeviceAddress1), |
| 321 kDeviceId1); |
| 322 |
| 323 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 324 EXPECT_FALSE(map1_->IsConnectedToDeviceWithId(kDeviceId1)); |
| 325 } |
| 326 |
| 327 TEST_F(FrameConnectedBluetoothDevicesTest, Destruction_MultipleDevices) { |
| 328 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 329 map0_->Insert(kDeviceId1, GetConnection(kDeviceAddress1)); |
| 330 |
| 331 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 332 |
| 333 map0_.reset(); |
| 334 |
| 335 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 336 } |
| 337 |
| 338 TEST_F(FrameConnectedBluetoothDevicesTest, Destruction_MultipleMaps) { |
| 339 map0_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 340 map0_->Insert(kDeviceId1, GetConnection(kDeviceAddress1)); |
| 341 |
| 342 map1_->Insert(kDeviceId0, GetConnection(kDeviceAddress0)); |
| 343 map1_->Insert(kDeviceId1, GetConnection(kDeviceAddress1)); |
| 344 |
| 345 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 346 |
| 347 map0_.reset(); |
| 348 |
| 349 // WebContents should still be connected because of map1_. |
| 350 EXPECT_TRUE(contents()->IsConnectedToBluetoothDevice()); |
| 351 |
| 352 map1_.reset(); |
| 353 |
| 354 EXPECT_FALSE(contents()->IsConnectedToBluetoothDevice()); |
| 355 } |
| 356 |
| 357 } // namespace content |
| OLD | NEW |