bluetooth_adapter_mac_metrics_unittest.mm 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2019 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. #include "base/logging.h"
  5. #include "base/memory/raw_ptr.h"
  6. #include "base/test/metrics/histogram_tester.h"
  7. #include "device/bluetooth/test/bluetooth_test_mac.h"
  8. namespace device {
  9. class BluetoothAdapterMacMetricsTest : public BluetoothTest {
  10. public:
  11. void FakeDeviceBoilerPlate() {
  12. if (!PlatformSupportsLowEnergy()) {
  13. LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
  14. return;
  15. }
  16. InitWithFakeAdapter();
  17. StartLowEnergyDiscoverySession();
  18. device_ = SimulateLowEnergyDevice(3);
  19. }
  20. void FakeServiceBoilerPlate() {
  21. ASSERT_NO_FATAL_FAILURE(FakeDeviceBoilerPlate());
  22. device_->CreateGattConnection(
  23. GetGattConnectionCallback(Call::EXPECTED, Result::SUCCESS));
  24. SimulateGattConnection(device_);
  25. base::RunLoop().RunUntilIdle();
  26. SimulateGattServicesDiscovered(
  27. device_, std::vector<std::string>({kTestUUIDGenericAccess}));
  28. base::RunLoop().RunUntilIdle();
  29. EXPECT_EQ(1u, device_->GetGattServices().size());
  30. service_ = device_->GetGattServices()[0];
  31. }
  32. void FakeCharacteristicBoilerplate(int property = 0) {
  33. ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerPlate());
  34. SimulateGattCharacteristic(service_, kTestUUIDDeviceName, property);
  35. base::RunLoop().RunUntilIdle();
  36. EXPECT_EQ(1u, service_->GetCharacteristics().size());
  37. characteristic_ = service_->GetCharacteristics()[0];
  38. }
  39. raw_ptr<BluetoothDevice> device_ = nullptr;
  40. raw_ptr<BluetoothRemoteGattService> service_ = nullptr;
  41. raw_ptr<BluetoothRemoteGattCharacteristic> characteristic_ = nullptr;
  42. };
  43. TEST_F(BluetoothAdapterMacMetricsTest, DidFailToConnectToPeripheralError) {
  44. base::HistogramTester histogram_tester;
  45. ASSERT_NO_FATAL_FAILURE(FakeDeviceBoilerPlate());
  46. SimulateGattConnectionError(device_, BluetoothDevice::ERROR_FAILED);
  47. histogram_tester.ExpectTotalCount(
  48. "Bluetooth.MacOS.Errors.DidFailToConnectToPeripheral", 1);
  49. }
  50. TEST_F(BluetoothAdapterMacMetricsTest, DidDisconnectPeripheral) {
  51. base::HistogramTester histogram_tester;
  52. ASSERT_NO_FATAL_FAILURE(FakeDeviceBoilerPlate());
  53. SimulateGattDisconnectionError(device_);
  54. histogram_tester.ExpectTotalCount(
  55. "Bluetooth.MacOS.Errors.DidDisconnectPeripheral", 1);
  56. }
  57. TEST_F(BluetoothAdapterMacMetricsTest, DidDiscoverPrimaryServicesError) {
  58. base::HistogramTester histogram_tester;
  59. ASSERT_NO_FATAL_FAILURE(FakeDeviceBoilerPlate());
  60. SimulateGattConnection(device_);
  61. SimulateDidDiscoverServicesMacWithError(device_);
  62. histogram_tester.ExpectTotalCount(
  63. "Bluetooth.MacOS.Errors.DidDiscoverPrimaryServices", 1);
  64. }
  65. TEST_F(BluetoothAdapterMacMetricsTest, DidDiscoverCharacteristicsError) {
  66. ASSERT_NO_FATAL_FAILURE(FakeServiceBoilerPlate());
  67. base::HistogramTester histogram_tester;
  68. SimulateDidDiscoverCharacteristicsWithErrorMac(service_);
  69. histogram_tester.ExpectTotalCount(
  70. "Bluetooth.MacOS.Errors.DidDiscoverCharacteristics", 1);
  71. }
  72. TEST_F(BluetoothAdapterMacMetricsTest, DidUpdateValueError) {
  73. base::HistogramTester histogram_tester;
  74. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  75. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  76. SimulateGattCharacteristicReadError(characteristic_,
  77. BluetoothGattService::GATT_ERROR_FAILED);
  78. histogram_tester.ExpectTotalCount("Bluetooth.MacOS.Errors.DidUpdateValue", 1);
  79. }
  80. TEST_F(BluetoothAdapterMacMetricsTest, DidWriteValueError) {
  81. base::HistogramTester histogram_tester;
  82. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  83. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  84. SimulateGattCharacteristicWriteError(characteristic_,
  85. BluetoothGattService::GATT_ERROR_FAILED);
  86. histogram_tester.ExpectTotalCount("Bluetooth.MacOS.Errors.DidWriteValue", 1);
  87. }
  88. TEST_F(BluetoothAdapterMacMetricsTest, DidUpdateNotificationStateError) {
  89. base::HistogramTester histogram_tester;
  90. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  91. BluetoothRemoteGattCharacteristic::PROPERTY_NOTIFY));
  92. SimulateGattDescriptor(
  93. characteristic_,
  94. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  95. .canonical_value());
  96. base::RunLoop().RunUntilIdle();
  97. ASSERT_EQ(1u, characteristic_->GetDescriptors().size());
  98. characteristic_->StartNotifySession(GetNotifyCallback(Call::NOT_EXPECTED),
  99. GetGattErrorCallback(Call::EXPECTED));
  100. ExpectedChangeNotifyValueAttempts(1);
  101. ExpectedNotifyValue(NotifyValueState::NOTIFY);
  102. SimulateGattNotifySessionStartError(characteristic_,
  103. BluetoothGattService::GATT_ERROR_FAILED);
  104. histogram_tester.ExpectTotalCount(
  105. "Bluetooth.MacOS.Errors.DidUpdateNotificationState", 1);
  106. }
  107. TEST_F(BluetoothAdapterMacMetricsTest, DidDiscoverDescriptorsError) {
  108. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  109. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  110. base::HistogramTester histogram_tester;
  111. SimulateDidDiscoverDescriptorsWithErrorMac(characteristic_);
  112. histogram_tester.ExpectTotalCount(
  113. "Bluetooth.MacOS.Errors.DidDiscoverDescriptors", 1);
  114. }
  115. TEST_F(BluetoothAdapterMacMetricsTest, DidUpdateValueForDescriptorError) {
  116. base::HistogramTester histogram_tester;
  117. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  118. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  119. SimulateGattDescriptor(
  120. characteristic_,
  121. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  122. .canonical_value());
  123. EXPECT_EQ(1u, characteristic_->GetDescriptors().size());
  124. BluetoothRemoteGattDescriptor* descriptor =
  125. characteristic_->GetDescriptors()[0];
  126. descriptor->ReadRemoteDescriptor(
  127. GetReadValueCallback(Call::EXPECTED, Result::FAILURE));
  128. SimulateGattDescriptorUpdateError(descriptor,
  129. BluetoothGattService::GATT_ERROR_FAILED);
  130. base::RunLoop().RunUntilIdle();
  131. histogram_tester.ExpectTotalCount(
  132. "Bluetooth.MacOS.Errors.DidUpdateValueForDescriptor", 1);
  133. }
  134. TEST_F(BluetoothAdapterMacMetricsTest, DidWriteValueForDescriptorError) {
  135. base::HistogramTester histogram_tester;
  136. ASSERT_NO_FATAL_FAILURE(FakeCharacteristicBoilerplate(
  137. BluetoothRemoteGattCharacteristic::PROPERTY_WRITE));
  138. SimulateGattDescriptor(
  139. characteristic_,
  140. BluetoothRemoteGattDescriptor::ClientCharacteristicConfigurationUuid()
  141. .canonical_value());
  142. EXPECT_EQ(1u, characteristic_->GetDescriptors().size());
  143. BluetoothRemoteGattDescriptor* descriptor =
  144. characteristic_->GetDescriptors()[0];
  145. std::vector<uint8_t> empty_vector;
  146. descriptor->WriteRemoteDescriptor(empty_vector,
  147. GetCallback(Call::NOT_EXPECTED),
  148. GetGattErrorCallback(Call::EXPECTED));
  149. SimulateGattDescriptorWriteError(descriptor,
  150. BluetoothGattService::GATT_ERROR_FAILED);
  151. histogram_tester.ExpectTotalCount(
  152. "Bluetooth.MacOS.Errors.DidWriteValueForDescriptor", 1);
  153. }
  154. } // namespace device