1 package com.android.bluetooth.gatt; 2 3 import static org.mockito.Mockito.*; 4 5 import android.content.Context; 6 7 import androidx.test.InstrumentationRegistry; 8 import androidx.test.filters.SmallTest; 9 import androidx.test.rule.ServiceTestRule; 10 import androidx.test.runner.AndroidJUnit4; 11 12 import com.android.bluetooth.R; 13 import com.android.bluetooth.TestUtils; 14 import com.android.bluetooth.btservice.AdapterService; 15 16 import org.junit.After; 17 import org.junit.Assert; 18 import org.junit.Assume; 19 import org.junit.Before; 20 import org.junit.Rule; 21 import org.junit.Test; 22 import org.junit.runner.RunWith; 23 import org.mockito.Mock; 24 import org.mockito.MockitoAnnotations; 25 26 /** 27 * Test cases for {@link GattService}. 28 */ 29 @SmallTest 30 @RunWith(AndroidJUnit4.class) 31 public class GattServiceTest { 32 private static final int TIMES_UP_AND_DOWN = 3; 33 private Context mTargetContext; 34 private GattService mService; 35 36 @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule(); 37 38 @Mock private AdapterService mAdapterService; 39 40 @Before setUp()41 public void setUp() throws Exception { 42 mTargetContext = InstrumentationRegistry.getTargetContext(); 43 Assume.assumeTrue("Ignore test when GattService is not enabled", 44 mTargetContext.getResources().getBoolean(R.bool.profile_supported_gatt)); 45 MockitoAnnotations.initMocks(this); 46 TestUtils.setAdapterService(mAdapterService); 47 doReturn(true).when(mAdapterService).isStartedProfile(anyString()); 48 TestUtils.startService(mServiceRule, GattService.class); 49 mService = GattService.getGattService(); 50 Assert.assertNotNull(mService); 51 } 52 53 @After tearDown()54 public void tearDown() throws Exception { 55 if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_gatt)) { 56 return; 57 } 58 doReturn(false).when(mAdapterService).isStartedProfile(anyString()); 59 TestUtils.stopService(mServiceRule, GattService.class); 60 mService = GattService.getGattService(); 61 Assert.assertNull(mService); 62 TestUtils.clearAdapterService(mAdapterService); 63 } 64 65 @Test testInitialize()66 public void testInitialize() { 67 Assert.assertNotNull(GattService.getGattService()); 68 } 69 70 @Test testServiceUpAndDown()71 public void testServiceUpAndDown() throws Exception { 72 for (int i = 0; i < TIMES_UP_AND_DOWN; i++) { 73 GattService gattService = GattService.getGattService(); 74 doReturn(false).when(mAdapterService).isStartedProfile(anyString()); 75 TestUtils.stopService(mServiceRule, GattService.class); 76 mService = GattService.getGattService(); 77 Assert.assertNull(mService); 78 gattService.cleanup(); 79 TestUtils.clearAdapterService(mAdapterService); 80 reset(mAdapterService); 81 TestUtils.setAdapterService(mAdapterService); 82 doReturn(true).when(mAdapterService).isStartedProfile(anyString()); 83 TestUtils.startService(mServiceRule, GattService.class); 84 mService = GattService.getGattService(); 85 Assert.assertNotNull(mService); 86 } 87 } 88 89 @Test testParseBatchTimestamp()90 public void testParseBatchTimestamp() { 91 long timestampNanos = mService.parseTimestampNanos(new byte[]{ 92 -54, 7 93 }); 94 Assert.assertEquals(99700000000L, timestampNanos); 95 } 96 } 97