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

Commit 0ae19606 authored by Yiyi Shen's avatar Yiyi Shen
Browse files

Increase code coverage.

Test: atest
Bug: 296507968
Change-Id: I031cc4b4f762bb574623da9485bb1981d9c912ea
parent 43d6a231
Loading
Loading
Loading
Loading
+64 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.settings.connecteddevice.fastpair;

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

import android.app.settings.SettingsEnums;

import com.android.settings.R;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class FastPairDeviceDashboardFragmentTest {

    @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();
    private FastPairDeviceDashboardFragment mFragment;

    @Before
    public void setUp() {
        mFragment = new FastPairDeviceDashboardFragment();
    }

    @Test
    public void getPreferenceScreenResId_returnsCorrectXml() {
        assertThat(mFragment.getPreferenceScreenResId()).isEqualTo(R.xml.fast_pair_devices);
    }

    @Test
    public void getLogTag_returnsCorrectTag() {
        assertThat(mFragment.getLogTag()).isEqualTo("FastPairDeviceFrag");
    }

    @Test
    public void getMetricsCategory_returnsCorrectCategory() {
        assertThat(mFragment.getMetricsCategory()).isEqualTo(SettingsEnums.FAST_PAIR_DEVICES);
    }

    @Test
    public void getHelpResource_returnsCorrectResource() {
        assertThat(mFragment.getHelpResource())
                .isEqualTo(R.string.help_url_connected_devices_fast_pair_devices);
    }
}
+81 −7
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.robolectric.Shadows.shadowOf;

import android.bluetooth.BluetoothAdapter;
@@ -41,6 +42,7 @@ import androidx.lifecycle.LifecycleOwner;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;

import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.flags.Flags;
@@ -70,10 +72,13 @@ public class FastPairDeviceGroupControllerTest {
    @Rule
    public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule();

    private static final String KEY = "fast_pair_device_list";

    @Mock private DashboardFragment mDashboardFragment;
    @Mock private FastPairDeviceUpdater mFastPairDeviceUpdater;
    @Mock private PackageManager mPackageManager;
    @Mock private PreferenceManager mPreferenceManager;
    @Mock private PreferenceScreen mScreen;
    private ShadowBluetoothAdapter mShadowBluetoothAdapter;
    private Context mContext;
    private FastPairDeviceGroupController mFastPairDeviceGroupController;
@@ -96,12 +101,14 @@ public class FastPairDeviceGroupControllerTest {
        doReturn(mPreferenceManager).when(mPreferenceGroup).getPreferenceManager();
        mPreferenceGroup.setVisible(false);
        mFastPairDeviceGroupController.setPreferenceGroup(mPreferenceGroup);
        when(mScreen.findPreference(KEY)).thenReturn(mPreferenceGroup);
        when(mScreen.getContext()).thenReturn(mContext);
        mShadowBluetoothAdapter = Shadow.extract(BluetoothAdapter.getDefaultAdapter());
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void testRegister() {
    public void onStart_flagOn_registerCallback() {
        // register the callback in onStart()
        mFastPairDeviceGroupController.onStart(mLifecycleOwner);
        verify(mFastPairDeviceUpdater).registerCallback();
@@ -114,7 +121,7 @@ public class FastPairDeviceGroupControllerTest {

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void testUnregister() {
    public void onStop_flagOn_unregisterCallback() {
        // register broadcast first
        mContext.registerReceiver(
                mFastPairDeviceGroupController.mReceiver, null, Context.RECEIVER_EXPORTED);
@@ -127,16 +134,51 @@ public class FastPairDeviceGroupControllerTest {

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void testGetAvailabilityStatus_noFastPairFeature_returnUnSupported() {
    public void onStart_flagOff_registerCallback() {
        // register the callback in onStart()
        mFastPairDeviceGroupController.onStart(mLifecycleOwner);
        assertThat(mFastPairDeviceUpdater).isNull();
        verify(mContext)
                .registerReceiver(
                        mFastPairDeviceGroupController.mReceiver,
                        mFastPairDeviceGroupController.mIntentFilter,
                        Context.RECEIVER_EXPORTED);
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void onStop_flagOff_unregisterCallback() {
        // register broadcast first
        mContext.registerReceiver(
                mFastPairDeviceGroupController.mReceiver, null, Context.RECEIVER_EXPORTED);

        // unregister the callback in onStop()
        mFastPairDeviceGroupController.onStop(mLifecycleOwner);
        assertThat(mFastPairDeviceUpdater).isNull();
        verify(mContext).unregisterReceiver(mFastPairDeviceGroupController.mReceiver);
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void getAvailabilityStatus_noFastPairFeature_returnUnSupported() {
        doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

        assertThat(mFastPairDeviceGroupController.getAvailabilityStatus())
                .isEqualTo(UNSUPPORTED_ON_DEVICE);
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void getAvailabilityStatus_noBluetoothFastPairFeature_returnUnSupported() {
        doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

        assertThat(mFastPairDeviceGroupController.getAvailabilityStatus())
                .isEqualTo(UNSUPPORTED_ON_DEVICE);
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void testGetAvailabilityStatus_noBluetoothFeature_returnUnSupported() {
    public void getAvailabilityStatus_noBluetoothFeature_returnUnSupported() {
        doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

        assertThat(mFastPairDeviceGroupController.getAvailabilityStatus())
@@ -145,15 +187,14 @@ public class FastPairDeviceGroupControllerTest {

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void testGetAvailabilityStatus_withBluetoothFastPairFeature_returnSupported() {
    public void getAvailabilityStatus_withBluetoothFastPairFeature_returnSupported() {
        doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

        assertThat(mFastPairDeviceGroupController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void testUpdatePreferenceVisibility_bluetoothIsDisable_shouldHidePreference() {
    public void updatePreferenceVisibility_bluetoothIsDisable_shouldHidePreference() {
        mShadowBluetoothAdapter.setEnabled(true);
        final GearPreference preference1 = new GearPreference(mContext, null /* AttributeSet */);
        mFastPairDeviceGroupController.onDeviceAdded(preference1);
@@ -171,4 +212,37 @@ public class FastPairDeviceGroupControllerTest {
        shadowOf(Looper.getMainLooper()).idle();
        assertThat(mPreferenceGroup.isVisible()).isFalse();
    }

    @Test
    public void onDeviceAdd_bluetoothIsDisable_shouldHidePreference() {
        mShadowBluetoothAdapter.setEnabled(true);
        final GearPreference preference1 = new GearPreference(mContext, null /* AttributeSet */);
        mFastPairDeviceGroupController.onDeviceAdded(preference1);
        assertThat(mPreferenceGroup.isVisible()).isTrue();
    }

    @Test
    public void onDeviceRemoved_bluetoothIsDisable_shouldHidePreference() {
        mShadowBluetoothAdapter.setEnabled(true);
        final GearPreference preference1 = new GearPreference(mContext, null /* AttributeSet */);
        mPreferenceGroup.addPreference(preference1);
        mFastPairDeviceGroupController.onDeviceRemoved(preference1);
        assertThat(mPreferenceGroup.isVisible()).isFalse();
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void displayPreference_notAvailable_doNothing() {
        mFastPairDeviceGroupController.displayPreference(mScreen);
        assertThat(mPreferenceGroup.isVisible()).isFalse();
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void displayPreference_isAvailable_fetchFastPairDevices() {
        doReturn(true).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

        mFastPairDeviceGroupController.displayPreference(mScreen);
        verify(mFastPairDeviceUpdater).forceUpdate();
    }
}
+37 −6
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ public class FastPairDevicePreferenceControllerTest {

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void onStart_registerCallback() {
    public void onStart_flagOn_registerCallback() {
        // register the callback in onStart()
        mFastPairDevicePrefController.onStart(mLifecycleOwner);
        verify(mFastPairDeviceUpdater).registerCallback();
@@ -120,9 +120,22 @@ public class FastPairDevicePreferenceControllerTest {
                        Context.RECEIVER_EXPORTED_UNAUDITED);
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void onStart_flagOff_registerCallback() {
        // register the callback in onStart()
        mFastPairDevicePrefController.onStart(mLifecycleOwner);
        assertThat(mFastPairDeviceUpdater).isNull();
        verify(mContext)
                .registerReceiver(
                        mFastPairDevicePrefController.mReceiver,
                        mFastPairDevicePrefController.mIntentFilter,
                        Context.RECEIVER_EXPORTED_UNAUDITED);
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void onStop_unregisterCallback() {
    public void onStop_flagOn_unregisterCallback() {
        // register broadcast first
        mContext.registerReceiver(
                mFastPairDevicePrefController.mReceiver, null, Context.RECEIVER_EXPORTED_UNAUDITED);
@@ -133,6 +146,19 @@ public class FastPairDevicePreferenceControllerTest {
        verify(mContext).unregisterReceiver(mFastPairDevicePrefController.mReceiver);
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void onStop_flagOff_unregisterCallback() {
        // register broadcast first
        mContext.registerReceiver(
                mFastPairDevicePrefController.mReceiver, null, Context.RECEIVER_EXPORTED_UNAUDITED);

        // unregister the callback in onStop()
        mFastPairDevicePrefController.onStop(mLifecycleOwner);
        assertThat(mFastPairDeviceUpdater).isNull();
        verify(mContext).unregisterReceiver(mFastPairDevicePrefController.mReceiver);
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void getAvailabilityStatus_noBluetoothFeature_returnUnsupported() {
@@ -151,6 +177,15 @@ public class FastPairDevicePreferenceControllerTest {
                .isEqualTo(UNSUPPORTED_ON_DEVICE);
    }

    @Test
    @RequiresFlagsDisabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void getAvailabilityStatus_noBluetoothFastPairFeature_returnUnsupported() {
        doReturn(false).when(mPackageManager).hasSystemFeature(PackageManager.FEATURE_BLUETOOTH);

        assertThat(mFastPairDevicePrefController.getAvailabilityStatus())
                .isEqualTo(UNSUPPORTED_ON_DEVICE);
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void getAvailabilityStatus_bothBluetoothFastPairFeature_returnSupported() {
@@ -160,7 +195,6 @@ public class FastPairDevicePreferenceControllerTest {
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void onDeviceAdded_addThreeFastPairDevicePreference_displayThreeNoSeeAll() {
        mShadowBluetoothAdapter.setEnabled(true);
        final GearPreference preference1 = new GearPreference(mContext, null /* AttributeSet */);
@@ -177,7 +211,6 @@ public class FastPairDevicePreferenceControllerTest {
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void onDeviceAdded_addFourDevicePreference_onlyDisplayThreeWithSeeAll() {
        mShadowBluetoothAdapter.setEnabled(true);
        final GearPreference preference1 = new GearPreference(mContext, null /* AttributeSet */);
@@ -206,7 +239,6 @@ public class FastPairDevicePreferenceControllerTest {
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void onDeviceRemoved_removeFourthDevice_hideSeeAll() {
        mShadowBluetoothAdapter.setEnabled(true);
        final GearPreference preference1 = new GearPreference(mContext, null /* AttributeSet */);
@@ -238,7 +270,6 @@ public class FastPairDevicePreferenceControllerTest {
    }

    @Test
    @RequiresFlagsEnabled(Flags.FLAG_ENABLE_SUBSEQUENT_PAIR_SETTINGS_INTEGRATION)
    public void updatePreferenceVisibility_bluetoothIsDisable_shouldHidePreference() {
        mShadowBluetoothAdapter.setEnabled(true);
        final GearPreference preference1 = new GearPreference(mContext, null /* AttributeSet */);
+56 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.settings.connecteddevice.fastpair;

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

import android.content.Context;

import androidx.test.core.app.ApplicationProvider;

import com.android.settings.connecteddevice.DevicePreferenceCallback;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class FastPairFeatureProviderImplTest {
    @Rule public final MockitoRule mMockitoRule = MockitoJUnit.rule();

    @Mock private DevicePreferenceCallback mDevicePreferenceCallback;

    private Context mContext;
    private FastPairFeatureProviderImpl mFeatureProvider;

    @Before
    public void setUp() {
        mContext = ApplicationProvider.getApplicationContext();
        mFeatureProvider = new FastPairFeatureProviderImpl();
    }

    @Test
    public void getFastPairDeviceUpdater_returnsEmptyClass() {
        assertThat(mFeatureProvider.getFastPairDeviceUpdater(mContext, mDevicePreferenceCallback))
                .isInstanceOf(FastPairDeviceUpdater.class);
    }
}