Merge "[Testcase] To migration DeleteSimProfilePreferenceControllerTest"

This commit is contained in:
SongFerng Wang
2020-11-19 04:53:54 +00:00
committed by Android (Google) Code Review

View File

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2019 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.
@@ -18,40 +18,41 @@ package com.android.settings.network.telephony;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.content.Intent;
import android.os.Looper;
import android.provider.Settings;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.euicc.EuiccManager;
import androidx.fragment.app.Fragment;
import androidx.preference.Preference;
import androidx.preference.PreferenceManager;
import androidx.preference.PreferenceScreen;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.android.settings.network.SubscriptionUtil;
import com.android.settings.security.ConfirmSimDeletionPreferenceController;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.util.ArrayList;
import java.util.Arrays;
@RunWith(RobolectricTestRunner.class)
@RunWith(AndroidJUnit4.class)
public class DeleteSimProfilePreferenceControllerTest {
private static final String PREF_KEY = "delete_profile_key";
private static final int REQUEST_CODE = 4321;
@@ -62,25 +63,29 @@ public class DeleteSimProfilePreferenceControllerTest {
private Fragment mFragment;
@Mock
private SubscriptionInfo mSubscriptionInfo;
@Mock
private PreferenceScreen mScreen;
private Context mContext;
private PreferenceScreen mScreen;
private Preference mPreference;
private DeleteSimProfilePreferenceController mController;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = spy(RuntimeEnvironment.application);
mContext = spy(ApplicationProvider.getApplicationContext());
SubscriptionUtil.setAvailableSubscriptionsForTesting(Arrays.asList(mSubscriptionInfo));
when(mSubscriptionInfo.getSubscriptionId()).thenReturn(SUB_ID);
when(mSubscriptionInfo.isEmbedded()).thenReturn(true);
if (Looper.myLooper() == null) {
Looper.prepare();
}
PreferenceManager preferenceManager = new PreferenceManager(mContext);
mScreen = preferenceManager.createPreferenceScreen(mContext);
mPreference = new Preference(mContext);
mPreference.setKey(PREF_KEY);
when(mScreen.findPreference(PREF_KEY)).thenReturn(mPreference);
mScreen.addPreference(mPreference);
mController = new DeleteSimProfilePreferenceController(mContext, PREF_KEY);
}
@@ -118,22 +123,17 @@ public class DeleteSimProfilePreferenceControllerTest {
}
@Test
@Ignore
public void onPreferenceClick_startsIntent() {
mController.init(SUB_ID, mFragment, REQUEST_CODE);
mController.displayPreference(mScreen);
// turn off confirmation before click
Settings.Global.putInt(mContext.getContentResolver(),
ConfirmSimDeletionPreferenceController.KEY_CONFIRM_SIM_DELETION, 0);
final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
doNothing().when(mContext).startActivity(intentCaptor.capture());
mController.handlePreferenceTreeClick(mPreference);
final ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class);
verify(mFragment).startActivityForResult(intentCaptor.capture(), eq(REQUEST_CODE));
final Intent intent = intentCaptor.getValue();
assertThat(intent.getAction()).isEqualTo(
EuiccManager.ACTION_DELETE_SUBSCRIPTION_PRIVILEGED);
assertThat(intent.getIntExtra(EuiccManager.EXTRA_SUBSCRIPTION_ID,
SubscriptionManager.INVALID_SUBSCRIPTION_ID)).isEqualTo(SUB_ID);
verify(mContext, times(1)).startActivity(any());
}
}