Home | History | Annotate | Download | only in cellbroadcastreceiver
      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 
     17 package com.android.cellbroadcastreceiver;
     18 
     19 import android.content.Intent;
     20 import android.provider.Telephony;
     21 import android.telephony.CellBroadcastMessage;
     22 import android.telephony.SmsCbCmasInfo;
     23 import android.telephony.SmsCbLocation;
     24 import android.telephony.SmsCbMessage;
     25 
     26 import com.android.internal.telephony.gsm.SmsCbConstants;
     27 
     28 import org.junit.After;
     29 import org.junit.Before;
     30 
     31 import java.util.ArrayList;
     32 
     33 import static com.android.cellbroadcastreceiver.CellBroadcastAlertAudio.ALERT_AUDIO_TONE_TYPE;
     34 import static com.android.cellbroadcastreceiver.CellBroadcastAlertService.SHOW_NEW_ALERT_ACTION;
     35 
     36 public class CellBroadcastAlertServiceTest extends
     37         CellBroadcastServiceTestCase<CellBroadcastAlertService> {
     38 
     39     public CellBroadcastAlertServiceTest() {
     40         super(CellBroadcastAlertService.class);
     41     }
     42 
     43     static SmsCbMessage createMessage() {
     44         return new SmsCbMessage(1, 2, 3, new SmsCbLocation(),
     45                 SmsCbConstants.MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL, "language", "body",
     46                 SmsCbMessage.MESSAGE_PRIORITY_EMERGENCY, null, new SmsCbCmasInfo(1, 2, 3, 4, 5, 6));
     47     }
     48 
     49     @Before
     50     public void setUp() throws Exception {
     51         super.setUp();
     52     }
     53 
     54     @After
     55     public void tearDown() throws Exception {
     56         super.tearDown();
     57     }
     58 
     59     private static void compareCellBroadCastMessage(CellBroadcastMessage cbm1,
     60                                                     CellBroadcastMessage cbm2) {
     61         assertEquals(cbm1.getCmasMessageClass(), cbm2.getCmasMessageClass());
     62         assertEquals(cbm1.getCmasWarningInfo(), cbm2.getCmasWarningInfo());
     63         assertEquals(cbm1.getEtwsWarningInfo(), cbm2.getEtwsWarningInfo());
     64         assertEquals(cbm1.getLanguageCode(), cbm2.getLanguageCode());
     65         assertEquals(cbm1.getMessageBody(), cbm2.getMessageBody());
     66         assertEquals(cbm1.getSerialNumber(), cbm2.getSerialNumber());
     67         assertEquals(cbm1.getServiceCategory(), cbm2.getServiceCategory());
     68         assertEquals(cbm1.getSubId(), cbm2.getSubId());
     69         assertEquals(cbm1.getSerialNumber(), cbm2.getSerialNumber());
     70     }
     71 
     72     // Test handleCellBroadcastIntent method
     73     public void testHandleCellBroadcastIntent() throws Exception {
     74         Intent intent = new Intent(mContext, CellBroadcastAlertService.class);
     75         intent.setAction(Telephony.Sms.Intents.SMS_EMERGENCY_CB_RECEIVED_ACTION);
     76 
     77         SmsCbMessage m = createMessage();
     78         intent.putExtra("message", m);
     79 
     80         startService(intent);
     81         waitForMs(200);
     82 
     83         assertEquals(SHOW_NEW_ALERT_ACTION, mServiceIntentToVerify.getAction());
     84         assertEquals(CellBroadcastAlertService.class.getName(),
     85                 intent.getComponent().getClassName());
     86 
     87         CellBroadcastMessage cbmTest =
     88                 (CellBroadcastMessage) mServiceIntentToVerify.getExtras().get("message");
     89         CellBroadcastMessage cbm = new CellBroadcastMessage(m);
     90 
     91         compareCellBroadCastMessage(cbm, cbmTest);
     92     }
     93 
     94     // Test showNewAlert method
     95     public void testShowNewAlert() throws Exception {
     96 
     97         Intent intent = new Intent(mContext, CellBroadcastAlertService.class);
     98         intent.setAction(SHOW_NEW_ALERT_ACTION);
     99 
    100         SmsCbMessage message = createMessage();
    101         intent.putExtra("message", new CellBroadcastMessage(message));
    102 
    103         startService(intent);
    104         waitForMs(200);
    105 
    106         // verify audio service intent
    107         assertEquals(CellBroadcastAlertAudio.ACTION_START_ALERT_AUDIO,
    108                 mServiceIntentToVerify.getAction());
    109         assertEquals(CellBroadcastAlertAudio.ToneType.CMAS_DEFAULT,
    110                 mServiceIntentToVerify.getSerializableExtra(ALERT_AUDIO_TONE_TYPE));
    111         assertEquals(message.getMessageBody(),
    112                 mServiceIntentToVerify.getStringExtra(
    113                         CellBroadcastAlertAudio.ALERT_AUDIO_MESSAGE_BODY));
    114 
    115         // verify alert dialog activity intent
    116         ArrayList<CellBroadcastMessage> newMessageList = mActivityIntentToVerify
    117                 .getParcelableArrayListExtra(CellBroadcastMessage.SMS_CB_MESSAGE_EXTRA);
    118         assertEquals(1, newMessageList.size());
    119         assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK,
    120                 (mActivityIntentToVerify.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK));
    121         compareCellBroadCastMessage(new CellBroadcastMessage(message), newMessageList.get(0));
    122     }
    123 }