blob: 6882b50de29fb1316b7491583f13f2bd3173ac59 [file] [log] [blame]
package com.android.bluetooth.gatt;
import static org.mockito.Mockito.*;
import android.content.Context;
import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import androidx.test.rule.ServiceTestRule;
import androidx.test.runner.AndroidJUnit4;
import com.android.bluetooth.R;
import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;
import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
/**
* Test cases for {@link GattService}.
*/
@SmallTest
@RunWith(AndroidJUnit4.class)
public class GattServiceTest {
private static final int TIMES_UP_AND_DOWN = 3;
private Context mTargetContext;
private GattService mService;
@Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
@Mock private AdapterService mAdapterService;
@Before
public void setUp() throws Exception {
mTargetContext = InstrumentationRegistry.getTargetContext();
Assume.assumeTrue("Ignore test when GattService is not enabled",
mTargetContext.getResources().getBoolean(R.bool.profile_supported_gatt));
MockitoAnnotations.initMocks(this);
TestUtils.setAdapterService(mAdapterService);
TestUtils.startService(mServiceRule, GattService.class);
mService = GattService.getGattService();
Assert.assertNotNull(mService);
}
@After
public void tearDown() throws Exception {
if (!mTargetContext.getResources().getBoolean(R.bool.profile_supported_gatt)) {
return;
}
TestUtils.stopService(mServiceRule, GattService.class);
mService = GattService.getGattService();
Assert.assertNull(mService);
TestUtils.clearAdapterService(mAdapterService);
}
@Test
public void testInitialize() {
Assert.assertNotNull(GattService.getGattService());
}
@Test
public void testServiceUpAndDown() throws Exception {
for (int i = 0; i < TIMES_UP_AND_DOWN; i++) {
GattService gattService = GattService.getGattService();
TestUtils.stopService(mServiceRule, GattService.class);
mService = GattService.getGattService();
Assert.assertNull(mService);
gattService.cleanup();
TestUtils.clearAdapterService(mAdapterService);
reset(mAdapterService);
TestUtils.setAdapterService(mAdapterService);
TestUtils.startService(mServiceRule, GattService.class);
mService = GattService.getGattService();
Assert.assertNotNull(mService);
}
}
@Test
public void testParseBatchTimestamp() {
long timestampNanos = mService.parseTimestampNanos(new byte[]{
-54, 7
});
Assert.assertEquals(99700000000L, timestampNanos);
}
}