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