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

Commit 8f717b49 authored by Sungsoo Lim's avatar Sungsoo Lim Committed by Gerrit Code Review
Browse files

Merge "Add HidDeviceNativeInterfaceTest"

parents 666c804f 00d1af36
Loading
Loading
Loading
Loading
+15 −7
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.util.Log;
import com.android.bluetooth.btservice.AdapterService;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;

import java.util.Objects;

/**
@@ -179,7 +180,8 @@ public class HidDeviceNativeInterface {
        return reportErrorNative(error);
    }

    private synchronized void onApplicationStateChanged(byte[] address, boolean registered) {
    @VisibleForTesting
    synchronized void onApplicationStateChanged(byte[] address, boolean registered) {
        HidDeviceService service = HidDeviceService.getHidDeviceService();
        if (service != null) {
            service.onApplicationStateChangedFromNative(getDevice(address), registered);
@@ -189,7 +191,8 @@ public class HidDeviceNativeInterface {
        }
    }

    private synchronized void onConnectStateChanged(byte[] address, int state) {
    @VisibleForTesting
    synchronized void onConnectStateChanged(byte[] address, int state) {
        HidDeviceService service = HidDeviceService.getHidDeviceService();
        if (service != null) {
            service.onConnectStateChangedFromNative(getDevice(address), state);
@@ -199,7 +202,8 @@ public class HidDeviceNativeInterface {
        }
    }

    private synchronized void onGetReport(byte type, byte id, short bufferSize) {
    @VisibleForTesting
    synchronized void onGetReport(byte type, byte id, short bufferSize) {
        HidDeviceService service = HidDeviceService.getHidDeviceService();
        if (service != null) {
            service.onGetReportFromNative(type, id, bufferSize);
@@ -209,7 +213,8 @@ public class HidDeviceNativeInterface {
        }
    }

    private synchronized void onSetReport(byte reportType, byte reportId, byte[] data) {
    @VisibleForTesting
    synchronized void onSetReport(byte reportType, byte reportId, byte[] data) {
        HidDeviceService service = HidDeviceService.getHidDeviceService();
        if (service != null) {
            service.onSetReportFromNative(reportType, reportId, data);
@@ -219,7 +224,8 @@ public class HidDeviceNativeInterface {
        }
    }

    private synchronized void onSetProtocol(byte protocol) {
    @VisibleForTesting
    synchronized void onSetProtocol(byte protocol) {
        HidDeviceService service = HidDeviceService.getHidDeviceService();
        if (service != null) {
            service.onSetProtocolFromNative(protocol);
@@ -229,7 +235,8 @@ public class HidDeviceNativeInterface {
        }
    }

    private synchronized void onInterruptData(byte reportId, byte[] data) {
    @VisibleForTesting
    synchronized void onInterruptData(byte reportId, byte[] data) {
        HidDeviceService service = HidDeviceService.getHidDeviceService();
        if (service != null) {
            service.onInterruptDataFromNative(reportId, data);
@@ -239,7 +246,8 @@ public class HidDeviceNativeInterface {
        }
    }

    private synchronized void onVirtualCableUnplug() {
    @VisibleForTesting
    synchronized void onVirtualCableUnplug() {
        HidDeviceService service = HidDeviceService.getHidDeviceService();
        if (service != null) {
            service.onVirtualCableUnplugFromNative();
+2 −1
Original line number Diff line number Diff line
@@ -830,7 +830,8 @@ public class HidDeviceService extends ProfileService {
        return sHidDeviceService;
    }

    private static synchronized void setHidDeviceService(HidDeviceService instance) {
    @VisibleForTesting
    static synchronized void setHidDeviceService(HidDeviceService instance) {
        if (DBG) {
            Log.d(TAG, "setHidDeviceService(): set to: " + instance);
        }
+111 −0
Original line number Diff line number Diff line
/*
 * Copyright 2022 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.hid;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.bluetooth.BluetoothHidDevice;

import com.android.bluetooth.btservice.AdapterService;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

public class HidDeviceNativeInterfaceTest {
    private static final byte[] TEST_DEVICE_ADDRESS =
            new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
    @Mock
    HidDeviceService mService;
    @Mock
    AdapterService mAdapterService;

    private HidDeviceNativeInterface mNativeInterface;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(mService.isAvailable()).thenReturn(true);
        HidDeviceService.setHidDeviceService(mService);
        AdapterService.setAdapterService(mAdapterService);
        mNativeInterface = HidDeviceNativeInterface.getInstance();
    }

    @After
    public void tearDown() {
        HidDeviceService.setHidDeviceService(null);
        AdapterService.setAdapterService(null);
    }

    @Test
    public void onApplicationStateChanged() {
        mNativeInterface.onApplicationStateChanged(TEST_DEVICE_ADDRESS, true);
        verify(mService).onApplicationStateChangedFromNative(any(), anyBoolean());
    }

    @Test
    public void onConnectStateChanged() {
        mNativeInterface.onConnectStateChanged(TEST_DEVICE_ADDRESS,
                BluetoothHidDevice.STATE_DISCONNECTED);
        verify(mService).onConnectStateChangedFromNative(any(), anyInt());
    }

    @Test
    public void onGetReport() {
        byte type = 1;
        byte id = 2;
        short bufferSize = 100;
        mNativeInterface.onGetReport(type, id, bufferSize);
        verify(mService).onGetReportFromNative(type, id, bufferSize);
    }

    @Test
    public void onSetReport() {
        byte reportType = 1;
        byte reportId = 2;
        byte[] data = new byte[] { 0x00, 0x00 };
        mNativeInterface.onSetReport(reportType, reportId, data);
        verify(mService).onSetReportFromNative(reportType, reportId, data);
    }

    @Test
    public void onSetProtocol() {
        byte protocol = 1;
        mNativeInterface.onSetProtocol(protocol);
        verify(mService).onSetProtocolFromNative(protocol);
    }

    @Test
    public void onInterruptData() {
        byte reportId = 3;
        byte[] data = new byte[] { 0x00, 0x00 };
        mNativeInterface.onInterruptData(reportId, data);
        verify(mService).onInterruptDataFromNative(reportId, data);
    }

    @Test
    public void onVirtualCableUnplug() {
        mNativeInterface.onVirtualCableUnplug();
        verify(mService).onVirtualCableUnplugFromNative();
    }
}