[Testcase] To migration CarrierPreferenceControllerTest

1. Use AndroidJunit4 instead of RobolectricTestRunner
2. Use ApplicationProvider instead of RuntimeEnvironment
   to get context
3. Change Copy Right
4. Remove the mock of PreferenceScreen

Bug: 173340087
Test: atest CarrierPreferenceControllerTest
Change-Id: Ia0dcc9db5e99626ce0f410d62d16089e0f75e123
This commit is contained in:
SongFerngWang
2020-11-16 13:57:20 +08:00
committed by SongFerng Wang
parent d58a9cdd05
commit f21a4fb50c

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2018 The Android Open Source Project
* Copyright (C) 2020 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.
@@ -23,10 +23,12 @@ import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.ComponentName;
import android.content.Context;
@@ -38,6 +40,9 @@ import android.telephony.CarrierConfigManager;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settingslib.RestrictedPreference;
import org.junit.Before;
@@ -47,10 +52,8 @@ import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class CarrierPreferenceControllerTest {
private static final int SUB_ID = 2;
private static final String CARRIER_SETTINGS_COMPONENT = "packageName/className";
@@ -72,13 +75,14 @@ public class CarrierPreferenceControllerTest {
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
doReturn(mSubscriptionManager).when(mContext).getSystemService(SubscriptionManager.class);
mContext = spy(ApplicationProvider.getApplicationContext());
when(mContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(mTelephonyManager);
when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
doReturn(mTelephonyManager).when(mTelephonyManager).createForSubscriptionId(SUB_ID);
doReturn(mInvalidTelephonyManager).when(mTelephonyManager).createForSubscriptionId(
SubscriptionManager.INVALID_SUBSCRIPTION_ID);
doReturn(mCarrierConfigManager).when(mContext).getSystemService(CarrierConfigManager.class);
when(mContext.getSystemService(CarrierConfigManager.class)).thenReturn(
mCarrierConfigManager);
mPreference = new RestrictedPreference(mContext);
mController = new CarrierPreferenceController(mContext, "mobile_data");
@@ -127,10 +131,11 @@ public class CarrierPreferenceControllerTest {
PackageManager pm = Mockito.mock(PackageManager.class);
doReturn(pm).when(mContext).getPackageManager();
doReturn(new ResolveInfo()).when(pm).resolveActivity(any(Intent.class), anyInt());
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
doNothing().when(mContext).startActivity(captor.capture());
mController.handlePreferenceTreeClick(mPreference);
final ArgumentCaptor<Intent> captor = ArgumentCaptor.forClass(Intent.class);
verify(mContext).startActivity(captor.capture());
final Intent intent = captor.getValue();
assertThat(intent.getComponent()).isEqualTo(