/* * Copyright 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.bluetooth.btservice; import androidx.test.filters.MediumTest; import androidx.test.runner.AndroidJUnit4; import com.android.bluetooth.BluetoothMetricsProto.BluetoothLog; import com.android.bluetooth.BluetoothMetricsProto.ProfileConnectionStats; import com.android.bluetooth.BluetoothMetricsProto.ProfileId; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import java.util.HashMap; import java.util.List; /** * Unit tests for {@link MetricsLogger} */ @MediumTest @RunWith(AndroidJUnit4.class) public class MetricsLoggerTest { @Before public void setUp() { // Dump metrics to clean up internal states MetricsLogger.dumpProto(BluetoothLog.newBuilder()); } @After public void tearDown() { // Dump metrics to clean up internal states MetricsLogger.dumpProto(BluetoothLog.newBuilder()); } /** * Simple test to verify that profile connection event can be logged, dumped, and cleaned */ @Test public void testLogProfileConnectionEvent() { MetricsLogger.logProfileConnectionEvent(ProfileId.AVRCP); BluetoothLog.Builder metricsBuilder = BluetoothLog.newBuilder(); MetricsLogger.dumpProto(metricsBuilder); BluetoothLog metricsProto = metricsBuilder.build(); Assert.assertEquals(1, metricsProto.getProfileConnectionStatsCount()); ProfileConnectionStats profileUsageStatsAvrcp = metricsProto.getProfileConnectionStats(0); Assert.assertEquals(ProfileId.AVRCP, profileUsageStatsAvrcp.getProfileId()); Assert.assertEquals(1, profileUsageStatsAvrcp.getNumTimesConnected()); // Verify that MetricsLogger's internal state is cleared after a dump BluetoothLog.Builder metricsBuilderAfterDump = BluetoothLog.newBuilder(); MetricsLogger.dumpProto(metricsBuilderAfterDump); BluetoothLog metricsProtoAfterDump = metricsBuilderAfterDump.build(); Assert.assertEquals(0, metricsProtoAfterDump.getProfileConnectionStatsCount()); } /** * Test whether multiple profile's connection events can be logged interleaving */ @Test public void testLogProfileConnectionEventMultipleProfile() { MetricsLogger.logProfileConnectionEvent(ProfileId.AVRCP); MetricsLogger.logProfileConnectionEvent(ProfileId.HEADSET); MetricsLogger.logProfileConnectionEvent(ProfileId.AVRCP); BluetoothLog.Builder metricsBuilder = BluetoothLog.newBuilder(); MetricsLogger.dumpProto(metricsBuilder); BluetoothLog metricsProto = metricsBuilder.build(); Assert.assertEquals(2, metricsProto.getProfileConnectionStatsCount()); HashMap profileConnectionCountMap = getProfileUsageStatsMap(metricsProto.getProfileConnectionStatsList()); Assert.assertTrue(profileConnectionCountMap.containsKey(ProfileId.AVRCP)); Assert.assertEquals(2, profileConnectionCountMap.get(ProfileId.AVRCP).getNumTimesConnected()); Assert.assertTrue(profileConnectionCountMap.containsKey(ProfileId.HEADSET)); Assert.assertEquals(1, profileConnectionCountMap.get(ProfileId.HEADSET).getNumTimesConnected()); // Verify that MetricsLogger's internal state is cleared after a dump BluetoothLog.Builder metricsBuilderAfterDump = BluetoothLog.newBuilder(); MetricsLogger.dumpProto(metricsBuilderAfterDump); BluetoothLog metricsProtoAfterDump = metricsBuilderAfterDump.build(); Assert.assertEquals(0, metricsProtoAfterDump.getProfileConnectionStatsCount()); } private static HashMap getProfileUsageStatsMap( List profileUsageStats) { HashMap profileUsageStatsMap = new HashMap<>(); profileUsageStats.forEach(item -> profileUsageStatsMap.put(item.getProfileId(), item)); return profileUsageStatsMap; } }