Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8e3ff356 authored by Hyundo Moon's avatar Hyundo Moon
Browse files

Cherry-pick SapServiceTest from AOSP

This CL cherry-picks the SapServiceTest from commit
7cc6a4da.

Bug: 237467631
Test: atest SapServiceTest
Change-Id: Ide4a03a45b2721a7a62b2cabe70d4110faedbe3e
Merged-In: I3d95be363d0b8b237dc2247c1326d9824ea3ef3c
parent 7d39416c
Loading
Loading
Loading
Loading
+83 −7
Original line number Diff line number Diff line
@@ -13,12 +13,18 @@
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.sap;

import static com.google.common.truth.Truth.assertThat;

import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothProfile;
import android.content.Context;

import androidx.test.InstrumentationRegistry;
@@ -26,12 +32,11 @@ import androidx.test.filters.MediumTest;
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 com.android.bluetooth.btservice.storage.DatabaseManager;

import org.junit.After;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
@@ -40,9 +45,15 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

@MediumTest
@RunWith(AndroidJUnit4.class)
public class SapServiceTest {
    private static final int TIMEOUT_MS = 5_000;

    private SapService mService = null;
    private BluetoothAdapter mAdapter = null;
    private Context mTargetContext;
@@ -50,6 +61,8 @@ public class SapServiceTest {
    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();

    @Mock private AdapterService mAdapterService;
    @Mock private DatabaseManager mDatabaseManager;
    private BluetoothDevice mDevice;

    @Before
    public void setUp() throws Exception {
@@ -61,10 +74,11 @@ public class SapServiceTest {
        doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
        TestUtils.startService(mServiceRule, SapService.class);
        mService = SapService.getSapService();
        Assert.assertNotNull(mService);
        assertThat(mService).isNotNull();
        // Try getting the Bluetooth adapter
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        Assert.assertNotNull(mAdapter);
        assertThat(mAdapter).isNotNull();
        mDevice = TestUtils.getTestDevice(mAdapter, 0);
    }

    @After
@@ -74,12 +88,74 @@ public class SapServiceTest {
        }
        TestUtils.stopService(mServiceRule, SapService.class);
        mService = SapService.getSapService();
        Assert.assertNull(mService);
        assertThat(mService).isNull();
        TestUtils.clearAdapterService(mAdapterService);
    }

    @Test
    public void testInitialize() {
        Assert.assertNotNull(SapService.getSapService());
    public void testGetSapService() {
        assertThat(mService).isEqualTo(SapService.getSapService());
        assertThat(mService.getConnectedDevices()).isEmpty();
    }

    /**
     * Test stop SAP Service
     */
    @Test
    public void testStopSapService() throws Exception {
        AtomicBoolean stopResult = new AtomicBoolean();
        AtomicBoolean startResult = new AtomicBoolean();
        CountDownLatch latch = new CountDownLatch(1);

        // SAP Service is already running: test stop(). Note: must be done on the main thread
        InstrumentationRegistry.getInstrumentation().runOnMainSync(new Runnable() {
            public void run() {
                stopResult.set(mService.stop());
                startResult.set(mService.start());
                latch.countDown();
            }
        });

        assertThat(latch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)).isTrue();
        assertThat(stopResult.get()).isTrue();
        assertThat(startResult.get()).isTrue();
    }

    /**
     * Test get connection policy for BluetoothDevice
     */
    @Test
    public void testGetConnectionPolicy() {
        when(mAdapterService.getDatabase()).thenReturn(mDatabaseManager);
        when(mDatabaseManager
                .getProfileConnectionPolicy(mDevice, BluetoothProfile.SAP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);
        assertThat(mService.getConnectionPolicy(mDevice))
                .isEqualTo(BluetoothProfile.CONNECTION_POLICY_UNKNOWN);

        when(mDatabaseManager
                .getProfileConnectionPolicy(mDevice, BluetoothProfile.SAP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);
        assertThat(mService.getConnectionPolicy(mDevice))
                .isEqualTo(BluetoothProfile.CONNECTION_POLICY_FORBIDDEN);

        when(mDatabaseManager
                .getProfileConnectionPolicy(mDevice, BluetoothProfile.SAP))
                .thenReturn(BluetoothProfile.CONNECTION_POLICY_ALLOWED);

        assertThat(mService.getConnectionPolicy(mDevice))
                .isEqualTo(BluetoothProfile.CONNECTION_POLICY_ALLOWED);
    }

    @Test
    public void testGetRemoteDevice() {
        assertThat(mService.getRemoteDevice()).isNull();
    }

    @Test
    public void testGetRemoteDeviceName() {
        assertThat(SapService.getRemoteDeviceName()).isNull();
    }
}