Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1177)

Side by Side Diff: device/bluetooth/bluetooth_remote_gatt_characteristic_unittest.cc

Issue 2051333004: Implement BluetoothGattNotifySession::Stop on Android, 2nd attempt (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make the rest of the methods in BluetoothGattNotifySession virtual Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h> 5 #include <stdint.h>
6 #include <utility> 6 #include <utility>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/run_loop.h" 10 #include "base/run_loop.h"
(...skipping 1151 matching lines...) Expand 10 before | Expand all | Expand 10 after
1162 1162
1163 SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr); 1163 SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr);
1164 EXPECT_EQ(0, callback_count_); 1164 EXPECT_EQ(0, callback_count_);
1165 EXPECT_EQ(1, error_callback_count_); 1165 EXPECT_EQ(1, error_callback_count_);
1166 ASSERT_EQ(0u, notify_sessions_.size()); 1166 ASSERT_EQ(0u, notify_sessions_.size());
1167 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, 1167 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
1168 last_gatt_error_code_); 1168 last_gatt_error_code_);
1169 } 1169 }
1170 #endif // defined(OS_ANDROID) 1170 #endif // defined(OS_ANDROID)
1171 1171
1172 #if defined(OS_ANDROID)
ortuno 2016/07/28 21:59:30 How do you feel about splitting the testing in two
tommyt 2016/08/01 12:48:27 I don't really have a strong opinion on this. Ther
ortuno 2016/08/02 01:56:31 I'm a fan of unit tests but scheib prefers integra
1173 // Tests StartNotifySession completing before chrome objects are deleted.
1174 TEST_F(BluetoothRemoteGattCharacteristicTest,
1175 StartNotifySession_BeforeDeleted) {
1176 ASSERT_NO_FATAL_FAILURE(
1177 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1178 SimulateGattDescriptor(
1179 characteristic1_,
1180 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1181 .canonical_value());
1182 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1183
1184 characteristic1_->StartNotifySession(
1185 GetNotifyCallback(Call::EXPECTED),
1186 GetGattErrorCallback(Call::NOT_EXPECTED));
1187 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
1188 EXPECT_EQ(0, callback_count_);
1189
1190 RememberCharacteristicForSubsequentAction(characteristic1_);
1191 RememberCCCDescriptorForSubsequentAction(characteristic1_);
1192
1193 SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr);
1194 ASSERT_EQ(1u, notify_sessions_.size());
1195
1196 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
1197
1198 notify_sessions_[0]->Stop(
1199 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1200 SimulateGattNotifySessionStopped(/* use remembered characteristic */ nullptr);
1201 EXPECT_TRUE("Did not crash!");
1202 ASSERT_EQ(0u, notify_sessions_.size());
1203 }
1204 #endif // defined(OS_ANDROID)
1205
1172 #if defined(OS_MACOSX) || defined(OS_WIN) 1206 #if defined(OS_MACOSX) || defined(OS_WIN)
1173 // Tests StartNotifySession reentrant in start notify session success callback 1207 // Tests StartNotifySession reentrant in start notify session success callback
1174 // and the reentrant start notify session success. 1208 // and the reentrant start notify session success.
1175 TEST_F(BluetoothRemoteGattCharacteristicTest, 1209 TEST_F(BluetoothRemoteGattCharacteristicTest,
1176 StartNotifySession_Reentrant_Success_Success) { 1210 StartNotifySession_Reentrant_Success_Success) {
1177 if (!PlatformSupportsLowEnergy()) { 1211 if (!PlatformSupportsLowEnergy()) {
1178 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1212 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1179 return; 1213 return;
1180 } 1214 }
1181 ASSERT_NO_FATAL_FAILURE( 1215 ASSERT_NO_FATAL_FAILURE(
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1280 // Simulate reentrant StartNotifySession request from 1314 // Simulate reentrant StartNotifySession request from
1281 // BluetoothTestBase::ReentrantStartNotifySessionErrorCallback. 1315 // BluetoothTestBase::ReentrantStartNotifySessionErrorCallback.
1282 SimulateGattNotifySessionStarted(characteristic1_); 1316 SimulateGattNotifySessionStarted(characteristic1_);
1283 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); 1317 EXPECT_EQ(0, gatt_notify_characteristic_attempts_);
1284 EXPECT_EQ(0, callback_count_); 1318 EXPECT_EQ(0, callback_count_);
1285 EXPECT_EQ(2, error_callback_count_); 1319 EXPECT_EQ(2, error_callback_count_);
1286 ASSERT_EQ(0u, notify_sessions_.size()); 1320 ASSERT_EQ(0u, notify_sessions_.size());
1287 } 1321 }
1288 #endif // defined(OS_WIN) 1322 #endif // defined(OS_WIN)
1289 1323
1324 #if defined(OS_ANDROID)
1325 // Tests StopNotifySession success on a characteristic enabling Notify.
1326 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession) {
1327 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1328 /* properties: NOTIFY */ 0x10,
1329 /* expected_config_descriptor_value: NOTIFY */ 1));
1330 notify_sessions_[0]->Stop(
1331 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1332 SimulateGattNotifySessionStopped(characteristic1_);
1333 base::RunLoop().RunUntilIdle();
1334 ASSERT_EQ(0u, notify_sessions_.size());
1335 }
1336 #endif // defined(OS_ANDROID)
1337
1338 #if defined(OS_ANDROID)
1339 // Tests StopNotifySession success on a characteristic enabling Indicate.
1340 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_OnIndicate) {
1341 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1342 /* properties: INDICATE */ 0x20,
1343 /* expected_config_descriptor_value: INDICATE */ 2));
1344 notify_sessions_[0]->Stop(
1345 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1346 SimulateGattNotifySessionStopped(characteristic1_);
1347 base::RunLoop().RunUntilIdle();
1348 ASSERT_EQ(0u, notify_sessions_.size());
1349 }
1350 #endif // defined(OS_ANDROID)
1351
1352 #if defined(OS_ANDROID)
1353 // Tests StopNotifySession success on a characteristic enabling Notify &
1354 // Indicate.
1355 TEST_F(BluetoothRemoteGattCharacteristicTest,
1356 StopNotifySession_OnNotifyAndIndicate) {
1357 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1358 /* properties: INDICATE */ 0x30,
1359 /* expected_config_descriptor_value: INDICATE */ 1));
1360 notify_sessions_[0]->Stop(
1361 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1362 SimulateGattNotifySessionStopped(characteristic1_);
1363 base::RunLoop().RunUntilIdle();
1364 ASSERT_EQ(0u, notify_sessions_.size());
1365 }
1366 #endif // defined(OS_ANDROID)
1367
1368 #if defined(OS_ANDROID)
1369 // Tests StopNotifySession error
1370 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_Error) {
1371 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1372 /* properties: NOTIFY */ 0x10,
1373 /* expected_config_descriptor_value: NOTIFY */ 1));
1374 notify_sessions_[0]->Stop(
1375 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1376 SimulateGattNotifySessionStopError(
1377 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
1378 base::RunLoop().RunUntilIdle();
1379 ASSERT_EQ(0u, notify_sessions_.size());
1380 }
1381 #endif // defined(OS_ANDROID)
1382
1383 #if defined(OS_ANDROID)
1384 // Tests multiple StopNotifySession calls for a single session.
1385 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_Multiple1) {
1386 ASSERT_NO_FATAL_FAILURE(
1387 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1388 SimulateGattDescriptor(
1389 characteristic1_,
1390 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1391 .canonical_value());
1392 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1393
1394 // Start notify session
1395 characteristic1_->StartNotifySession(
1396 GetNotifyCallback(Call::EXPECTED),
1397 GetGattErrorCallback(Call::NOT_EXPECTED));
1398 EXPECT_EQ(0, callback_count_);
1399 SimulateGattNotifySessionStarted(characteristic1_);
1400 base::RunLoop().RunUntilIdle();
1401 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
1402 EXPECT_EQ(1, callback_count_);
1403 EXPECT_EQ(0, error_callback_count_);
1404 ASSERT_EQ(1u, notify_sessions_.size());
1405 ASSERT_TRUE(notify_sessions_[0]);
1406 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1407 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1408
1409 // Stop the notify session twice
1410 notify_sessions_[0]->Stop(
1411 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1412 notify_sessions_[0]->Stop(
1413 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1414 SimulateGattNotifySessionStopped(characteristic1_);
1415 base::RunLoop().RunUntilIdle();
1416 ASSERT_EQ(0u, notify_sessions_.size());
1417 }
1418 #endif // defined(OS_ANDROID)
1419
1420 #if defined(OS_ANDROID)
1421 // Tests multiple StartNotifySession calls and multiple StopNotifySession calls.
1422 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_Multiple2) {
1423 ASSERT_NO_FATAL_FAILURE(
1424 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1425 SimulateGattDescriptor(
1426 characteristic1_,
1427 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1428 .canonical_value());
1429 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1430
1431 // Start notify sessions
1432 characteristic1_->StartNotifySession(
1433 GetNotifyCallback(Call::EXPECTED),
1434 GetGattErrorCallback(Call::NOT_EXPECTED));
1435 characteristic1_->StartNotifySession(
1436 GetNotifyCallback(Call::EXPECTED),
1437 GetGattErrorCallback(Call::NOT_EXPECTED));
1438 EXPECT_EQ(0, callback_count_);
1439 SimulateGattNotifySessionStarted(characteristic1_);
1440 base::RunLoop().RunUntilIdle();
1441 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
1442 EXPECT_EQ(2, callback_count_);
1443 EXPECT_EQ(0, error_callback_count_);
1444 ASSERT_EQ(2u, notify_sessions_.size());
1445 ASSERT_TRUE(notify_sessions_[0]);
1446 ASSERT_TRUE(notify_sessions_[1]);
1447 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1448 EXPECT_EQ(characteristic1_, notify_sessions_[1]->GetCharacteristic());
1449 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1450 EXPECT_TRUE(notify_sessions_[1]->IsActive());
1451
1452 // Stop the notify sessions
1453 notify_sessions_[1]->Stop(
1454 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[1]));
1455 base::RunLoop().RunUntilIdle();
1456 ASSERT_EQ(1u, notify_sessions_.size());
1457
1458 notify_sessions_[0]->Stop(
1459 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1460 SimulateGattNotifySessionStopped(characteristic1_);
1461 base::RunLoop().RunUntilIdle();
1462 ASSERT_EQ(0u, notify_sessions_.size());
1463 }
1464 #endif // defined(OS_ANDROID)
1465
1466 #if defined(OS_ANDROID)
1467 // Tests starting a new notify session before the previous stop request resolves
1468 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_StopStart) {
1469 ASSERT_NO_FATAL_FAILURE(
1470 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1471 SimulateGattDescriptor(
1472 characteristic1_,
1473 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1474 .canonical_value());
1475 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1476
1477 // Start notify session
1478 characteristic1_->StartNotifySession(
1479 GetNotifyCallback(Call::EXPECTED),
1480 GetGattErrorCallback(Call::NOT_EXPECTED));
1481 SimulateGattNotifySessionStarted(characteristic1_);
1482 base::RunLoop().RunUntilIdle();
1483 ASSERT_EQ(1u, notify_sessions_.size());
1484 ASSERT_TRUE(notify_sessions_[0]);
1485 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1486
1487 // Stop the notify session
1488 notify_sessions_[0]->Stop(
1489 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1490
1491 // Start another notify session
1492 characteristic1_->StartNotifySession(
1493 GetNotifyCallback(Call::EXPECTED),
1494 GetGattErrorCallback(Call::NOT_EXPECTED));
1495
1496 SimulateGattNotifySessionStopped(characteristic1_);
1497 base::RunLoop().RunUntilIdle();
1498 ASSERT_EQ(0u, notify_sessions_.size());
1499
1500 SimulateGattNotifySessionStarted(characteristic1_);
1501 base::RunLoop().RunUntilIdle();
1502 ASSERT_EQ(1u, notify_sessions_.size());
1503 ASSERT_TRUE(notify_sessions_[0]);
1504 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1505 }
1506 #endif // defined(OS_ANDROID)
1507
1508 #if defined(OS_ANDROID)
1509 // Tests starting a new notify session before the previous stop request resolves
1510 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_StopStopStart) {
1511 ASSERT_NO_FATAL_FAILURE(
1512 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1513 SimulateGattDescriptor(
1514 characteristic1_,
1515 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1516 .canonical_value());
1517 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1518
1519 // Start notify session
1520 characteristic1_->StartNotifySession(
1521 GetNotifyCallback(Call::EXPECTED),
1522 GetGattErrorCallback(Call::NOT_EXPECTED));
1523 SimulateGattNotifySessionStarted(characteristic1_);
1524 base::RunLoop().RunUntilIdle();
1525 ASSERT_EQ(1u, notify_sessions_.size());
1526 ASSERT_TRUE(notify_sessions_[0]);
1527 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1528
1529 // Stop the notify session twice
1530 notify_sessions_[0]->Stop(
1531 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1532 notify_sessions_[0]->Stop(
1533 GetStopNotifyCallback(Call::EXPECTED, notify_sessions_[0]));
1534
1535 // Start another notify session
1536 characteristic1_->StartNotifySession(
1537 GetNotifyCallback(Call::EXPECTED),
1538 GetGattErrorCallback(Call::NOT_EXPECTED));
1539
1540 SimulateGattNotifySessionStopped(characteristic1_);
1541 base::RunLoop().RunUntilIdle();
1542 ASSERT_EQ(0u, notify_sessions_.size());
1543
1544 SimulateGattNotifySessionStarted(characteristic1_);
1545 base::RunLoop().RunUntilIdle();
1546 ASSERT_EQ(1u, notify_sessions_.size());
1547 ASSERT_TRUE(notify_sessions_[0]);
1548 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1549 }
1550 #endif // defined(OS_ANDROID)
1551
1290 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 1552 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1291 // Tests Characteristic Value changes during a Notify Session. 1553 // Tests Characteristic Value changes during a Notify Session.
1292 TEST_F(BluetoothRemoteGattCharacteristicTest, GattCharacteristicValueChanged) { 1554 TEST_F(BluetoothRemoteGattCharacteristicTest, GattCharacteristicValueChanged) {
1293 if (!PlatformSupportsLowEnergy()) { 1555 if (!PlatformSupportsLowEnergy()) {
1294 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1556 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1295 return; 1557 return;
1296 } 1558 }
1297 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( 1559 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1298 /* properties: NOTIFY */ 0x10, 1560 /* properties: NOTIFY */ 0x10,
1299 /* expected_config_descriptor_value: NOTIFY */ 1)); 1561 /* expected_config_descriptor_value: NOTIFY */ 1));
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
1415 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size()); 1677 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size());
1416 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size()); 1678 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size());
1417 1679
1418 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size()); 1680 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size());
1419 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size()); 1681 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size());
1420 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size()); 1682 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size());
1421 } 1683 }
1422 #endif // defined(OS_ANDROID) || defined(OS_WIN) 1684 #endif // defined(OS_ANDROID) || defined(OS_WIN)
1423 1685
1424 } // namespace device 1686 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698