blob: a46c8969f3db96015758889da3a6c755ac2f316a [file] [log] [blame]
Jack Hef3175622016-12-08 19:29:00 -08001/******************************************************************************
2 *
Jakub Pawlowski5b790fe2017-09-18 09:00:20 -07003 * Copyright 2016 Google, Inc.
Jack Hef3175622016-12-08 19:29:00 -08004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18#include <chrono>
19#include <cstdint>
20#include <string>
21#include <thread>
22#include <vector>
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26
27#include <base/logging.h>
Jack Hefeeaf5f2018-06-08 20:13:23 -070028#include <include/hardware/bt_av.h>
Jack Hef3175622016-12-08 19:29:00 -080029
Jack Head18de02018-03-03 00:03:25 -080030#include "bluetooth/metrics/bluetooth.pb.h"
Jack He959bc332018-08-15 12:38:37 -070031#include "common/metrics.h"
32#include "common/time_util.h"
Jack Hef3175622016-12-08 19:29:00 -080033
34#define BTM_COD_MAJOR_AUDIO_TEST 0x04
35
36namespace testing {
37
Jack He959bc332018-08-15 12:38:37 -070038using bluetooth::common::A2dpSessionMetrics;
39using bluetooth::common::BluetoothMetricsLogger;
Jack Head18de02018-03-03 00:03:25 -080040using bluetooth::metrics::BluetoothMetricsProto::A2DPSession;
Jack Hefeeaf5f2018-06-08 20:13:23 -070041using bluetooth::metrics::BluetoothMetricsProto::A2dpSourceCodec;
Jack Head18de02018-03-03 00:03:25 -080042using bluetooth::metrics::BluetoothMetricsProto::BluetoothLog;
43using bluetooth::metrics::BluetoothMetricsProto::BluetoothSession;
44using bluetooth::metrics::BluetoothMetricsProto::
45 BluetoothSession_ConnectionTechnologyType;
46using bluetooth::metrics::BluetoothMetricsProto::
47 BluetoothSession_DisconnectReasonType;
48using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo;
49using bluetooth::metrics::BluetoothMetricsProto::DeviceInfo_DeviceType;
Jack He959bc332018-08-15 12:38:37 -070050using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileConnectionStats;
51using bluetooth::metrics::BluetoothMetricsProto::HeadsetProfileType;
Jack Head18de02018-03-03 00:03:25 -080052using bluetooth::metrics::BluetoothMetricsProto::PairEvent;
53using bluetooth::metrics::BluetoothMetricsProto::RFCommSession;
54using bluetooth::metrics::BluetoothMetricsProto::ScanEvent;
Jack Head18de02018-03-03 00:03:25 -080055using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanEventType;
Jack He959bc332018-08-15 12:38:37 -070056using bluetooth::metrics::BluetoothMetricsProto::ScanEvent_ScanTechnologyType;
Jack Head18de02018-03-03 00:03:25 -080057using bluetooth::metrics::BluetoothMetricsProto::WakeEvent;
58using bluetooth::metrics::BluetoothMetricsProto::WakeEvent_WakeEventType;
Jack Hef3175622016-12-08 19:29:00 -080059
Jack Hea3f831c2017-01-17 15:41:30 -080060namespace {
61const size_t kMaxEventGenerationLimit = 5000;
62}
63
Jack Hef3175622016-12-08 19:29:00 -080064static void sleep_ms(int64_t t) {
65 std::this_thread::sleep_for(std::chrono::milliseconds(t));
66}
67
68DeviceInfo* 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);
73 return info;
74}
75
76PairEvent* 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);
82 return event;
83}
84
85WakeEvent* 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);
93 return event;
94}
95
96ScanEvent* 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);
106 return event;
107}
108
Jack Hefeeaf5f2018-06-08 20:13:23 -0700109A2DPSession* MakeA2DPSession(const A2dpSessionMetrics& metrics,
110 A2dpSourceCodec source_codec) {
Jack Hef3175622016-12-08 19:29:00 -0800111 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);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700120 session->set_source_codec(source_codec);
121 session->set_is_a2dp_offload(metrics.is_a2dp_offload);
Jack Hef3175622016-12-08 19:29:00 -0800122 return session;
123}
124
125BluetoothSession* MakeBluetoothSession(
126 int64_t session_duration_sec,
127 BluetoothSession_ConnectionTechnologyType conn_type,
Jack Hea3f831c2017-01-17 15:41:30 -0800128 BluetoothSession_DisconnectReasonType disconnect_reason,
129 DeviceInfo* device_info, RFCommSession* rfcomm_session,
130 A2DPSession* a2dp_session) {
Jack Hef3175622016-12-08 19:29:00 -0800131 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);
Jack Hea3f831c2017-01-17 15:41:30 -0800137 session->set_disconnect_reason_type(disconnect_reason);
Jack Hef3175622016-12-08 19:29:00 -0800138 return session;
139}
140
141BluetoothLog* 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
Jack Hea3f831c2017-01-17 15:41:30 -0800165void GenerateWakeEvents(size_t start, size_t end,
Jack Hef3175622016-12-08 19:29:00 -0800166 std::vector<WakeEvent*>* wake_events) {
Jack Hea3f831c2017-01-17 15:41:30 -0800167 for (size_t i = start; i < end; ++i) {
Jack Hef3175622016-12-08 19:29:00 -0800168 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));
172 }
173}
174
Chih-Hung Hsiehfc25b192017-08-01 15:04:23 -0700175#define COMPARE_A2DP_METRICS(a, b) \
176 do { \
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); \
Jack Hefeeaf5f2018-06-08 20:13:23 -0700188 EXPECT_EQ((a).codec_index, (b).codec_index); \
189 EXPECT_EQ((a).is_a2dp_offload, (b).is_a2dp_offload); \
Jack Hef3175622016-12-08 19:29:00 -0800190 } while (0)
191
192/*
193 * metrics_sum = metrics1 + metrics2
194 */
195TEST(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;
Jack Hefeeaf5f2018-06-08 20:13:23 -0700223 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;
Jack Hef3175622016-12-08 19:29:00 -0800229 metrics1.Update(metrics2);
230 COMPARE_A2DP_METRICS(metrics1, metrics_sum);
231 EXPECT_TRUE(metrics1 == metrics_sum);
232 EXPECT_EQ(metrics1, metrics_sum);
233}
234
235TEST(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;
Jack Hefeeaf5f2018-06-08 20:13:23 -0700255 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;
Jack Hef3175622016-12-08 19:29:00 -0800259 metrics1.Update(metrics2);
260 COMPARE_A2DP_METRICS(metrics1, metrics_sum);
261 EXPECT_TRUE(metrics1 == metrics_sum);
262 EXPECT_EQ(metrics1, metrics_sum);
263}
264
265TEST(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;
Jack Hefeeaf5f2018-06-08 20:13:23 -0700285 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;
Jack Hef3175622016-12-08 19:29:00 -0800289 metrics2.Update(metrics1);
290 COMPARE_A2DP_METRICS(metrics2, metrics_sum);
291 EXPECT_TRUE(metrics2 == metrics_sum);
292 EXPECT_EQ(metrics2, metrics_sum);
293}
294
295TEST(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);
322}
323
324class BluetoothMetricsLoggerTest : public Test {
325 protected:
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_;
Jack Hea3f831c2017-01-17 15:41:30 -0800331 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;
Jack Hef3175622016-12-08 19:29:00 -0800335 BluetoothLog* bt_log_;
336 std::string bt_log_str_;
337 std::string bt_log_ascii_str_;
338
339 void UpdateLog() {
340 for (BluetoothSession* session : bt_sessions_) {
341 bt_log_->mutable_session()->AddAllocated(session);
342 }
Jack Hea3f831c2017-01-17 15:41:30 -0800343 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());
347 }
Jack Hef3175622016-12-08 19:29:00 -0800348 bt_sessions_.clear();
349 for (PairEvent* event : pair_events_) {
350 bt_log_->mutable_pair_event()->AddAllocated(event);
351 }
Jack Hea3f831c2017-01-17 15:41:30 -0800352 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());
356 }
Jack Hef3175622016-12-08 19:29:00 -0800357 pair_events_.clear();
358 for (WakeEvent* event : wake_events_) {
359 bt_log_->mutable_wake_event()->AddAllocated(event);
360 }
Jack Hea3f831c2017-01-17 15:41:30 -0800361 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());
365 }
Jack Hef3175622016-12-08 19:29:00 -0800366 wake_events_.clear();
367 for (ScanEvent* event : scan_events_) {
368 bt_log_->mutable_scan_event()->AddAllocated(event);
369 }
Jack Hea3f831c2017-01-17 15:41:30 -0800370 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());
374 }
Jack Hef3175622016-12-08 19:29:00 -0800375 scan_events_.clear();
376 bt_log_->SerializeToString(&bt_log_str_);
377 }
378
379 void ClearLog() {
380 for (BluetoothSession* session : bt_sessions_) {
381 session->Clear();
382 delete session;
383 }
384 bt_sessions_.clear();
385 for (PairEvent* event : pair_events_) {
386 event->Clear();
387 delete event;
388 }
389 pair_events_.clear();
390 for (WakeEvent* event : wake_events_) {
391 event->Clear();
392 delete event;
393 }
394 wake_events_.clear();
395 for (ScanEvent* event : scan_events_) {
396 event->Clear();
397 delete event;
398 }
399 scan_events_.clear();
400 bt_log_->Clear();
401 }
402
Yi Kong183eace2019-03-29 15:42:58 -0700403 void SetUp() override {
Jack Hef3175622016-12-08 19:29:00 -0800404 bt_log_ = new BluetoothLog();
405 // Clear existing metrics entries, if any
406 BluetoothMetricsLogger::GetInstance()->Reset();
407 }
Yi Kong183eace2019-03-29 15:42:58 -0700408 void TearDown() override {
Jack Hef3175622016-12-08 19:29:00 -0800409 // Clear remaining metrics entries, if any
410 BluetoothMetricsLogger::GetInstance()->Reset();
411 ClearLog();
412 delete bt_log_;
413 }
414
415 public:
416};
417
418TEST_F(BluetoothMetricsLoggerTest, PairEventTest) {
419 pair_events_.push_back(MakePairEvent(
420 35, 12345,
421 MakeDeviceInfo(
422 42, DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR)));
423 UpdateLog();
424 BluetoothMetricsLogger::GetInstance()->LogPairEvent(
Jack He959bc332018-08-15 12:38:37 -0700425 35, 12345, 42, bluetooth::common::DEVICE_TYPE_BREDR);
Jack Hef3175622016-12-08 19:29:00 -0800426 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700427 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800428 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
429}
430
431TEST_F(BluetoothMetricsLoggerTest, WakeEventTest) {
432 wake_events_.push_back(
433 MakeWakeEvent(WakeEvent_WakeEventType::WakeEvent_WakeEventType_ACQUIRED,
434 "TEST_REQ", "TEST_NAME", 12345));
435 UpdateLog();
436 BluetoothMetricsLogger::GetInstance()->LogWakeEvent(
Jack He959bc332018-08-15 12:38:37 -0700437 bluetooth::common::WAKE_EVENT_ACQUIRED, "TEST_REQ", "TEST_NAME", 12345);
Jack Hef3175622016-12-08 19:29:00 -0800438 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700439 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800440 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
441}
442
Jack Hea3f831c2017-01-17 15:41:30 -0800443TEST_F(BluetoothMetricsLoggerTest, WakeEventOverrunTest) {
444 GenerateWakeEvents(
445 kMaxEventGenerationLimit - BluetoothMetricsLogger::kMaxNumWakeEvent,
446 kMaxEventGenerationLimit, &wake_events_);
447 num_wake_event_ = kMaxEventGenerationLimit;
Jack Hef3175622016-12-08 19:29:00 -0800448 UpdateLog();
Jack Hea3f831c2017-01-17 15:41:30 -0800449 for (size_t i = 0; i < kMaxEventGenerationLimit; ++i) {
Jack Hef3175622016-12-08 19:29:00 -0800450 BluetoothMetricsLogger::GetInstance()->LogWakeEvent(
Jack He959bc332018-08-15 12:38:37 -0700451 i % 2 == 0 ? bluetooth::common::WAKE_EVENT_ACQUIRED
452 : bluetooth::common::WAKE_EVENT_RELEASED,
Jack Hef3175622016-12-08 19:29:00 -0800453 "TEST_REQ", "TEST_NAME", i);
454 }
455 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700456 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800457 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
458}
459
460TEST_F(BluetoothMetricsLoggerTest, ScanEventTest) {
461 scan_events_.push_back(MakeScanEvent(
462 ScanEvent_ScanEventType::ScanEvent_ScanEventType_SCAN_EVENT_STOP,
Jack He959bc332018-08-15 12:38:37 -0700463 "TEST_INITIATOR",
464 ScanEvent_ScanTechnologyType::
465 ScanEvent_ScanTechnologyType_SCAN_TECH_TYPE_BREDR,
Jack Hef3175622016-12-08 19:29:00 -0800466 42, 123456));
467 UpdateLog();
468 BluetoothMetricsLogger::GetInstance()->LogScanEvent(
Jack He959bc332018-08-15 12:38:37 -0700469 false, "TEST_INITIATOR", bluetooth::common::SCAN_TECH_TYPE_BREDR, 42,
470 123456);
Jack Hef3175622016-12-08 19:29:00 -0800471 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700472 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800473 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
474}
475
476TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionTest) {
477 bt_sessions_.push_back(MakeBluetoothSession(
478 10,
479 BluetoothSession_ConnectionTechnologyType::
480 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE,
Jack Hea3f831c2017-01-17 15:41:30 -0800481 BluetoothSession_DisconnectReasonType::
482 BluetoothSession_DisconnectReasonType_UNKNOWN,
483 nullptr, nullptr, nullptr));
Jack Hef3175622016-12-08 19:29:00 -0800484 UpdateLog();
485 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
Jack He959bc332018-08-15 12:38:37 -0700486 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_LE, 123456);
Jack Hef3175622016-12-08 19:29:00 -0800487 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
Jack He959bc332018-08-15 12:38:37 -0700488 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 133456);
Jack Hef3175622016-12-08 19:29:00 -0800489 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700490 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800491 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
492}
493
494TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionDumpBeforeEndTest) {
495 bt_sessions_.push_back(MakeBluetoothSession(
496 1,
497 BluetoothSession_ConnectionTechnologyType::
498 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE,
Jack Hea3f831c2017-01-17 15:41:30 -0800499 BluetoothSession_DisconnectReasonType::
500 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
501 nullptr, nullptr, nullptr));
Jack Hef3175622016-12-08 19:29:00 -0800502 UpdateLog();
503 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
Jack He959bc332018-08-15 12:38:37 -0700504 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_LE,
505 bluetooth::common::time_get_os_boottime_ms());
Jack Hef3175622016-12-08 19:29:00 -0800506 sleep_ms(1000);
507 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700508 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800509 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
510}
511
512TEST_F(BluetoothMetricsLoggerTest, BluetoothSessionStartBeforeEndTest) {
513 bt_sessions_.push_back(MakeBluetoothSession(
514 1,
515 BluetoothSession_ConnectionTechnologyType::
516 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_UNKNOWN,
Jack Hea3f831c2017-01-17 15:41:30 -0800517 BluetoothSession_DisconnectReasonType::
518 BluetoothSession_DisconnectReasonType_NEXT_START_WITHOUT_END_PREVIOUS,
519 nullptr, nullptr, nullptr));
Jack Hef3175622016-12-08 19:29:00 -0800520 bt_sessions_.push_back(MakeBluetoothSession(
521 2,
522 BluetoothSession_ConnectionTechnologyType::
523 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_LE,
Jack Hea3f831c2017-01-17 15:41:30 -0800524 BluetoothSession_DisconnectReasonType::
525 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
526 nullptr, nullptr, nullptr));
Jack Hef3175622016-12-08 19:29:00 -0800527 UpdateLog();
528 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
Jack He959bc332018-08-15 12:38:37 -0700529 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_UNKNOWN, 0);
Jack Hef3175622016-12-08 19:29:00 -0800530 sleep_ms(1000);
531 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
Jack He959bc332018-08-15 12:38:37 -0700532 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_LE, 0);
Jack Hef3175622016-12-08 19:29:00 -0800533 sleep_ms(2000);
534 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700535 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800536 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
537}
538
539/*
540 * Test Case: A2DPSessionTwoUpdatesTest
541 *
542 * 1. Create Instance
543 * 2. LogBluetoothSessionStart
544 * 3. LogBluetoothSessionDeviceInfo
545 * 4. LogA2dpSession
546 * 5. LogA2dpSession
547 * 6. LogBluetoothSessionEnd
548 * 7. WriteString
549 *
550 */
551TEST_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;
Jack Hefeeaf5f2018-06-08 20:13:23 -0700580 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;
Jack Hef3175622016-12-08 19:29:00 -0800586 DeviceInfo* info = MakeDeviceInfo(
587 BTM_COD_MAJOR_AUDIO_TEST,
588 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700589 A2DPSession* session =
590 MakeA2DPSession(metrics_sum, A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC);
Jack Hef3175622016-12-08 19:29:00 -0800591 bt_sessions_.push_back(MakeBluetoothSession(
592 10,
593 BluetoothSession_ConnectionTechnologyType::
594 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
Jack Hea3f831c2017-01-17 15:41:30 -0800595 BluetoothSession_DisconnectReasonType::
596 BluetoothSession_DisconnectReasonType_UNKNOWN,
597 info, nullptr, session));
Jack Hef3175622016-12-08 19:29:00 -0800598 UpdateLog();
599 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
Jack He959bc332018-08-15 12:38:37 -0700600 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 123456);
Jack Hef3175622016-12-08 19:29:00 -0800601 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
Jack He959bc332018-08-15 12:38:37 -0700602 BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR);
Jack Hef3175622016-12-08 19:29:00 -0800603 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
604 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
605 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
Jack He959bc332018-08-15 12:38:37 -0700606 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 133456);
Jack Hef3175622016-12-08 19:29:00 -0800607 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700608 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800609 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
610}
611
612/*
613 * Test Case: A2DPSessionTwoUpdatesSeparatedbyDumpTest
614 *
615 * 1. Create Instance
616 * 2. LogBluetoothSessionStart
617 * 3. LogBluetoothSessionDeviceInfo
618 * 4. LogA2dpSession
619 * 5. WriteString
620 * 6. LogA2dpSession
621 * 7. LogBluetoothSessionEnd
622 * 8. WriteString
623 *
624 */
625TEST_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;
Jack Hefeeaf5f2018-06-08 20:13:23 -0700645 metrics1.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_SBC;
646 metrics2.codec_index = BTAV_A2DP_CODEC_INDEX_SOURCE_AAC;
Jack Hef3175622016-12-08 19:29:00 -0800647 DeviceInfo* info = MakeDeviceInfo(
648 BTM_COD_MAJOR_AUDIO_TEST,
649 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700650 A2DPSession* session =
651 MakeA2DPSession(metrics1, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC);
Jack Hef3175622016-12-08 19:29:00 -0800652 bt_sessions_.push_back(MakeBluetoothSession(
653 1,
654 BluetoothSession_ConnectionTechnologyType::
655 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
Jack Hea3f831c2017-01-17 15:41:30 -0800656 BluetoothSession_DisconnectReasonType::
657 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
658 info, nullptr, session));
Jack Hef3175622016-12-08 19:29:00 -0800659 UpdateLog();
660 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
Jack He959bc332018-08-15 12:38:37 -0700661 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
Jack Hef3175622016-12-08 19:29:00 -0800662 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
Jack He959bc332018-08-15 12:38:37 -0700663 BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR);
Jack Hef3175622016-12-08 19:29:00 -0800664 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
665 sleep_ms(1000);
666 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700667 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800668 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
669 ClearLog();
670 info = MakeDeviceInfo(
671 BTM_COD_MAJOR_AUDIO_TEST,
672 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700673 session = MakeA2DPSession(metrics2, A2dpSourceCodec::A2DP_SOURCE_CODEC_AAC);
Jack Hef3175622016-12-08 19:29:00 -0800674 bt_sessions_.push_back(MakeBluetoothSession(
675 1,
676 BluetoothSession_ConnectionTechnologyType::
677 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
Jack Hea3f831c2017-01-17 15:41:30 -0800678 BluetoothSession_DisconnectReasonType::
679 BluetoothSession_DisconnectReasonType_UNKNOWN,
680 info, nullptr, session));
Jack Hef3175622016-12-08 19:29:00 -0800681 UpdateLog();
682 sleep_ms(1000);
683 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
684 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
Jack He959bc332018-08-15 12:38:37 -0700685 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
Jack Hef3175622016-12-08 19:29:00 -0800686 msg_str.clear();
Jack He8bc22a12018-04-02 13:04:58 -0700687 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800688 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
689}
690
691/*
Jack Hefeeaf5f2018-06-08 20:13:23 -0700692 * Test Case: A2DPSessionTwoUpdatesSeparatedbyEndTest
693 *
694 * 1. Create Instance
695 * 2. LogBluetoothSessionStart
696 * 3. LogA2dpSession
697 * 4. LogBluetoothSessionEnd
698 * 5. LogBluetoothSessionStart
699 * 6. LogA2dpSession
700 * 7. LogBluetoothSessionEnd
701 * 8. WriteString
702 *
703 */
704TEST_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(
722 1,
723 BluetoothSession_ConnectionTechnologyType::
724 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
725 BluetoothSession_DisconnectReasonType::
726 BluetoothSession_DisconnectReasonType_UNKNOWN,
727 info, nullptr, session));
728 UpdateLog();
729 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
Jack He959bc332018-08-15 12:38:37 -0700730 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700731 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
Jack He959bc332018-08-15 12:38:37 -0700732 BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700733 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
734 sleep_ms(1000);
735 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
Jack He959bc332018-08-15 12:38:37 -0700736 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700737 std::string msg_str;
738 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
739 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
740 ClearLog();
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(
753 1,
754 BluetoothSession_ConnectionTechnologyType::
755 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
756 BluetoothSession_DisconnectReasonType::
757 BluetoothSession_DisconnectReasonType_UNKNOWN,
758 nullptr, nullptr, session));
759 UpdateLog();
760 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
Jack He959bc332018-08-15 12:38:37 -0700761 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700762 sleep_ms(1000);
763 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
764 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
Jack He959bc332018-08-15 12:38:37 -0700765 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700766 msg_str.clear();
767 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
768 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
769}
770
771/*
Jack Hef3175622016-12-08 19:29:00 -0800772 * Test Case 1: A2DPSessionOnlyTest
773 *
774 * 1. Create Instance
775 * 4. LogA2dpSession
776 * 5. WriteString
777 * 6. LogA2dpSession
778 * 8. WriteString
779 *
780 */
781TEST_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;
Jack Hefeeaf5f2018-06-08 20:13:23 -0700810 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;
Jack Hef3175622016-12-08 19:29:00 -0800813 DeviceInfo* info = MakeDeviceInfo(
814 BTM_COD_MAJOR_AUDIO_TEST,
815 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700816 A2DPSession* session =
817 MakeA2DPSession(metrics_sum, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC);
Jack Hef3175622016-12-08 19:29:00 -0800818 bt_sessions_.push_back(MakeBluetoothSession(
819 1,
820 BluetoothSession_ConnectionTechnologyType::
821 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
Jack Hea3f831c2017-01-17 15:41:30 -0800822 BluetoothSession_DisconnectReasonType::
823 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
824 info, nullptr, session));
Jack Hef3175622016-12-08 19:29:00 -0800825 UpdateLog();
826 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
827 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
828 sleep_ms(1000);
829 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700830 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800831 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
832}
833
834/*
835 * Test Case: A2DPSessionDumpBeforeTwoUpdatesTest
836 *
837 * 1. Create Instance
838 * 2. LogBluetoothSessionStart
839 * 3. LogBluetoothSessionDeviceInfo
840 * 5. WriteString
841 * 6. LogA2dpSession
842 * 7. LogA2dpSession
843 * 8. LogBluetoothSessionEnd
844 * 9. WriteString
845 *
846 */
847TEST_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;
Jack Hefeeaf5f2018-06-08 20:13:23 -0700876 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;
Jack Hef3175622016-12-08 19:29:00 -0800879 DeviceInfo* info = MakeDeviceInfo(
880 BTM_COD_MAJOR_AUDIO_TEST,
881 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
882 bt_sessions_.push_back(MakeBluetoothSession(
883 1,
884 BluetoothSession_ConnectionTechnologyType::
885 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
Jack Hea3f831c2017-01-17 15:41:30 -0800886 BluetoothSession_DisconnectReasonType::
887 BluetoothSession_DisconnectReasonType_METRICS_DUMP,
888 info, nullptr, nullptr));
Jack Hef3175622016-12-08 19:29:00 -0800889 UpdateLog();
890 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionStart(
Jack He959bc332018-08-15 12:38:37 -0700891 bluetooth::common::CONNECTION_TECHNOLOGY_TYPE_BREDR, 0);
Jack Hef3175622016-12-08 19:29:00 -0800892 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionDeviceInfo(
Jack He959bc332018-08-15 12:38:37 -0700893 BTM_COD_MAJOR_AUDIO_TEST, bluetooth::common::DEVICE_TYPE_BREDR);
Jack Hef3175622016-12-08 19:29:00 -0800894 sleep_ms(1000);
895 std::string msg_str;
Jack He8bc22a12018-04-02 13:04:58 -0700896 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800897 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
898 ClearLog();
899 info = MakeDeviceInfo(
900 BTM_COD_MAJOR_AUDIO_TEST,
901 DeviceInfo_DeviceType::DeviceInfo_DeviceType_DEVICE_TYPE_BREDR);
Jack Hefeeaf5f2018-06-08 20:13:23 -0700902 A2DPSession* session =
903 MakeA2DPSession(metrics_sum, A2dpSourceCodec::A2DP_SOURCE_CODEC_SBC);
Jack Hef3175622016-12-08 19:29:00 -0800904 bt_sessions_.push_back(MakeBluetoothSession(
905 1,
906 BluetoothSession_ConnectionTechnologyType::
907 BluetoothSession_ConnectionTechnologyType_CONNECTION_TECHNOLOGY_TYPE_BREDR,
Jack Hea3f831c2017-01-17 15:41:30 -0800908 BluetoothSession_DisconnectReasonType::
909 BluetoothSession_DisconnectReasonType_UNKNOWN,
910 info, nullptr, session));
Jack Hef3175622016-12-08 19:29:00 -0800911 UpdateLog();
912 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics1);
913 BluetoothMetricsLogger::GetInstance()->LogA2dpSession(metrics2);
914 sleep_ms(1000);
915 BluetoothMetricsLogger::GetInstance()->LogBluetoothSessionEnd(
Jack He959bc332018-08-15 12:38:37 -0700916 bluetooth::common::DISCONNECT_REASON_UNKNOWN, 0);
Jack Hef3175622016-12-08 19:29:00 -0800917 msg_str.clear();
Jack He8bc22a12018-04-02 13:04:58 -0700918 BluetoothMetricsLogger::GetInstance()->WriteString(&msg_str);
Jack Hef3175622016-12-08 19:29:00 -0800919 EXPECT_THAT(msg_str, StrEq(bt_log_str_));
920}
Jack He8bc22a12018-04-02 13:04:58 -0700921
922TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionTest) {
923 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
924 BTA_HSP_SERVICE_ID);
925 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
926 BTA_HFP_SERVICE_ID);
927 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
928 BTA_HFP_SERVICE_ID);
929 std::string msg_str;
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);
941 hfp_correct = true;
942 break;
943 case HeadsetProfileType::HSP:
944 EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
945 hsp_correct = true;
946 break;
947 default:
948 FAIL();
949 }
950 }
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);
955 msg_str.clear();
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);
960 delete metrics;
961}
962
963TEST_F(BluetoothMetricsLoggerTest, LogHeadsetProfileRfcConnectionErrorTest) {
964 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
965 BTA_HSP_SERVICE_ID);
966 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
967 BTA_HFP_SERVICE_ID);
968 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
969 BTA_BIP_SERVICE_ID);
970 BluetoothMetricsLogger::GetInstance()->LogHeadsetProfileRfcConnection(
971 BTA_HSP_SERVICE_ID);
972 std::string msg_str;
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);
985 hfp_correct = true;
986 break;
987 case HeadsetProfileType::HSP:
988 EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 2);
989 hsp_correct = true;
990 break;
991 default:
992 EXPECT_EQ(headset_profile_connection_stats.num_times_connected(), 1);
993 unknown_correct = true;
994 break;
995 }
996 }
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);
1006 delete metrics;
1007}
Jack He959bc332018-08-15 12:38:37 -07001008} // namespace testing