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 static android.telephony.SmsManager.CELL_BROADCAST_RAN_TYPE_CDMA;
     20 import static android.telephony.SmsManager.CELL_BROADCAST_RAN_TYPE_GSM;
     21 
     22 import static com.android.internal.telephony.cdma.sms.SmsEnvelope.SERVICE_CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY;
     23 import static com.android.internal.telephony.cdma.sms.SmsEnvelope.SERVICE_CATEGORY_CMAS_EXTREME_THREAT;
     24 import static com.android.internal.telephony.cdma.sms.SmsEnvelope.SERVICE_CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT;
     25 import static com.android.internal.telephony.cdma.sms.SmsEnvelope.SERVICE_CATEGORY_CMAS_SEVERE_THREAT;
     26 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_CHILD_ABDUCTION_EMERGENCY;
     27 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_CHILD_ABDUCTION_EMERGENCY_LANGUAGE;
     28 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_OBSERVED;
     29 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_OBSERVED_LANGUAGE;
     30 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_LIKELY;
     31 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_LIKELY_LANGUAGE;
     32 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_OBSERVED;
     33 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_OBSERVED_LANGUAGE;
     34 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL;
     35 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL_LANGUAGE;
     36 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_LIKELY;
     37 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_LIKELY_LANGUAGE;
     38 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_ETWS_EARTHQUAKE_AND_TSUNAMI_WARNING;
     39 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_ETWS_EARTHQUAKE_WARNING;
     40 import static com.android.internal.telephony.gsm.SmsCbConstants.MESSAGE_ID_ETWS_OTHER_EMERGENCY_TYPE;
     41 
     42 import static org.mockito.Matchers.anyInt;
     43 import static org.mockito.Matchers.anyString;
     44 import static org.mockito.Mockito.doReturn;
     45 import static org.mockito.Mockito.times;
     46 import static org.mockito.Mockito.verify;
     47 
     48 import android.content.Intent;
     49 
     50 import com.android.internal.telephony.ISms;
     51 
     52 import org.junit.After;
     53 import org.junit.Before;
     54 import org.mockito.ArgumentCaptor;
     55 import org.mockito.Captor;
     56 import org.mockito.Mock;
     57 
     58 public class CellBroadcastBootupConfigTest extends
     59         CellBroadcastServiceTestCase<CellBroadcastConfigService> {
     60 
     61     @Mock
     62     ISms.Stub mSmsService;
     63 
     64     @Captor
     65     private ArgumentCaptor<Integer> mStartIds;
     66 
     67     @Captor
     68     private ArgumentCaptor<Integer> mEndIds;
     69 
     70     @Captor
     71     private ArgumentCaptor<Integer> mTypes;
     72 
     73     private MockedServiceManager mMockedServiceManager;
     74 
     75     public CellBroadcastBootupConfigTest() {
     76         super(CellBroadcastConfigService.class);
     77     }
     78 
     79     @Before
     80     public void setUp() throws Exception {
     81         super.setUp();
     82 
     83         doReturn(mSmsService).when(mSmsService).queryLocalInterface(anyString());
     84         mMockedServiceManager = new MockedServiceManager();
     85         mMockedServiceManager.replaceService("isms", mSmsService);
     86         putResources(R.array.cmas_presidential_alerts_channels_range_strings, new String[]{
     87                 "0x1112-0x1112:rat=gsm",
     88                 "0x1000-0x1000:rat=cdma",
     89                 "0x111F-0x111F:rat=gsm",
     90         });
     91         putResources(R.array.cmas_alert_extreme_channels_range_strings, new String[]{
     92                 "0x1113-0x1114:rat=gsm",
     93                 "0x1001-0x1001:rat=cdma",
     94                 "0x1120-0x1121:rat=gsm",
     95         });
     96         putResources(R.array.cmas_alerts_severe_range_strings, new String[]{
     97                 "0x1115-0x111A:rat=gsm",
     98                 "0x1002-0x1002:rat=cdma",
     99                 "0x1122-0x1127:rat=gsm",
    100         });
    101         putResources(R.array.cmas_amber_alerts_channels_range_strings, new String[]{
    102                 "0x111B-0x111B:rat=gsm",
    103                 "0x1003-0x1003:rat=cdma",
    104                 "0x1128-0x1128:rat=gsm",
    105         });
    106         putResources(R.array.etws_alerts_range_strings, new String[]{
    107                 "0x1100-0x1102:rat=gsm",
    108                 "0x1104-0x1104:rat=gsm",
    109         });
    110     }
    111 
    112     @After
    113     public void tearDown() throws Exception {
    114         mMockedServiceManager.restoreAllServices();
    115         super.tearDown();
    116     }
    117 
    118     private static class CbConfig {
    119         final int startId;
    120         final int endId;
    121         final int type;
    122 
    123         CbConfig(int startId, int endId, int type) {
    124             this.startId = startId;
    125             this.endId = endId;
    126             this.type = type;
    127         }
    128     }
    129 
    130     // Test if CellbroadcastConfigService properly configure all the required channels.
    131     public void testConfiguration() throws Exception {
    132 
    133         Intent intent = new Intent(mContext, CellBroadcastConfigService.class);
    134         intent.setAction(CellBroadcastConfigService.ACTION_ENABLE_CHANNELS);
    135 
    136         startService(intent);
    137         waitForMs(200);
    138 
    139         CbConfig[] configs = new CbConfig[] {
    140                 new CbConfig(MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL,
    141                         MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL,
    142                         CELL_BROADCAST_RAN_TYPE_GSM),
    143                 new CbConfig(SERVICE_CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT,
    144                         SERVICE_CATEGORY_CMAS_PRESIDENTIAL_LEVEL_ALERT,
    145                         CELL_BROADCAST_RAN_TYPE_CDMA),
    146                 new CbConfig(MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL_LANGUAGE,
    147                         MESSAGE_ID_CMAS_ALERT_PRESIDENTIAL_LEVEL_LANGUAGE,
    148                         CELL_BROADCAST_RAN_TYPE_GSM),
    149                 new CbConfig(MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_OBSERVED,
    150                         MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_LIKELY,
    151                         CELL_BROADCAST_RAN_TYPE_GSM),
    152                 new CbConfig(SERVICE_CATEGORY_CMAS_EXTREME_THREAT,
    153                         SERVICE_CATEGORY_CMAS_EXTREME_THREAT,
    154                         CELL_BROADCAST_RAN_TYPE_CDMA),
    155                 new CbConfig(MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_OBSERVED_LANGUAGE,
    156                         MESSAGE_ID_CMAS_ALERT_EXTREME_IMMEDIATE_LIKELY_LANGUAGE,
    157                         CELL_BROADCAST_RAN_TYPE_GSM),
    158                 new CbConfig(MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_OBSERVED,
    159                         MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_LIKELY,
    160                         CELL_BROADCAST_RAN_TYPE_GSM),
    161                 new CbConfig(SERVICE_CATEGORY_CMAS_SEVERE_THREAT,
    162                         SERVICE_CATEGORY_CMAS_SEVERE_THREAT,
    163                         CELL_BROADCAST_RAN_TYPE_CDMA),
    164                 new CbConfig(MESSAGE_ID_CMAS_ALERT_EXTREME_EXPECTED_OBSERVED_LANGUAGE,
    165                         MESSAGE_ID_CMAS_ALERT_SEVERE_EXPECTED_LIKELY_LANGUAGE,
    166                         CELL_BROADCAST_RAN_TYPE_GSM),
    167                 new CbConfig(MESSAGE_ID_CMAS_ALERT_CHILD_ABDUCTION_EMERGENCY,
    168                         MESSAGE_ID_CMAS_ALERT_CHILD_ABDUCTION_EMERGENCY,
    169                         CELL_BROADCAST_RAN_TYPE_GSM),
    170                 new CbConfig(SERVICE_CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY,
    171                         SERVICE_CATEGORY_CMAS_CHILD_ABDUCTION_EMERGENCY,
    172                         CELL_BROADCAST_RAN_TYPE_CDMA),
    173                 new CbConfig(MESSAGE_ID_CMAS_ALERT_CHILD_ABDUCTION_EMERGENCY_LANGUAGE,
    174                         MESSAGE_ID_CMAS_ALERT_CHILD_ABDUCTION_EMERGENCY_LANGUAGE,
    175                         CELL_BROADCAST_RAN_TYPE_GSM),
    176                 new CbConfig(MESSAGE_ID_ETWS_EARTHQUAKE_WARNING,
    177                         MESSAGE_ID_ETWS_EARTHQUAKE_AND_TSUNAMI_WARNING,
    178                         CELL_BROADCAST_RAN_TYPE_GSM),
    179                 new CbConfig(MESSAGE_ID_ETWS_OTHER_EMERGENCY_TYPE,
    180                         MESSAGE_ID_ETWS_OTHER_EMERGENCY_TYPE,
    181                         CELL_BROADCAST_RAN_TYPE_GSM),
    182         };
    183 
    184         verify(mSmsService, times(configs.length)).enableCellBroadcastRangeForSubscriber(anyInt(),
    185                 mStartIds.capture(), mEndIds.capture(), mTypes.capture());
    186 
    187         for (int i = 0; i < configs.length; i++) {
    188             assertEquals("i=" + i, configs[i].startId, mStartIds.getAllValues().get(i).intValue());
    189             assertEquals("i=" + i, configs[i].endId, mEndIds.getAllValues().get(i).intValue());
    190             assertEquals("i=" + i, configs[i].type, mTypes.getAllValues().get(i).intValue());
    191         }
    192      }
    193 }