Fix robotests failure in connectivity settings

Bug: 174212358
Test: make -j42 RunSettingsRoboTests
Change-Id: I50baeb18b00f16808dc958d77251a01ba991adf6
This commit is contained in:
Hugh Chen
2020-11-25 16:13:18 +08:00
parent 2a6ffedfee
commit e61a5e3bc7
9 changed files with 112 additions and 59 deletions

View File

@@ -34,6 +34,7 @@ import androidx.test.core.content.pm.ApplicationInfoBuilder;
import com.android.settings.R;
import com.android.settings.nfc.NfcPreferenceController;
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
import org.junit.Before;
import org.junit.Test;
@@ -43,7 +44,7 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.Shadows;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowNfcAdapter;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.shadows.ShadowPackageManager;
import org.robolectric.util.ReflectionHelpers;
@@ -70,7 +71,7 @@ public class AdvancedConnectedDeviceControllerTest {
mContentResolver = mContext.getContentResolver();
mNfcController = new NfcPreferenceController(mContext,
NfcPreferenceController.KEY_TOGGLE_NFC);
mShadowNfcAdapter = Shadows.shadowOf(NfcAdapter.getNfcAdapter(mContext));
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
}

View File

@@ -18,7 +18,6 @@ package com.android.settings.connecteddevice;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.nfc.NfcAdapter;
@@ -26,35 +25,38 @@ import android.provider.SearchIndexableResource;
import com.android.settings.nfc.AndroidBeamPreferenceController;
import com.android.settings.testutils.shadow.ShadowConnectivityManager;
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
import com.android.settings.testutils.shadow.ShadowUserManager;
import com.android.settingslib.drawer.CategoryKey;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = {ShadowUserManager.class,
ShadowConnectivityManager.class})
ShadowConnectivityManager.class, ShadowNfcAdapter.class})
public class AdvancedConnectedDeviceDashboardFragmentTest {
private AdvancedConnectedDeviceDashboardFragment mFragment;
@Mock
private NfcAdapter mNfcAdapter;
private Context mContext;
private ShadowNfcAdapter mShadowNfcAdapter;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mFragment = new AdvancedConnectedDeviceDashboardFragment();
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
}
@Test
@@ -79,13 +81,10 @@ public class AdvancedConnectedDeviceDashboardFragmentTest {
@Test
public void testSearchIndexProvider_correctNonIndexables() {
Context context = spy(RuntimeEnvironment.application);
when(context.getApplicationContext()).thenReturn(context);
when(NfcAdapter.getDefaultAdapter(context)).thenReturn(mNfcAdapter);
when(mNfcAdapter.isSecureNfcSupported()).thenReturn(true);
mShadowNfcAdapter.setSecureNfcSupported(true);
final List<String> niks =
AdvancedConnectedDeviceDashboardFragment.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(context);
.getNonIndexableKeys(mContext);
assertThat(niks).contains(AndroidBeamPreferenceController.KEY_ANDROID_BEAM_SETTINGS);
}

View File

@@ -29,6 +29,7 @@ import android.nfc.NfcManager;
import android.os.UserManager;
import com.android.settings.R;
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
import org.junit.Before;
import org.junit.Test;
@@ -37,9 +38,12 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowNfcAdapter.class)
public class NfcAndPaymentFragmentControllerTest {
private NfcAndPaymentFragmentController mController;
private Context mContext;
@@ -50,29 +54,28 @@ public class NfcAndPaymentFragmentControllerTest {
private UserManager mUserManager;
@Mock
private NfcManager mNfcManager;
@Mock
private NfcAdapter mNfcAdapter;
private ShadowNfcAdapter mShadowNfcAdapter;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
when(mContext.getApplicationContext()).thenReturn(mContext);
when(mContext.getPackageManager()).thenReturn(mPackageManager);
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
when(mContext.getSystemService(Context.NFC_SERVICE)).thenReturn(mNfcManager);
when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
mController = new NfcAndPaymentFragmentController(mContext, "fakeKey");
ReflectionHelpers.setField(mController, "mNfcAdapter", mNfcAdapter);
}
@Test
public void getAvailabilityStatus_hasNfc_shouldReturnAvailable() {
when(mPackageManager.hasSystemFeature(anyString())).thenReturn(true);
when(mUserManager.isAdminUser()).thenReturn(true);
when(mNfcAdapter.isEnabled()).thenReturn(true);
mShadowNfcAdapter.setEnabled(true);
assertThat(mController.getAvailabilityStatus())
.isEqualTo(NfcAndPaymentFragmentController.AVAILABLE);
@@ -87,14 +90,14 @@ public class NfcAndPaymentFragmentControllerTest {
@Test
public void getSummary_nfcOn_shouldProvideOnSummary() {
when(mNfcAdapter.isEnabled()).thenReturn(true);
mShadowNfcAdapter.setEnabled(true);
assertThat(mController.getSummary().toString()).contains(
mContext.getString(R.string.switch_on_text));
}
@Test
public void getSummary_nfcOff_shouldProvideOffSummary() {
when(mNfcAdapter.isEnabled()).thenReturn(false);
mShadowNfcAdapter.setEnabled(false);
assertThat(mController.getSummary().toString()).contains(
mContext.getString(R.string.switch_off_text));
}

View File

@@ -22,9 +22,12 @@ import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.pm.PackageManager;
import android.nfc.NfcAdapter;
import android.provider.SearchIndexableResource;
import com.android.settings.testutils.shadow.ShadowNfcAdapter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -32,16 +35,20 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.annotation.Config;
import org.robolectric.shadow.api.Shadow;
import java.util.List;
@RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowNfcAdapter.class)
public class NfcAndPaymentFragmentTest {
@Mock
private PackageManager mPackageManager;
private NfcAndPaymentFragment mFragment;
private Context mContext;
@Mock
private NfcAdapter mNfcAdapter;
private ShadowNfcAdapter mShadowNfcAdapter;
@Before
public void setUp() {
@@ -49,6 +56,9 @@ public class NfcAndPaymentFragmentTest {
mFragment = new NfcAndPaymentFragment();
mContext = spy(RuntimeEnvironment.application);
mShadowNfcAdapter = Shadow.extract(NfcAdapter.getDefaultAdapter(mContext));
when(mContext.getPackageManager()).thenReturn(mPackageManager);
}
@Test
@@ -64,8 +74,10 @@ public class NfcAndPaymentFragmentTest {
@Test
public void searchIndexProvider_shouldIndexValidItems() {
when(mContext.getApplicationContext()).thenReturn(mContext);
when(NfcAdapter.getDefaultAdapter(mContext)).thenReturn(mNfcAdapter);
when(mNfcAdapter.isSecureNfcSupported()).thenReturn(true);
when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_NFC)).thenReturn(true);
when(mPackageManager.hasSystemFeature(
PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)).thenReturn(true);
mShadowNfcAdapter.setSecureNfcSupported(true);
final List<String> niks = NfcAndPaymentFragment.SEARCH_INDEX_DATA_PROVIDER
.getNonIndexableKeys(mContext);