Home | History | Annotate | Download | only in btservice
      1 /*
      2  * Copyright 2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.bluetooth.btservice;
     17 
     18 import android.support.test.filters.MediumTest;
     19 import android.support.test.runner.AndroidJUnit4;
     20 
     21 import com.android.bluetooth.BluetoothMetricsProto.BluetoothLog;
     22 import com.android.bluetooth.BluetoothMetricsProto.ProfileConnectionStats;
     23 import com.android.bluetooth.BluetoothMetricsProto.ProfileId;
     24 
     25 import org.junit.After;
     26 import org.junit.Assert;
     27 import org.junit.Before;
     28 import org.junit.Test;
     29 import org.junit.runner.RunWith;
     30 
     31 import java.util.HashMap;
     32 import java.util.List;
     33 
     34 /**
     35  * Unit tests for {@link MetricsLogger}
     36  */
     37 @MediumTest
     38 @RunWith(AndroidJUnit4.class)
     39 public class MetricsLoggerTest {
     40 
     41     @Before
     42     public void setUp() {
     43         // Dump metrics to clean up internal states
     44         MetricsLogger.dumpProto(BluetoothLog.newBuilder());
     45     }
     46 
     47     @After
     48     public void tearDown() {
     49         // Dump metrics to clean up internal states
     50         MetricsLogger.dumpProto(BluetoothLog.newBuilder());
     51     }
     52 
     53     /**
     54      * Simple test to verify that profile connection event can be logged, dumped, and cleaned
     55      */
     56     @Test
     57     public void testLogProfileConnectionEvent() {
     58         MetricsLogger.logProfileConnectionEvent(ProfileId.AVRCP);
     59         BluetoothLog.Builder metricsBuilder = BluetoothLog.newBuilder();
     60         MetricsLogger.dumpProto(metricsBuilder);
     61         BluetoothLog metricsProto = metricsBuilder.build();
     62         Assert.assertEquals(1, metricsProto.getProfileConnectionStatsCount());
     63         ProfileConnectionStats profileUsageStatsAvrcp = metricsProto.getProfileConnectionStats(0);
     64         Assert.assertEquals(ProfileId.AVRCP, profileUsageStatsAvrcp.getProfileId());
     65         Assert.assertEquals(1, profileUsageStatsAvrcp.getNumTimesConnected());
     66         // Verify that MetricsLogger's internal state is cleared after a dump
     67         BluetoothLog.Builder metricsBuilderAfterDump = BluetoothLog.newBuilder();
     68         MetricsLogger.dumpProto(metricsBuilderAfterDump);
     69         BluetoothLog metricsProtoAfterDump = metricsBuilderAfterDump.build();
     70         Assert.assertEquals(0, metricsProtoAfterDump.getProfileConnectionStatsCount());
     71     }
     72 
     73     /**
     74      * Test whether multiple profile's connection events can be logged interleaving
     75      */
     76     @Test
     77     public void testLogProfileConnectionEventMultipleProfile() {
     78         MetricsLogger.logProfileConnectionEvent(ProfileId.AVRCP);
     79         MetricsLogger.logProfileConnectionEvent(ProfileId.HEADSET);
     80         MetricsLogger.logProfileConnectionEvent(ProfileId.AVRCP);
     81         BluetoothLog.Builder metricsBuilder = BluetoothLog.newBuilder();
     82         MetricsLogger.dumpProto(metricsBuilder);
     83         BluetoothLog metricsProto = metricsBuilder.build();
     84         Assert.assertEquals(2, metricsProto.getProfileConnectionStatsCount());
     85         HashMap<ProfileId, ProfileConnectionStats> profileConnectionCountMap =
     86                 getProfileUsageStatsMap(metricsProto.getProfileConnectionStatsList());
     87         Assert.assertTrue(profileConnectionCountMap.containsKey(ProfileId.AVRCP));
     88         Assert.assertEquals(2,
     89                 profileConnectionCountMap.get(ProfileId.AVRCP).getNumTimesConnected());
     90         Assert.assertTrue(profileConnectionCountMap.containsKey(ProfileId.HEADSET));
     91         Assert.assertEquals(1,
     92                 profileConnectionCountMap.get(ProfileId.HEADSET).getNumTimesConnected());
     93         // Verify that MetricsLogger's internal state is cleared after a dump
     94         BluetoothLog.Builder metricsBuilderAfterDump = BluetoothLog.newBuilder();
     95         MetricsLogger.dumpProto(metricsBuilderAfterDump);
     96         BluetoothLog metricsProtoAfterDump = metricsBuilderAfterDump.build();
     97         Assert.assertEquals(0, metricsProtoAfterDump.getProfileConnectionStatsCount());
     98     }
     99 
    100     private static HashMap<ProfileId, ProfileConnectionStats> getProfileUsageStatsMap(
    101             List<ProfileConnectionStats> profileUsageStats) {
    102         HashMap<ProfileId, ProfileConnectionStats> profileUsageStatsMap = new HashMap<>();
    103         profileUsageStats.forEach(item -> profileUsageStatsMap.put(item.getProfileId(), item));
    104         return profileUsageStatsMap;
    105     }
    106 
    107 }
    108