From 4fdf8be369b5c591e5719b433c4f75e3aff10112 Mon Sep 17 00:00:00 2001 From: SongFerngWang Date: Mon, 22 Nov 2021 07:50:43 +0800 Subject: [PATCH] [MEP] eSIM deprecated API Refactoring The telephony APIs were changed, the Settings needs the refactoring. Bug: 206801604 Test: build pass. atest QuerySimSlotIndexTest (PASS) atest SelectableSubscriptionsTest (PASS) Change-Id: Ic3244333b7d3750b88ecb3dbc5d7036da50b9d4d Merged-In: Ic3244333b7d3750b88ecb3dbc5d7036da50b9d4d --- .../network/helper/QuerySimSlotIndex.java | 36 +- .../network/helper/QuerySimSlotIndexTest.java | 322 ++++++++++++++++++ 2 files changed, 342 insertions(+), 16 deletions(-) create mode 100644 tests/unit/src/com/android/settings/network/helper/QuerySimSlotIndexTest.java diff --git a/src/com/android/settings/network/helper/QuerySimSlotIndex.java b/src/com/android/settings/network/helper/QuerySimSlotIndex.java index b70a148d2e4..ee53f94966f 100644 --- a/src/com/android/settings/network/helper/QuerySimSlotIndex.java +++ b/src/com/android/settings/network/helper/QuerySimSlotIndex.java @@ -17,14 +17,16 @@ package com.android.settings.network.helper; import android.telephony.SubscriptionManager; import android.telephony.TelephonyManager; +import android.telephony.UiccPortInfo; import android.telephony.UiccSlotInfo; import java.util.Arrays; import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicIntegerArray; +import java.util.stream.IntStream; /** - * This is a Callable class which query slot index within device + * This is a Callable class which query logical slot index within device */ public class QuerySimSlotIndex implements Callable { private static final String TAG = "QuerySimSlotIndex"; @@ -58,30 +60,32 @@ public class QuerySimSlotIndex implements Callable { return new AtomicIntegerArray(0); } int slotIndexFilter = mOnlySlotWithSim ? 0 : SubscriptionManager.INVALID_SIM_SLOT_INDEX; + return new AtomicIntegerArray(Arrays.stream(slotInfo) - .filter(slot -> filterSlot(slot)) - .mapToInt(slot -> mapToSlotIndex(slot)) + .flatMapToInt(slot -> mapToLogicalSlotIndex(slot)) .filter(slotIndex -> (slotIndex >= slotIndexFilter)) .toArray()); } - protected boolean filterSlot(UiccSlotInfo slotInfo) { + protected IntStream mapToLogicalSlotIndex(UiccSlotInfo slotInfo) { + if (slotInfo == null) { + return IntStream.of(SubscriptionManager.INVALID_SIM_SLOT_INDEX); + } + if (slotInfo.getCardStateInfo() == UiccSlotInfo.CARD_STATE_INFO_ABSENT) { + return IntStream.of(SubscriptionManager.INVALID_SIM_SLOT_INDEX); + } + return slotInfo.getPorts().stream() + .filter(port -> filterPort(port)) + .mapToInt(port -> port.getLogicalSlotIndex()); + } + + protected boolean filterPort(UiccPortInfo uiccPortInfo) { if (mDisabledSlotsIncluded) { return true; } - if (slotInfo == null) { + if (uiccPortInfo == null) { return false; } - return slotInfo.getIsActive(); - } - - protected int mapToSlotIndex(UiccSlotInfo slotInfo) { - if (slotInfo == null) { - return SubscriptionManager.INVALID_SIM_SLOT_INDEX; - } - if (slotInfo.getCardStateInfo() == UiccSlotInfo.CARD_STATE_INFO_ABSENT) { - return SubscriptionManager.INVALID_SIM_SLOT_INDEX; - } - return slotInfo.getLogicalSlotIdx(); + return uiccPortInfo.isActive(); } } \ No newline at end of file diff --git a/tests/unit/src/com/android/settings/network/helper/QuerySimSlotIndexTest.java b/tests/unit/src/com/android/settings/network/helper/QuerySimSlotIndexTest.java new file mode 100644 index 00000000000..36532a12bdc --- /dev/null +++ b/tests/unit/src/com/android/settings/network/helper/QuerySimSlotIndexTest.java @@ -0,0 +1,322 @@ +/* + * Copyright (C) 2021 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.settings.network.helper; + +import static android.telephony.UiccSlotInfo.CARD_STATE_INFO_PRESENT; + +import static com.google.common.truth.Truth.assertThat; + +import static org.mockito.Mockito.when; + +import android.telephony.TelephonyManager; +import android.telephony.UiccPortInfo; +import android.telephony.UiccSlotInfo; +import android.util.Log; + +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import com.android.settingslib.utils.ThreadUtils; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.Future; +import java.util.concurrent.atomic.AtomicIntegerArray; + +@RunWith(AndroidJUnit4.class) +public class QuerySimSlotIndexTest { + private static final String TAG = "QSSI_Test"; + + @Mock + private TelephonyManager mTelephonyManager; + + Future mActiveSimSlotIndex; + Future mAllSimSlotIndex; + + @Before + public void setUp() { + // query in background thread + mAllSimSlotIndex = ThreadUtils.postOnBackgroundThread( + new QuerySimSlotIndex(mTelephonyManager, true, true)); + // query in background thread + mActiveSimSlotIndex = ThreadUtils.postOnBackgroundThread( + new QuerySimSlotIndex(mTelephonyManager, false, true)); + } + + @Test + public void allSimSlotIndexCall_nullInput_getNoneNullEmptyList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(null); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(0); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void allSimSlotIndexCall_oneSimAndActivePsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(oneSim_ActivePsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(1); + assertThat(result.get(0)).isEqualTo(0); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void allSimSlotIndexCall_oneSimAndActiveEsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(oneSim_ActiveEsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(1); + assertThat(result.get(0)).isEqualTo(1); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void allSimSlotIndexCall_twoSimsAndActivePsimActiveEsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(twoSims_ActivePsimActiveEsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(2); + assertThat(result.get(0)).isEqualTo(0); + assertThat(result.get(1)).isEqualTo(1); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void allSimSlotIndexCall_twoSimsAndtwoActiveEsims_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(twoSims_twoActiveEsims()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(2); + assertThat(result.get(0)).isEqualTo(0); + assertThat(result.get(1)).isEqualTo(1); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void allSimSlotIndexCall_twoSimsAndActivePsimInactiveEsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(twoSims_ActivePsimInactiveEsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(2); + assertThat(result.get(0)).isEqualTo(0); + assertThat(result.get(1)).isEqualTo(1); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void allSimSlotIndexCall_twoSimsAndActiveEsimInactivePsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(twoSims_ActiveEsimInactivePsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(2); + assertThat(result.get(0)).isEqualTo(0); + assertThat(result.get(1)).isEqualTo(1); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void activeSimSlotIndexCall_nullInput_getNoneNullEmptyList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(null); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(0); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void activeSimSlotIndexCall_oneSimAndActivePsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(oneSim_ActivePsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(1); + assertThat(result.get(0)).isEqualTo(0); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void activeSimSlotIndexCall_oneSimAndActiveEsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(oneSim_ActiveEsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(1); + assertThat(result.get(0)).isEqualTo(1); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void activeSimSlotIndexCall_twoSimsAndActivePsimActiveEsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(twoSims_ActivePsimActiveEsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(2); + assertThat(result.get(0)).isEqualTo(0); + assertThat(result.get(1)).isEqualTo(1); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void activeSimSlotIndexCall_twoSimsAndtwoActiveEsims_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(twoSims_twoActiveEsims()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(2); + assertThat(result.get(0)).isEqualTo(0); + assertThat(result.get(1)).isEqualTo(1); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void activeSimSlotIndexCall_twoSimsAndActivePsimInactiveEsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(twoSims_ActivePsimInactiveEsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(1); + assertThat(result.get(0)).isEqualTo(0); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + @Test + public void activeSimSlotIndexCall_twoSimsAndActiveEsimInactivePsim_getList() { + try { + when(mTelephonyManager.getUiccSlotsInfo()).thenReturn(twoSims_ActiveEsimInactivePsim()); + List result = SelectableSubscriptions.atomicToList(mActiveSimSlotIndex.get()); + + assertThat(result.size()).isEqualTo(1); + assertThat(result.get(0)).isEqualTo(1); + } catch (Exception exception) { + Log.w(TAG, "Fail to request subIdList", exception); + } + } + + private UiccSlotInfo[] oneSim_ActivePsim() { + return new UiccSlotInfo[]{createUiccSlotInfo(false, 0, true)}; + } + + private UiccSlotInfo[] oneSim_ActiveEsim() { + return new UiccSlotInfo[]{createUiccSlotInfo(true, 1, true)}; + } + + private UiccSlotInfo[] twoSims_ActivePsimActiveEsim() { + return new UiccSlotInfo[]{ + createUiccSlotInfo(false, 0, true), + createUiccSlotInfo(true, 1, true)}; + } + + private UiccSlotInfo[] twoSims_twoActiveEsims() { + return new UiccSlotInfo[]{ + createUiccSlotInfoForTwoEsim(true, true)}; + } + + private UiccSlotInfo[] twoSims_ActivePsimInactiveEsim() { + return new UiccSlotInfo[]{ + createUiccSlotInfo(false, 0, true), + createUiccSlotInfo(true, 1, false)}; + } + + private UiccSlotInfo[] twoSims_ActiveEsimInactivePsim() { + return new UiccSlotInfo[]{ + createUiccSlotInfo(false, 0, false), + createUiccSlotInfo(true, 1, true)}; + } + + //ToDo: add more cases. + /* + private List threeSims_ActivePsimTwoinactiveEsim(){ + } + private List threeSims_twoActiveEsimsInactivePsim(){ + } + private List threeSims_ActiveEsimInactivePsimInactiveEsim(){ + } + private List threeSims_ActivePsimActiveEsimInactiveEsim(){ + } + */ + + private UiccSlotInfo createUiccSlotInfo(boolean isEuicc, int logicalSlotIdx, + boolean isActive) { + return new UiccSlotInfo( + isEuicc, /* isEuicc */ + "123", /* cardId */ + CARD_STATE_INFO_PRESENT, /* cardStateInfo */ + true, /* isExtendApduSupported */ + true, /* isRemovable */ + Collections.singletonList( + new UiccPortInfo("" /* iccId */, 0 /* portIdx */, + logicalSlotIdx /* logicalSlotIdx */, isActive /* isActive */)) + ); + } + + private UiccSlotInfo createUiccSlotInfoForTwoEsim(boolean isActiveEsim1, + boolean isActiveEsim2) { + return new UiccSlotInfo( + true, /* isEuicc */ + "123", /* cardId */ + CARD_STATE_INFO_PRESENT, /* cardStateInfo */ + true, /* isExtendApduSupported */ + true, /* isRemovable */ + Arrays.asList( + new UiccPortInfo("" /* iccId */, 0 /* portIdx */, + 0 /* logicalSlotIdx */, isActiveEsim1 /* isActive */), + new UiccPortInfo("" /* iccId */, 1 /* portIdx */, + 1 /* logicalSlotIdx */, isActiveEsim2 /* isActive */)) + ); + } +}