Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2018 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 package android.telephony.cts;
     17 
     18 import static android.telephony.ServiceState.DUPLEX_MODE_FDD;
     19 import static android.telephony.ServiceState.DUPLEX_MODE_TDD;
     20 import static android.telephony.ServiceState.DUPLEX_MODE_UNKNOWN;
     21 import static android.telephony.ServiceState.STATE_OUT_OF_SERVICE;
     22 import static android.telephony.ServiceState.STATE_POWER_OFF;
     23 import static android.telephony.ServiceState.ROAMING_TYPE_DOMESTIC;
     24 import static android.telephony.ServiceState.ROAMING_TYPE_NOT_ROAMING;
     25 
     26 import static org.junit.Assert.assertEquals;
     27 import static org.junit.Assert.assertFalse;
     28 import static org.junit.Assert.assertNotEquals;
     29 import static org.junit.Assert.assertNotNull;
     30 import static org.junit.Assert.assertNull;
     31 import static org.junit.Assert.assertTrue;
     32 
     33 import android.os.Parcel;
     34 import android.telephony.AccessNetworkConstants;
     35 import android.telephony.LteVopsSupportInfo;
     36 import android.telephony.NetworkRegistrationInfo;
     37 import android.telephony.ServiceState;
     38 import android.telephony.TelephonyManager;
     39 
     40 import org.junit.Before;
     41 import org.junit.Test;
     42 
     43 import java.util.List;
     44 
     45 public class ServiceStateTest {
     46     private static final String OPERATOR_ALPHA_LONG = "CtsOperatorLong";
     47     private static final String OPERATOR_ALPHA_SHORT = "CtsOp";
     48     private static final String OPERATOR_NUMERIC = "02871";
     49     private static final int SYSTEM_ID = 123;
     50     private static final int NETWORK_ID = 456;
     51     private static final int CHANNEL_NUMBER_BAND_66 = 66436;
     52     private static final int CHANNEL_NUMBER_BAND_33 = 36000;
     53     private static final int[] CELL_BANDWIDTH = {1, 2, 3};
     54 
     55     private ServiceState serviceState;
     56 
     57     @Before
     58     public void setUp() {
     59         serviceState = new ServiceState();
     60     }
     61 
     62     @Test
     63     public void testDescribeContents() {
     64         assertEquals(0, serviceState.describeContents());
     65     }
     66 
     67     @Test
     68     public void testSetStateOff() {
     69         serviceState.setStateOff();
     70         assertEquals(STATE_POWER_OFF, serviceState.getState());
     71         checkOffStatus(serviceState);
     72     }
     73 
     74     @Test
     75     public void testSetStateOutOfService() {
     76         serviceState.setStateOutOfService();
     77         assertEquals(STATE_OUT_OF_SERVICE, serviceState.getState());
     78         checkOffStatus(serviceState);
     79     }
     80 
     81     @Test
     82     public void testSetState() {
     83         serviceState.setState(ServiceState.STATE_IN_SERVICE);
     84         assertEquals(ServiceState.STATE_IN_SERVICE, serviceState.getState());
     85     }
     86 
     87     @Test
     88     public void testGetRoaming() {
     89         serviceState.setRoaming(false);
     90         assertFalse(serviceState.getRoaming());
     91         serviceState.setRoaming(true);
     92         assertTrue(serviceState.getRoaming());
     93     }
     94 
     95     @Test
     96     public void testGetIsManualSelection() {
     97         serviceState.setIsManualSelection(false);
     98         assertFalse(serviceState.getIsManualSelection());
     99         serviceState.setIsManualSelection(true);
    100         assertTrue(serviceState.getIsManualSelection());
    101     }
    102 
    103     @Test
    104     public void testGetOperator() {
    105         serviceState.setOperatorName(OPERATOR_ALPHA_LONG, OPERATOR_ALPHA_SHORT, OPERATOR_NUMERIC);
    106         assertEquals(OPERATOR_ALPHA_LONG, serviceState.getOperatorAlphaLong());
    107         assertEquals(OPERATOR_ALPHA_SHORT, serviceState.getOperatorAlphaShort());
    108         assertEquals(OPERATOR_NUMERIC, serviceState.getOperatorNumeric());
    109     }
    110 
    111     @Test
    112     public void testGetCdma() {
    113         serviceState.setCdmaSystemAndNetworkId(SYSTEM_ID, NETWORK_ID);
    114         assertEquals(SYSTEM_ID, serviceState.getCdmaSystemId());
    115         assertEquals(NETWORK_ID, serviceState.getCdmaNetworkId());
    116     }
    117 
    118     @Test
    119     public void testGetChannelNumber() {
    120         serviceState.setChannelNumber(CHANNEL_NUMBER_BAND_66);
    121         assertEquals(CHANNEL_NUMBER_BAND_66, serviceState.getChannelNumber());
    122     }
    123 
    124     @Test
    125     public void testGetCellBandwidths() {
    126         serviceState.setCellBandwidths(CELL_BANDWIDTH);
    127         assertEquals(CELL_BANDWIDTH, serviceState.getCellBandwidths());
    128     }
    129 
    130     @Test
    131     public void testGetDuplexMode() {
    132         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
    133                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
    134                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_GSM)
    135                 .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
    136                 .build();
    137         serviceState.addNetworkRegistrationInfo(nri);
    138         assertEquals(DUPLEX_MODE_UNKNOWN, serviceState.getDuplexMode());
    139 
    140         nri = new NetworkRegistrationInfo.Builder()
    141                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
    142                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE)
    143                 .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
    144                 .build();
    145         serviceState.addNetworkRegistrationInfo(nri);
    146 
    147         assertEquals(DUPLEX_MODE_FDD, serviceState.getDuplexMode());
    148 
    149         serviceState.setChannelNumber(CHANNEL_NUMBER_BAND_33);
    150         assertEquals(DUPLEX_MODE_TDD, serviceState.getDuplexMode());
    151     }
    152 
    153     @Test
    154     public void testToString() {
    155         assertNotNull(serviceState.toString());
    156     }
    157 
    158     @Test
    159     public void testCopyConstructor() {
    160         ServiceState serviceState = getServiceStateWithOperatorName("name", "numeric");
    161         assertEquals(serviceState, new ServiceState(serviceState));
    162     }
    163 
    164     @Test
    165     public void testParcelConstructor() {
    166         ServiceState serviceState = getServiceStateWithOperatorName("name", "numeric");
    167         Parcel stateParcel = Parcel.obtain();
    168         serviceState.writeToParcel(stateParcel, 0);
    169         stateParcel.setDataPosition(0);
    170         assertEquals(serviceState, new ServiceState(stateParcel));
    171     }
    172 
    173     @Test
    174     public void testHashCode() {
    175         ServiceState serviceStateA = getServiceStateWithOperatorName("a", "b");
    176         ServiceState serviceStateB = getServiceStateWithOperatorName("a", "b");
    177         ServiceState serviceStateC = getServiceStateWithOperatorName("c", "d");
    178 
    179         // well-written hashCode functions shouldn't produce "0"
    180         assertNotEquals(serviceStateA.hashCode(), 0);
    181         assertNotEquals(serviceStateB.hashCode(), 0);
    182 
    183         // If serviceStateA.equals(serviceStateB), then serviceStateA.hashCode()
    184         // should equal serviceStateB.hashCode().
    185         assertEquals(serviceStateA.hashCode(), serviceStateB.hashCode());
    186         assertEquals(serviceStateA, serviceStateB);
    187 
    188         // If serviceStateA.hashCode() != serviceStateC.hashCode(), then
    189         // serviceStateA.equals(serviceStateB) should be false.
    190         assertNotEquals(serviceStateA.hashCode(), serviceStateC.hashCode());
    191         assertNotEquals(serviceStateA, serviceStateC);
    192     }
    193 
    194     @Test
    195     public void testRoaming() {
    196         ServiceState notRoaming = getServiceStateWithRoamingTypes(ROAMING_TYPE_NOT_ROAMING,
    197                                                                     ROAMING_TYPE_NOT_ROAMING);
    198         ServiceState dataRoaming = getServiceStateWithRoamingTypes(ROAMING_TYPE_DOMESTIC,
    199                                                                     ROAMING_TYPE_NOT_ROAMING);
    200         ServiceState voiceRoaming = getServiceStateWithRoamingTypes(ROAMING_TYPE_NOT_ROAMING,
    201                                                                     ROAMING_TYPE_DOMESTIC);
    202         ServiceState dataVoiceRoaming = getServiceStateWithRoamingTypes(ROAMING_TYPE_NOT_ROAMING,
    203                                                                     ROAMING_TYPE_DOMESTIC);
    204 
    205         assertFalse(notRoaming.getRoaming());
    206         assertTrue(dataRoaming.getRoaming());
    207         assertTrue(voiceRoaming.getRoaming());
    208         assertTrue(dataVoiceRoaming.getRoaming());
    209     }
    210 
    211     @Test
    212     public void testIsManualSelection() {
    213         serviceState.setIsManualSelection(false);
    214         assertFalse(serviceState.getIsManualSelection());
    215         serviceState.setIsManualSelection(true);
    216         assertTrue(serviceState.getIsManualSelection());
    217     }
    218 
    219     private ServiceState getServiceStateWithOperatorName(String name, String numeric) {
    220         ServiceState serviceState = new ServiceState();
    221         serviceState.setOperatorName(name, name, numeric);
    222         return serviceState;
    223     }
    224 
    225     private ServiceState getServiceStateWithRoamingTypes(int dataRoaming, int voiceRoaming) {
    226         ServiceState serviceState = new ServiceState();
    227         serviceState.setDataRoamingType(dataRoaming);
    228         serviceState.setVoiceRoamingType(voiceRoaming);
    229         return serviceState;
    230     }
    231 
    232     /**
    233      * Check the ServiceState fields in STATE_OUT_OF_SERVICE or STATE_POWER_OFF
    234      */
    235     private void checkOffStatus(ServiceState s) {
    236         assertFalse(s.getRoaming());
    237         assertNull(s.getOperatorAlphaLong());
    238         assertNull(s.getOperatorAlphaShort());
    239         assertNull(s.getOperatorNumeric());
    240         assertFalse(s.getIsManualSelection());
    241     }
    242 
    243     @Test
    244     public void testGetRegistrationInfo() {
    245         NetworkRegistrationInfo nri = new NetworkRegistrationInfo.Builder()
    246                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_LTE)
    247                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
    248                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WWAN)
    249                 .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
    250                 .build();
    251         serviceState.addNetworkRegistrationInfo(nri);
    252 
    253         assertEquals(nri, serviceState.getNetworkRegistrationInfo(
    254                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN));
    255         assertNull(serviceState.getNetworkRegistrationInfo(
    256                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WLAN));
    257         assertNull(serviceState.getNetworkRegistrationInfo(
    258                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN));
    259         assertNull(serviceState.getNetworkRegistrationInfo(
    260                 NetworkRegistrationInfo.DOMAIN_CS, AccessNetworkConstants.TRANSPORT_TYPE_WLAN));
    261 
    262         List<NetworkRegistrationInfo> nris = serviceState.getNetworkRegistrationInfoList();
    263         assertEquals(1, nris.size());
    264         assertEquals(nri, nris.get(0));
    265 
    266         nri = new NetworkRegistrationInfo.Builder()
    267                 .setAccessNetworkTechnology(TelephonyManager.NETWORK_TYPE_IWLAN)
    268                 .setRegistrationState(NetworkRegistrationInfo.REGISTRATION_STATE_HOME)
    269                 .setTransportType(AccessNetworkConstants.TRANSPORT_TYPE_WLAN)
    270                 .setDomain(NetworkRegistrationInfo.DOMAIN_PS)
    271                 .build();
    272         serviceState.addNetworkRegistrationInfo(nri);
    273         assertEquals(nri, serviceState.getNetworkRegistrationInfo(
    274                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WLAN));
    275 
    276         nris = serviceState.getNetworkRegistrationInfoListForDomain(
    277                 NetworkRegistrationInfo.DOMAIN_PS);
    278         assertEquals(2, nris.size());
    279         assertEquals(nri, nris.get(1));
    280 
    281         nris = serviceState.getNetworkRegistrationInfoList();
    282         assertEquals(2, nris.size());
    283 
    284         nris = serviceState.getNetworkRegistrationInfoListForTransportType(
    285                 AccessNetworkConstants.TRANSPORT_TYPE_WLAN);
    286         assertEquals(1, nris.size());
    287         assertEquals(nri, nris.get(0));
    288     }
    289 
    290     @Test
    291     public void testLteVopsSupportInfo() {
    292         LteVopsSupportInfo lteVopsSupportInfo =
    293                 new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_NOT_AVAILABLE,
    294                         LteVopsSupportInfo.LTE_STATUS_NOT_AVAILABLE);
    295 
    296         NetworkRegistrationInfo wwanDataRegState = new NetworkRegistrationInfo(
    297                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
    298                 0, 0, 0, true, null, null, 0, false, false, false, lteVopsSupportInfo, false);
    299 
    300         ServiceState ss = new ServiceState();
    301 
    302         ss.addNetworkRegistrationInfo(wwanDataRegState);
    303 
    304         assertEquals(ss.getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS,
    305                 AccessNetworkConstants.TRANSPORT_TYPE_WWAN), wwanDataRegState);
    306 
    307         lteVopsSupportInfo =
    308                 new LteVopsSupportInfo(LteVopsSupportInfo.LTE_STATUS_SUPPORTED,
    309                         LteVopsSupportInfo.LTE_STATUS_NOT_SUPPORTED);
    310 
    311         wwanDataRegState = new NetworkRegistrationInfo(
    312                 NetworkRegistrationInfo.DOMAIN_PS, AccessNetworkConstants.TRANSPORT_TYPE_WWAN,
    313                 0, 0, 0, true, null, null, 0, false, false, false, lteVopsSupportInfo, false);
    314         ss.addNetworkRegistrationInfo(wwanDataRegState);
    315         assertEquals(ss.getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS,
    316                 AccessNetworkConstants.TRANSPORT_TYPE_WWAN), wwanDataRegState);
    317         assertEquals(ss.getNetworkRegistrationInfo(NetworkRegistrationInfo.DOMAIN_PS,
    318                 AccessNetworkConstants.TRANSPORT_TYPE_WWAN).getDataSpecificInfo().getLteVopsSupportInfo(),
    319             lteVopsSupportInfo);
    320     }
    321 }
    322