1 /******************************************************************************
3 * Copyright 2016 Google, Inc.
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
27 #include <base/logging.h>
28 #include <include/hardware/bt_av.h>
30 #include "bluetooth/metrics/bluetooth.pb.h"
31 #include "common/metrics.h"
32 #include "common/time_util.h"
34 #define BTM_COD_MAJOR_AUDIO_TEST 0x04
38 using bluetooth::common::A2dpSessionMetrics;
39 using bluetooth::common::BluetoothMetricsLogger;
40 using bluetooth::metrics::BluetoothMetricsProto::A2DPSession;
41 using bluetooth::metrics::BluetoothMetricsProto::A2dpSourceCodec;
42 using bluetooth::metrics::BluetoothMetricsProto::BluetoothLog;
43 using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession;
44 using bluetooth::metrics::BluetoothMetricsProto::
45 BluetoothSession_ConnectionTechnologyType;
46 using bluetooth::metrics::BluetoothMetricsProto::
47 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;
61 const size_t kMaxEventGenerationLimit = 5000;
64 static void sleep_ms(int64_t t) {
65 std::this_thread::sleep_for(std::chrono::milliseconds(t));
68 DeviceInfo* MakeDeviceInfo(int32_t device_class,
69 DeviceInfo_DeviceType device_type) {
70 DeviceInfo* info = new DeviceInfo();
71 info->set_device_class(device_class);
72 info->set_device_type(device_type);
76 PairEvent* MakePairEvent(int32_t disconnect_reason, int64_t timestamp_ms,
77 DeviceInfo* device_info) {
78 PairEvent* event = new PairEvent();
79 event->set_disconnect_reason(disconnect_reason);
80 event->set_event_time_millis(timestamp_ms);
81 if (device_info) event->set_allocated_device_paired_with(device_info);
85 WakeEvent* MakeWakeEvent(WakeEvent_WakeEventType event_type,
86 const std::string& requestor, const std::string& name,
87 int64_t timestamp_ms) {
88 WakeEvent* event = new WakeEvent();
89 event->set_wake_event_type(event_type);
90 event->set_requestor(requestor);
91 event->set_name(name);
92 event->set_event_time_millis(timestamp_ms);
96 ScanEvent* MakeScanEvent(ScanEvent_ScanEventType event_type,
97 const std::string& initiator,
98 ScanEvent_ScanTechnologyType tech_type,
99 int32_t num_results, int64_t timestamp_ms) {
100 ScanEvent* event = new ScanEvent();
101 event->set_scan_event_type(event_type);
102 event->set_initiator(initiator);
103 event->set_scan_technology_type(tech_type);
104 event->set_number_results(num_results);
105 event->set_event_time_millis(timestamp_ms);
109 A2DPSession* MakeA2DPSession(const A2dpSessionMetrics& metrics,
110 A2dpSourceCodec source_codec) {
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 session->set_source_codec(source_codec);
121 session->set_is_a2dp_offload(metrics.is_a2dp_offload);
125 BluetoothSession* MakeBluetoothSession(
126 int64_t session_duration_sec,
127 BluetoothSession_ConnectionTechnologyType conn_type,
128 BluetoothSession_DisconnectReasonType disconnect_reason,
129 DeviceInfo* device_info, RFCommSession* rfcomm_session,
130 A2DPSession* a2dp_session) {
131 BluetoothSession* session = new BluetoothSession();
132 if (a2dp_session) session->set_allocated_a2dp_session(a2dp_session);
133 if (rfcomm_session) session->set_allocated_rfcomm_session(rfcomm_session);
134 if (device_info) session->set_allocated_device_connected_to(device_info);
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);
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);
150 for (PairEvent* event : pair_events) {
151 bt_log->mutable_pair_event()->AddAllocated(event);
154 for (WakeEvent* event : wake_events) {
155 bt_log->mutable_wake_event()->AddAllocated(event);
158 for (ScanEvent* event : scan_events) {
159 bt_log->mutable_scan_event()->AddAllocated(event);
165 void GenerateWakeEvents(size_t start, size_t end,
166 std::vector<WakeEvent*>* wake_events) {
167 for (size_t i = start; i < end; ++i) {
168 wake_events->push_back(MakeWakeEvent(
169 i % 2 == 0 ? WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED
170 : WakeEvent_WakeEventType::WakeEvent_WakeEventType_RELEASED,
171 "TEST_REQ", "TEST_NAME", i));
175 #define COMPARE_A2DP_METRICS(a, b) \
177 EXPECT_EQ((a).audio_duration_ms, (b).audio_duration_ms); \
178 EXPECT_EQ((a).media_timer_min_ms, (b).media_timer_min_ms); \
179 EXPECT_EQ((a).media_timer_max_ms, (b).media_timer_max_ms); \
180 EXPECT_EQ((a).media_timer_avg_ms, (b).media_timer_avg_ms); \
181 EXPECT_EQ((a).total_scheduling_count, (b).total_scheduling_count); \
182 EXPECT_EQ((a).buffer_overruns_max_count, (b).buffer_overruns_max_count); \
183 EXPECT_EQ((a).buffer_overruns_total, (b).buffer_overruns_total); \
184 EXPECT_THAT((a).buffer_underruns_average, \
185 FloatNear((b).buffer_underruns_average, 0.01)); \
186 (a).buffer_underruns_average = (b).buffer_underruns_average; \
187 EXPECT_EQ((a).buffer_underruns_count, (b).buffer_underruns_count); \
188 EXPECT_EQ((a).codec_index, (b).codec_index); \
189 EXPECT_EQ((a).is_a2dp_offload, (b).is_a2dp_offload); \
193 * metrics_sum = metrics1 + metrics2
195 TEST(BluetoothA2DPSessionMetricsTest, TestUpdateNormal) {
196 A2dpSessionMetrics metrics1;
197 A2dpSessionMetrics metrics2;
198 A2dpSessionMetrics metrics_sum;
199 metrics1.audio_duration_ms = 10;
200 metrics2.audio_duration_ms = 25;
201 metrics_sum.audio_duration_ms = 35;
202 metrics1.media_timer_min_ms = 10;
203 metrics2.media_timer_min_ms = 25;
204 metrics_sum.media_timer_min_ms = 10;
205 metrics1.media_timer_max_ms = 100;
206 metrics2.media_timer_max_ms = 200;
207 metrics_sum.media_timer_max_ms = 200;
208 metrics1.media_timer_avg_ms = 50;
209 metrics1.total_scheduling_count = 50;
210 metrics2.media_timer_avg_ms = 100;
211 metrics2.total_scheduling_count = 50;
212 metrics_sum.media_timer_avg_ms = 75;
213 metrics_sum.total_scheduling_count = 100;
214 metrics1.buffer_overruns_max_count = 70;
215 metrics2.buffer_overruns_max_count = 80;
216 metrics_sum.buffer_overruns_max_count = 80;
217 metrics1.buffer_underruns_average = 80;
218 metrics1.buffer_underruns_count = 1200;
219 metrics2.buffer_underruns_average = 130;
220 metrics2.buffer_underruns_count = 2400;
221 metrics_sum.buffer_underruns_average = 113.33333333;
222 metrics_sum.buffer_underruns_count = 3600;
223 metrics1.codec_index = -1;
224 metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
225 metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
226 metrics1.is_a2dp_offload = false;
227 metrics2.is_a2dp_offload = true;
228 metrics_sum.is_a2dp_offload = true;
229 metrics1.Update(metrics2);
230 COMPARE_A2DP_METRICS(metrics1, metrics_sum);
231 EXPECT_TRUE(metrics1 == metrics_sum);
232 EXPECT_EQ(metrics1, metrics_sum);
235 TEST(BluetoothA2DPSessionMetricsTest, TestUpdateNew) {
236 A2dpSessionMetrics metrics1;
237 A2dpSessionMetrics metrics2;
238 A2dpSessionMetrics metrics_sum;
239 metrics2.audio_duration_ms = 25;
240 metrics_sum.audio_duration_ms = 25;
241 metrics2.media_timer_min_ms = 25;
242 metrics_sum.media_timer_min_ms = 25;
243 metrics2.media_timer_max_ms = 200;
244 metrics_sum.media_timer_max_ms = 200;
245 metrics2.media_timer_avg_ms = 100;
246 metrics2.total_scheduling_count = 50;
247 metrics_sum.media_timer_avg_ms = 100;
248 metrics_sum.total_scheduling_count = 50;
249 metrics2.buffer_overruns_max_count = 80;
250 metrics_sum.buffer_overruns_max_count = 80;
251 metrics2.buffer_underruns_average = 130;
252 metrics2.buffer_underruns_count = 2400;
253 metrics_sum.buffer_underruns_average = 130;
254 metrics_sum.buffer_underruns_count = 2400;
255 metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_APTX;
256 metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_APTX;
257 metrics2.is_a2dp_offload = true;
258 metrics_sum.is_a2dp_offload = true;
259 metrics1.Update(metrics2);
260 COMPARE_A2DP_METRICS(metrics1, metrics_sum);
261 EXPECT_TRUE(metrics1 == metrics_sum);
262 EXPECT_EQ(metrics1, metrics_sum);
265 TEST(BluetoothA2DPSessionMetricsTest, TestNullUpdate) {
266 A2dpSessionMetrics metrics1;
267 A2dpSessionMetrics metrics2;
268 A2dpSessionMetrics metrics_sum;
269 metrics2.audio_duration_ms = 25;
270 metrics_sum.audio_duration_ms = 25;
271 metrics2.media_timer_min_ms = 25;
272 metrics_sum.media_timer_min_ms = 25;
273 metrics2.media_timer_max_ms = 200;
274 metrics_sum.media_timer_max_ms = 200;
275 metrics2.media_timer_avg_ms = 100;
276 metrics2.total_scheduling_count = 50;
277 metrics_sum.media_timer_avg_ms = 100;
278 metrics_sum.total_scheduling_count = 50;
279 metrics2.buffer_overruns_max_count = 80;
280 metrics_sum.buffer_overruns_max_count = 80;
281 metrics2.buffer_underruns_average = 130;
282 metrics2.buffer_underruns_count = 2400;
283 metrics_sum.buffer_underruns_average = 130;
284 metrics_sum.buffer_underruns_count = 2400;
285 metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD;
286 metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_APTX_HD;
287 metrics2.is_a2dp_offload = true;
288 metrics_sum.is_a2dp_offload = true;
289 metrics2.Update(metrics1);
290 COMPARE_A2DP_METRICS(metrics2, metrics_sum);
291 EXPECT_TRUE(metrics2 == metrics_sum);
292 EXPECT_EQ(metrics2, metrics_sum);
295 TEST(BluetoothA2DPSessionMetricsTest, TestPartialUpdate) {
296 A2dpSessionMetrics metrics1;
297 A2dpSessionMetrics metrics2;
298 A2dpSessionMetrics metrics_sum;
299 metrics1.audio_duration_ms = 10;
300 metrics2.audio_duration_ms = 25;
301 metrics_sum.audio_duration_ms = 35;
302 metrics1.media_timer_min_ms = 10;
303 metrics_sum.media_timer_min_ms = 10;
304 metrics1.media_timer_max_ms = 100;
305 metrics_sum.media_timer_max_ms = 100;
306 metrics1.media_timer_avg_ms = 50;
307 metrics1.total_scheduling_count = 50;
308 metrics2.media_timer_avg_ms = 100;
309 metrics_sum.media_timer_avg_ms = 50;
310 metrics_sum.total_scheduling_count = 50;
311 metrics1.buffer_overruns_max_count = 70;
312 metrics_sum.buffer_overruns_max_count = 70;
313 metrics1.buffer_underruns_average = 80;
314 metrics1.buffer_underruns_count = 1200;
315 metrics2.buffer_underruns_count = 2400;
316 metrics_sum.buffer_underruns_average = 80;
317 metrics_sum.buffer_underruns_count = 1200;
318 metrics1.Update(metrics2);
319 COMPARE_A2DP_METRICS(metrics1, metrics_sum);
320 EXPECT_TRUE(metrics1 == metrics_sum);
321 EXPECT_EQ(metrics1, metrics_sum);
324 class BluetoothMetricsLoggerTest : public Test {
326 // Use to hold test protos
327 std::vector<PairEvent*> pair_events_;
328 std::vector<WakeEvent*> wake_events_;
329 std::vector<ScanEvent*> scan_events_;
330 std::vector<BluetoothSession*> bt_sessions_;
331 int64_t num_pair_event_ = 0;
332 int64_t num_wake_event_ = 0;
333 int64_t num_scan_event_ = 0;
334 int64_t num_bt_session_ = 0;
335 BluetoothLog* bt_log_;
336 std::string bt_log_str_;
337 std::string bt_log_ascii_str_;
340 for (BluetoothSession* session : bt_sessions_) {
341 bt_log_->mutable_session()->AddAllocated(session);
343 if (num_bt_session_ > 0) {
344 bt_log_->set_num_bluetooth_session(num_bt_session_);
345 } else if (bt_sessions_.size() > 0) {
346 bt_log_->set_num_bluetooth_session(bt_sessions_.size());
348 bt_sessions_.clear();
349 for (PairEvent* event : pair_events_) {
350 bt_log_->mutable_pair_event()->AddAllocated(event);
352 if (num_pair_event_ > 0) {
353 bt_log_->set_num_pair_event(num_pair_event_);
354 } else if (pair_events_.size() > 0) {
355 bt_log_->set_num_pair_event(pair_events_.size());
357 pair_events_.clear();
358 for (WakeEvent* event : wake_events_) {
359 bt_log_->mutable_wake_event()->AddAllocated(event);
361 if (num_wake_event_ > 0) {
362 bt_log_->set_num_wake_event(num_wake_event_);
363 } else if (wake_events_.size() > 0) {
364 bt_log_->set_num_wake_event(wake_events_.size());
366 wake_events_.clear();
367 for (ScanEvent* event : scan_events_) {
368 bt_log_->mutable_scan_event()->AddAllocated(event);
370 if (num_scan_event_ > 0) {
371 bt_log_->set_num_scan_event(num_scan_event_);
372 } else if (scan_events_.size() > 0) {
373 bt_log_->set_num_scan_event(scan_events_.size());
375 scan_events_.clear();
376 bt_log_->SerializeToString(&bt_log_str_);
380 for (BluetoothSession* session : bt_sessions_) {
384 bt_sessions_.clear();
385 for (PairEvent* event : pair_events_) {
389 pair_events_.clear();
390 for (WakeEvent* event : wake_events_) {
394 wake_events_.clear();
395 for (ScanEvent* event : scan_events_) {
399 scan_events_.clear();
404 bt_log_ = new BluetoothLog();
405 // Clear existing metrics entries, if any
406 BluetoothMetricsLogger::GetInstance()->Reset();
409 // Clear remaining metrics entries, if any
410 BluetoothMetricsLogger::GetInstance()->Reset();
418 TEST_F(BluetoothMetricsLoggerTest, PairEventTest) {
419 pair_events_.push_back(MakePairEvent(
422 42, DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR)));
424 BluetoothMetricsLogger::GetInstance()->LogPairEvent(
425 35, 12345, 42, bluetooth::common::DEVICE_TYPE_BREDR);
427 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
428 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
431 TEST_F(BluetoothMetricsLoggerTest, WakeEventTest) {
432 wake_events_.push_back(
433 MakeWakeEvent(WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED,
434 "TEST_REQ", "TEST_NAME", 12345));
436 BluetoothMetricsLogger::GetInstance()->LogWakeEvent(
437 bluetooth::common::WAKE_EVENT_ACQUIRED, "TEST_REQ", "TEST_NAME", 12345);
439 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
440 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
443 TEST_F(BluetoothMetricsLoggerTest, WakeEventOverrunTest) {
445 kMaxEventGenerationLimit - BluetoothMetricsLogger::kMaxNumWakeEvent,
446 kMaxEventGenerationLimit, &wake_events_);
447 num_wake_event_ = kMaxEventGenerationLimit;
449 for (size_t i = 0; i < kMaxEventGenerationLimit; ++i) {
450 BluetoothMetricsLogger::GetInstance()->LogWakeEvent(
451 i % 2 == 0 ? bluetooth::common::WAKE_EVENT_ACQUIRED
452 : bluetooth::common::WAKE_EVENT_RELEASED,
453 "TEST_REQ", "TEST_NAME", i);
456 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
457 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
460 TEST_F(BluetoothMetricsLoggerTest, ScanEventTest) {
461 scan_events_.push_back(MakeScanEvent(
462 ScanEvent_ScanEventType::ScanEvent_ScanEventType_SCAN_EVENT_STOP,
464 ScanEvent_ScanTechnologyType::
465 ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BREDR,
468 BluetoothMetricsLogger::GetInstance()->LogScanEvent(
469 false, "TEST_INITIATOR", bluetooth::common::SCAN_TECH_TYPE_BREDR, 42,
472 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
473 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
476 TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionTest) {
477 bt_sessions_.push_back(MakeBluetoothSession(
479 BluetoothSession_ConnectionTechnologyType::
480 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE,
481 BluetoothSession_DisconnectReasonType::
482 BluetoothSession_DisconnectReasonType_UNKNOWN,
483 nullptr, nullptr, nullptr));
485 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
486 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_LE, 123456);
487 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
488 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 133456);
490 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
491 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
494 TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionDumpBeforeEndTest) {
495 bt_sessions_.push_back(MakeBluetoothSession(
497 BluetoothSession_ConnectionTechnologyType::
498 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE,
499 BluetoothSession_DisconnectReasonType::
500 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
501 nullptr, nullptr, nullptr));
503 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
504 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_LE,
505 bluetooth::common::time_get_os_boottime_ms());
508 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
509 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
512 TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionStartBeforeEndTest) {
513 bt_sessions_.push_back(MakeBluetoothSession(
515 BluetoothSession_ConnectionTechnologyType::
516 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_UNKNOWN,
517 BluetoothSession_DisconnectReasonType::
518 BluetoothSession_DisconnectReasonType_NEXT_START_WITHOUT_END_PREVIOUS,
519 nullptr, nullptr, nullptr));
520 bt_sessions_.push_back(MakeBluetoothSession(
522 BluetoothSession_ConnectionTechnologyType::
523 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE,
524 BluetoothSession_DisconnectReasonType::
525 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
526 nullptr, nullptr, nullptr));
528 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
529 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, 0);
531 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
532 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_LE, 0);
535 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
536 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
540 * Test Case: A2DPSessionTwoUpdatesTest
543 * 2. LogBluetoothSessionStart
544 * 3. LogBluetoothSessionDeviceInfo
547 * 6. LogBluetoothSessionEnd
551 TEST_F(BluetoothMetricsLoggerTest, A2DPSessionTwoUpdatesTest) {
552 /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */
553 A2dpSessionMetrics metrics1;
554 A2dpSessionMetrics metrics2;
555 A2dpSessionMetrics metrics_sum;
556 metrics1.audio_duration_ms = 10;
557 metrics2.audio_duration_ms = 25;
558 metrics_sum.audio_duration_ms = 35;
559 metrics1.media_timer_min_ms = 10;
560 metrics2.media_timer_min_ms = 25;
561 metrics_sum.media_timer_min_ms = 10;
562 metrics1.media_timer_max_ms = 100;
563 metrics2.media_timer_max_ms = 200;
564 metrics_sum.media_timer_max_ms = 200;
565 metrics1.media_timer_avg_ms = 50;
566 metrics1.total_scheduling_count = 50;
567 metrics2.media_timer_avg_ms = 100;
568 metrics2.total_scheduling_count = 50;
569 metrics_sum.media_timer_avg_ms = 75;
570 metrics_sum.total_scheduling_count = 100;
571 metrics1.buffer_overruns_max_count = 70;
572 metrics2.buffer_overruns_max_count = 80;
573 metrics_sum.buffer_overruns_max_count = 80;
574 metrics1.buffer_underruns_average = 80;
575 metrics1.buffer_underruns_count = 1200;
576 metrics2.buffer_underruns_average = 130;
577 metrics2.buffer_underruns_count = 2400;
578 metrics_sum.buffer_underruns_average = 113.33333333;
579 metrics_sum.buffer_underruns_count = 3600;
580 metrics1.codec_index = -1;
581 metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
582 metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
583 metrics1.is_a2dp_offload = false;
584 metrics2.is_a2dp_offload = true;
585 metrics_sum.is_a2dp_offload = true;
586 DeviceInfo* info = MakeDeviceInfo(
587 BTM_COD_MAJOR_AUDIO_TEST,
588 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
589 A2DPSession* session =
590 MakeA2DPSession(metrics_sum, A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC);
591 bt_sessions_.push_back(MakeBluetoothSession(
593 BluetoothSession_ConnectionTechnologyType::
594 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
595 BluetoothSession_DisconnectReasonType::
596 BluetoothSession_DisconnectReasonType_UNKNOWN,
597 info, nullptr, session));
599 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
600 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 123456);
601 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
602 BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR);
603 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
604 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
605 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
606 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 133456);
608 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
609 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
613 * Test Case: A2DPSessionTwoUpdatesSeparatedbyDumpTest
616 * 2. LogBluetoothSessionStart
617 * 3. LogBluetoothSessionDeviceInfo
621 * 7. LogBluetoothSessionEnd
625 TEST_F(BluetoothMetricsLoggerTest, A2DPSessionTwoUpdatesSeparatedbyDumpTest) {
626 /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */
627 A2dpSessionMetrics metrics1;
628 A2dpSessionMetrics metrics2;
629 metrics1.audio_duration_ms = 10;
630 metrics2.audio_duration_ms = 25;
631 metrics1.media_timer_min_ms = 10;
632 metrics2.media_timer_min_ms = 25;
633 metrics1.media_timer_max_ms = 100;
634 metrics2.media_timer_max_ms = 200;
635 metrics1.media_timer_avg_ms = 50;
636 metrics1.total_scheduling_count = 50;
637 metrics2.media_timer_avg_ms = 100;
638 metrics2.total_scheduling_count = 50;
639 metrics1.buffer_overruns_max_count = 70;
640 metrics2.buffer_overruns_max_count = 80;
641 metrics1.buffer_underruns_average = 80;
642 metrics1.buffer_underruns_count = 1200;
643 metrics2.buffer_underruns_average = 130;
644 metrics2.buffer_underruns_count = 2400;
645 metrics1.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
646 metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
647 DeviceInfo* info = MakeDeviceInfo(
648 BTM_COD_MAJOR_AUDIO_TEST,
649 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
650 A2DPSession* session =
651 MakeA2DPSession(metrics1, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC);
652 bt_sessions_.push_back(MakeBluetoothSession(
654 BluetoothSession_ConnectionTechnologyType::
655 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
656 BluetoothSession_DisconnectReasonType::
657 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
658 info, nullptr, session));
660 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
661 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
662 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
663 BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR);
664 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
667 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
668 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
670 info = MakeDeviceInfo(
671 BTM_COD_MAJOR_AUDIO_TEST,
672 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
673 session = MakeA2DPSession(metrics2, A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC);
674 bt_sessions_.push_back(MakeBluetoothSession(
676 BluetoothSession_ConnectionTechnologyType::
677 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
678 BluetoothSession_DisconnectReasonType::
679 BluetoothSession_DisconnectReasonType_UNKNOWN,
680 info, nullptr, session));
683 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
684 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
685 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
687 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
688 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
692 * Test Case: A2DPSessionTwoUpdatesSeparatedbyEndTest
695 * 2. LogBluetoothSessionStart
697 * 4. LogBluetoothSessionEnd
698 * 5. LogBluetoothSessionStart
700 * 7. LogBluetoothSessionEnd
704 TEST_F(BluetoothMetricsLoggerTest, A2DPSessionTwoUpdatesSeparatedbyEndTest) {
705 /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */
706 A2dpSessionMetrics metrics1;
707 metrics1.audio_duration_ms = 10;
708 metrics1.media_timer_min_ms = 10;
709 metrics1.media_timer_max_ms = 100;
710 metrics1.media_timer_avg_ms = 50;
711 metrics1.total_scheduling_count = 50;
712 metrics1.buffer_overruns_max_count = 70;
713 metrics1.buffer_underruns_average = 80;
714 metrics1.buffer_underruns_count = 1200;
715 metrics1.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
716 DeviceInfo* info = MakeDeviceInfo(
717 BTM_COD_MAJOR_AUDIO_TEST,
718 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
719 A2DPSession* session =
720 MakeA2DPSession(metrics1, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC);
721 bt_sessions_.push_back(MakeBluetoothSession(
723 BluetoothSession_ConnectionTechnologyType::
724 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
725 BluetoothSession_DisconnectReasonType::
726 BluetoothSession_DisconnectReasonType_UNKNOWN,
727 info, nullptr, session));
729 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
730 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
731 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
732 BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR);
733 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
735 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
736 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
738 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
739 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
741 A2dpSessionMetrics metrics2;
742 metrics2.audio_duration_ms = 25;
743 metrics2.media_timer_min_ms = 25;
744 metrics2.media_timer_max_ms = 200;
745 metrics2.media_timer_avg_ms = 100;
746 metrics2.total_scheduling_count = 50;
747 metrics2.buffer_overruns_max_count = 80;
748 metrics2.buffer_underruns_average = 130;
749 metrics2.buffer_underruns_count = 2400;
750 metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
751 session = MakeA2DPSession(metrics2, A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC);
752 bt_sessions_.push_back(MakeBluetoothSession(
754 BluetoothSession_ConnectionTechnologyType::
755 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
756 BluetoothSession_DisconnectReasonType::
757 BluetoothSession_DisconnectReasonType_UNKNOWN,
758 nullptr, nullptr, session));
760 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
761 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
763 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
764 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
765 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
767 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
768 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
772 * Test Case 1: A2DPSessionOnlyTest
781 TEST_F(BluetoothMetricsLoggerTest, A2DPSessionOnlyTest) {
782 /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */
783 A2dpSessionMetrics metrics1;
784 A2dpSessionMetrics metrics2;
785 A2dpSessionMetrics metrics_sum;
786 metrics1.audio_duration_ms = 10;
787 metrics2.audio_duration_ms = 25;
788 metrics_sum.audio_duration_ms = 35;
789 metrics1.media_timer_min_ms = 10;
790 metrics2.media_timer_min_ms = 25;
791 metrics_sum.media_timer_min_ms = 10;
792 metrics1.media_timer_max_ms = 100;
793 metrics2.media_timer_max_ms = 200;
794 metrics_sum.media_timer_max_ms = 200;
795 metrics1.media_timer_avg_ms = 50;
796 metrics1.total_scheduling_count = 50;
797 metrics2.media_timer_avg_ms = 100;
798 metrics2.total_scheduling_count = 50;
799 metrics_sum.media_timer_avg_ms = 75;
800 metrics_sum.total_scheduling_count = 100;
801 metrics1.buffer_overruns_max_count = 70;
802 metrics2.buffer_overruns_max_count = 80;
803 metrics_sum.buffer_overruns_max_count = 80;
804 metrics1.buffer_underruns_average = 80;
805 metrics1.buffer_underruns_count = 1200;
806 metrics2.buffer_underruns_average = 130;
807 metrics2.buffer_underruns_count = 2400;
808 metrics_sum.buffer_underruns_average = 113.33333333;
809 metrics_sum.buffer_underruns_count = 3600;
810 metrics1.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
811 metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
812 metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
813 DeviceInfo* info = MakeDeviceInfo(
814 BTM_COD_MAJOR_AUDIO_TEST,
815 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
816 A2DPSession* session =
817 MakeA2DPSession(metrics_sum, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC);
818 bt_sessions_.push_back(MakeBluetoothSession(
820 BluetoothSession_ConnectionTechnologyType::
821 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
822 BluetoothSession_DisconnectReasonType::
823 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
824 info, nullptr, session));
826 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
827 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
830 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
831 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
835 * Test Case: A2DPSessionDumpBeforeTwoUpdatesTest
838 * 2. LogBluetoothSessionStart
839 * 3. LogBluetoothSessionDeviceInfo
843 * 8. LogBluetoothSessionEnd
847 TEST_F(BluetoothMetricsLoggerTest, A2DPSessionDumpBeforeTwoUpdatesTest) {
848 /* Same metrics from BluetoothA2DPSessionMetricsTest.TestUpdateNormal */
849 A2dpSessionMetrics metrics1;
850 A2dpSessionMetrics metrics2;
851 A2dpSessionMetrics metrics_sum;
852 metrics1.audio_duration_ms = 10;
853 metrics2.audio_duration_ms = 25;
854 metrics_sum.audio_duration_ms = 35;
855 metrics1.media_timer_min_ms = 10;
856 metrics2.media_timer_min_ms = 25;
857 metrics_sum.media_timer_min_ms = 10;
858 metrics1.media_timer_max_ms = 100;
859 metrics2.media_timer_max_ms = 200;
860 metrics_sum.media_timer_max_ms = 200;
861 metrics1.media_timer_avg_ms = 50;
862 metrics1.total_scheduling_count = 50;
863 metrics2.media_timer_avg_ms = 100;
864 metrics2.total_scheduling_count = 50;
865 metrics_sum.media_timer_avg_ms = 75;
866 metrics_sum.total_scheduling_count = 100;
867 metrics1.buffer_overruns_max_count = 70;
868 metrics2.buffer_overruns_max_count = 80;
869 metrics_sum.buffer_overruns_max_count = 80;
870 metrics1.buffer_underruns_average = 80;
871 metrics1.buffer_underruns_count = 1200;
872 metrics2.buffer_underruns_average = 130;
873 metrics2.buffer_underruns_count = 2400;
874 metrics_sum.buffer_underruns_average = 113.33333333;
875 metrics_sum.buffer_underruns_count = 3600;
876 metrics1.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
877 metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
878 metrics_sum.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
879 DeviceInfo* info = MakeDeviceInfo(
880 BTM_COD_MAJOR_AUDIO_TEST,
881 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
882 bt_sessions_.push_back(MakeBluetoothSession(
884 BluetoothSession_ConnectionTechnologyType::
885 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
886 BluetoothSession_DisconnectReasonType::
887 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
888 info, nullptr, nullptr));
890 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
891 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
892 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
893 BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR);
896 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
897 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
899 info = MakeDeviceInfo(
900 BTM_COD_MAJOR_AUDIO_TEST,
901 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
902 A2DPSession* session =
903 MakeA2DPSession(metrics_sum, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC);
904 bt_sessions_.push_back(MakeBluetoothSession(
906 BluetoothSession_ConnectionTechnologyType::
907 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
908 BluetoothSession_DisconnectReasonType::
909 BluetoothSession_DisconnectReasonType_UNKNOWN,
910 info, nullptr, session));
912 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
913 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
915 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
916 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
918 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
919 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
922 TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionTest) {
923 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
925 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
927 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
930 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
931 BluetoothLog* metrics = BluetoothLog::default_instance().New();
932 metrics->ParseFromString(msg_str);
933 EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 2);
934 bool hfp_correct = false;
935 bool hsp_correct = false;
936 for (const HeadsetProfileConnectionStats& headset_profile_connection_stats :
937 metrics->headset_profile_connection_stats()) {
938 switch (headset_profile_connection_stats.headset_profile_type()) {
939 case HeadsetProfileType::HFP:
940 EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 2);
943 case HeadsetProfileType::HSP:
944 EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
951 EXPECT_TRUE(hfp_correct);
952 EXPECT_TRUE(hsp_correct);
953 metrics->clear_headset_profile_connection_stats();
954 EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
956 // Verify that dump after clean up result in an empty list
957 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
958 metrics->ParseFromString(msg_str);
959 EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
963 TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionErrorTest) {
964 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
966 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
968 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
970 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
973 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
974 BluetoothLog* metrics = BluetoothLog::default_instance().New();
975 metrics->ParseFromString(msg_str);
976 EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 3);
977 bool hfp_correct = false;
978 bool hsp_correct = false;
979 bool unknown_correct = false;
980 for (const HeadsetProfileConnectionStats& headset_profile_connection_stats :
981 metrics->headset_profile_connection_stats()) {
982 switch (headset_profile_connection_stats.headset_profile_type()) {
983 case HeadsetProfileType::HFP:
984 EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
987 case HeadsetProfileType::HSP:
988 EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 2);
992 EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
993 unknown_correct = true;
997 EXPECT_TRUE(hfp_correct);
998 EXPECT_TRUE(hsp_correct);
999 EXPECT_TRUE(unknown_correct);
1000 metrics->clear_headset_profile_connection_stats();
1001 EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
1002 // Verify that dump after clean up result in an empty list
1003 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
1004 metrics->ParseFromString(msg_str);
1005 EXPECT_EQ(metrics->headset_profile_connection_stats_size(), 0);
1008 } // namespace testing