/* * Copyright (C) 2016 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.internal.telephony.uicc; import android.content.ContentValues; import android.os.HandlerThread; import android.os.Message; import android.os.AsyncResult; import com.android.internal.telephony.IccPhoneBookInterfaceManager; import com.android.internal.telephony.TelephonyTest; import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; import android.test.suitebuilder.annotation.SmallTest; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.anyInt; import static org.mockito.Mockito.anyString; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.util.Arrays; import java.util.List; public class IccPhoneBookInterfaceManagerTest extends TelephonyTest { @Mock private AdnRecordCache mAdnRecordCache; @Mock private AdnRecord mAdnRecord; private IccPhoneBookInterfaceManager mIccPhoneBookInterfaceMgr; private IccPhoneBookInterfaceManagerHandler mIccPhoneBookInterfaceManagerHandler; private List<AdnRecord> mAdnList = Arrays.asList(mAdnRecord); private SimPhoneBookAdnRecordCache mSimPbCache; private class IccPhoneBookInterfaceManagerHandler extends HandlerThread { private IccPhoneBookInterfaceManagerHandler(String name) { super(name); } @Override public void onLooperPrepared() { mIccPhoneBookInterfaceMgr = new IccPhoneBookInterfaceManager(mPhone); mSimPbCache = new SimPhoneBookAdnRecordCache( mPhone.getContext(), mPhone.getPhoneId(), mPhone.mCi); setReady(true); } } @Before public void setUp() throws Exception { super.setUp(this.getClass().getSimpleName()); //APP_FAM_3GPP default mPhone is GSM doReturn(mSimRecords).when(mPhone).getIccRecords(); doReturn(mAdnRecordCache).when(mSimRecords).getAdnCache(); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Message response = (Message) invocation.getArguments()[2]; //set result for load ADN EF AsyncResult.forMessage(response).result = mAdnList; response.sendToTarget(); return null; } }).when(mAdnRecordCache).requestLoadAllAdnLike(anyInt(), anyInt(), (Message) anyObject()); mIccPhoneBookInterfaceManagerHandler = new IccPhoneBookInterfaceManagerHandler(TAG); mIccPhoneBookInterfaceManagerHandler.start(); waitUntilReady(); } @After public void tearDown() throws Exception { mIccPhoneBookInterfaceManagerHandler.quitSafely(); super.tearDown(); } @Test @SmallTest public void testAdnEFLoadWithFailure() { List<AdnRecord> adnListResult = mIccPhoneBookInterfaceMgr.getAdnRecordsInEf( IccConstants.EF_ADN); assertEquals(mAdnList, adnListResult); //mock a ADN Ef load failure doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Message response = (Message) invocation.getArguments()[2]; AsyncResult.forMessage(response).exception = new RuntimeException(); response.sendToTarget(); return null; } }).when(mAdnRecordCache).requestLoadAllAdnLike(anyInt(), anyInt(), (Message) anyObject()); List<AdnRecord> adnListResultNew = mIccPhoneBookInterfaceMgr.getAdnRecordsInEf( IccConstants.EF_ADN); //the later read return null due to exception assertNull(adnListResultNew); //verify the previous read is not got affected assertEquals(mAdnList, adnListResult); } @Test @SmallTest public void testUpdateAdnRecord() { doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocation) throws Throwable { Message response = (Message) invocation.getArguments()[4]; //set result for update ADN EF AsyncResult.forMessage(response).exception = null; response.sendToTarget(); return null; } }).when(mAdnRecordCache).updateAdnBySearch( anyInt(), (AdnRecord) anyObject(), (AdnRecord) anyObject(), anyString(), (Message) anyObject()); ContentValues values = new ContentValues(); values.put("tag", ""); values.put("number", ""); values.put("emails", ""); values.put("anrs", ""); values.put("newTag", "test"); values.put("newNumber", "123456"); values.put("newEmails", ""); values.put("newAnrs", ""); boolean result = mIccPhoneBookInterfaceMgr.updateAdnRecordsWithContentValuesInEfBySearch( IccConstants.EF_ADN, values , null); assertTrue(result); } @Test @SmallTest public void testGetAdnCapacity() { int[] capacityArray = {500, 0, 500, 0, 500, 0}; Message fetchCapacityDone = mSimPbCache.obtainMessage( SimPhoneBookAdnRecordCache.EVENT_QUERY_ADN_RECORD_DONE); AsyncResult.forMessage(fetchCapacityDone, capacityArray, null); fetchCapacityDone.sendToTarget(); try { final Field field = IccPhoneBookInterfaceManager.class.getDeclaredField("mSimPbAdnCache"); field.setAccessible(true); field.set(mIccPhoneBookInterfaceMgr, mSimPbCache); Method method = IccPhoneBookInterfaceManager.class.getDeclaredMethod("isSimPhoneBookEnabled"); method.setAccessible(true); method.invoke(mIccPhoneBookInterfaceMgr); when(method.invoke(mIccPhoneBookInterfaceMgr)).thenReturn(true); }catch (Exception e) { e.printStackTrace(); } int[] capacity = mIccPhoneBookInterfaceMgr.getAdnRecordsCapacity(); assertArrayEquals(capacityArray, capacity); } }