1 #include "metrics_state.h"
2 
3 #include <gmock/gmock.h>
4 
5 #include <cstdint>
6 #include <vector>
7 
8 #include "gtest/gtest.h"
9 #include "hci/address.h"
10 #include "metrics_state.h"
11 #include "os/metrics.h"
12 
13 //
14 using android::bluetooth::hci::StatusEnum;
15 using android::bluetooth::le::LeAclConnectionState;
16 using android::bluetooth::le::LeConnectionOriginType;
17 using android::bluetooth::le::LeConnectionState;
18 using android::bluetooth::le::LeConnectionType;
19 
20 LeAclConnectionState le_acl_state = LeAclConnectionState::LE_ACL_UNSPECIFIED;
21 LeConnectionOriginType origin_type = LeConnectionOriginType::ORIGIN_UNSPECIFIED;
22 LeConnectionType connection_type = LeConnectionType::CONNECTION_TYPE_UNSPECIFIED;
23 StatusEnum status = StatusEnum::STATUS_UNKNOWN;
24 bluetooth::hci::Address remote_address = bluetooth::hci::Address::kEmpty;
25 int latency = 0;
26 int acl_latency = 0;
27 bool is_cancelled = false;
28 
29 namespace bluetooth {
30 namespace metrics {
31 
32 const hci::Address address1 = hci::Address({0x11, 0x22, 0x33, 0x44, 0x55, 0x66});
33 const hci::Address empty_address = hci::Address::kEmpty;
34 
35 class TestMetricsLoggerModule : public BaseMetricsLoggerModule {
36  public:
TestMetricsLoggerModule()37   TestMetricsLoggerModule() {}
38   void LogMetricBluetoothLESession(os::LEConnectionSessionOptions session_options);
~TestMetricsLoggerModule()39   virtual ~TestMetricsLoggerModule() {}
40 };
41 
LogMetricBluetoothLESession(os::LEConnectionSessionOptions session_options)42 void TestMetricsLoggerModule::LogMetricBluetoothLESession(
43     os::LEConnectionSessionOptions session_options) {
44   le_acl_state = session_options.acl_connection_state;
45   origin_type = session_options.origin_type;
46   connection_type = session_options.transaction_type;
47   is_cancelled = session_options.is_cancelled;
48   status = session_options.status;
49   remote_address = session_options.remote_address;
50 }
51 
52 class MockMetricsCollector {
53  public:
54   static LEConnectionMetricsRemoteDevice* GetLEConnectionMetricsCollector();
55 
56   static LEConnectionMetricsRemoteDevice* le_connection_metrics_remote_device;
57 };
58 
59 
60 
61 LEConnectionMetricsRemoteDevice* MockMetricsCollector::le_connection_metrics_remote_device =
62     new LEConnectionMetricsRemoteDevice(new TestMetricsLoggerModule());
63 
GetLEConnectionMetricsCollector()64 LEConnectionMetricsRemoteDevice* MockMetricsCollector::GetLEConnectionMetricsCollector() {
65   return MockMetricsCollector::le_connection_metrics_remote_device;
66 }
67 
68 namespace {
69 
70 class LEConnectionMetricsRemoteDeviceTest : public ::testing::Test {};
71 
TEST(LEConnectionMetricsRemoteDeviceTest,Initialize)72 TEST(LEConnectionMetricsRemoteDeviceTest, Initialize) {
73   ASSERT_EQ(0, 0);
74 }
75 
TEST(LEConnectionMetricsRemoteDeviceTest,ConnectionSuccess)76 TEST(LEConnectionMetricsRemoteDeviceTest, ConnectionSuccess) {
77   auto argument_list = std::vector<std::pair<os::ArgumentType, int>>();
78   argument_list.push_back(std::make_pair(
79       os::ArgumentType::ACL_STATUS_CODE,
80       static_cast<int>(android::bluetooth::hci::StatusEnum::STATUS_SUCCESS)));
81 
82   MockMetricsCollector::GetLEConnectionMetricsCollector()->AddStateChangedEvent(
83       address1,
84       LeConnectionOriginType::ORIGIN_NATIVE,
85       LeConnectionType::CONNECTION_TYPE_LE_ACL,
86       LeConnectionState::STATE_LE_ACL_START,
87       argument_list);
88 
89   MockMetricsCollector::GetLEConnectionMetricsCollector()->AddStateChangedEvent(
90       address1,
91       LeConnectionOriginType::ORIGIN_NATIVE,
92       LeConnectionType::CONNECTION_TYPE_LE_ACL,
93       LeConnectionState::STATE_LE_ACL_END,
94       argument_list);
95   // assert that these are equal
96   ASSERT_EQ(le_acl_state, LeAclConnectionState::LE_ACL_SUCCESS);
97   ASSERT_EQ(origin_type, LeConnectionOriginType::ORIGIN_NATIVE);
98   ASSERT_EQ(connection_type, LeConnectionType::CONNECTION_TYPE_LE_ACL);
99   ASSERT_EQ(remote_address, address1);
100   ASSERT_EQ(is_cancelled, false);
101 }
102 
TEST(LEConnectionMetricsRemoteDeviceTest,ConnectionFailed)103 TEST(LEConnectionMetricsRemoteDeviceTest, ConnectionFailed) {
104   auto argument_list = std::vector<std::pair<os::ArgumentType, int>>();
105   argument_list.push_back(std::make_pair(
106       os::ArgumentType::ACL_STATUS_CODE,
107       static_cast<int>(android::bluetooth::hci::StatusEnum::STATUS_NO_CONNECTION)));
108 
109   MockMetricsCollector::GetLEConnectionMetricsCollector()->AddStateChangedEvent(
110       address1,
111       LeConnectionOriginType::ORIGIN_NATIVE,
112       LeConnectionType::CONNECTION_TYPE_LE_ACL,
113       LeConnectionState::STATE_LE_ACL_START,
114       argument_list);
115 
116   MockMetricsCollector::GetLEConnectionMetricsCollector()->AddStateChangedEvent(
117       address1,
118       LeConnectionOriginType::ORIGIN_NATIVE,
119       LeConnectionType::CONNECTION_TYPE_LE_ACL,
120       LeConnectionState::STATE_LE_ACL_END,
121       argument_list);
122   // assert that these are equal
123   ASSERT_EQ(le_acl_state, LeAclConnectionState::LE_ACL_FAILED);
124   ASSERT_EQ(origin_type, LeConnectionOriginType::ORIGIN_NATIVE);
125   ASSERT_EQ(connection_type, LeConnectionType::CONNECTION_TYPE_LE_ACL);
126   ASSERT_EQ(remote_address, address1);
127   ASSERT_EQ(is_cancelled, false);
128 }
129 
TEST(LEConnectionMetricsRemoteDeviceTest,Cancellation)130 TEST(LEConnectionMetricsRemoteDeviceTest, Cancellation) {
131   auto argument_list = std::vector<std::pair<os::ArgumentType, int>>();
132   auto no_connection_argument_list = std::vector<std::pair<os::ArgumentType, int>>();
133   no_connection_argument_list.push_back(std::make_pair(
134       os::ArgumentType::ACL_STATUS_CODE,
135       static_cast<int>(android::bluetooth::hci::StatusEnum::STATUS_NO_CONNECTION)));
136 
137   // Start of the LE-ACL Connection
138   MockMetricsCollector::GetLEConnectionMetricsCollector()->AddStateChangedEvent(
139       address1,
140       LeConnectionOriginType::ORIGIN_NATIVE,
141       LeConnectionType::CONNECTION_TYPE_LE_ACL,
142       LeConnectionState::STATE_LE_ACL_START,
143       argument_list);
144 
145   // Cancellation of the LE-ACL Connection
146   MockMetricsCollector::GetLEConnectionMetricsCollector()->AddStateChangedEvent(
147       empty_address,
148       LeConnectionOriginType::ORIGIN_NATIVE,
149       LeConnectionType::CONNECTION_TYPE_LE_ACL,
150       LeConnectionState::STATE_LE_ACL_CANCEL,
151       argument_list);
152 
153   // Ending of the LE-ACL Connection
154   MockMetricsCollector::GetLEConnectionMetricsCollector()->AddStateChangedEvent(
155       address1,
156       LeConnectionOriginType::ORIGIN_NATIVE,
157       LeConnectionType::CONNECTION_TYPE_LE_ACL,
158       LeConnectionState::STATE_LE_ACL_END,
159       no_connection_argument_list);
160 
161   ASSERT_EQ(le_acl_state, LeAclConnectionState::LE_ACL_FAILED);
162   ASSERT_EQ(origin_type, LeConnectionOriginType::ORIGIN_NATIVE);
163   ASSERT_EQ(connection_type, LeConnectionType::CONNECTION_TYPE_LE_ACL);
164   ASSERT_EQ(remote_address, address1);
165   ASSERT_EQ(is_cancelled, true);
166 }
167 
TEST(LEConnectionMetricsRemoteDeviceTest,Timeout)168 TEST(LEConnectionMetricsRemoteDeviceTest, Timeout) {
169   auto argument_list = std::vector<std::pair<os::ArgumentType, int>>();
170 
171   // Start of the LE-ACL Connection
172   MockMetricsCollector::GetLEConnectionMetricsCollector()->AddStateChangedEvent(
173       address1,
174       LeConnectionOriginType::ORIGIN_NATIVE,
175       LeConnectionType::CONNECTION_TYPE_LE_ACL,
176       LeConnectionState::STATE_LE_ACL_START,
177       argument_list);
178 
179   // Timeout of the LE-ACL Connection
180   MockMetricsCollector::GetLEConnectionMetricsCollector()->AddStateChangedEvent(
181       address1,
182       LeConnectionOriginType::ORIGIN_NATIVE,
183       LeConnectionType::CONNECTION_TYPE_LE_ACL,
184       LeConnectionState::STATE_LE_ACL_TIMEOUT,
185       argument_list);
186 
187   ASSERT_EQ(le_acl_state, LeAclConnectionState::LE_ACL_FAILED);
188   ASSERT_EQ(origin_type, LeConnectionOriginType::ORIGIN_NATIVE);
189   ASSERT_EQ(connection_type, LeConnectionType::CONNECTION_TYPE_LE_ACL);
190   ASSERT_EQ(remote_address, address1);
191   ASSERT_EQ(is_cancelled, false);
192 }
193 
194 }  // namespace
195 }  // namespace metrics
196 }  // namespace bluetooth
197