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.gsm; 18 19import android.content.Intent; 20import android.os.HandlerThread; 21import android.provider.Telephony; 22import android.test.suitebuilder.annotation.SmallTest; 23 24import com.android.internal.telephony.SmsStorageMonitor; 25import com.android.internal.telephony.TelephonyTest; 26import com.android.internal.telephony.TelephonyTestUtils; 27 28import org.junit.After; 29import org.junit.Before; 30import org.junit.Test; 31import org.mockito.ArgumentCaptor; 32import org.mockito.Mock; 33 34import static org.junit.Assert.assertTrue; 35import static org.mockito.Mockito.anyBoolean; 36import static org.mockito.Mockito.anyInt; 37import static org.mockito.Mockito.doReturn; 38import static org.mockito.Mockito.verify; 39 40public class GsmCellBroadcastHandlerTest extends TelephonyTest { 41 @Mock 42 private SmsStorageMonitor mSmsStorageMonitor; 43 @Mock 44 private android.telephony.SmsMessage mSmsMessage; 45 @Mock 46 private SmsMessage mGsmSmsMessage; 47 48 private GsmCellBroadcastHandler mGsmCellBroadcastHandler; 49 private GsmCellBroadcastHandlerTestHandler mGsmCellBroadcastHandlerTestHandler; 50 51 private class GsmCellBroadcastHandlerTestHandler extends HandlerThread { 52 53 private GsmCellBroadcastHandlerTestHandler(String name) { 54 super(name); 55 } 56 57 @Override 58 public void onLooperPrepared() { 59 mGsmCellBroadcastHandler = GsmCellBroadcastHandler.makeGsmCellBroadcastHandler( 60 mContextFixture.getTestDouble(), mPhone); 61 setReady(true); 62 } 63 } 64 65 @Before 66 public void setUp() throws Exception { 67 68 super.setUp(getClass().getSimpleName()); 69 70 doReturn(true).when(mTelephonyManager).getSmsReceiveCapableForPhone(anyInt(), anyBoolean()); 71 doReturn(true).when(mSmsStorageMonitor).isStorageAvailable(); 72 73 mGsmCellBroadcastHandlerTestHandler = 74 new GsmCellBroadcastHandlerTestHandler(getClass().getSimpleName()); 75 mGsmCellBroadcastHandlerTestHandler.start(); 76 waitUntilReady(); 77 } 78 79 @After 80 public void tearDown() throws Exception { 81 mGsmCellBroadcastHandler = null; 82 mGsmCellBroadcastHandlerTestHandler.quitSafely(); 83 super.tearDown(); 84 } 85 86 @Test @SmallTest 87 public void testBroadcastSms() { 88 mSimulatedCommands.notifyGsmBroadcastSms(new byte[] { 89 (byte)0xc0, //geographical scope 90 (byte)0x01, //serial number 91 (byte)0x01, //serial number 92 (byte)0x01, //message identifier 93 (byte)0x01, //message identifier 94 (byte)0x01 95 }); 96 TelephonyTestUtils.waitForMs(50); 97 ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class); 98 verify(mContextFixture.getTestDouble()).sendBroadcast(intentArgumentCaptor.capture()); 99 assertTrue(intentArgumentCaptor.getValue().getAction().equals( 100 Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION) || 101 intentArgumentCaptor.getValue().getAction().equals( 102 Telephony.Sms.Intents.SMS_CB_RECEIVED_ACTION)); 103 } 104} 105