CdmaInboundSmsHandlerTest.java revision 21e1b42d578d90611f76c37d58841543821502fd
1/* 2 * Copyright (C) 2015 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.cdma; 18 19import android.content.Intent; 20import android.os.AsyncResult; 21import android.os.HandlerThread; 22import android.provider.Telephony; 23import android.telephony.*; 24import android.test.suitebuilder.annotation.SmallTest; 25 26import com.android.internal.telephony.InboundSmsHandler; 27import com.android.internal.telephony.SmsStorageMonitor; 28import com.android.internal.telephony.TelephonyTest; 29import com.android.internal.telephony.TelephonyTestUtils; 30import com.android.internal.telephony.cdma.sms.SmsEnvelope; 31import com.android.internal.util.IState; 32import com.android.internal.util.StateMachine; 33 34import static org.junit.Assert.*; 35import static org.mockito.Mockito.*; 36 37import org.junit.After; 38import org.junit.Before; 39import org.junit.Test; 40import org.mockito.ArgumentCaptor; 41import org.mockito.Mock; 42import org.mockito.MockitoAnnotations; 43import org.mockito.Spy; 44 45import java.lang.reflect.Field; 46import java.lang.reflect.Method; 47import java.util.List; 48 49public class CdmaInboundSmsHandlerTest extends TelephonyTest { 50 @Mock 51 private SmsStorageMonitor mSmsStorageMonitor; 52 @Mock 53 private android.telephony.SmsMessage mSmsMessage; 54 @Mock 55 private SmsMessage mCdmaSmsMessage; 56 57 private CdmaInboundSmsHandler mCdmaInboundSmsHandler; 58 private TelephonyManager mTelephonyManager; 59 private SmsEnvelope mSmsEnvelope = new SmsEnvelope(); 60 61 private class CdmaInboundSmsHandlerTestHandler extends HandlerThread { 62 63 private CdmaInboundSmsHandlerTestHandler(String name) { 64 super(name); 65 } 66 67 @Override 68 public void onLooperPrepared() { 69 mCdmaInboundSmsHandler = CdmaInboundSmsHandler.makeInboundSmsHandler(mContext, 70 mSmsStorageMonitor, mPhone, null); 71 setReady(true); 72 } 73 } 74 75 private IState getCurrentState() { 76 try { 77 Method method = StateMachine.class.getDeclaredMethod("getCurrentState"); 78 method.setAccessible(true); 79 return (IState) method.invoke(mCdmaInboundSmsHandler); 80 } catch (Exception e) { 81 fail(e.toString()); 82 return null; 83 } 84 } 85 86 @Before 87 public void setUp() throws Exception { 88 super.setUp("CdmaInboundSmsHandlerTest"); 89 90 Field field = SmsMessage.class.getDeclaredField("mEnvelope"); 91 field.setAccessible(true); 92 field.set(mCdmaSmsMessage, mSmsEnvelope); 93 94 mTelephonyManager = TelephonyManager.from(mContext); 95 doReturn(true).when(mTelephonyManager).getSmsReceiveCapableForPhone(anyInt(), anyBoolean()); 96 doReturn(true).when(mSmsStorageMonitor).isStorageAvailable(); 97 doReturn(mIDeviceIdleController).when(mTelephonyComponentFactory). 98 getIDeviceIdleController(); 99 100 new CdmaInboundSmsHandlerTestHandler(TAG).start(); 101 waitUntilReady(); 102 } 103 104 @After 105 public void tearDown() throws Exception { 106 mCdmaInboundSmsHandler = null; 107 super.tearDown(); 108 } 109 110 @Test @SmallTest 111 public void testNewSms() { 112 // verify initially in StartupState 113 assertEquals("StartupState", getCurrentState().getName()); 114 115 // trigger transition to IdleState 116 mCdmaInboundSmsHandler.sendMessage(InboundSmsHandler.EVENT_START_ACCEPTING_SMS); 117 TelephonyTestUtils.waitForMs(50); 118 119 assertEquals("IdleState", getCurrentState().getName()); 120 121 // send new SMS to state machine and verify that triggers SMS_DELIVER_ACTION 122 byte[] smsPdu = new byte[]{(byte)0xFF, (byte)0xFF, (byte)0xFF}; 123 mSmsMessage.mWrappedSmsMessage = mCdmaSmsMessage; 124 doReturn(smsPdu).when(mCdmaSmsMessage).getPdu(); 125 doReturn(SmsEnvelope.TELESERVICE_WMT).when(mCdmaSmsMessage).getTeleService(); 126 mCdmaInboundSmsHandler.sendMessage(InboundSmsHandler.EVENT_NEW_SMS, 127 new AsyncResult(null, mSmsMessage, null)); 128 TelephonyTestUtils.waitForMs(100); 129 130 ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); 131 verify(mContext, times(2)).sendBroadcast(intentArgumentCaptor.capture()); 132 133 List<Intent> list = intentArgumentCaptor.getAllValues(); 134 /* logd("list.size() " + list.size()); 135 for (int i = 0; i < list.size(); i++) { 136 logd("list.get(i) " + list.get(i)); 137 } */ 138 //todo: seems to be some issue with ArgumentCaptor. Both DELIVER and RECEIVED broadcasts 139 //can be seen in logs but according to list both are RECEIVED 140 //assertEquals(Telephony.Sms.Intents.SMS_DELIVER_ACTION, 141 // list.get(0).getAction()); 142 boolean smsReceivedAction = false; 143 for (Intent i : list) { 144 if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(i.getAction())) { 145 smsReceivedAction = true; 146 break; 147 } 148 } 149 assertTrue(smsReceivedAction); 150 151 assertEquals("IdleState", getCurrentState().getName()); 152 } 153} 154