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