1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package com.android.internal.telephony.uicc; 18 19import android.content.Context; 20import android.content.Intent; 21import android.content.res.Resources; 22import android.os.Looper; 23import android.os.Message; 24import android.telephony.TelephonyManager; 25import android.test.suitebuilder.annotation.SmallTest; 26 27import com.android.internal.R; 28import com.android.internal.telephony.TelephonyIntents; 29import com.android.internal.telephony.TelephonyTest; 30import com.android.internal.telephony.uicc.IccCardStatus.CardState; 31 32import org.junit.After; 33import org.junit.Before; 34import org.junit.Test; 35import org.mockito.ArgumentCaptor; 36import org.mockito.Mock; 37import org.mockito.MockitoAnnotations; 38import static org.junit.Assert.assertEquals; 39import static org.mockito.Mockito.any; 40import static org.mockito.Mockito.anyInt; 41import static org.mockito.Mockito.anyObject; 42import static org.mockito.Mockito.eq; 43import static org.mockito.Mockito.never; 44import static org.mockito.Mockito.times; 45import static org.mockito.Mockito.verify; 46import static org.mockito.Mockito.when; 47 48public class UiccStateChangedLauncherTest extends TelephonyTest { 49 private static final String TAG = UiccStateChangedLauncherTest.class.getName(); 50 private static final int CARD_COUNT = 1; 51 private static final String PROVISIONING_PACKAGE_NAME = "test.provisioning.package"; 52 53 @Mock 54 private Context mContext; 55 @Mock 56 private Resources mResources; 57 58 private IccCardStatus makeCardStatus(CardState state) { 59 IccCardStatus status = new IccCardStatus(); 60 status.setCardState(state.ordinal()); 61 status.mApplications = new IccCardApplicationStatus[0]; 62 status.mCdmaSubscriptionAppIndex = -1; 63 status.mGsmUmtsSubscriptionAppIndex = -1; 64 status.mImsSubscriptionAppIndex = -1; 65 return status; 66 } 67 68 @Before 69 public void setUp() throws Exception { 70 super.setUp(TAG); 71 72 MockitoAnnotations.initMocks(this); 73 when(mContext.getResources()).thenReturn(mResources); 74 when(TelephonyManager.getDefault().getPhoneCount()).thenReturn(CARD_COUNT); 75 } 76 77 @After 78 public void tearDown() throws Exception { 79 super.tearDown(); 80 } 81 82 @Test @SmallTest 83 public void testProvisioningPackageSet() { 84 // deviceProvisioningPackage is set. 85 when(mResources.getString(eq(R.string.config_deviceProvisioningPackage))) 86 .thenReturn(PROVISIONING_PACKAGE_NAME); 87 88 if (Looper.myLooper() == null) { 89 Looper.prepare(); 90 } 91 92 UiccStateChangedLauncher uiccLauncher = 93 new UiccStateChangedLauncher(mContext, UiccController.getInstance()); 94 ArgumentCaptor<Integer> integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class); 95 verify(UiccController.getInstance(), times(1)).registerForIccChanged(eq(uiccLauncher), 96 integerArgumentCaptor.capture(), 97 anyObject()); 98 Message msg = Message.obtain(); 99 msg.what = integerArgumentCaptor.getValue(); 100 101 // The first broadcast should be sent after initialization. 102 UiccCard[] cards = new UiccCard[CARD_COUNT]; 103 cards[0] = new UiccCard(mContext, mSimulatedCommands, 104 makeCardStatus(CardState.CARDSTATE_PRESENT)); 105 when(UiccController.getInstance().getUiccCards()).thenReturn(cards); 106 uiccLauncher.handleMessage(msg); 107 108 ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); 109 110 // Amount of sent broadcasts to the device provisioning package. 111 int broadcast_count = 1; 112 verify(mContext, times(broadcast_count)).sendBroadcast(intentArgumentCaptor.capture()); 113 assertEquals(PROVISIONING_PACKAGE_NAME, intentArgumentCaptor.getValue().getPackage()); 114 assertEquals(TelephonyIntents.ACTION_SIM_STATE_CHANGED, 115 intentArgumentCaptor.getValue().getAction()); 116 117 // Card state's changed to restricted. Broadcast should be sent. 118 cards[0].update(mContext, mSimulatedCommands, 119 makeCardStatus(CardState.CARDSTATE_RESTRICTED)); 120 when(UiccController.getInstance().getUiccCards()).thenReturn(cards); 121 uiccLauncher.handleMessage(msg); 122 123 broadcast_count++; 124 verify(mContext, times(broadcast_count)).sendBroadcast(intentArgumentCaptor.capture()); 125 assertEquals(PROVISIONING_PACKAGE_NAME, intentArgumentCaptor.getValue().getPackage()); 126 assertEquals(TelephonyIntents.ACTION_SIM_STATE_CHANGED, 127 intentArgumentCaptor.getValue().getAction()); 128 129 // Nothing's changed. Broadcast should not be sent. 130 uiccLauncher.handleMessage(msg); 131 verify(mContext, times(broadcast_count)).sendBroadcast(any(Intent.class)); 132 133 // Card state's changed from restricted. Broadcast should be sent. 134 cards[0].update(mContext, mSimulatedCommands, 135 makeCardStatus(CardState.CARDSTATE_PRESENT)); 136 when(UiccController.getInstance().getUiccCards()).thenReturn(cards); 137 uiccLauncher.handleMessage(msg); 138 139 broadcast_count++; 140 verify(mContext, times(broadcast_count)).sendBroadcast(intentArgumentCaptor.capture()); 141 assertEquals(PROVISIONING_PACKAGE_NAME, intentArgumentCaptor.getValue().getPackage()); 142 assertEquals(TelephonyIntents.ACTION_SIM_STATE_CHANGED, 143 intentArgumentCaptor.getValue().getAction()); 144 } 145 146 @Test @SmallTest 147 public void testProvisioningPackageUnset() { 148 // deviceProvisionigPackage is not set. 149 when(mResources.getString(eq(R.string.config_deviceProvisioningPackage))) 150 .thenReturn(null); 151 152 if (Looper.myLooper() == null) { 153 Looper.prepare(); 154 } 155 156 UiccStateChangedLauncher uiccLauncher = 157 new UiccStateChangedLauncher(mContext, UiccController.getInstance()); 158 verify(UiccController.getInstance(), never()).registerForIccChanged(eq(uiccLauncher), 159 anyInt(), anyObject()); 160 } 161} 162