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