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

Commit c3ebd225 authored by Ömer Faruk Yılmaz's avatar Ömer Faruk Yılmaz
Browse files

Add additional GattServiceTest

Bug: 237467631
Test: atest GattServiceTest
Change-Id: I2247d0bbc3de8e1d6a18f63ac0f1b4a4e9a2cfb4
parent 3d803ead
Loading
Loading
Loading
Loading
+89 −5
Original line number Original line Diff line number Diff line
/*
 * Copyright 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.bluetooth.gatt;
package com.android.bluetooth.gatt;


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

import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.*;


import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.content.AttributionSource;
import android.content.Context;
import android.content.Context;


import androidx.test.InstrumentationRegistry;
import androidx.test.InstrumentationRegistry;
@@ -26,10 +45,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

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


/**
/**
 * Test cases for {@link GattService}.
 * Test cases for {@link GattService}.
@@ -37,14 +52,21 @@ import java.util.concurrent.TimeUnit;
@SmallTest
@SmallTest
@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
public class GattServiceTest {
public class GattServiceTest {

    private static final String REMOTE_DEVICE_ADDRESS = "00:00:00:00:00:00";

    private static final int TIMES_UP_AND_DOWN = 3;
    private static final int TIMES_UP_AND_DOWN = 3;
    private static final int TIMEOUT_MS = 5_000;
    private static final int TIMEOUT_MS = 5_000;
    private Context mTargetContext;
    private Context mTargetContext;
    private GattService mService;
    private GattService mService;
    @Mock private GattService.ClientMap mClientMap;
    @Mock private GattService.ScannerMap mScannerMap;


    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();
    @Rule public final ServiceTestRule mServiceRule = new ServiceTestRule();


    private BluetoothAdapter mAdapter;
    private BluetoothAdapter mAdapter;
    private AttributionSource mAttributionSource;

    @Mock private AdapterService mAdapterService;
    @Mock private AdapterService mAdapterService;
    @Mock private GattObjectsFactory mFactory;
    @Mock private GattObjectsFactory mFactory;
    @Mock private GattNativeInterface mNativeInterface;
    @Mock private GattNativeInterface mNativeInterface;
@@ -62,10 +84,14 @@ public class GattServiceTest {
        doReturn(mNativeInterface).when(mFactory).getNativeInterface();
        doReturn(mNativeInterface).when(mFactory).getNativeInterface();


        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mAttributionSource = mAdapter.getAttributionSource();


        TestUtils.startService(mServiceRule, GattService.class);
        TestUtils.startService(mServiceRule, GattService.class);
        mService = GattService.getGattService();
        mService = GattService.getGattService();
        Assert.assertNotNull(mService);
        Assert.assertNotNull(mService);

        mService.mClientMap = mClientMap;
        mService.mScannerMap = mScannerMap;
    }
    }


    @After
    @After
@@ -113,4 +139,62 @@ public class GattServiceTest {
        });
        });
        Assert.assertEquals(99700000000L, timestampNanos);
        Assert.assertEquals(99700000000L, timestampNanos);
    }
    }

    @Test
    public void emptyClearServices() {
        int serverIf = 1;

        mService.clearServices(serverIf, mAttributionSource);
        verify(mNativeInterface, times(0)).gattServerDeleteService(eq(serverIf), anyInt());
    }

    @Test
    public void clientReadPhy() {
        int clientIf = 1;
        String address = REMOTE_DEVICE_ADDRESS;

        Integer connId = 1;
        doReturn(connId).when(mClientMap).connIdByAddress(clientIf, address);

        mService.clientReadPhy(clientIf, address, mAttributionSource);
        verify(mNativeInterface).gattClientReadPhy(clientIf, address);
    }

    @Test
    public void clientSetPreferredPhy() {
        int clientIf = 1;
        String address = REMOTE_DEVICE_ADDRESS;
        int txPhy = 2;
        int rxPhy = 1;
        int phyOptions = 3;

        Integer connId = 1;
        doReturn(connId).when(mClientMap).connIdByAddress(clientIf, address);

        mService.clientSetPreferredPhy(clientIf, address, txPhy, rxPhy, phyOptions,
                mAttributionSource);
        verify(mNativeInterface).gattClientSetPreferredPhy(clientIf, address, txPhy, rxPhy,
                phyOptions);
    }

    @Test
    public void connectionParameterUpdate() {
        int clientIf = 1;
        String address = REMOTE_DEVICE_ADDRESS;

        int connectionPriority = BluetoothGatt.CONNECTION_PRIORITY_HIGH;
        mService.connectionParameterUpdate(clientIf, address, connectionPriority,
                mAttributionSource);

        connectionPriority = BluetoothGatt.CONNECTION_PRIORITY_LOW_POWER;
        mService.connectionParameterUpdate(clientIf, address, connectionPriority,
                mAttributionSource);

        connectionPriority = BluetoothGatt.CONNECTION_PRIORITY_BALANCED;;
        mService.connectionParameterUpdate(clientIf, address, connectionPriority,
                mAttributionSource);

        verify(mNativeInterface, times(3)).gattConnectionParameterUpdate(eq(clientIf),
                eq(address), anyInt(), anyInt(), anyInt(), anyInt(), eq(0), eq(0));
    }
}
}