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