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