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

Commit 96f1c0d2 authored by Stephanie Bak's avatar Stephanie Bak
Browse files

Adding tests for APM enhancement

Bug: 239983569
Test: atest ServiceBluetoothTests
Ignore-AOSP-First: feature merged internally first to avoid merge
conflicts in AOSP

Change-Id: I4c73840f8c6e3a983a3c1f065733e26cf7140bf9
parent 99033023
Loading
Loading
Loading
Loading
+153 −10
Original line number Diff line number Diff line
@@ -16,14 +16,25 @@

package com.android.server.bluetooth;

import static com.android.server.bluetooth.BluetoothAirplaneModeListener.APM_BT_NOTIFICATION;
import static com.android.server.bluetooth.BluetoothAirplaneModeListener.APM_ENHANCEMENT;
import static com.android.server.bluetooth.BluetoothAirplaneModeListener.APM_USER_TOGGLED_BLUETOOTH;
import static com.android.server.bluetooth.BluetoothAirplaneModeListener.APM_WIFI_BT_NOTIFICATION;
import static com.android.server.bluetooth.BluetoothAirplaneModeListener.NOTIFICATION_NOT_SHOWN;
import static com.android.server.bluetooth.BluetoothAirplaneModeListener.NOTIFICATION_SHOWN;
import static com.android.server.bluetooth.BluetoothAirplaneModeListener.UNUSED;
import static com.android.server.bluetooth.BluetoothAirplaneModeListener.USED;
import static com.android.server.bluetooth.BluetoothAirplaneModeListener.WIFI_APM_STATE;

import static org.mockito.Mockito.*;

import android.bluetooth.BluetoothAdapter;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.os.Looper;
import android.provider.Settings;

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

@@ -32,23 +43,27 @@ import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

@MediumTest
@RunWith(AndroidJUnit4.class)
public class BluetoothAirplaneModeListenerTest {
    private Context mContext;
    private static final String PACKAGE_NAME = "TestPackage";

    private BluetoothAirplaneModeListener mBluetoothAirplaneModeListener;
    private BluetoothAdapter mBluetoothAdapter;
    private BluetoothModeChangeHelper mHelper;
    private BluetoothNotificationManager mBluetoothNotificationManager;

    @Mock BluetoothManagerService mBluetoothManagerService;
    @Mock private Context mContext;
    @Mock private ContentResolver mContentResolver;
    @Mock private BluetoothManagerService mBluetoothManagerService;
    @Mock private BluetoothModeChangeHelper mHelper;
    @Mock private BluetoothNotificationManager mBluetoothNotificationManager;
    @Mock private PackageManager mPackageManager;
    @Mock private Resources mResources;

    @Before
    public void setUp() throws Exception {
        mContext = InstrumentationRegistry.getTargetContext();

        mHelper = mock(BluetoothModeChangeHelper.class);
        MockitoAnnotations.initMocks(this);
        when(mContext.getContentResolver()).thenReturn(mContentResolver);
        when(mHelper.getSettingsInt(BluetoothAirplaneModeListener.TOAST_COUNT))
                .thenReturn(BluetoothAirplaneModeListener.MAX_TOAST_COUNT);
        doNothing().when(mHelper).setSettingsInt(anyString(), anyInt());
@@ -75,6 +90,59 @@ public class BluetoothAirplaneModeListenerTest {
        Assert.assertTrue(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());
    }

    @Test
    public void testIgnoreOnAirplanModeChangeApmEnhancement() {
        when(mHelper.isAirplaneModeOn()).thenReturn(true);
        when(mHelper.isBluetoothOn()).thenReturn(true);

        // When APM enhancement is disabled, BT remains on when connected to a media profile
        when(mHelper.getSettingsInt(APM_ENHANCEMENT)).thenReturn(0);
        when(mHelper.isMediaProfileConnected()).thenReturn(true);
        Assert.assertTrue(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        // When APM enhancement is disabled, BT turns off when not connected to a media profile
        when(mHelper.isMediaProfileConnected()).thenReturn(false);
        Assert.assertFalse(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        // When APM enhancement is enabled but not activated by toggling BT in APM,
        // BT remains on when connected to a media profile
        when(mHelper.getSettingsInt(APM_ENHANCEMENT)).thenReturn(1);
        when(mHelper.getSettingsSecureInt(APM_USER_TOGGLED_BLUETOOTH, UNUSED)).thenReturn(UNUSED);
        when(mHelper.isMediaProfileConnected()).thenReturn(true);
        Assert.assertTrue(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        // When APM enhancement is enabled but not activated by toggling BT in APM,
        // BT turns off when not connected to a media profile
        when(mHelper.isMediaProfileConnected()).thenReturn(false);
        Assert.assertFalse(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        // When APM enhancement is enabled but not activated by toggling BT in APM,
        // BT remains on when the default value for BT in APM is on
        when(mHelper.isBluetoothOnAPM()).thenReturn(true);
        Assert.assertTrue(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        // When APM enhancement is enabled but not activated by toggling BT in APM,
        // BT remains off when the default value for BT in APM is off
        when(mHelper.isBluetoothOnAPM()).thenReturn(false);
        Assert.assertFalse(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        // When APM enhancement is enabled and activated by toggling BT in APM,
        // BT remains on if user's last choice in APM was on
        when(mHelper.getSettingsSecureInt(APM_USER_TOGGLED_BLUETOOTH, UNUSED)).thenReturn(USED);
        when(mHelper.isBluetoothOnAPM()).thenReturn(true);
        Assert.assertTrue(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        // When APM enhancement is enabled and activated by toggling BT in APM,
        // BT turns off if user's last choice in APM was off
        when(mHelper.isBluetoothOnAPM()).thenReturn(false);
        Assert.assertFalse(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());

        // When APM enhancement is enabled and activated by toggling BT in APM,
        // BT turns off if user's last choice in APM was off even when connected to a media profile
        when(mHelper.isMediaProfileConnected()).thenReturn(true);
        Assert.assertFalse(mBluetoothAirplaneModeListener.shouldSkipAirplaneModeChange());
    }

    @Test
    public void testHandleAirplaneModeChange_InvokeAirplaneModeChanged() {
        mBluetoothAirplaneModeListener.handleAirplaneModeChange();
@@ -109,6 +177,81 @@ public class BluetoothAirplaneModeListenerTest {
        verify(mHelper, times(0)).onAirplaneModeChanged(mBluetoothManagerService);
    }

    private void setUpApmNotificationTests() throws Exception {
        when(mHelper.isBluetoothOn()).thenReturn(true);
        when(mHelper.isAirplaneModeOn()).thenReturn(true);
        when(mHelper.isBluetoothOnAPM()).thenReturn(true);
        when(mHelper.getSettingsInt(APM_ENHANCEMENT)).thenReturn(1);
        when(mHelper.getSettingsSecureInt(APM_USER_TOGGLED_BLUETOOTH, UNUSED)).thenReturn(USED);
        when(mHelper.getBluetoothPackageName()).thenReturn(PACKAGE_NAME);
        when(mContext.getPackageManager()).thenReturn(mPackageManager);
        when(mPackageManager.getResourcesForApplication(PACKAGE_NAME)).thenReturn(mResources);
    }

    @Test
    public void testHandleAirplaneModeChange_ShowBtAndWifiApmNotification() throws Exception {
        setUpApmNotificationTests();
        when(mHelper.getSettingsInt(Settings.Global.WIFI_ON)).thenReturn(1);
        when(mHelper.getSettingsSecureInt(WIFI_APM_STATE, 0)).thenReturn(1);
        when(mHelper.getSettingsSecureInt(APM_WIFI_BT_NOTIFICATION, NOTIFICATION_NOT_SHOWN))
                .thenReturn(NOTIFICATION_NOT_SHOWN);

        mBluetoothAirplaneModeListener.handleAirplaneModeChange();

        verify(mHelper).setSettingsInt(Settings.Global.BLUETOOTH_ON,
                BluetoothManagerService.BLUETOOTH_ON_AIRPLANE);
        verify(mBluetoothNotificationManager).sendApmNotification(any(), any());
        verify(mHelper).setSettingsSecureInt(APM_WIFI_BT_NOTIFICATION, NOTIFICATION_SHOWN);
    }

    @Test
    public void testHandleAirplaneModeChange_NotShowBtAndWifiApmNotification() throws Exception {
        setUpApmNotificationTests();
        when(mHelper.getSettingsInt(Settings.Global.WIFI_ON)).thenReturn(1);
        when(mHelper.getSettingsSecureInt(WIFI_APM_STATE, 0)).thenReturn(1);
        when(mHelper.getSettingsSecureInt(APM_WIFI_BT_NOTIFICATION, NOTIFICATION_NOT_SHOWN))
                .thenReturn(NOTIFICATION_SHOWN);

        mBluetoothAirplaneModeListener.handleAirplaneModeChange();

        verify(mHelper).setSettingsInt(Settings.Global.BLUETOOTH_ON,
                BluetoothManagerService.BLUETOOTH_ON_AIRPLANE);
        verify(mBluetoothNotificationManager, never()).sendApmNotification(any(), any());
        verify(mHelper, never()).setSettingsSecureInt(APM_WIFI_BT_NOTIFICATION, NOTIFICATION_SHOWN);
    }

    @Test
    public void testHandleAirplaneModeChange_ShowBtApmNotification() throws Exception {
        setUpApmNotificationTests();
        when(mHelper.getSettingsInt(Settings.Global.WIFI_ON)).thenReturn(1);
        when(mHelper.getSettingsSecureInt(WIFI_APM_STATE, 0)).thenReturn(0);
        when(mHelper.getSettingsSecureInt(APM_BT_NOTIFICATION, NOTIFICATION_NOT_SHOWN))
                .thenReturn(NOTIFICATION_NOT_SHOWN);

        mBluetoothAirplaneModeListener.handleAirplaneModeChange();

        verify(mHelper).setSettingsInt(Settings.Global.BLUETOOTH_ON,
                BluetoothManagerService.BLUETOOTH_ON_AIRPLANE);
        verify(mBluetoothNotificationManager).sendApmNotification(any(), any());
        verify(mHelper).setSettingsSecureInt(APM_BT_NOTIFICATION, NOTIFICATION_SHOWN);
    }

    @Test
    public void testHandleAirplaneModeChange_NotShowBtApmNotification() throws Exception {
        setUpApmNotificationTests();
        when(mHelper.getSettingsInt(Settings.Global.WIFI_ON)).thenReturn(1);
        when(mHelper.getSettingsSecureInt(WIFI_APM_STATE, 0)).thenReturn(0);
        when(mHelper.getSettingsSecureInt(APM_BT_NOTIFICATION, NOTIFICATION_NOT_SHOWN))
                .thenReturn(NOTIFICATION_SHOWN);

        mBluetoothAirplaneModeListener.handleAirplaneModeChange();

        verify(mHelper).setSettingsInt(Settings.Global.BLUETOOTH_ON,
                BluetoothManagerService.BLUETOOTH_ON_AIRPLANE);
        verify(mBluetoothNotificationManager, never()).sendApmNotification(any(), any());
        verify(mHelper, never()).setSettingsSecureInt(APM_BT_NOTIFICATION, NOTIFICATION_SHOWN);
    }

    @Test
    public void testIsPopToast_PopToast() {
        mBluetoothAirplaneModeListener.mToastCount = 0;