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

Commit a0a88c3d authored by Hyundo Moon's avatar Hyundo Moon Committed by Gerrit Code Review
Browse files

Merge "Add more tests for com.android.bluetooth package"

parents e4cbfd22 b760a773
Loading
Loading
Loading
Loading
+88 −0
Original line number 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;

import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
import static android.content.pm.PackageManager.DONT_KILL_APP;

import static androidx.test.espresso.intent.Intents.intended;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;

import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.intent.Intents;
import androidx.test.espresso.intent.matcher.IntentMatchers;
import androidx.test.filters.LargeTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

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

@LargeTest
@RunWith(AndroidJUnit4.class)
public class BluetoothPrefsTest {

    Context mTargetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    Intent mIntent;

    ActivityScenario<BluetoothPrefs> mActivityScenario;

    @Before
    public void setUp() {
        Intents.init();
        enableActivity(true);

        mIntent = new Intent();
        mIntent.setClass(mTargetContext, BluetoothPrefs.class);

        mActivityScenario = ActivityScenario.launch(mIntent);
    }

    @After
    public void tearDown() throws Exception {
        if (mActivityScenario != null) {
            // Workaround for b/159805732. Without this, test hangs for 45 seconds.
            Thread.sleep(1_000);
            mActivityScenario.close();
        }

        enableActivity(false);
    }

    @Test
    public void initialize_launchesBluetoothSettingsActivity() {
        intended(IntentMatchers.hasAction(BluetoothPrefs.BLUETOOTH_SETTING_ACTION));
    }

    private void enableActivity(boolean enable) {
        int enabledState = enable ? COMPONENT_ENABLED_STATE_ENABLED
                : COMPONENT_ENABLED_STATE_DEFAULT;

        mTargetContext.getPackageManager().setApplicationEnabledSetting(
                mTargetContext.getPackageName(), enabledState, DONT_KILL_APP);

        ComponentName activityName = new ComponentName(mTargetContext, BluetoothPrefs.class);
        mTargetContext.getPackageManager().setComponentEnabledSetting(
                activityName, enabledState, DONT_KILL_APP);
    }
}
 No newline at end of file
+139 −0
Original line number 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;

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

import static org.junit.Assert.assertThrows;

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

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

/**
 * Test for SignedLongLong.java
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
public class SignedLongLongTest {

    @Test
    public void compareTo_sameValue_returnsZero() {
        long mostSigBits = 1352;
        long leastSigBits = 53423;

        SignedLongLong value = new SignedLongLong(mostSigBits, leastSigBits);
        SignedLongLong sameValue = new SignedLongLong(mostSigBits, leastSigBits);

        assertThat(value.compareTo(sameValue)).isEqualTo(0);
    }

    @Test
    public void compareTo_biggerLeastSigBits_returnsMinusOne() {
        long commonMostSigBits = 12345;
        long leastSigBits = 1;
        SignedLongLong value = new SignedLongLong(leastSigBits, commonMostSigBits);

        long biggerLeastSigBits = 2;
        SignedLongLong biggerValue = new SignedLongLong(biggerLeastSigBits, commonMostSigBits);

        assertThat(value.compareTo(biggerValue)).isEqualTo(-1);
    }

    @Test
    public void compareTo_smallerLeastSigBits_returnsOne() {
        long commonMostSigBits = 12345;
        long leastSigBits = 2;
        SignedLongLong value = new SignedLongLong(leastSigBits, commonMostSigBits);

        long smallerLeastSigBits = 1;
        SignedLongLong smallerValue = new SignedLongLong(smallerLeastSigBits, commonMostSigBits);

        assertThat(value.compareTo(smallerValue)).isEqualTo(1);
    }

    @Test
    public void compareTo_biggerMostSigBits_returnsMinusOne() {
        long commonLeastSigBits = 12345;
        long mostSigBits = 1;
        SignedLongLong value = new SignedLongLong(commonLeastSigBits, mostSigBits);

        long biggerMostSigBits = 2;
        SignedLongLong biggerValue = new SignedLongLong(commonLeastSigBits, biggerMostSigBits);

        assertThat(value.compareTo(biggerValue)).isEqualTo(-1);
    }

    @Test
    public void compareTo_smallerMostSigBits_returnsOne() {
        long commonLeastSigBits = 12345;
        long mostSigBits = 2;
        SignedLongLong value = new SignedLongLong(commonLeastSigBits, mostSigBits);

        long smallerMostSigBits = 1;
        SignedLongLong smallerValue = new SignedLongLong(commonLeastSigBits, smallerMostSigBits);

        assertThat(value.compareTo(smallerValue)).isEqualTo(1);
    }

    @Test
    public void toString_RepresentedAsHexValues() {
        SignedLongLong value = new SignedLongLong(2, 11);

        assertThat(value.toString()).isEqualTo("B0000000000000002");
    }

    @SuppressWarnings("EqualsIncompatibleType")
    @Test
    public void equals_variousCases() {
        SignedLongLong value = new SignedLongLong(1, 2);

        assertThat(value.equals(value)).isTrue();
        assertThat(value.equals(null)).isFalse();
        assertThat(value.equals("a random string")).isFalse();
        assertThat(value.equals(new SignedLongLong(1, 1))).isFalse();
        assertThat(value.equals(new SignedLongLong(2, 2))).isFalse();
        assertThat(value.equals(new SignedLongLong(1, 2))).isTrue();
    }

    @Test
    public void fromString_whenStringIsNull_throwsNPE() {
        assertThrows(NullPointerException.class, () -> SignedLongLong.fromString(null));
    }

    @Test
    public void fromString_whenLengthIsInvalid_throwsNumberFormatException() {
        assertThrows(NumberFormatException.class, () -> SignedLongLong.fromString(""));
    }

    @Test
    public void fromString_whenLengthIsNotGreaterThan16() throws Exception {
        String strValue = "1";

        assertThat(SignedLongLong.fromString(strValue))
                .isEqualTo(new SignedLongLong(1, 0));
    }

    @Test
    public void fromString_whenLengthIsGreaterThan16() throws Exception {
        String strValue = "B0000000000000002";

        assertThat(SignedLongLong.fromString(strValue))
                .isEqualTo(new SignedLongLong(2, 11));
    }
}
+81 −0
Original line number Diff line number Diff line
@@ -17,12 +17,16 @@ package com.android.bluetooth;

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

import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.location.LocationManager;
import android.os.Build;
import android.os.ParcelUuid;
import android.os.UserHandle;

@@ -36,6 +40,11 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.UUID;
@@ -168,4 +177,76 @@ public class UtilsTest {
        String loggableAddress = "xx:xx:xx:xx:" + device.getAddress().substring(12);
        assertThat(Utils.getLoggableAddress(device)).isEqualTo(loggableAddress);
    }

    @Test
    public void checkCallerIsSystemMethods_doesNotCrash() {
        Context context = InstrumentationRegistry.getTargetContext();
        String tag = "test_tag";

        Utils.checkCallerIsSystemOrActiveOrManagedUser(context, tag);
        Utils.checkCallerIsSystemOrActiveOrManagedUser(null, tag);
        Utils.checkCallerIsSystemOrActiveUser(tag);
    }

    @Test
    public void testCopyStream() throws Exception {
        byte[] data = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
        ByteArrayInputStream in = new ByteArrayInputStream(data);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        int bufferSize = 4;

        Utils.copyStream(in, out, bufferSize);

        assertThat(out.toByteArray()).isEqualTo(data);
    }

    @Test
    public void debugGetAdapterStateString() {
        assertThat(Utils.debugGetAdapterStateString(BluetoothAdapter.STATE_OFF))
                .isEqualTo("STATE_OFF");
        assertThat(Utils.debugGetAdapterStateString(BluetoothAdapter.STATE_ON))
                .isEqualTo("STATE_ON");
        assertThat(Utils.debugGetAdapterStateString(BluetoothAdapter.STATE_TURNING_ON))
                .isEqualTo("STATE_TURNING_ON");
        assertThat(Utils.debugGetAdapterStateString(BluetoothAdapter.STATE_TURNING_OFF))
                .isEqualTo("STATE_TURNING_OFF");
        assertThat(Utils.debugGetAdapterStateString(-124))
                .isEqualTo("UNKNOWN");
    }

    @Test
    public void ellipsize() {
        if (!Build.TYPE.equals("user")) {
            // Only ellipsize release builds
            String input = "a_long_string";
            assertThat(Utils.ellipsize(input)).isEqualTo(input);
            return;
        }

        assertThat(Utils.ellipsize("ab")).isEqualTo("ab");
        assertThat(Utils.ellipsize("abc")).isEqualTo("a⋯c");
        assertThat(Utils.ellipsize(null)).isEqualTo(null);
    }

    @Test
    public void safeCloseStream_inputStream_doesNotCrash() throws Exception {
        InputStream is = mock(InputStream.class);
        Utils.safeCloseStream(is);
        verify(is).close();

        Mockito.clearInvocations(is);
        doThrow(new IOException()).when(is).close();
        Utils.safeCloseStream(is);
    }

    @Test
    public void safeCloseStream_outputStream_doesNotCrash() throws Exception {
        OutputStream os = mock(OutputStream.class);
        Utils.safeCloseStream(os);
        verify(os).close();

        Mockito.clearInvocations(os);
        doThrow(new IOException()).when(os).close();
        Utils.safeCloseStream(os);
    }
}