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

Commit 2c1f236c authored by Hyundo Moon's avatar Hyundo Moon
Browse files

Add two tests for com.android.server.bluetooth

Bug: 237467631
Test: atest BluetoothModeChangeHelperTest
      atest BluetoothNotificationManagerTest
Change-Id: I11abddcb7cf11f1837de1eb7cf60851a70b8f92a
(cherry picked from commit e30bb4cb)
Merged-In: I11abddcb7cf11f1837de1eb7cf60851a70b8f92a
parent d81cc97a
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -35,7 +35,10 @@
         android:targetPackage="com.android.server.bluetooth.test"
         android:label="Service Bluetooth Tests"/>

    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS"/>

</manifest>
+3 −0
Original line number Diff line number Diff line
@@ -17,6 +17,9 @@
    <target_preparer class="com.android.tradefed.targetprep.suite.SuiteApkInstaller">
        <option name="test-file-name" value="ServiceBluetoothTests.apk" />
    </target_preparer>
    <target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer">
        <option name="force-root" value="true" />
    </target_preparer>

    <option name="test-suite-tag" value="apct" />
    <option name="test-tag" value="ServiceBluetoothTests" />
+133 −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.server.bluetooth;

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

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

import android.content.Context;
import android.content.ContextWrapper;
import android.provider.Settings;
import android.text.TextUtils;

import androidx.test.InstrumentationRegistry;
import androidx.test.annotation.UiThreadTest;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

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 BluetoothModeChangeHelperTest {

    @Mock
    BluetoothManagerService mService;

    Context mContext;
    BluetoothModeChangeHelper mHelper;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mContext = spy(new ContextWrapper(InstrumentationRegistry.getTargetContext()));

        mHelper = new BluetoothModeChangeHelper(mContext);
    }

    @Test
    public void isMediaProfileConnected() {
        assertThat(mHelper.isMediaProfileConnected()).isFalse();
    }

    @Test
    public void isBluetoothOn_doesNotCrash() {
        // assertThat(mHelper.isBluetoothOn()).isFalse();
        // TODO: Strangely, isBluetoothOn() does not call BluetoothAdapter.isEnabled().
        //       Instead, it calls isLeEnabled(). Two results can be different.
        //       Is this a mistake, or in purpose?
        mHelper.isBluetoothOn();
    }

    @Test
    public void isAirplaneModeOn() {
        assertThat(mHelper.isAirplaneModeOn()).isFalse();
    }

    @Test
    public void onAirplaneModeChanged() {
        mHelper.onAirplaneModeChanged(mService);

        verify(mService).onAirplaneModeChanged();
    }

    @Test
    public void setSettingsInt() {
        String testSettingsName = "BluetoothModeChangeHelperTest_test_settings_name";
        int value = 9876;

        try {
            mHelper.setSettingsInt(testSettingsName, value);
            assertThat(mHelper.getSettingsInt(testSettingsName)).isEqualTo(value);
        } finally {
            Settings.Global.resetToDefaults(mContext.getContentResolver(), null);
        }
    }

    @Test
    public void setSettingsSecureInt() {
        String testSettingsName = "BluetoothModeChangeHelperTest_test_settings_name";
        int value = 1234;

        try {
            mHelper.setSettingsSecureInt(testSettingsName, value);
            assertThat(mHelper.getSettingsSecureInt(testSettingsName, 0)).isEqualTo(value);
        } finally {
            Settings.Global.resetToDefaults(mContext.getContentResolver(), null);
        }
    }

    @Test
    public void isBluetoothOnAPM_doesNotCrash() {
        mHelper.isBluetoothOnAPM();
    }

    @UiThreadTest
    @Test
    public void showToastMessage_doesNotCrash() {
        mHelper.showToastMessage();
    }

    @Test
    public void getBluetoothPackageName() {
        // TODO: Find a good way to specify the exact name of bluetooth package.
        //       mContext.getPackageName() does not work as this is not a test for BT app.
        String bluetoothPackageName = mHelper.getBluetoothPackageName();

        boolean packageNameFound =
                TextUtils.equals(bluetoothPackageName, "com.android.bluetooth")
                || TextUtils.equals(bluetoothPackageName, "com.google.android.bluetooth");

        assertThat(packageNameFound).isTrue();
    }
}
+101 −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.server.bluetooth;

import static com.android.internal.messages.nano.SystemMessageProto.SystemMessage.NOTE_BT_APM_NOTIFICATION;

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

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.service.notification.StatusBarNotification;

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

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 BluetoothNotificationManagerTest {

    @Mock
    NotificationManager mNotificationManager;

    Context mContext;
    BluetoothNotificationManager mBluetoothNotificationManager;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mContext = spy(new ContextWrapper(InstrumentationRegistry.getTargetContext()));
        doReturn(mNotificationManager).when(mContext).getSystemService(NotificationManager.class);
        doReturn(mContext).when(mContext).createPackageContextAsUser(anyString(), anyInt(), any());

        mBluetoothNotificationManager = new BluetoothNotificationManager(mContext);
    }

    @Test
    public void createNotificationChannels_callsNotificationManagerCreateNotificationChannels() {
        mBluetoothNotificationManager.createNotificationChannels();

        verify(mNotificationManager).createNotificationChannels(any());
    }

    @Test
    public void notify_callsNotificationManagerNotify() {
        int id = 1234;
        Notification notification = mock(Notification.class);

        mBluetoothNotificationManager.notify(id, notification);

        verify(mNotificationManager).notify(anyString(), eq(id), eq(notification));
    }

    @Test
    public void sendApmNotification_callsNotificationManagerNotify_withApmNotificationId() {
        mBluetoothNotificationManager.sendApmNotification("test_title", "test_message");

        verify(mNotificationManager).notify(anyString(), eq(NOTE_BT_APM_NOTIFICATION), any());
    }

    @Test
    public void getActiveNotifications() {
        StatusBarNotification[] notifications = new StatusBarNotification[0];
        when(mNotificationManager.getActiveNotifications()).thenReturn(notifications);

        assertThat(mBluetoothNotificationManager.getActiveNotifications())
                .isEqualTo(notifications);
    }
}