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