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

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: Fix a silly mistake in commit e4725886 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 1004 matching lines...) Expand 10 before | Expand all | Expand 10 after
1015 EXPECT_EQ(0, error_callback_count_); 1015 EXPECT_EQ(0, error_callback_count_);
1016 base::RunLoop().RunUntilIdle(); 1016 base::RunLoop().RunUntilIdle();
1017 EXPECT_EQ(1, error_callback_count_); 1017 EXPECT_EQ(1, error_callback_count_);
1018 1018
1019 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); 1019 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
1020 ASSERT_EQ(0u, notify_sessions_.size()); 1020 ASSERT_EQ(0u, notify_sessions_.size());
1021 } 1021 }
1022 #endif // defined(OS_ANDROID) 1022 #endif // defined(OS_ANDROID)
1023 1023
1024 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 1024 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1025 // Tests StartNotifySession success on a characteristic enabling Notify. 1025 // Tests StartNotifySession success on a characteristic that enabled Notify.
1026 TEST_F(BluetoothRemoteGattCharacteristicTest, StartNotifySession) { 1026 TEST_F(BluetoothRemoteGattCharacteristicTest, StartNotifySession) {
1027 if (!PlatformSupportsLowEnergy()) { 1027 if (!PlatformSupportsLowEnergy()) {
1028 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1028 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1029 return; 1029 return;
1030 } 1030 }
1031 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( 1031 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1032 /* properties: NOTIFY */ 0x10, 1032 /* properties: NOTIFY */ 0x10,
1033 /* expected_config_descriptor_value: NOTIFY */ 1)); 1033 /* expected_config_descriptor_value: NOTIFY */ 1));
1034 } 1034 }
1035 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 1035 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1036 1036
1037 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 1037 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1038 // Tests StartNotifySession success on a characteristic enabling Indicate. 1038 // Tests StartNotifySession success on a characteristic that enabled Indicate.
1039 TEST_F(BluetoothRemoteGattCharacteristicTest, StartNotifySession_OnIndicate) { 1039 TEST_F(BluetoothRemoteGattCharacteristicTest, StartNotifySession_OnIndicate) {
1040 if (!PlatformSupportsLowEnergy()) { 1040 if (!PlatformSupportsLowEnergy()) {
1041 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1041 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1042 return; 1042 return;
1043 } 1043 }
1044 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( 1044 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1045 /* properties: INDICATE */ 0x20, 1045 /* properties: INDICATE */ 0x20,
1046 /* expected_config_descriptor_value: INDICATE */ 2)); 1046 /* expected_config_descriptor_value: INDICATE */ 2));
1047 } 1047 }
1048 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 1048 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1049 1049
1050 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 1050 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1051 // Tests StartNotifySession success on a characteristic enabling Notify & 1051 // Tests StartNotifySession success on a characteristic that enabled Notify &
1052 // Indicate. 1052 // Indicate.
1053 TEST_F(BluetoothRemoteGattCharacteristicTest, 1053 TEST_F(BluetoothRemoteGattCharacteristicTest,
1054 StartNotifySession_OnNotifyAndIndicate) { 1054 StartNotifySession_OnNotifyAndIndicate) {
1055 if (!PlatformSupportsLowEnergy()) { 1055 if (!PlatformSupportsLowEnergy()) {
1056 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1056 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1057 return; 1057 return;
1058 } 1058 }
1059 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( 1059 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1060 /* properties: NOTIFY and INDICATE bits set */ 0x30, 1060 /* properties: NOTIFY and INDICATE bits set */ 0x30,
1061 /* expected_config_descriptor_value: NOTIFY */ 1)); 1061 /* expected_config_descriptor_value: NOTIFY */ 1));
(...skipping 19 matching lines...) Expand all
1081 #endif // !defined(OS_MACOSX) 1081 #endif // !defined(OS_MACOSX)
1082 1082
1083 characteristic1_->StartNotifySession( 1083 characteristic1_->StartNotifySession(
1084 GetNotifyCallback(Call::EXPECTED), 1084 GetNotifyCallback(Call::EXPECTED),
1085 GetGattErrorCallback(Call::NOT_EXPECTED)); 1085 GetGattErrorCallback(Call::NOT_EXPECTED));
1086 characteristic1_->StartNotifySession( 1086 characteristic1_->StartNotifySession(
1087 GetNotifyCallback(Call::EXPECTED), 1087 GetNotifyCallback(Call::EXPECTED),
1088 GetGattErrorCallback(Call::NOT_EXPECTED)); 1088 GetGattErrorCallback(Call::NOT_EXPECTED));
1089 EXPECT_EQ(0, callback_count_); 1089 EXPECT_EQ(0, callback_count_);
1090 SimulateGattNotifySessionStarted(characteristic1_); 1090 SimulateGattNotifySessionStarted(characteristic1_);
1091 base::RunLoop().RunUntilIdle();
1091 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); 1092 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
1092 EXPECT_EQ(2, callback_count_); 1093 EXPECT_EQ(2, callback_count_);
1093 EXPECT_EQ(0, error_callback_count_); 1094 EXPECT_EQ(0, error_callback_count_);
1094 ASSERT_EQ(2u, notify_sessions_.size()); 1095 ASSERT_EQ(2u, notify_sessions_.size());
1095 ASSERT_TRUE(notify_sessions_[0]); 1096 ASSERT_TRUE(notify_sessions_[0]);
1096 ASSERT_TRUE(notify_sessions_[1]); 1097 ASSERT_TRUE(notify_sessions_[1]);
1097 EXPECT_EQ(characteristic1_->GetIdentifier(), 1098 EXPECT_EQ(characteristic1_->GetIdentifier(),
1098 notify_sessions_[0]->GetCharacteristicIdentifier()); 1099 notify_sessions_[0]->GetCharacteristicIdentifier());
1099 EXPECT_EQ(characteristic1_->GetIdentifier(), 1100 EXPECT_EQ(characteristic1_->GetIdentifier(),
1100 notify_sessions_[1]->GetCharacteristicIdentifier()); 1101 notify_sessions_[1]->GetCharacteristicIdentifier());
(...skipping 22 matching lines...) Expand all
1123 #endif // !defined(OS_MACOSX) 1124 #endif // !defined(OS_MACOSX)
1124 1125
1125 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), 1126 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
1126 GetGattErrorCallback(Call::EXPECTED)); 1127 GetGattErrorCallback(Call::EXPECTED));
1127 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED), 1128 characteristic1_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
1128 GetGattErrorCallback(Call::EXPECTED)); 1129 GetGattErrorCallback(Call::EXPECTED));
1129 EXPECT_EQ(1, gatt_notify_characteristic_attempts_); 1130 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
1130 EXPECT_EQ(0, callback_count_); 1131 EXPECT_EQ(0, callback_count_);
1131 SimulateGattNotifySessionStartError( 1132 SimulateGattNotifySessionStartError(
1132 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_FAILED); 1133 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_FAILED);
1134 base::RunLoop().RunUntilIdle();
1133 EXPECT_EQ(0, callback_count_); 1135 EXPECT_EQ(0, callback_count_);
1134 EXPECT_EQ(2, error_callback_count_); 1136 EXPECT_EQ(2, error_callback_count_);
1135 ASSERT_EQ(0u, notify_sessions_.size()); 1137 ASSERT_EQ(0u, notify_sessions_.size());
1136 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, 1138 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
1137 last_gatt_error_code_); 1139 last_gatt_error_code_);
1138 } 1140 }
1139 #endif // defined(OS_ANDROID) || defined(OS_MACOSX) 1141 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
1140 1142
1141 #if defined(OS_ANDROID) 1143 #if defined(OS_ANDROID)
1142 // Tests StartNotifySession completing after chrome objects are deleted. 1144 // Tests StartNotifySession completing after chrome objects are deleted.
(...skipping 19 matching lines...) Expand all
1162 1164
1163 SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr); 1165 SimulateGattNotifySessionStarted(/* use remembered characteristic */ nullptr);
1164 EXPECT_EQ(0, callback_count_); 1166 EXPECT_EQ(0, callback_count_);
1165 EXPECT_EQ(1, error_callback_count_); 1167 EXPECT_EQ(1, error_callback_count_);
1166 ASSERT_EQ(0u, notify_sessions_.size()); 1168 ASSERT_EQ(0u, notify_sessions_.size());
1167 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED, 1169 EXPECT_EQ(BluetoothRemoteGattService::GATT_ERROR_FAILED,
1168 last_gatt_error_code_); 1170 last_gatt_error_code_);
1169 } 1171 }
1170 #endif // defined(OS_ANDROID) 1172 #endif // defined(OS_ANDROID)
1171 1173
1174 #if defined(OS_ANDROID)
1175 // Tests StartNotifySession completing before chrome objects are deleted.
1176 TEST_F(BluetoothRemoteGattCharacteristicTest,
1177 StartNotifySession_BeforeDeleted) {
1178 ASSERT_NO_FATAL_FAILURE(
1179 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1180 SimulateGattDescriptor(
1181 characteristic1_,
1182 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1183 .canonical_value());
1184 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1185
1186 characteristic1_->StartNotifySession(
1187 GetNotifyCallback(Call::EXPECTED),
1188 GetGattErrorCallback(Call::NOT_EXPECTED));
1189 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
1190 EXPECT_EQ(0, callback_count_);
1191
1192 SimulateGattNotifySessionStarted(characteristic1_);
1193 ASSERT_EQ(1u, notify_sessions_.size());
1194
1195 std::string characteristic_identifier = characteristic1_->GetIdentifier();
1196
1197 EXPECT_EQ(characteristic_identifier,
1198 notify_sessions_[0]->GetCharacteristicIdentifier());
1199 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1200 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1201
1202 RememberCharacteristicForSubsequentAction(characteristic1_);
1203 RememberCCCDescriptorForSubsequentAction(characteristic1_);
1204
1205 DeleteDevice(device_); // TODO(576906) delete only the characteristic.
1206
1207 notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
1208 SimulateGattNotifySessionStopped(/* use remembered characteristic */ nullptr);
1209 EXPECT_TRUE("Did not crash!");
1210 ASSERT_TRUE(notify_sessions_[0]);
1211 EXPECT_EQ(characteristic_identifier,
1212 notify_sessions_[0]->GetCharacteristicIdentifier());
1213 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1214 }
1215 #endif // defined(OS_ANDROID)
1216
1172 #if defined(OS_MACOSX) || defined(OS_WIN) 1217 #if defined(OS_MACOSX) || defined(OS_WIN)
1173 // Tests StartNotifySession reentrant in start notify session success callback 1218 // Tests StartNotifySession reentrant in start notify session success callback
1174 // and the reentrant start notify session success. 1219 // and the reentrant start notify session success.
1175 TEST_F(BluetoothRemoteGattCharacteristicTest, 1220 TEST_F(BluetoothRemoteGattCharacteristicTest,
1176 StartNotifySession_Reentrant_Success_Success) { 1221 StartNotifySession_Reentrant_Success_Success) {
1177 if (!PlatformSupportsLowEnergy()) { 1222 if (!PlatformSupportsLowEnergy()) {
1178 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1223 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1179 return; 1224 return;
1180 } 1225 }
1181 ASSERT_NO_FATAL_FAILURE( 1226 ASSERT_NO_FATAL_FAILURE(
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
1280 // Simulate reentrant StartNotifySession request from 1325 // Simulate reentrant StartNotifySession request from
1281 // BluetoothTestBase::ReentrantStartNotifySessionErrorCallback. 1326 // BluetoothTestBase::ReentrantStartNotifySessionErrorCallback.
1282 SimulateGattNotifySessionStarted(characteristic1_); 1327 SimulateGattNotifySessionStarted(characteristic1_);
1283 EXPECT_EQ(0, gatt_notify_characteristic_attempts_); 1328 EXPECT_EQ(0, gatt_notify_characteristic_attempts_);
1284 EXPECT_EQ(0, callback_count_); 1329 EXPECT_EQ(0, callback_count_);
1285 EXPECT_EQ(2, error_callback_count_); 1330 EXPECT_EQ(2, error_callback_count_);
1286 ASSERT_EQ(0u, notify_sessions_.size()); 1331 ASSERT_EQ(0u, notify_sessions_.size());
1287 } 1332 }
1288 #endif // defined(OS_WIN) 1333 #endif // defined(OS_WIN)
1289 1334
1335 #if defined(OS_ANDROID)
1336 // Tests StopNotifySession success on a characteristic that enabled Notify.
1337 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession) {
1338 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1339 /* properties: NOTIFY */ 0x10,
1340 /* expected_config_descriptor_value: NOTIFY */ 1));
1341 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
1342
1343 notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
1344 SimulateGattNotifySessionStopped(characteristic1_);
1345 base::RunLoop().RunUntilIdle();
1346
1347 // Check that the right values were written to the descriptor.
1348 EXPECT_EQ(2, gatt_write_descriptor_attempts_);
1349 ASSERT_EQ(2u, last_write_value_.size());
1350 EXPECT_EQ(0, last_write_value_[0]);
1351 EXPECT_EQ(0, last_write_value_[1]);
1352
1353 // Check that the notify session is inactive.
1354 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1355 EXPECT_FALSE(characteristic1_->IsNotifying());
1356 }
1357 #endif // defined(OS_ANDROID)
1358
1359 #if defined(OS_ANDROID)
1360 // Tests that deleted sessions are stopped.
1361 TEST_F(BluetoothRemoteGattCharacteristicTest,
1362 StopNotifySession_SessionDeleted) {
1363 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1364 /* properties: NOTIFY */ 0x10,
1365 /* expected_config_descriptor_value: NOTIFY */ 1));
1366 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
1367
1368 notify_sessions_.clear();
1369 SimulateGattNotifySessionStopped(characteristic1_);
1370 base::RunLoop().RunUntilIdle();
1371
1372 // Check that the right values were written to the descriptor.
1373 EXPECT_EQ(2, gatt_write_descriptor_attempts_);
1374 ASSERT_EQ(2u, last_write_value_.size());
1375 EXPECT_EQ(0, last_write_value_[0]);
1376 EXPECT_EQ(0, last_write_value_[1]);
1377
1378 // Check that the notify session is inactive.
1379 EXPECT_FALSE(characteristic1_->IsNotifying());
1380 }
1381 #endif // defined(OS_ANDROID)
1382
1383 #if defined(OS_ANDROID)
1384 // Tests StopNotifySession success on a characteristic that enabled Indicate.
1385 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_OnIndicate) {
1386 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1387 /* properties: INDICATE */ 0x20,
1388 /* expected_config_descriptor_value: INDICATE */ 2));
1389 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
1390
1391 notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
1392 SimulateGattNotifySessionStopped(characteristic1_);
1393 base::RunLoop().RunUntilIdle();
1394
1395 // Check that the right values were written to the descriptor.
1396 EXPECT_EQ(2, gatt_write_descriptor_attempts_);
1397 ASSERT_EQ(2u, last_write_value_.size());
1398 EXPECT_EQ(0, last_write_value_[0]);
1399 EXPECT_EQ(0, last_write_value_[1]);
1400
1401 // Check that the notify session is inactive.
1402 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1403 EXPECT_FALSE(characteristic1_->IsNotifying());
1404 }
1405 #endif // defined(OS_ANDROID)
1406
1407 #if defined(OS_ANDROID)
1408 // Tests StopNotifySession success on a characteristic that enabled Notify &
1409 // Indicate.
1410 TEST_F(BluetoothRemoteGattCharacteristicTest,
1411 StopNotifySession_OnNotifyAndIndicate) {
1412 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1413 /* properties: NOTIFY and INDICATE bits set */ 0x30,
1414 /* expected_config_descriptor_value: INDICATE */ 1));
1415 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
1416
1417 notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
1418 SimulateGattNotifySessionStopped(characteristic1_);
1419 base::RunLoop().RunUntilIdle();
1420
1421 // Check that the right values were written to the descriptor.
1422 EXPECT_EQ(2, gatt_write_descriptor_attempts_);
1423 ASSERT_EQ(2u, last_write_value_.size());
1424 EXPECT_EQ(0, last_write_value_[0]);
1425 EXPECT_EQ(0, last_write_value_[1]);
1426
1427 // Check that the notify session is inactive.
1428 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1429 EXPECT_FALSE(characteristic1_->IsNotifying());
1430 }
1431 #endif // defined(OS_ANDROID)
1432
1433 #if defined(OS_ANDROID)
1434 // Tests StopNotifySession error
1435 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_Error) {
1436 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1437 /* properties: NOTIFY */ 0x10,
1438 /* expected_config_descriptor_value: NOTIFY */ 1));
1439
1440 // Check that the notify session is active.
1441 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1442 EXPECT_EQ(characteristic1_->GetIdentifier(),
1443 notify_sessions_[0]->GetCharacteristicIdentifier());
1444 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1445 EXPECT_TRUE(characteristic1_->IsNotifying());
1446
1447 notify_sessions_[0]->Stop(GetStopNotifyCallback(Call::EXPECTED));
1448 SimulateGattNotifySessionStopError(
1449 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_UNKNOWN);
1450 base::RunLoop().RunUntilIdle();
1451
1452 // Check that the notify session is inactive.
1453 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1454 EXPECT_FALSE(characteristic1_->IsNotifying());
1455 }
1456 #endif // defined(OS_ANDROID)
1457
1458 #if defined(OS_ANDROID)
1459 // Tests multiple StopNotifySession calls for a single session.
1460 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_Multiple1) {
1461 ASSERT_NO_FATAL_FAILURE(
1462 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1463
1464 #if !defined(OS_MACOSX)
1465 // TODO(624017) enable for macosx
1466 SimulateGattDescriptor(
1467 characteristic1_,
1468 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1469 .canonical_value());
1470 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1471 #endif
1472
1473 // Start notify session
1474 characteristic1_->StartNotifySession(
1475 GetNotifyCallback(Call::EXPECTED),
1476 GetGattErrorCallback(Call::NOT_EXPECTED));
1477 EXPECT_EQ(0, callback_count_);
1478 SimulateGattNotifySessionStarted(characteristic1_);
1479 base::RunLoop().RunUntilIdle();
1480 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
1481 EXPECT_EQ(1, callback_count_);
1482 EXPECT_EQ(0, error_callback_count_);
1483 ASSERT_EQ(1u, notify_sessions_.size());
1484 ASSERT_TRUE(notify_sessions_[0]);
1485 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1486 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1487 EXPECT_TRUE(characteristic1_->IsNotifying());
1488
1489 // Stop the notify session twice
1490 ResetEventCounts();
1491 notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(0));
1492 notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(1));
1493 SimulateGattNotifySessionStopped(characteristic1_);
1494 base::RunLoop().RunUntilIdle();
1495
1496 // Check that the notify session is inactive.
1497 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1498 EXPECT_FALSE(characteristic1_->IsNotifying());
1499 }
1500 #endif // defined(OS_ANDROID)
1501
1502 #if defined(OS_ANDROID)
1503 // Tests multiple StartNotifySession calls and multiple StopNotifySession calls.
1504 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_Multiple2) {
1505 ASSERT_NO_FATAL_FAILURE(
1506 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1507
1508 #if !defined(OS_MACOSX)
1509 // TODO(624017) enable for macosx
1510 SimulateGattDescriptor(
1511 characteristic1_,
1512 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1513 .canonical_value());
1514 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1515 #endif
1516
1517 // Start notify sessions
1518 characteristic1_->StartNotifySession(
1519 GetNotifyCallback(Call::EXPECTED),
1520 GetGattErrorCallback(Call::NOT_EXPECTED));
1521 characteristic1_->StartNotifySession(
1522 GetNotifyCallback(Call::EXPECTED),
1523 GetGattErrorCallback(Call::NOT_EXPECTED));
1524 EXPECT_EQ(0, callback_count_);
1525 SimulateGattNotifySessionStarted(characteristic1_);
1526 base::RunLoop().RunUntilIdle();
1527 EXPECT_EQ(1, gatt_notify_characteristic_attempts_);
1528 EXPECT_EQ(2, callback_count_);
1529 EXPECT_EQ(0, error_callback_count_);
1530 ASSERT_EQ(2u, notify_sessions_.size());
1531 ASSERT_TRUE(notify_sessions_[0]);
1532 ASSERT_TRUE(notify_sessions_[1]);
1533 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1534 EXPECT_EQ(characteristic1_, notify_sessions_[1]->GetCharacteristic());
1535 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1536 EXPECT_TRUE(notify_sessions_[1]->IsActive());
1537 EXPECT_TRUE(characteristic1_->IsNotifying());
1538
1539 // Stop the notify sessions
1540 ResetEventCounts();
1541 notify_sessions_[1]->Stop(GetStopNotifyCheckForPrecedingCalls(0));
1542 base::RunLoop().RunUntilIdle();
1543 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1544 EXPECT_FALSE(notify_sessions_[1]->IsActive());
1545 EXPECT_TRUE(characteristic1_->IsNotifying());
1546
1547 notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(1));
1548 SimulateGattNotifySessionStopped(characteristic1_);
1549 base::RunLoop().RunUntilIdle();
1550
1551 // Check that the notify sessions is inactive.
1552 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1553 EXPECT_FALSE(notify_sessions_[1]->IsActive());
1554 EXPECT_FALSE(characteristic1_->IsNotifying());
1555 }
1556 #endif // defined(OS_ANDROID)
1557
1558 #if defined(OS_ANDROID)
1559 // Tests starting a new notify session before the previous stop request
1560 // resolves.
1561 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_StopStart) {
1562 ASSERT_NO_FATAL_FAILURE(
1563 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1564 SimulateGattDescriptor(
1565 characteristic1_,
1566 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1567 .canonical_value());
1568 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1569
1570 // Start notify session
1571 ResetEventCounts();
1572 characteristic1_->StartNotifySession(
1573 GetNotifyCheckForPrecedingCalls(0),
1574 GetGattErrorCallback(Call::NOT_EXPECTED));
1575 SimulateGattNotifySessionStarted(characteristic1_);
1576 base::RunLoop().RunUntilIdle();
1577 ASSERT_EQ(1u, notify_sessions_.size());
1578 ASSERT_TRUE(notify_sessions_[0]);
1579 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1580
1581 // Stop the notify session
1582 notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(1));
1583
1584 // Start another notify session
1585 characteristic1_->StartNotifySession(
1586 GetNotifyCheckForPrecedingCalls(2),
1587 GetGattErrorCallback(Call::NOT_EXPECTED));
1588
1589 SimulateGattNotifySessionStopped(characteristic1_);
1590 base::RunLoop().RunUntilIdle();
1591 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1592
1593 SimulateGattNotifySessionStarted(characteristic1_);
1594 base::RunLoop().RunUntilIdle();
1595 ASSERT_EQ(2u, notify_sessions_.size());
1596 ASSERT_TRUE(notify_sessions_[0]);
1597 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1598 ASSERT_TRUE(notify_sessions_[1]);
1599 EXPECT_TRUE(notify_sessions_[1]->IsActive());
1600 EXPECT_TRUE(characteristic1_->IsNotifying());
1601 }
1602 #endif // defined(OS_ANDROID)
1603
1604 #if defined(OS_ANDROID)
1605 TEST_F(BluetoothRemoteGattCharacteristicTest,
1606 StopNotifySession_StartStopStart) {
1607 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1608 /* properties: NOTIFY */ 0x10,
1609 /* expected_config_descriptor_value: NOTIFY */ 1));
1610
1611 // Check that the initial notify session is active.
1612 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1613 EXPECT_EQ(characteristic1_->GetIdentifier(),
1614 notify_sessions_[0]->GetCharacteristicIdentifier());
1615 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1616 EXPECT_TRUE(characteristic1_->IsNotifying());
1617
1618 // Queue up the first event.
1619 ResetEventCounts();
1620 characteristic1_->StartNotifySession(
1621 GetNotifyCheckForPrecedingCalls(0),
1622 GetGattErrorCallback(Call::NOT_EXPECTED));
1623
1624 // Queue up the second event.
1625 notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(1));
1626
1627 // Queue up the third event.
1628 characteristic1_->StartNotifySession(
1629 GetNotifyCheckForPrecedingCalls(2),
1630 GetGattErrorCallback(Call::NOT_EXPECTED));
1631
1632 // Run the event loop to resolve all three events.
1633 base::RunLoop().RunUntilIdle();
1634
1635 // Check the state of all the sessions.
1636 ASSERT_EQ(3u, notify_sessions_.size());
1637 EXPECT_EQ(characteristic1_->GetIdentifier(),
1638 notify_sessions_[0]->GetCharacteristicIdentifier());
1639 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1640 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1641
1642 EXPECT_EQ(characteristic1_->GetIdentifier(),
1643 notify_sessions_[1]->GetCharacteristicIdentifier());
1644 EXPECT_EQ(characteristic1_, notify_sessions_[1]->GetCharacteristic());
1645 EXPECT_TRUE(notify_sessions_[1]->IsActive());
1646
1647 EXPECT_EQ(characteristic1_->GetIdentifier(),
1648 notify_sessions_[2]->GetCharacteristicIdentifier());
1649 EXPECT_EQ(characteristic1_, notify_sessions_[2]->GetCharacteristic());
1650 EXPECT_TRUE(notify_sessions_[2]->IsActive());
1651
1652 EXPECT_TRUE(characteristic1_->IsNotifying());
1653 }
1654 #endif
1655
1656 #if defined(OS_ANDROID)
1657 // Tests starting a new notify session before the previous stop requests
1658 // resolve.
1659 TEST_F(BluetoothRemoteGattCharacteristicTest, StopNotifySession_StopStopStart) {
1660 ASSERT_NO_FATAL_FAILURE(
1661 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1662 SimulateGattDescriptor(
1663 characteristic1_,
1664 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1665 .canonical_value());
1666 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1667
1668 // Start notify session
1669 ResetEventCounts();
1670 characteristic1_->StartNotifySession(
1671 GetNotifyCheckForPrecedingCalls(0),
1672 GetGattErrorCallback(Call::NOT_EXPECTED));
1673 SimulateGattNotifySessionStarted(characteristic1_);
1674 base::RunLoop().RunUntilIdle();
1675 ASSERT_EQ(1u, notify_sessions_.size());
1676 ASSERT_TRUE(notify_sessions_[0]);
1677 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1678 EXPECT_EQ(1, gatt_write_descriptor_attempts_);
1679
1680 // Stop the notify session twice
1681 notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(1));
1682 notify_sessions_[0]->Stop(GetStopNotifyCheckForPrecedingCalls(2));
1683
1684 // Check that the right values were written to the descriptor.
1685 EXPECT_EQ(2, gatt_write_descriptor_attempts_);
1686 ASSERT_EQ(2u, last_write_value_.size());
1687 EXPECT_EQ(0, last_write_value_[0]);
1688 EXPECT_EQ(0, last_write_value_[1]);
1689
1690 // Start another notify session
1691 characteristic1_->StartNotifySession(
1692 GetNotifyCheckForPrecedingCalls(3),
1693 GetGattErrorCallback(Call::NOT_EXPECTED));
1694
1695 // Check that nothing was written by the StartNotifySession call above
1696 EXPECT_EQ(2, gatt_write_descriptor_attempts_);
1697
1698 SimulateGattNotifySessionStopped(characteristic1_);
1699 base::RunLoop().RunUntilIdle();
1700 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1701
1702 SimulateGattNotifySessionStarted(characteristic1_);
1703 base::RunLoop().RunUntilIdle();
1704
1705 // Check that the right values were written to the descriptor.
1706 EXPECT_EQ(3, gatt_write_descriptor_attempts_);
1707 ASSERT_EQ(2u, last_write_value_.size());
1708 EXPECT_EQ(1, last_write_value_[0]);
1709 EXPECT_EQ(0, last_write_value_[1]);
1710
1711 // Check the notify state
1712 ASSERT_EQ(2u, notify_sessions_.size());
1713 ASSERT_TRUE(notify_sessions_[0]);
1714 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1715 ASSERT_TRUE(notify_sessions_[1]);
1716 EXPECT_TRUE(notify_sessions_[1]->IsActive());
1717 EXPECT_TRUE(characteristic1_->IsNotifying());
1718 }
1719 #endif // defined(OS_ANDROID)
1720
1721 #if defined(OS_ANDROID)
1722 TEST_F(BluetoothRemoteGattCharacteristicTest,
1723 StopNotifySession_Reentrant_Success_Stop) {
1724 ASSERT_NO_FATAL_FAILURE(
1725 FakeCharacteristicBoilerplate(/* properties: NOTIFY */ 0x10));
1726 SimulateGattDescriptor(
1727 characteristic1_,
1728 BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
1729 .canonical_value());
1730 ASSERT_EQ(1u, characteristic1_->GetDescriptors().size());
1731
1732 // Start notify session
1733 characteristic1_->StartNotifySession(
1734 base::Bind(
1735 [](BluetoothRemoteGattCharacteristic::NotifySessionCallback
1736 notifyCallback,
1737 base::Closure stopNotifyCallback,
1738 std::unique_ptr<BluetoothGattNotifySession> session) {
1739 BluetoothGattNotifySession* s = session.get();
1740 notifyCallback.Run(std::move(session));
1741 s->Stop(stopNotifyCallback);
1742 },
1743 GetNotifyCallback(Call::EXPECTED),
1744 GetStopNotifyCallback(Call::EXPECTED)),
1745 GetGattErrorCallback(Call::NOT_EXPECTED));
1746
1747 SimulateGattNotifySessionStarted(characteristic1_);
1748 base::RunLoop().RunUntilIdle();
1749 ASSERT_EQ(1u, notify_sessions_.size());
1750 ASSERT_TRUE(notify_sessions_[0]);
1751 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1752 EXPECT_TRUE(characteristic1_->IsNotifying());
1753
1754 SimulateGattNotifySessionStopped(characteristic1_);
1755 base::RunLoop().RunUntilIdle();
1756 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1757 EXPECT_FALSE(characteristic1_->IsNotifying());
1758 }
1759 #endif
1760
1761 #if defined(OS_ANDROID)
1762 TEST_F(BluetoothRemoteGattCharacteristicTest,
1763 StopNotifySession_Reentrant_Stop_StartSuccess) {
1764 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1765 /* properties: NOTIFY */ 0x10,
1766 /* expected_config_descriptor_value: NOTIFY */ 1));
1767
1768 // Check that the notify session is active.
1769 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1770 EXPECT_EQ(characteristic1_->GetIdentifier(),
1771 notify_sessions_[0]->GetCharacteristicIdentifier());
1772 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1773 EXPECT_TRUE(characteristic1_->IsNotifying());
1774
1775 notify_sessions_[0]->Stop(base::Bind(
1776 [](BluetoothRemoteGattCharacteristic* characteristic,
1777 BluetoothRemoteGattCharacteristic::NotifySessionCallback
1778 notifyCallback,
1779 BluetoothRemoteGattCharacteristic::ErrorCallback errorCallback) {
1780 characteristic->StartNotifySession(notifyCallback, errorCallback);
1781 },
1782 characteristic1_, GetNotifyCallback(Call::EXPECTED),
1783 GetGattErrorCallback(Call::NOT_EXPECTED)));
1784
1785 SimulateGattNotifySessionStopped(characteristic1_);
1786 base::RunLoop().RunUntilIdle();
1787 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1788 EXPECT_FALSE(characteristic1_->IsNotifying());
1789
1790 SimulateGattNotifySessionStarted(characteristic1_);
1791 base::RunLoop().RunUntilIdle();
1792 ASSERT_EQ(2u, notify_sessions_.size());
1793 ASSERT_TRUE(notify_sessions_[0]);
1794 ASSERT_TRUE(notify_sessions_[1]);
1795 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1796 EXPECT_EQ(characteristic1_, notify_sessions_[1]->GetCharacteristic());
1797 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1798 EXPECT_TRUE(notify_sessions_[1]->IsActive());
1799 EXPECT_TRUE(characteristic1_->IsNotifying());
1800 }
1801 #endif
1802
1803 #if defined(OS_ANDROID)
1804 TEST_F(BluetoothRemoteGattCharacteristicTest,
1805 StopNotifySession_Reentrant_Stop_StartError) {
1806 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1807 /* properties: NOTIFY */ 0x10,
1808 /* expected_config_descriptor_value: NOTIFY */ 1));
1809
1810 // Check that the notify session is active.
1811 EXPECT_TRUE(notify_sessions_[0]->IsActive());
1812 EXPECT_EQ(characteristic1_->GetIdentifier(),
1813 notify_sessions_[0]->GetCharacteristicIdentifier());
1814 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1815 EXPECT_TRUE(characteristic1_->IsNotifying());
1816
1817 notify_sessions_[0]->Stop(base::Bind(
1818 [](BluetoothRemoteGattCharacteristic* characteristic,
1819 BluetoothRemoteGattCharacteristic::NotifySessionCallback
1820 notifyCallback,
1821 BluetoothRemoteGattCharacteristic::ErrorCallback errorCallback) {
1822 characteristic->StartNotifySession(notifyCallback, errorCallback);
1823 },
1824 characteristic1_, GetNotifyCallback(Call::NOT_EXPECTED),
1825 GetGattErrorCallback(Call::EXPECTED)));
1826
1827 SimulateGattNotifySessionStopped(characteristic1_);
1828 base::RunLoop().RunUntilIdle();
1829 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1830 EXPECT_FALSE(characteristic1_->IsNotifying());
1831
1832 SimulateGattNotifySessionStartError(
1833 characteristic1_, BluetoothRemoteGattService::GATT_ERROR_FAILED);
1834 base::RunLoop().RunUntilIdle();
1835 ASSERT_EQ(1u, notify_sessions_.size());
1836 ASSERT_TRUE(notify_sessions_[0]);
1837 EXPECT_EQ(characteristic1_, notify_sessions_[0]->GetCharacteristic());
1838 EXPECT_FALSE(notify_sessions_[0]->IsActive());
1839 EXPECT_FALSE(characteristic1_->IsNotifying());
1840 }
1841 #endif
1842
1290 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN) 1843 #if defined(OS_ANDROID) || defined(OS_MACOSX) || defined(OS_WIN)
1291 // Tests Characteristic Value changes during a Notify Session. 1844 // Tests Characteristic Value changes during a Notify Session.
1292 TEST_F(BluetoothRemoteGattCharacteristicTest, GattCharacteristicValueChanged) { 1845 TEST_F(BluetoothRemoteGattCharacteristicTest, GattCharacteristicValueChanged) {
1293 if (!PlatformSupportsLowEnergy()) { 1846 if (!PlatformSupportsLowEnergy()) {
1294 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 1847 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
1295 return; 1848 return;
1296 } 1849 }
1297 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate( 1850 ASSERT_NO_FATAL_FAILURE(StartNotifyBoilerplate(
1298 /* properties: NOTIFY */ 0x10, 1851 /* properties: NOTIFY */ 0x10,
1299 /* expected_config_descriptor_value: NOTIFY */ 1)); 1852 /* 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()); 1968 EXPECT_EQ(1u, characteristic1_->GetDescriptorsByUUID(id2).size());
1416 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size()); 1969 EXPECT_EQ(2u, characteristic2_->GetDescriptorsByUUID(id3).size());
1417 1970
1418 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size()); 1971 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id1).size());
1419 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size()); 1972 EXPECT_EQ(0u, characteristic2_->GetDescriptorsByUUID(id2).size());
1420 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size()); 1973 EXPECT_EQ(0u, characteristic1_->GetDescriptorsByUUID(id3).size());
1421 } 1974 }
1422 #endif // defined(OS_ANDROID) || defined(OS_WIN) 1975 #endif // defined(OS_ANDROID) || defined(OS_WIN)
1423 1976
1424 } // namespace device 1977 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698