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

Commit 4cc14d65 authored by Aleksander Morgado's avatar Aleksander Morgado
Browse files

Added unit tests for SimStatusPreferenceController visibility

The new test checks whether the item is available when
config_show_sim_info=false, when telephony is not data capable and
when the user is not admin,

The default visibility status for all tests is defined in the test
setup().

Bug: 395714454
Flag: EXEMPT test only
Test: atest SimStatusPreferenceControllerTest
Change-Id: I8791d9fd4d7ab8648946f044d7aa6a0e77283a00
parent 64fb695d
Loading
Loading
Loading
Loading
+49 −5
Original line number Diff line number Diff line
@@ -44,9 +44,6 @@ import com.android.settings.R;
import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.search.SearchIndexableRaw;

import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
@@ -57,6 +54,9 @@ import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;

import java.util.ArrayList;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
public class SimStatusPreferenceControllerTest {

@@ -93,7 +93,6 @@ public class SimStatusPreferenceControllerTest {

        mResources = spy(mContext.getResources());
        when(mContext.getResources()).thenReturn(mResources);
        when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);

        mockService(Context.TELEPHONY_SERVICE, TelephonyManager.class, mTelephonyManager);
        mockService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, SubscriptionManager.class,
@@ -114,7 +113,12 @@ public class SimStatusPreferenceControllerTest {
                return 0;
            }
        });
        doReturn(BasePreferenceController.AVAILABLE).when(mController).getAvailabilityStatus();

        // Availability defaults
        when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
        when(mTelephonyManager.isDataCapable()).thenReturn(true);
        when(mUserManager.isAdminUser()).thenReturn(true);

        when(mScreen.getContext()).thenReturn(mContext);
        final String categoryKey = "device_detail_category";
        when(mScreen.findPreference(categoryKey)).thenReturn(mCategory);
@@ -210,6 +214,46 @@ public class SimStatusPreferenceControllerTest {
        assertThat(rawData.size()).isEqualTo(1);
    }

    @Test
    public void getAvailabilityStatus_showSimInfo_telephonyDataCapable_userAdmindisplayed() {
        SlotSimStatus slotSimStatus = new TestSlotSimStatus(mContext);
        mController.init(mFragment, slotSimStatus);

        // Use defaults
        assertThat(mController.getAvailabilityStatus()).isEqualTo(
                BasePreferenceController.AVAILABLE);
    }

    @Test
    public void getAvailabilityStatus_notShowSimInfo_telephonyDataCapable_userAdmin_notDisplayed() {
        SlotSimStatus slotSimStatus = new TestSlotSimStatus(mContext);
        mController.init(mFragment, slotSimStatus);

        when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(false);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(
                BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void getAvailabilityStatus_showSimInfo_notTelephonyDataCapable_userAdmin_notDisplayed() {
        SlotSimStatus slotSimStatus = new TestSlotSimStatus(mContext);
        mController.init(mFragment, slotSimStatus);

        when(mTelephonyManager.isDataCapable()).thenReturn(false);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(
                BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
    }

    @Test
    public void getAvailabilityStatus_showSimInfo_telephonyDataCapable_notUserAdmin_notDisplayed() {
        SlotSimStatus slotSimStatus = new TestSlotSimStatus(mContext);
        mController.init(mFragment, slotSimStatus);

        when(mUserManager.isAdminUser()).thenReturn(false);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(
                BasePreferenceController.CONDITIONALLY_UNAVAILABLE);
    }

    private <T> void mockService(String serviceName, Class<T> serviceClass, T service) {
        when(mContext.getSystemServiceName(serviceClass)).thenReturn(serviceName);
        when(mContext.getSystemService(serviceName)).thenReturn(service);