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 static org.junit.Assert.assertEquals; 20import static org.mockito.Mockito.any; 21import static org.mockito.Mockito.anyInt; 22import static org.mockito.Mockito.anyObject; 23import static org.mockito.Mockito.eq; 24import static org.mockito.Mockito.never; 25import static org.mockito.Mockito.times; 26import static org.mockito.Mockito.verify; 27import static org.mockito.Mockito.when; 28 29import android.content.Context; 30import android.content.Intent; 31import android.content.res.Resources; 32import android.os.Looper; 33import android.os.Message; 34import android.telephony.TelephonyManager; 35import android.test.suitebuilder.annotation.SmallTest; 36 37import com.android.internal.R; 38import com.android.internal.telephony.TelephonyIntents; 39import com.android.internal.telephony.TelephonyTest; 40import com.android.internal.telephony.uicc.IccCardStatus.CardState; 41 42import org.junit.After; 43import org.junit.Before; 44import org.junit.Test; 45import org.mockito.ArgumentCaptor; 46import org.mockito.Mock; 47import org.mockito.MockitoAnnotations; 48 49public class UiccStateChangedLauncherTest extends TelephonyTest { 50 private static final String TAG = UiccStateChangedLauncherTest.class.getName(); 51 private static final int CARD_COUNT = 1; 52 private static final String PROVISIONING_PACKAGE_NAME = "test.provisioning.package"; 53 54 @Mock 55 private Context mContext; 56 @Mock 57 private Resources mResources; 58 59 private IccCardStatus makeCardStatus(CardState state) { 60 IccCardStatus status = new IccCardStatus(); 61 status.setCardState(state.ordinal()); 62 status.mApplications = new IccCardApplicationStatus[0]; 63 status.mCdmaSubscriptionAppIndex = -1; 64 status.mGsmUmtsSubscriptionAppIndex = -1; 65 status.mImsSubscriptionAppIndex = -1; 66 return status; 67 } 68 69 @Before 70 public void setUp() throws Exception { 71 super.setUp(TAG); 72 73 MockitoAnnotations.initMocks(this); 74 when(mContext.getResources()).thenReturn(mResources); 75 when(TelephonyManager.getDefault().getPhoneCount()).thenReturn(CARD_COUNT); 76 } 77 78 @After 79 public void tearDown() throws Exception { 80 super.tearDown(); 81 } 82 83 @Test @SmallTest 84 public void testProvisioningPackageSet() { 85 // deviceProvisioningPackage is set. 86 when(mResources.getString(eq(R.string.config_deviceProvisioningPackage))) 87 .thenReturn(PROVISIONING_PACKAGE_NAME); 88 89 if (Looper.myLooper() == null) { 90 Looper.prepare(); 91 } 92 93 UiccStateChangedLauncher uiccLauncher = 94 new UiccStateChangedLauncher(mContext, UiccController.getInstance()); 95 ArgumentCaptor<Integer> integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class); 96 verify(UiccController.getInstance(), times(1)).registerForIccChanged(eq(uiccLauncher), 97 integerArgumentCaptor.capture(), 98 anyObject()); 99 Message msg = Message.obtain(); 100 msg.what = integerArgumentCaptor.getValue(); 101 102 // The first broadcast should be sent after initialization. 103 UiccCard card = new UiccCard(mContext, mSimulatedCommands, 104 makeCardStatus(CardState.CARDSTATE_PRESENT), 0 /* phoneId */, new Object()); 105 when(UiccController.getInstance().getUiccCardForPhone(0)).thenReturn(card); 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 card.update(mContext, mSimulatedCommands, 119 makeCardStatus(CardState.CARDSTATE_RESTRICTED)); 120 uiccLauncher.handleMessage(msg); 121 122 broadcast_count++; 123 verify(mContext, times(broadcast_count)).sendBroadcast(intentArgumentCaptor.capture()); 124 assertEquals(PROVISIONING_PACKAGE_NAME, intentArgumentCaptor.getValue().getPackage()); 125 assertEquals(TelephonyIntents.ACTION_SIM_STATE_CHANGED, 126 intentArgumentCaptor.getValue().getAction()); 127 128 // Nothing's changed. Broadcast should not be sent. 129 uiccLauncher.handleMessage(msg); 130 verify(mContext, times(broadcast_count)).sendBroadcast(any(Intent.class)); 131 132 // Card state's changed from restricted. Broadcast should be sent. 133 card.update(mContext, mSimulatedCommands, 134 makeCardStatus(CardState.CARDSTATE_PRESENT)); 135 uiccLauncher.handleMessage(msg); 136 137 broadcast_count++; 138 verify(mContext, times(broadcast_count)).sendBroadcast(intentArgumentCaptor.capture()); 139 assertEquals(PROVISIONING_PACKAGE_NAME, intentArgumentCaptor.getValue().getPackage()); 140 assertEquals(TelephonyIntents.ACTION_SIM_STATE_CHANGED, 141 intentArgumentCaptor.getValue().getAction()); 142 } 143 144 @Test @SmallTest 145 public void testProvisioningPackageUnset() { 146 // deviceProvisionigPackage is not set. 147 when(mResources.getString(eq(R.string.config_deviceProvisioningPackage))) 148 .thenReturn(null); 149 150 if (Looper.myLooper() == null) { 151 Looper.prepare(); 152 } 153 154 UiccStateChangedLauncher uiccLauncher = 155 new UiccStateChangedLauncher(mContext, UiccController.getInstance()); 156 verify(UiccController.getInstance(), never()).registerForIccChanged(eq(uiccLauncher), 157 anyInt(), anyObject()); 158 } 159} 160