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

Commit 227517dc authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Add tests for classes in package HfpClient"

parents 4c46bdb3 31975812
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -21,6 +21,8 @@ package com.android.bluetooth.hfpclient;

import android.bluetooth.BluetoothDevice;

import com.android.internal.annotations.VisibleForTesting;

public class StackEvent {
    // Type of event that signifies a native event and consumed by state machine
    public static final int STACK_EVENT = 100;
@@ -76,7 +78,8 @@ public class StackEvent {
    }

    // for debugging only
    private static String eventTypeToString(int type) {
    @VisibleForTesting
    static String eventTypeToString(int type) {
        switch (type) {
            case EVENT_TYPE_NONE:
                return "EVENT_TYPE_NONE";
+91 −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.hfpclient;

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

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class StackEventTest {

    @Test
    public void toString_returnsInfo() {
        int type = StackEvent.EVENT_TYPE_RING_INDICATION;

        StackEvent event = new StackEvent(type);
        String expectedString = "StackEvent {type:" + StackEvent.eventTypeToString(type)
                + ", value1:" + event.valueInt + ", value2:" + event.valueInt2 + ", value3:"
                + event.valueInt3 + ", value4:" + event.valueInt4 + ", string: \""
                + event.valueString + "\"" + ", device:" + event.device + "}";

        assertThat(event.toString()).isEqualTo(expectedString);
    }

    @Test
    public void eventTypeToString() {
        int invalidType = 23;

        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_NONE)).isEqualTo(
                "EVENT_TYPE_NONE");
        assertThat(StackEvent.eventTypeToString(
                StackEvent.EVENT_TYPE_CONNECTION_STATE_CHANGED)).isEqualTo(
                "EVENT_TYPE_CONNECTION_STATE_CHANGED");
        assertThat(
                StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_AUDIO_STATE_CHANGED)).isEqualTo(
                "EVENT_TYPE_AUDIO_STATE_CHANGED");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_NETWORK_STATE)).isEqualTo(
                "EVENT_TYPE_NETWORK_STATE");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_ROAMING_STATE)).isEqualTo(
                "EVENT_TYPE_ROAMING_STATE");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_NETWORK_SIGNAL)).isEqualTo(
                "EVENT_TYPE_NETWORK_SIGNAL");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_BATTERY_LEVEL)).isEqualTo(
                "EVENT_TYPE_BATTERY_LEVEL");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_OPERATOR_NAME)).isEqualTo(
                "EVENT_TYPE_OPERATOR_NAME");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_CALL)).isEqualTo(
                "EVENT_TYPE_CALL");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_CALLSETUP)).isEqualTo(
                "EVENT_TYPE_CALLSETUP");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_CALLHELD)).isEqualTo(
                "EVENT_TYPE_CALLHELD");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_CLIP)).isEqualTo(
                "EVENT_TYPE_CLIP");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_CALL_WAITING)).isEqualTo(
                "EVENT_TYPE_CALL_WAITING");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_CURRENT_CALLS)).isEqualTo(
                "EVENT_TYPE_CURRENT_CALLS");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_VOLUME_CHANGED)).isEqualTo(
                "EVENT_TYPE_VOLUME_CHANGED");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_CMD_RESULT)).isEqualTo(
                "EVENT_TYPE_CMD_RESULT");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_SUBSCRIBER_INFO)).isEqualTo(
                "EVENT_TYPE_SUBSCRIBER_INFO");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_RESP_AND_HOLD)).isEqualTo(
                "EVENT_TYPE_RESP_AND_HOLD");
        assertThat(StackEvent.eventTypeToString(StackEvent.EVENT_TYPE_RING_INDICATION)).isEqualTo(
                "EVENT_TYPE_RING_INDICATION");
        assertThat(StackEvent.eventTypeToString(invalidType)).isEqualTo(
                "EVENT_TYPE_UNKNOWN:" + invalidType);
    }
}
+150 −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.hfpclient;

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

import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothAssignedNumbers;
import android.bluetooth.BluetoothDevice;

import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.bluetooth.TestUtils;
import com.android.bluetooth.btservice.AdapterService;

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

@SmallTest
@RunWith(AndroidJUnit4.class)
public class VendorCommandResponseProcessorTest {
    private static int TEST_VENDOR_ID = BluetoothAssignedNumbers.APPLE;

    private BluetoothAdapter mAdapter;
    private BluetoothDevice mTestDevice;
    private NativeInterface mNativeInterface;
    private VendorCommandResponseProcessor mProcessor;

    @Mock
    private AdapterService mAdapterService;
    @Mock
    private HeadsetClientService mHeadsetClientService;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        TestUtils.setAdapterService(mAdapterService);
        mNativeInterface = spy(NativeInterface.getInstance());

        mAdapter = BluetoothAdapter.getDefaultAdapter();
        mTestDevice = mAdapter.getRemoteDevice("00:01:02:03:04:05");
        mProcessor = new VendorCommandResponseProcessor(mHeadsetClientService, mNativeInterface);
    }

    @After
    public void tearDown() throws Exception {
        TestUtils.clearAdapterService(mAdapterService);
    }

    @Test
    public void sendCommand_withSemicolon() {
        String atCommand = "command;";

        assertThat(mProcessor.sendCommand(TEST_VENDOR_ID, atCommand, mTestDevice)).isFalse();
    }

    @Test
    public void sendCommand_withNullDevice() {
        String atCommand = "+XAPL=";

        assertThat(mProcessor.sendCommand(TEST_VENDOR_ID, atCommand, null)).isFalse();
    }

    @Test
    public void sendCommand_withInvalidCommand() {
        String invalidCommand = "invalidCommand";

        assertThat(mProcessor.sendCommand(TEST_VENDOR_ID, invalidCommand, mTestDevice)).isFalse();
    }

    @Test
    public void sendCommand_withEqualSign() {
        String atCommand = "+XAPL=";
        doReturn(true).when(mNativeInterface).sendATCmd(mTestDevice,
                HeadsetClientHalConstants.HANDSFREECLIENT_AT_CMD_VENDOR_SPECIFIC_CMD, 0, 0,
                atCommand);

        assertThat(mProcessor.sendCommand(TEST_VENDOR_ID, atCommand, mTestDevice)).isTrue();
    }

    @Test
    public void sendCommand_withQuestionMarkSign() {
        String atCommand = "+APLSIRI?";
        doReturn(true).when(mNativeInterface).sendATCmd(mTestDevice,
                HeadsetClientHalConstants.HANDSFREECLIENT_AT_CMD_VENDOR_SPECIFIC_CMD, 0, 0,
                atCommand);

        assertThat(mProcessor.sendCommand(TEST_VENDOR_ID, atCommand, mTestDevice)).isTrue();
    }

    @Test
    public void sendCommand_failingToSendATCommand() {
        String atCommand = "+APLSIRI?";
        doReturn(false).when(mNativeInterface).sendATCmd(mTestDevice,
                HeadsetClientHalConstants.HANDSFREECLIENT_AT_CMD_VENDOR_SPECIFIC_CMD, 0, 0,
                atCommand);

        assertThat(mProcessor.sendCommand(TEST_VENDOR_ID, atCommand, mTestDevice)).isFalse();
    }

    @Test
    public void processEvent_withNullDevice() {
        String atString = "+XAPL=";

        assertThat(mProcessor.processEvent(atString, null)).isFalse();
    }

    @Test
    public void processEvent_withInvalidString() {
        String invalidString = "+APLSIRI?";

        assertThat(mProcessor.processEvent(invalidString, mTestDevice)).isFalse();
    }

    @Test
    public void processEvent_withEqualSign() {
        String atString = "+XAPL=";

        assertThat(mProcessor.processEvent(atString, mTestDevice)).isTrue();
    }

    @Test
    public void processEvent_withColonSign() {
        String atString = "+APLSIRI:";

        assertThat(mProcessor.processEvent(atString, mTestDevice)).isTrue();
    }
}
 No newline at end of file