• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 2016 Google, Inc.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 #include <chrono>
19 #include <cstdint>
20 #include <string>
21 #include <thread>
22 #include <vector>
23 
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26 
27 #include <base/logging.h>
28 
29 #include "bluetooth/metrics/bluetooth.pb.h"
30 #include "osi/include/metrics.h"
31 #include "osi/include/time.h"
32 
33 #define BTM_COD_MAJOR_AUDIO_TEST 0x04
34 
35 namespace testing {
36 
37 using bluetooth::metrics::BluetoothMetricsProto::A2DPSession;
38 using bluetooth::metrics::BluetoothMetricsProto::BluetoothLog;
39 using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession;
40 using bluetooth::metrics::BluetoothMetricsProto::
41     BluetoothSession_ConnectionTechnologyType;
42 using bluetooth::metrics::BluetoothMetricsProto::
43     BluetoothSession_DisconnectReasonType;
44 using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo;
45 using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo_DeviceType;
46 using bluetooth::metrics::BluetoothMetricsProto::PairEvent;
47 using bluetooth::metrics::BluetoothMetricsProto::RFCommSession;
48 using bluetooth::metrics::BluetoothMetricsProto::ScanEvent;
49 using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanTechnologyType;
50 using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanEventType;
51 using bluetooth::metrics::BluetoothMetricsProto::WakeEvent;
52 using bluetooth::metrics::BluetoothMetricsProto::WakeEvent_WakeEventType;
53 using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType;
54 using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileConnectionStats;
55 using system_bt_osi::BluetoothMetricsLogger;
56 using system_bt_osi::A2dpSessionMetrics;
57 
58 namespace {
59 const size_t kMaxEventGenerationLimit = 5000;
60 }
61 
62 /*
63  * Get current OS boot time in ms
64  */
time_get_os_boottime_ms(void)65 static int64_t time_get_os_boottime_ms(void) {
66   return time_get_os_boottime_us() / 1000;
67 }
68 
sleep_ms(int64_t t)69 static void sleep_ms(int64_t t) {
70   std::this_thread::sleep_for(std::chrono::milliseconds(t));
71 }
72 
MakeDeviceInfo(int32_t device_class,DeviceInfo_DeviceType device_type)73 DeviceInfo* MakeDeviceInfo(int32_t device_class,
74                            DeviceInfo_DeviceType device_type) {
75   DeviceInfo* info = new DeviceInfo();
76   info->set_device_class(device_class);
77   info->set_device_type(device_type);
78   return info;
79 }
80 
MakePairEvent(int32_t disconnect_reason,int64_t timestamp_ms,DeviceInfo * device_info)81 PairEvent* MakePairEvent(int32_t disconnect_reason, int64_t timestamp_ms,
82                          DeviceInfo* device_info) {
83   PairEvent* event = new PairEvent();
84   event->set_disconnect_reason(disconnect_reason);
85   event->set_event_time_millis(timestamp_ms);
86   if (device_info) event->set_allocated_device_paired_with(device_info);
87   return event;
88 }
89 
MakeWakeEvent(WakeEvent_WakeEventType event_type,const std::string & requestor,const std::string & name,int64_t timestamp_ms)90 WakeEvent* MakeWakeEvent(WakeEvent_WakeEventType event_type,
91                          const std::string& requestor, const std::string& name,
92                          int64_t timestamp_ms) {
93   WakeEvent* event = new WakeEvent();
94   event->set_wake_event_type(event_type);
95   event->set_requestor(requestor);
96   event->set_name(name);
97   event->set_event_time_millis(timestamp_ms);
98   return event;
99 }
100 
MakeScanEvent(ScanEvent_ScanEventType event_type,const std::string & initiator,ScanEvent_ScanTechnologyType tech_type,int32_t num_results,int64_t timestamp_ms)101 ScanEvent* MakeScanEvent(ScanEvent_ScanEventType event_type,
102                          const std::string& initiator,
103                          ScanEvent_ScanTechnologyType tech_type,
104                          int32_t num_results, int64_t timestamp_ms) {
105   ScanEvent* event = new ScanEvent();
106   event->set_scan_event_type(event_type);
107   event->set_initiator(initiator);
108   event->set_scan_technology_type(tech_type);
109   event->set_number_results(num_results);
110   event->set_event_time_millis(timestamp_ms);
111   return event;
112 }
113 
MakeA2DPSession(const A2dpSessionMetrics & metrics)114 A2DPSession* MakeA2DPSession(const A2dpSessionMetrics& metrics) {
115   A2DPSession* session = new A2DPSession();
116   session->set_media_timer_min_millis(metrics.media_timer_min_ms);
117   session->set_media_timer_max_millis(metrics.media_timer_max_ms);
118   session->set_media_timer_avg_millis(metrics.media_timer_avg_ms);
119   session->set_buffer_overruns_max_count(metrics.buffer_overruns_max_count);
120   session->set_buffer_overruns_total(metrics.buffer_overruns_total);
121   session->set_buffer_underruns_average(metrics.buffer_underruns_average);
122   session->set_buffer_underruns_count(metrics.buffer_underruns_count);
123   session->set_audio_duration_millis(metrics.audio_duration_ms);
124   return session;
125 }
126 
MakeBluetoothSession(int64_t session_duration_sec,BluetoothSession_ConnectionTechnologyType conn_type,BluetoothSession_DisconnectReasonType disconnect_reason,DeviceInfo * device_info,RFCommSession * rfcomm_session,A2DPSession * a2dp_session)127 BluetoothSession* MakeBluetoothSession(
128     int64_t session_duration_sec,
129     BluetoothSession_ConnectionTechnologyType conn_type,
130     BluetoothSession_DisconnectReasonType disconnect_reason,
131     DeviceInfo* device_info, RFCommSession* rfcomm_session,
132     A2DPSession* a2dp_session) {
133   BluetoothSession* session = new BluetoothSession();
134   if (a2dp_session) session->set_allocated_a2dp_session(a2dp_session);
135   if (rfcomm_session) session->set_allocated_rfcomm_session(rfcomm_session);
136   if (device_info) session->set_allocated_device_connected_to(device_info);
137   session->set_session_duration_sec(session_duration_sec);
138   session->set_connection_technology_type(conn_type);
139   session->set_disconnect_reason_type(disconnect_reason);
140   return session;
141 }
142 
MakeBluetoothLog(std::vector<BluetoothSession * > bt_sessions,std::vector<PairEvent * > pair_events,std::vector<WakeEvent * > wake_events,std::vector<ScanEvent * > scan_events)143 BluetoothLog* MakeBluetoothLog(std::vector<BluetoothSession*> bt_sessions,
144                                std::vector<PairEvent*> pair_events,
145                                std::vector<WakeEvent*> wake_events,
146                                std::vector<ScanEvent*> scan_events) {
147   BluetoothLog* bt_log = new BluetoothLog();
148   for (BluetoothSession* session : bt_sessions) {
149     bt_log->mutable_session()->AddAllocated(session);
150   }
151   bt_sessions.clear();
152   for (PairEvent* event : pair_events) {
153     bt_log->mutable_pair_event()->AddAllocated(event);
154   }
155   pair_events.clear();
156   for (WakeEvent* event : wake_events) {
157     bt_log->mutable_wake_event()->AddAllocated(event);
158   }
159   wake_events.clear();
160   for (ScanEvent* event : scan_events) {
161     bt_log->mutable_scan_event()->AddAllocated(event);
162   }
163   scan_events.clear();
164   return bt_log;
165 }
166 
GenerateWakeEvents(size_t start,size_t end,std::vector<WakeEvent * > * wake_events)167 void GenerateWakeEvents(size_t start, size_t end,
168                         std::vector<WakeEvent*>* wake_events) {
169   for (size_t i = start; i < end; ++i) {
170     wake_events->push_back(MakeWakeEvent(
171         i % 2 == 0 ? WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED
172                    : WakeEvent_WakeEventType::WakeEvent_WakeEventType_RELEASED,
173         "TEST_REQ", "TEST_NAME", i));
174   }
175 }
176 
177 #define COMPARE_A2DP_METRICS(a, b)                                           \
178   do {                                                                       \
179     EXPECT_EQ((a).audio_duration_ms, (b).audio_duration_ms);                 \
180     EXPECT_EQ((a).media_timer_min_ms, (b).media_timer_min_ms);               \
181     EXPECT_EQ((a).media_timer_max_ms, (b).media_timer_max_ms);               \
182     EXPECT_EQ((a).media_timer_avg_ms, (b).media_timer_avg_ms);               \
183     EXPECT_EQ((a).total_scheduling_count, (b).total_scheduling_count);       \
184     EXPECT_EQ((a).buffer_overruns_max_count, (b).buffer_overruns_max_count); \
185     EXPECT_EQ((a).buffer_overruns_total, (b).buffer_overruns_total);         \
186     EXPECT_THAT((a).buffer_underruns_average,                                \
187                 FloatNear((b).buffer_underruns_average, 0.01));              \
188     (a).buffer_underruns_average = (b).buffer_underruns_average;             \
189     EXPECT_EQ((a).buffer_underruns_count, (b).buffer_underruns_count);       \
190   } while (0)
191 
192 /*
193  * metrics_sum = metrics1 + metrics2
194  */
TEST(BluetoothA2DPSessionMetricsTest,TestUpdateNormal)195 TEST(BluetoothA2DPSessionMetricsTest, TestUpdateNormal) {
196   A2dpSessionMetrics metrics1;
197   A2dpSessionMetrics metrics2;
198   A2dpSessionMetrics metrics_sum;
199   metrics1.audio_duration_ms = 10;
200   metrics2.audio_duration_ms = 25;
201   metrics_sum.audio_duration_ms = 35;
202   metrics1.media_timer_min_ms = 10;
203   metrics2.media_timer_min_ms = 25;
204   metrics_sum.media_timer_min_ms = 10;
205   metrics1.media_timer_max_ms = 100;
206   metrics2.media_timer_max_ms = 200;
207   metrics_sum.media_timer_max_ms = 200;
208   metrics1.media_timer_avg_ms = 50;
209   metrics1.total_scheduling_count = 50;
210   metrics2.media_timer_avg_ms = 100;
211   metrics2.total_scheduling_count = 50;
212   metrics_sum.media_timer_avg_ms = 75;
213   metrics_sum.total_scheduling_count = 100;
214   metrics1.buffer_overruns_max_count = 70;
215   metrics2.buffer_overruns_max_count = 80;
216   metrics_sum.buffer_overruns_max_count = 80;
217   metrics1.buffer_underruns_average = 80;
218   metrics1.buffer_underruns_count = 1200;
219   metrics2.buffer_underruns_average = 130;
220   metrics2.buffer_underruns_count = 2400;
221   metrics_sum.buffer_underruns_average = 113.33333333;
222   metrics_sum.buffer_underruns_count = 3600;
223   metrics1.Update(metrics2);
224   COMPARE_A2DP_METRICS(metrics1, metrics_sum);
225   EXPECT_TRUE(metrics1 == metrics_sum);
226   EXPECT_EQ(metrics1, metrics_sum);
227 }
228 
TEST(BluetoothA2DPSessionMetricsTest,TestUpdateNew)229 TEST(BluetoothA2DPSessionMetricsTest, TestUpdateNew) {
230   A2dpSessionMetrics metrics1;
231   A2dpSessionMetrics metrics2;
232   A2dpSessionMetrics metrics_sum;
233   metrics2.audio_duration_ms = 25;
234   metrics_sum.audio_duration_ms = 25;
235   metrics2.media_timer_min_ms = 25;
236   metrics_sum.media_timer_min_ms = 25;
237   metrics2.media_timer_max_ms = 200;
238   metrics_sum.media_timer_max_ms = 200;
239   metrics2.media_timer_avg_ms = 100;
240   metrics2.total_scheduling_count = 50;
241   metrics_sum.media_timer_avg_ms = 100;
242   metrics_sum.total_scheduling_count = 50;
243   metrics2.buffer_overruns_max_count = 80;
244   metrics_sum.buffer_overruns_max_count = 80;
245   metrics2.buffer_underruns_average = 130;
246   metrics2.buffer_underruns_count = 2400;
247   metrics_sum.buffer_underruns_average = 130;
248   metrics_sum.buffer_underruns_count = 2400;
249   metrics1.Update(metrics2);
250   COMPARE_A2DP_METRICS(metrics1, metrics_sum);
251   EXPECT_TRUE(metrics1 == metrics_sum);
252   EXPECT_EQ(metrics1, metrics_sum);
253 }
254 
TEST(BluetoothA2DPSessionMetricsTest,TestNullUpdate)255 TEST(BluetoothA2DPSessionMetricsTest, TestNullUpdate) {
256   A2dpSessionMetrics metrics1;
257   A2dpSessionMetrics metrics2;
258   A2dpSessionMetrics metrics_sum;
259   metrics2.audio_duration_ms = 25;
260   metrics_sum.audio_duration_ms = 25;
261   metrics2.media_timer_min_ms = 25;
262   metrics_sum.media_timer_min_ms = 25;
263   metrics2.media_timer_max_ms = 200;
264   metrics_sum.media_timer_max_ms = 200;
265   metrics2.media_timer_avg_ms = 100;
266   metrics2.total_scheduling_count = 50;
267   metrics_sum.media_timer_avg_ms = 100;
268   metrics_sum.total_scheduling_count = 50;
269   metrics2.buffer_overruns_max_count = 80;
270   metrics_sum.buffer_overruns_max_count = 80;
271   metrics2.buffer_underruns_average = 130;
272   metrics2.buffer_underruns_count = 2400;
273   metrics_sum.buffer_underruns_average = 130;
274   metrics_sum.buffer_underruns_count = 2400;
275   metrics2.Update(metrics1);
276   COMPARE_A2DP_METRICS(metrics2, metrics_sum);
277   EXPECT_TRUE(metrics2 == metrics_sum);
278   EXPECT_EQ(metrics2, metrics_sum);
279 }
280 
TEST(BluetoothA2DPSessionMetricsTest,TestPartialUpdate)281 TEST(BluetoothA2DPSessionMetricsTest, TestPartialUpdate) {
282   A2dpSessionMetrics metrics1;
283   A2dpSessionMetrics metrics2;
284   A2dpSessionMetrics metrics_sum;
285   metrics1.audio_duration_ms = 10;
286   metrics2.audio_duration_ms = 25;
287   metrics_sum.audio_duration_ms = 35;
288   metrics1.media_timer_min_ms = 10;
289   metrics_sum.media_timer_min_ms = 10;
290   metrics1.media_timer_max_ms = 100;
291   metrics_sum.media_timer_max_ms = 100;
292   metrics1.media_timer_avg_ms = 50;
293   metrics1.total_scheduling_count = 50;
294   metrics2.media_timer_avg_ms = 100;
295   metrics_sum.media_timer_avg_ms = 50;
296   metrics_sum.total_scheduling_count = 50;
297   metrics1.buffer_overruns_max_count = 70;
298   metrics_sum.buffer_overruns_max_count = 70;
299   metrics1.buffer_underruns_average = 80;
300   metrics1.buffer_underruns_count = 1200;
301   metrics2.buffer_underruns_count = 2400;
302   metrics_sum.buffer_underruns_average = 80;
303   metrics_sum.buffer_underruns_count = 1200;
304   metrics1.Update(metrics2);
305   COMPARE_A2DP_METRICS(metrics1, metrics_sum);
306   EXPECT_TRUE(metrics1 == metrics_sum);
307   EXPECT_EQ(metrics1, metrics_sum);
308 }
309 
310 class BluetoothMetricsLoggerTest : public Test {
311  protected:
312   // Use to hold test protos
313   std::vector<PairEvent*> pair_events_;
314   std::vector<WakeEvent*> wake_events_;
315   std::vector<ScanEvent*> scan_events_;
316   std::vector<BluetoothSession*> bt_sessions_;
317   int64_t num_pair_event_ = 0;
318   int64_t num_wake_event_ = 0;
319   int64_t num_scan_event_ = 0;
320   int64_t num_bt_session_ = 0;
321   BluetoothLog* bt_log_;
322   std::string bt_log_str_;
323   std::string bt_log_ascii_str_;
324 
UpdateLog()325   void UpdateLog() {
326     for (BluetoothSession* session : bt_sessions_) {
327       bt_log_->mutable_session()->AddAllocated(session);
328     }
329     if (num_bt_session_ > 0) {
330       bt_log_->set_num_bluetooth_session(num_bt_session_);
331     } else if (bt_sessions_.size() > 0) {
332       bt_log_->set_num_bluetooth_session(bt_sessions_.size());
333     }
334     bt_sessions_.clear();
335     for (PairEvent* event : pair_events_) {
336       bt_log_->mutable_pair_event()->AddAllocated(event);
337     }
338     if (num_pair_event_ > 0) {
339       bt_log_->set_num_pair_event(num_pair_event_);
340     } else if (pair_events_.size() > 0) {
341       bt_log_->set_num_pair_event(pair_events_.size());
342     }
343     pair_events_.clear();
344     for (WakeEvent* event : wake_events_) {
345       bt_log_->mutable_wake_event()->AddAllocated(event);
346     }
347     if (num_wake_event_ > 0) {
348       bt_log_->set_num_wake_event(num_wake_event_);
349     } else if (wake_events_.size() > 0) {
350       bt_log_->set_num_wake_event(wake_events_.size());
351     }
352     wake_events_.clear();
353     for (ScanEvent* event : scan_events_) {
354       bt_log_->mutable_scan_event()->AddAllocated(event);
355     }
356     if (num_scan_event_ > 0) {
357       bt_log_->set_num_scan_event(num_scan_event_);
358     } else if (scan_events_.size() > 0) {
359       bt_log_->set_num_scan_event(scan_events_.size());
360     }
361     scan_events_.clear();
362     bt_log_->SerializeToString(&bt_log_str_);
363   }
364 
ClearLog()365   void ClearLog() {
366     for (BluetoothSession* session : bt_sessions_) {
367       session->Clear();
368       delete session;
369     }
370     bt_sessions_.clear();
371     for (PairEvent* event : pair_events_) {
372       event->Clear();
373       delete event;
374     }
375     pair_events_.clear();
376     for (WakeEvent* event : wake_events_) {
377       event->Clear();
378       delete event;
379     }
380     wake_events_.clear();
381     for (ScanEvent* event : scan_events_) {
382       event->Clear();
383       delete event;
384     }
385     scan_events_.clear();
386     bt_log_->Clear();
387   }
388 
SetUp()389   void SetUp() {
390     bt_log_ = new BluetoothLog();
391     // Clear existing metrics entries, if any
392     BluetoothMetricsLogger::GetInstance()->Reset();
393   }
TearDown()394   void TearDown() {
395     // Clear remaining metrics entries, if any
396     BluetoothMetricsLogger::GetInstance()->Reset();
397     ClearLog();
398     delete bt_log_;
399   }
400 
401  public:
402 };
403 
TEST_F(BluetoothMetricsLoggerTest,PairEventTest)404 TEST_F(BluetoothMetricsLoggerTest, PairEventTest) {
405   pair_events_.push_back(MakePairEvent(
406       35, 12345,
407       MakeDeviceInfo(
408           42, DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR)));
409   UpdateLog();
410   BluetoothMetricsLogger::GetInstance()->LogPairEvent(
411       35, 12345, 42, system_bt_osi::DEVICE_TYPE_BREDR);
412   std::string msg_str;
413   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
414   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
415 }
416 
TEST_F(BluetoothMetricsLoggerTest,WakeEventTest)417 TEST_F(BluetoothMetricsLoggerTest, WakeEventTest) {
418   wake_events_.push_back(
419       MakeWakeEvent(WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED,
420                     "TEST_REQ", "TEST_NAME", 12345));
421   UpdateLog();
422   BluetoothMetricsLogger::GetInstance()->LogWakeEvent(
423       system_bt_osi::WAKE_EVENT_ACQUIRED, "TEST_REQ", "TEST_NAME", 12345);
424   std::string msg_str;
425   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
426   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
427 }
428 
TEST_F(BluetoothMetricsLoggerTest,WakeEventOverrunTest)429 TEST_F(BluetoothMetricsLoggerTest, WakeEventOverrunTest) {
430   GenerateWakeEvents(
431       kMaxEventGenerationLimit - BluetoothMetricsLogger::kMaxNumWakeEvent,
432       kMaxEventGenerationLimit, &wake_events_);
433   num_wake_event_ = kMaxEventGenerationLimit;
434   UpdateLog();
435   for (size_t i = 0; i < kMaxEventGenerationLimit; ++i) {
436     BluetoothMetricsLogger::GetInstance()->LogWakeEvent(
437         i % 2 == 0 ? system_bt_osi::WAKE_EVENT_ACQUIRED
438                    : system_bt_osi::WAKE_EVENT_RELEASED,
439         "TEST_REQ", "TEST_NAME", i);
440   }
441   std::string msg_str;
442   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
443   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
444 }
445 
TEST_F(BluetoothMetricsLoggerTest,ScanEventTest)446 TEST_F(BluetoothMetricsLoggerTest, ScanEventTest) {
447   scan_events_.push_back(MakeScanEvent(
448       ScanEvent_ScanEventType::ScanEvent_ScanEventType_SCAN_EVENT_STOP,
449       "TEST_INITIATOR", ScanEvent_ScanTechnologyType::
450                             ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BREDR,
451       42, 123456));
452   UpdateLog();
453   BluetoothMetricsLogger::GetInstance()->LogScanEvent(
454       false, "TEST_INITIATOR", system_bt_osi::SCAN_TECH_TYPE_BREDR, 42, 123456);
455   std::string msg_str;
456   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
457   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
458 }
459 
TEST_F(BluetoothMetricsLoggerTest,BluetoothSessionTest)460 TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionTest) {
461   bt_sessions_.push_back(MakeBluetoothSession(
462       10,
463       BluetoothSession_ConnectionTechnologyType::
464           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE,
465       BluetoothSession_DisconnectReasonType::
466           BluetoothSession_DisconnectReasonType_UNKNOWN,
467       nullptr, nullptr, nullptr));
468   UpdateLog();
469   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
470       system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_LE, 123456);
471   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
472       system_bt_osi::DISCONNECT_REASON_UNKNOWN, 133456);
473   std::string msg_str;
474   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
475   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
476 }
477 
TEST_F(BluetoothMetricsLoggerTest,BluetoothSessionDumpBeforeEndTest)478 TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionDumpBeforeEndTest) {
479   bt_sessions_.push_back(MakeBluetoothSession(
480       1,
481       BluetoothSession_ConnectionTechnologyType::
482           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE,
483       BluetoothSession_DisconnectReasonType::
484           BluetoothSession_DisconnectReasonType_METRICS_DUMP,
485       nullptr, nullptr, nullptr));
486   UpdateLog();
487   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
488       system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_LE, time_get_os_boottime_ms());
489   sleep_ms(1000);
490   std::string msg_str;
491   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
492   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
493 }
494 
TEST_F(BluetoothMetricsLoggerTest,BluetoothSessionStartBeforeEndTest)495 TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionStartBeforeEndTest) {
496   bt_sessions_.push_back(MakeBluetoothSession(
497       1,
498       BluetoothSession_ConnectionTechnologyType::
499           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_UNKNOWN,
500       BluetoothSession_DisconnectReasonType::
501           BluetoothSession_DisconnectReasonType_NEXT_START_WITHOUT_END_PREVIOUS,
502       nullptr, nullptr, nullptr));
503   bt_sessions_.push_back(MakeBluetoothSession(
504       2,
505       BluetoothSession_ConnectionTechnologyType::
506           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE,
507       BluetoothSession_DisconnectReasonType::
508           BluetoothSession_DisconnectReasonType_METRICS_DUMP,
509       nullptr, nullptr, nullptr));
510   UpdateLog();
511   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
512       system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, 0);
513   sleep_ms(1000);
514   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
515       system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_LE, 0);
516   sleep_ms(2000);
517   std::string msg_str;
518   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
519   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
520 }
521 
522 /*
523  * Test Case: A2DPSessionTwoUpdatesTest
524  *
525  * 1. Create Instance
526  * 2. LogBluetoothSessionStart
527  * 3. LogBluetoothSessionDeviceInfo
528  * 4. LogA2dpSession
529  * 5. LogA2dpSession
530  * 6. LogBluetoothSessionEnd
531  * 7. WriteString
532  *
533  */
TEST_F(BluetoothMetricsLoggerTest,A2DPSessionTwoUpdatesTest)534 TEST_F(BluetoothMetricsLoggerTest, A2DPSessionTwoUpdatesTest) {
535   /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */
536   A2dpSessionMetrics metrics1;
537   A2dpSessionMetrics metrics2;
538   A2dpSessionMetrics metrics_sum;
539   metrics1.audio_duration_ms = 10;
540   metrics2.audio_duration_ms = 25;
541   metrics_sum.audio_duration_ms = 35;
542   metrics1.media_timer_min_ms = 10;
543   metrics2.media_timer_min_ms = 25;
544   metrics_sum.media_timer_min_ms = 10;
545   metrics1.media_timer_max_ms = 100;
546   metrics2.media_timer_max_ms = 200;
547   metrics_sum.media_timer_max_ms = 200;
548   metrics1.media_timer_avg_ms = 50;
549   metrics1.total_scheduling_count = 50;
550   metrics2.media_timer_avg_ms = 100;
551   metrics2.total_scheduling_count = 50;
552   metrics_sum.media_timer_avg_ms = 75;
553   metrics_sum.total_scheduling_count = 100;
554   metrics1.buffer_overruns_max_count = 70;
555   metrics2.buffer_overruns_max_count = 80;
556   metrics_sum.buffer_overruns_max_count = 80;
557   metrics1.buffer_underruns_average = 80;
558   metrics1.buffer_underruns_count = 1200;
559   metrics2.buffer_underruns_average = 130;
560   metrics2.buffer_underruns_count = 2400;
561   metrics_sum.buffer_underruns_average = 113.33333333;
562   metrics_sum.buffer_underruns_count = 3600;
563   DeviceInfo* info = MakeDeviceInfo(
564       BTM_COD_MAJOR_AUDIO_TEST,
565       DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
566   A2DPSession* session = MakeA2DPSession(metrics_sum);
567   bt_sessions_.push_back(MakeBluetoothSession(
568       10,
569       BluetoothSession_ConnectionTechnologyType::
570           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
571       BluetoothSession_DisconnectReasonType::
572           BluetoothSession_DisconnectReasonType_UNKNOWN,
573       info, nullptr, session));
574   UpdateLog();
575   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
576       system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_BREDR, 123456);
577   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
578       BTM_COD_MAJOR_AUDIO_TEST, system_bt_osi::DEVICE_TYPE_BREDR);
579   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
580   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
581   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
582       system_bt_osi::DISCONNECT_REASON_UNKNOWN, 133456);
583   std::string msg_str;
584   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
585   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
586 }
587 
588 /*
589  * Test Case: A2DPSessionTwoUpdatesSeparatedbyDumpTest
590  *
591  * 1. Create Instance
592  * 2. LogBluetoothSessionStart
593  * 3. LogBluetoothSessionDeviceInfo
594  * 4. LogA2dpSession
595  * 5. WriteString
596  * 6. LogA2dpSession
597  * 7. LogBluetoothSessionEnd
598  * 8. WriteString
599  *
600  */
TEST_F(BluetoothMetricsLoggerTest,A2DPSessionTwoUpdatesSeparatedbyDumpTest)601 TEST_F(BluetoothMetricsLoggerTest, A2DPSessionTwoUpdatesSeparatedbyDumpTest) {
602   /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */
603   A2dpSessionMetrics metrics1;
604   A2dpSessionMetrics metrics2;
605   metrics1.audio_duration_ms = 10;
606   metrics2.audio_duration_ms = 25;
607   metrics1.media_timer_min_ms = 10;
608   metrics2.media_timer_min_ms = 25;
609   metrics1.media_timer_max_ms = 100;
610   metrics2.media_timer_max_ms = 200;
611   metrics1.media_timer_avg_ms = 50;
612   metrics1.total_scheduling_count = 50;
613   metrics2.media_timer_avg_ms = 100;
614   metrics2.total_scheduling_count = 50;
615   metrics1.buffer_overruns_max_count = 70;
616   metrics2.buffer_overruns_max_count = 80;
617   metrics1.buffer_underruns_average = 80;
618   metrics1.buffer_underruns_count = 1200;
619   metrics2.buffer_underruns_average = 130;
620   metrics2.buffer_underruns_count = 2400;
621   DeviceInfo* info = MakeDeviceInfo(
622       BTM_COD_MAJOR_AUDIO_TEST,
623       DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
624   A2DPSession* session = MakeA2DPSession(metrics1);
625   bt_sessions_.push_back(MakeBluetoothSession(
626       1,
627       BluetoothSession_ConnectionTechnologyType::
628           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
629       BluetoothSession_DisconnectReasonType::
630           BluetoothSession_DisconnectReasonType_METRICS_DUMP,
631       info, nullptr, session));
632   UpdateLog();
633   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
634       system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
635   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
636       BTM_COD_MAJOR_AUDIO_TEST, system_bt_osi::DEVICE_TYPE_BREDR);
637   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
638   sleep_ms(1000);
639   std::string msg_str;
640   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
641   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
642   ClearLog();
643   info = MakeDeviceInfo(
644       BTM_COD_MAJOR_AUDIO_TEST,
645       DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
646   session = MakeA2DPSession(metrics2);
647   bt_sessions_.push_back(MakeBluetoothSession(
648       1,
649       BluetoothSession_ConnectionTechnologyType::
650           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
651       BluetoothSession_DisconnectReasonType::
652           BluetoothSession_DisconnectReasonType_UNKNOWN,
653       info, nullptr, session));
654   UpdateLog();
655   sleep_ms(1000);
656   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
657   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
658       system_bt_osi::DISCONNECT_REASON_UNKNOWN, 0);
659   msg_str.clear();
660   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
661   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
662 }
663 
664 /*
665  * Test Case 1: A2DPSessionOnlyTest
666  *
667  * 1. Create Instance
668  * 4. LogA2dpSession
669  * 5. WriteString
670  * 6. LogA2dpSession
671  * 8. WriteString
672  *
673  */
TEST_F(BluetoothMetricsLoggerTest,A2DPSessionOnlyTest)674 TEST_F(BluetoothMetricsLoggerTest, A2DPSessionOnlyTest) {
675   /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */
676   A2dpSessionMetrics metrics1;
677   A2dpSessionMetrics metrics2;
678   A2dpSessionMetrics metrics_sum;
679   metrics1.audio_duration_ms = 10;
680   metrics2.audio_duration_ms = 25;
681   metrics_sum.audio_duration_ms = 35;
682   metrics1.media_timer_min_ms = 10;
683   metrics2.media_timer_min_ms = 25;
684   metrics_sum.media_timer_min_ms = 10;
685   metrics1.media_timer_max_ms = 100;
686   metrics2.media_timer_max_ms = 200;
687   metrics_sum.media_timer_max_ms = 200;
688   metrics1.media_timer_avg_ms = 50;
689   metrics1.total_scheduling_count = 50;
690   metrics2.media_timer_avg_ms = 100;
691   metrics2.total_scheduling_count = 50;
692   metrics_sum.media_timer_avg_ms = 75;
693   metrics_sum.total_scheduling_count = 100;
694   metrics1.buffer_overruns_max_count = 70;
695   metrics2.buffer_overruns_max_count = 80;
696   metrics_sum.buffer_overruns_max_count = 80;
697   metrics1.buffer_underruns_average = 80;
698   metrics1.buffer_underruns_count = 1200;
699   metrics2.buffer_underruns_average = 130;
700   metrics2.buffer_underruns_count = 2400;
701   metrics_sum.buffer_underruns_average = 113.33333333;
702   metrics_sum.buffer_underruns_count = 3600;
703   DeviceInfo* info = MakeDeviceInfo(
704       BTM_COD_MAJOR_AUDIO_TEST,
705       DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
706   A2DPSession* session = MakeA2DPSession(metrics_sum);
707   bt_sessions_.push_back(MakeBluetoothSession(
708       1,
709       BluetoothSession_ConnectionTechnologyType::
710           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
711       BluetoothSession_DisconnectReasonType::
712           BluetoothSession_DisconnectReasonType_METRICS_DUMP,
713       info, nullptr, session));
714   UpdateLog();
715   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
716   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
717   sleep_ms(1000);
718   std::string msg_str;
719   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
720   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
721 }
722 
723 /*
724  * Test Case: A2DPSessionDumpBeforeTwoUpdatesTest
725  *
726  * 1. Create Instance
727  * 2. LogBluetoothSessionStart
728  * 3. LogBluetoothSessionDeviceInfo
729  * 5. WriteString
730  * 6. LogA2dpSession
731  * 7. LogA2dpSession
732  * 8. LogBluetoothSessionEnd
733  * 9. WriteString
734  *
735  */
TEST_F(BluetoothMetricsLoggerTest,A2DPSessionDumpBeforeTwoUpdatesTest)736 TEST_F(BluetoothMetricsLoggerTest, A2DPSessionDumpBeforeTwoUpdatesTest) {
737   /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */
738   A2dpSessionMetrics metrics1;
739   A2dpSessionMetrics metrics2;
740   A2dpSessionMetrics metrics_sum;
741   metrics1.audio_duration_ms = 10;
742   metrics2.audio_duration_ms = 25;
743   metrics_sum.audio_duration_ms = 35;
744   metrics1.media_timer_min_ms = 10;
745   metrics2.media_timer_min_ms = 25;
746   metrics_sum.media_timer_min_ms = 10;
747   metrics1.media_timer_max_ms = 100;
748   metrics2.media_timer_max_ms = 200;
749   metrics_sum.media_timer_max_ms = 200;
750   metrics1.media_timer_avg_ms = 50;
751   metrics1.total_scheduling_count = 50;
752   metrics2.media_timer_avg_ms = 100;
753   metrics2.total_scheduling_count = 50;
754   metrics_sum.media_timer_avg_ms = 75;
755   metrics_sum.total_scheduling_count = 100;
756   metrics1.buffer_overruns_max_count = 70;
757   metrics2.buffer_overruns_max_count = 80;
758   metrics_sum.buffer_overruns_max_count = 80;
759   metrics1.buffer_underruns_average = 80;
760   metrics1.buffer_underruns_count = 1200;
761   metrics2.buffer_underruns_average = 130;
762   metrics2.buffer_underruns_count = 2400;
763   metrics_sum.buffer_underruns_average = 113.33333333;
764   metrics_sum.buffer_underruns_count = 3600;
765   DeviceInfo* info = MakeDeviceInfo(
766       BTM_COD_MAJOR_AUDIO_TEST,
767       DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
768   bt_sessions_.push_back(MakeBluetoothSession(
769       1,
770       BluetoothSession_ConnectionTechnologyType::
771           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
772       BluetoothSession_DisconnectReasonType::
773           BluetoothSession_DisconnectReasonType_METRICS_DUMP,
774       info, nullptr, nullptr));
775   UpdateLog();
776   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
777       system_bt_osi::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
778   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
779       BTM_COD_MAJOR_AUDIO_TEST, system_bt_osi::DEVICE_TYPE_BREDR);
780   sleep_ms(1000);
781   std::string msg_str;
782   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
783   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
784   ClearLog();
785   info = MakeDeviceInfo(
786       BTM_COD_MAJOR_AUDIO_TEST,
787       DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
788   A2DPSession* session = MakeA2DPSession(metrics_sum);
789   bt_sessions_.push_back(MakeBluetoothSession(
790       1,
791       BluetoothSession_ConnectionTechnologyType::
792           BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
793       BluetoothSession_DisconnectReasonType::
794           BluetoothSession_DisconnectReasonType_UNKNOWN,
795       info, nullptr, session));
796   UpdateLog();
797   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
798   BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
799   sleep_ms(1000);
800   BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
801       system_bt_osi::DISCONNECT_REASON_UNKNOWN, 0);
802   msg_str.clear();
803   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
804   EXPECT_THAT(msg_str, StrEq(bt_log_str_));
805 }
806 
TEST_F(BluetoothMetricsLoggerTest,LogHeadsetProfileRfcConnectionTest)807 TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionTest) {
808   BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
809       BTA_HSP_SERVICE_ID);
810   BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
811       BTA_HFP_SERVICE_ID);
812   BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
813       BTA_HFP_SERVICE_ID);
814   std::string msg_str;
815   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
816   BluetoothLog* metrics = BluetoothLog::default_instance().New();
817   metrics->ParseFromString(msg_str);
818   EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 2);
819   bool hfp_correct = false;
820   bool hsp_correct = false;
821   for (const HeadsetProfileConnectionStats& headset_profile_connection_stats :
822        metrics->headset_profile_connection_stats()) {
823     switch (headset_profile_connection_stats.headset_profile_type()) {
824       case HeadsetProfileType::HFP:
825         EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 2);
826         hfp_correct = true;
827         break;
828       case HeadsetProfileType::HSP:
829         EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
830         hsp_correct = true;
831         break;
832       default:
833         FAIL();
834     }
835   }
836   EXPECT_TRUE(hfp_correct);
837   EXPECT_TRUE(hsp_correct);
838   metrics->clear_headset_profile_connection_stats();
839   EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
840   msg_str.clear();
841   // Verify that dump after clean up result in an empty list
842   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
843   metrics->ParseFromString(msg_str);
844   EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
845   delete metrics;
846 }
847 
TEST_F(BluetoothMetricsLoggerTest,LogHeadsetProfileRfcConnectionErrorTest)848 TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionErrorTest) {
849   BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
850       BTA_HSP_SERVICE_ID);
851   BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
852       BTA_HFP_SERVICE_ID);
853   BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
854       BTA_BIP_SERVICE_ID);
855   BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
856       BTA_HSP_SERVICE_ID);
857   std::string msg_str;
858   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
859   BluetoothLog* metrics = BluetoothLog::default_instance().New();
860   metrics->ParseFromString(msg_str);
861   EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 3);
862   bool hfp_correct = false;
863   bool hsp_correct = false;
864   bool unknown_correct = false;
865   for (const HeadsetProfileConnectionStats& headset_profile_connection_stats :
866        metrics->headset_profile_connection_stats()) {
867     switch (headset_profile_connection_stats.headset_profile_type()) {
868       case HeadsetProfileType::HFP:
869         EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
870         hfp_correct = true;
871         break;
872       case HeadsetProfileType::HSP:
873         EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 2);
874         hsp_correct = true;
875         break;
876       default:
877         EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
878         unknown_correct = true;
879         break;
880     }
881   }
882   EXPECT_TRUE(hfp_correct);
883   EXPECT_TRUE(hsp_correct);
884   EXPECT_TRUE(unknown_correct);
885   metrics->clear_headset_profile_connection_stats();
886   EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
887   // Verify that dump after clean up result in an empty list
888   BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
889   metrics->ParseFromString(msg_str);
890   EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
891   delete metrics;
892 }
893 }
894