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

Commit b02d1270 authored by Aleksander Morgado's avatar Aleksander Morgado
Browse files

Use isMobileDataCapable/isVoiceCapable helpers in deviceinfo/baseband

Disabling telephony support may be done either with the
config_show_sim_info config option, or based on whether
TelephonyManager reports data capabilities. The isMobileDataCapable()
helper method ensures both ways are supported.

Additionally, the logic is also updated to explicitly check for voice
support, so that disabling this telephony element happens only when
both mobile data and voice are unsupported.

The unit tests are updated to consider both no-data and no-voice
cases, by mocking both Resources and TelephonyManager.

Bug: 395714454
Test: mm && atest tests/robotests/src/com/android/settings/deviceinfo/firmwareversion/
Flag: EXEMPT bugfix
Change-Id: I355e8be8609cf6c8a0ec72c2754692a128aeceec
parent e0f437c0
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -42,7 +42,8 @@ class BasebandVersionPreference :
    override fun getSummary(context: Context): CharSequence? =
        SystemProperties.get(BASEBAND_PROPERTY, context.getString(R.string.device_info_default))

    override fun isAvailable(context: Context) = !Utils.isWifiOnly(context)
    override fun isAvailable(context: Context) =
        Utils.isMobileDataCapable(context) || Utils.isVoiceCapable(context)

    override fun bind(preference: Preference, metadata: PreferenceMetadata) {
        super.bind(preference, metadata)
+2 −1
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ public class BasebandVersionPreferenceController extends BasePreferenceControlle

    @Override
    public int getAvailabilityStatus() {
        return !Utils.isWifiOnly(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
        return (Utils.isMobileDataCapable(mContext)
                || Utils.isVoiceCapable(mContext)) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    }

    @Override
+39 −6
Original line number Diff line number Diff line
@@ -25,9 +25,12 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;

import android.content.Context;
import android.content.res.Resources;
import android.sysprop.TelephonyProperties;
import android.telephony.TelephonyManager;

import com.android.settings.R;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -46,27 +49,57 @@ public class BasebandVersionPreferenceControllerTest {
    private BasebandVersionPreferenceController mController;
    @Mock
    private TelephonyManager mTelephonyManager;
    @Mock
    private Resources mResources;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        mContext = spy(RuntimeEnvironment.application);
        mController = new BasebandVersionPreferenceController(mContext, "key");
        when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
        when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
        when(mContext.getResources()).thenReturn(mResources);

        // By default, available
        when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
        when(mTelephonyManager.isDataCapable()).thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);

        mController = new BasebandVersionPreferenceController(mContext, "key");
    }

    @Test
    public void getAvailability_wifiOnly_unavailable() {
        when(mTelephonyManager.isDataCapable()).thenReturn(false);
    public void getAvailability_default_available() {
        final String text = "test";
        TelephonyProperties.baseband_version(Arrays.asList(new String[]{text}));
        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    public void getAvailability_notShowSimInfo_unavailable() {
        when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(false);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
    }

    @Test
    public void getAvailability_hasMobile_available() {
        final String text = "test";
        TelephonyProperties.baseband_version(Arrays.asList(new String[]{text}));
    public void getAvailability_voiceCapable_notDataCapable_available() {
        when(mTelephonyManager.isDataCapable()).thenReturn(false);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(true);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    public void getAvailability_notVoiceCapable_dataCapable_available() {
        when(mTelephonyManager.isDataCapable()).thenReturn(true);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(false);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    }

    @Test
    public void getAvailability_notVoiceCapable_notDataCapable_unavailable() {
        when(mTelephonyManager.isDataCapable()).thenReturn(false);
        when(mTelephonyManager.isDeviceVoiceCapable()).thenReturn(false);
        assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
    }
}
// LINT.ThenChange(BasebandVersionPreferenceTest.kt)
+56 −13
Original line number Diff line number Diff line
@@ -17,44 +17,87 @@
package com.android.settings.deviceinfo.firmwareversion

import android.content.Context
import android.content.ContextWrapper
import android.content.res.Resources
import android.sysprop.TelephonyProperties
import android.telephony.TelephonyManager
import androidx.test.core.app.ApplicationProvider
import com.android.settings.R
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.spy
import org.mockito.kotlin.stub
import org.robolectric.RobolectricTestRunner

// LINT.IfChange
@RunWith(RobolectricTestRunner::class)
class BasebandVersionPreferenceTest {
    private lateinit var telephonyManager: TelephonyManager
    private val mockTelephonyManager = mock<TelephonyManager>()
    private val mockResources = mock<Resources>()

    private val context: Context =
        object : ContextWrapper(ApplicationProvider.getApplicationContext()) {
            override fun getSystemService(name: String): Any? =
                when {
                    name == getSystemServiceName(TelephonyManager::class.java) -> telephonyManager
                    else -> super.getSystemService(name)
                }
        spy(ApplicationProvider.getApplicationContext()) {
            on { getSystemService(TelephonyManager::class.java) } doReturn mockTelephonyManager
            on { getSystemService(Context.TELEPHONY_SERVICE) } doReturn mockTelephonyManager
            on { resources } doReturn mockResources
        }

    private val basebandVersionPreference = BasebandVersionPreference()

    @Before
    fun setup() {
        // By default, available
        mockTelephonyManager.stub {
            on { isDataCapable } doReturn true
            on { isDeviceVoiceCapable } doReturn true
        }
        mockResources.stub {
            on { getBoolean(R.bool.config_show_sim_info) } doReturn true
        }
    }

    @Test
    fun isAvailable_default_available() {
        TelephonyProperties.baseband_version(listOf("test"))
        assertThat(basebandVersionPreference.isAvailable(context)).isTrue()
    }

    @Test
    fun isAvailable_wifiOnly_unavailable() {
        telephonyManager = mock { on { isDataCapable } doReturn false }
    fun isAvailable_noShowSimInfo_unavailable() {
        mockResources.stub {
            on { getBoolean(R.bool.config_show_sim_info) } doReturn false
        }
        assertThat(basebandVersionPreference.isAvailable(context)).isFalse()
    }

    @Test
    fun isAvailable_hasMobile_available() {
        TelephonyProperties.baseband_version(listOf("test"))
        telephonyManager = mock { on { isDataCapable } doReturn true }
    fun isAvailable_voiceCapable_notDataCapable_available() {
        mockTelephonyManager.stub {
            on { isDataCapable } doReturn false
            on { isDeviceVoiceCapable } doReturn true
        }
        assertThat(basebandVersionPreference.isAvailable(context)).isTrue()
    }

    @Test
    fun isAvailable_notVoiceCapable_dataCapable_available() {
        mockTelephonyManager.stub {
            on { isDataCapable } doReturn true
            on { isDeviceVoiceCapable } doReturn false
        }
        assertThat(basebandVersionPreference.isAvailable(context)).isTrue()
    }

    @Test
    fun isAvailable_notVoiceCapable_notDataCapable_unavailable() {
        mockTelephonyManager.stub {
            on { isDataCapable } doReturn false
            on { isDeviceVoiceCapable } doReturn false
        }
        assertThat(basebandVersionPreference.isAvailable(context)).isFalse()
    }
}
// LINT.ThenChange(BasebandVersionPreferenceControllerTest.java)