Update mocking to correct target methods

The correct target context should use the mActivity in
MobileNetworkSettingsTest, so that the mocking for
telephony service could correctly applied.

Both AutoTimeZonePreferenceControllerTest and
BasebandVersionPreferenceControllerTest refer to the lib
implemented shadow Connectivitymanager but that does not
the correct reference after utils class being updated.
Update the test logic inside to refer to correct method.
The reference to ShadowConnectivityManager does not needed
anymore so remove it from the test.

Fix: 183068151
Fix: 183067742
Fix: 183068139
Test: make RunSettingsRoboTests ROBOTEST_FILTER=\
com.android.settings.network.telephony.MobileNetworkSettingsTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=\
com.android.settings.datetime.AutoTimeZonePreferenceControllerTest
Test: make RunSettingsRoboTests ROBOTEST_FILTER=com.android.settings\
.deviceinfo.firmwareversion.BasebandVersionPreferenceControllerTest

Change-Id: I15ecc6aab7d530d20cd23b06267cc184a2c62b40
This commit is contained in:
Chiachang Wang
2021-03-18 14:13:17 +08:00
parent 959325e88a
commit 6290ad9509
3 changed files with 25 additions and 32 deletions

View File

@@ -18,17 +18,16 @@ package com.android.settings.datetime;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.robolectric.shadow.api.Shadow.extract; import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager;
import android.provider.Settings; import android.provider.Settings;
import android.telephony.TelephonyManager;
import androidx.preference.Preference; import androidx.preference.Preference;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -36,27 +35,28 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowConnectivityManager.class)
public class AutoTimeZonePreferenceControllerTest { public class AutoTimeZonePreferenceControllerTest {
@Mock @Mock
private UpdateTimeAndDateCallback mCallback; private UpdateTimeAndDateCallback mCallback;
@Mock
private Context mContext; private Context mContext;
private AutoTimeZonePreferenceController mController; private AutoTimeZonePreferenceController mController;
private Preference mPreference; private Preference mPreference;
private ShadowConnectivityManager connectivityManager; @Mock
private TelephonyManager mTelephonyManager;
@Before @Before
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application; mContext = spy(RuntimeEnvironment.application);
mPreference = new Preference(mContext); mPreference = new Preference(mContext);
connectivityManager = extract(mContext.getSystemService(ConnectivityManager.class));
connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, true); when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mTelephonyManager.isDataCapable()).thenReturn(true);
} }
@Test @Test
@@ -77,8 +77,7 @@ public class AutoTimeZonePreferenceControllerTest {
@Test @Test
public void isWifiOnly_notAvailable() { public void isWifiOnly_notAvailable() {
connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, false); when(mTelephonyManager.isDataCapable()).thenReturn(false);
mController = new AutoTimeZonePreferenceController( mController = new AutoTimeZonePreferenceController(
mContext, null /* callback */, false /* fromSUW */); mContext, null /* callback */, false /* fromSUW */);
@@ -95,8 +94,7 @@ public class AutoTimeZonePreferenceControllerTest {
@Test @Test
public void isWifiOnly_notEnable() { public void isWifiOnly_notEnable() {
connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, false); when(mTelephonyManager.isDataCapable()).thenReturn(false);
mController = new AutoTimeZonePreferenceController( mController = new AutoTimeZonePreferenceController(
mContext, null /* callback */, false /* fromSUW */); mContext, null /* callback */, false /* fromSUW */);

View File

@@ -21,44 +21,42 @@ import static com.android.settings.core.BasePreferenceController.UNSUPPORTED_ON_
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.robolectric.shadow.api.Shadow.extract; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.net.ConnectivityManager;
import android.sysprop.TelephonyProperties; import android.sysprop.TelephonyProperties;
import android.telephony.TelephonyManager;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner; import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment; import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import java.util.Arrays; import java.util.Arrays;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowConnectivityManager.class)
public class BasebandVersionPreferenceControllerTest { public class BasebandVersionPreferenceControllerTest {
@Mock
private Context mContext; private Context mContext;
private BasebandVersionPreferenceController mController; private BasebandVersionPreferenceController mController;
@Mock
private TelephonyManager mTelephonyManager;
@Before @Before
public void setup() { public void setup() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application; mContext = spy(RuntimeEnvironment.application);
mController = new BasebandVersionPreferenceController(mContext, "key"); mController = new BasebandVersionPreferenceController(mContext, "key");
when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
} }
@Test @Test
public void getAvailability_wifiOnly_unavailable() { public void getAvailability_wifiOnly_unavailable() {
final ShadowConnectivityManager connectivityManager = when(mTelephonyManager.isDataCapable()).thenReturn(false);
extract(mContext.getSystemService(ConnectivityManager.class));
connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, false);
assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE); assertThat(mController.getAvailabilityStatus()).isEqualTo(UNSUPPORTED_ON_DEVICE);
} }
@@ -66,10 +64,7 @@ public class BasebandVersionPreferenceControllerTest {
public void getAvailability_hasMobile_available() { public void getAvailability_hasMobile_available() {
final String text = "test"; final String text = "test";
TelephonyProperties.baseband_version(Arrays.asList(new String[]{text})); TelephonyProperties.baseband_version(Arrays.asList(new String[]{text}));
ShadowConnectivityManager connectivityManager = when(mTelephonyManager.isDataCapable()).thenReturn(true);
extract(mContext.getSystemService(ConnectivityManager.class));
connectivityManager.setNetworkSupported(ConnectivityManager.TYPE_MOBILE, true);
assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE); assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
} }
} }

View File

@@ -74,7 +74,7 @@ public class MobileNetworkSettingsTest {
public void setUp() { public void setUp() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager); when(mActivity.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager); when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
when(mContext.getSystemService(NetworkStatsManager.class)).thenReturn(mNetworkStatsManager); when(mContext.getSystemService(NetworkStatsManager.class)).thenReturn(mNetworkStatsManager);
ShadowEntityHeaderController.setUseMock(mock(EntityHeaderController.class)); ShadowEntityHeaderController.setUseMock(mock(EntityHeaderController.class));