Home | History | Annotate | Download | only in telephony
      1 /*
      2  * Copyright (C) 2017 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.internal.telephony;
     18 
     19 import android.os.Parcel;
     20 import android.telephony.CellIdentity;
     21 import android.telephony.CellIdentityGsm;
     22 import android.test.AndroidTestCase;
     23 import android.test.suitebuilder.annotation.SmallTest;
     24 
     25 /** Unit tests for {@link CellIdentityGsm}. */
     26 
     27 public class CellIdentityGsmTest extends AndroidTestCase {
     28 
     29     // Location Area Code ranges from 0 to 65535.
     30     private static final int LAC = 65535;
     31     // GSM Cell Identity ranges from 0 to 65535.
     32     private static final int CID = 65535;
     33     // GSM Absolute RF Channel Number ranges from 0 to 65535.
     34     private static final int ARFCN = 65535;
     35     // Base Station Identity Code ranges from 0 to 63.
     36     private static final int BSIC = 63;
     37     private static final int MCC = 120;
     38     private static final int MNC = 260;
     39     private static final String MCC_STR = "120";
     40     private static final String MNC_STR = "260";
     41     private static final String ALPHA_LONG = "long";
     42     private static final String ALPHA_SHORT = "short";
     43 
     44 
     45     @SmallTest
     46     public void testDefaultConstructor() {
     47         CellIdentityGsm ci =
     48                 new CellIdentityGsm(LAC, CID, ARFCN, BSIC, MCC_STR, MNC_STR,
     49                         ALPHA_LONG, ALPHA_SHORT);
     50 
     51         assertEquals(LAC, ci.getLac());
     52         assertEquals(CID, ci.getCid());
     53         assertEquals(ARFCN, ci.getArfcn());
     54         assertEquals(ARFCN, ci.getChannelNumber());
     55         assertEquals(BSIC, ci.getBsic());
     56         assertEquals(MCC, ci.getMcc());
     57         assertEquals(MNC, ci.getMnc());
     58         assertEquals(MCC_STR, ci.getMccString());
     59         assertEquals(MNC_STR, ci.getMncString());
     60         assertEquals(MCC_STR + MNC_STR, ci.getMobileNetworkOperator());
     61         assertEquals(ALPHA_LONG, ci.getOperatorAlphaLong());
     62         assertEquals(ALPHA_SHORT, ci.getOperatorAlphaShort());
     63     }
     64 
     65     @SmallTest
     66     public void testConstructorWithThreeDigitMnc() {
     67         final String mncWithThreeDigit = "061";
     68         CellIdentityGsm ci =
     69                 new CellIdentityGsm(LAC, CID, ARFCN, BSIC, MCC_STR, mncWithThreeDigit,
     70                         ALPHA_LONG, ALPHA_SHORT);
     71 
     72         assertEquals(MCC, ci.getMcc());
     73         assertEquals(61, ci.getMnc());
     74         assertEquals(MCC_STR, ci.getMccString());
     75         assertEquals(mncWithThreeDigit, ci.getMncString());
     76         assertEquals(MCC_STR + mncWithThreeDigit, ci.getMobileNetworkOperator());
     77     }
     78 
     79     @SmallTest
     80     public void testConstructorWithTwoDigitMnc() {
     81         final String mncWithTwoDigit = "61";
     82         CellIdentityGsm ci =
     83                 new CellIdentityGsm(LAC, CID, ARFCN, BSIC, MCC_STR, mncWithTwoDigit,
     84                         ALPHA_LONG, ALPHA_SHORT);
     85 
     86         assertEquals(MCC, ci.getMcc());
     87         assertEquals(61, ci.getMnc());
     88         assertEquals(MCC_STR, ci.getMccString());
     89         assertEquals(mncWithTwoDigit, ci.getMncString());
     90         assertEquals(MCC_STR + mncWithTwoDigit, ci.getMobileNetworkOperator());
     91     }
     92 
     93     @SmallTest
     94     public void testConstructorWithEmptyMccMnc() {
     95         CellIdentityGsm ci =
     96                 new CellIdentityGsm(LAC, CID, ARFCN, BSIC, null, null, ALPHA_LONG, ALPHA_SHORT);
     97 
     98         assertEquals(Integer.MAX_VALUE, ci.getMcc());
     99         assertEquals(Integer.MAX_VALUE, ci.getMnc());
    100         assertNull(ci.getMccString());
    101         assertNull(ci.getMncString());
    102         assertNull(ci.getMobileNetworkOperator());
    103 
    104         ci = new CellIdentityGsm(LAC, CID, ARFCN, BSIC, MCC_STR, null, ALPHA_LONG, ALPHA_SHORT);
    105 
    106         assertEquals(MCC, ci.getMcc());
    107         assertEquals(Integer.MAX_VALUE, ci.getMnc());
    108         assertEquals(MCC_STR, ci.getMccString());
    109         assertNull(ci.getMncString());
    110         assertNull(ci.getMobileNetworkOperator());
    111 
    112         ci = new CellIdentityGsm(LAC, CID, ARFCN, BSIC, null, MNC_STR, ALPHA_LONG, ALPHA_SHORT);
    113 
    114         assertEquals(MNC, ci.getMnc());
    115         assertEquals(Integer.MAX_VALUE, ci.getMcc());
    116         assertEquals(MNC_STR, ci.getMncString());
    117         assertNull(ci.getMccString());
    118         assertNull(ci.getMobileNetworkOperator());
    119 
    120         ci = new CellIdentityGsm(LAC, CID, ARFCN, BSIC, "", "", ALPHA_LONG, ALPHA_SHORT);
    121 
    122         assertEquals(Integer.MAX_VALUE, ci.getMcc());
    123         assertEquals(Integer.MAX_VALUE, ci.getMnc());
    124         assertNull(ci.getMccString());
    125         assertNull(ci.getMncString());
    126         assertNull(ci.getMobileNetworkOperator());
    127     }
    128 
    129     @SmallTest
    130     public void testFormerConstructor() {
    131         CellIdentityGsm ci =
    132                 new CellIdentityGsm(MCC, MNC, LAC, CID);
    133 
    134         assertEquals(LAC, ci.getLac());
    135         assertEquals(CID, ci.getCid());
    136         assertEquals(Integer.MAX_VALUE, ci.getArfcn());
    137         assertEquals(Integer.MAX_VALUE, ci.getBsic());
    138         assertEquals(MCC, ci.getMcc());
    139         assertEquals(MNC, ci.getMnc());
    140         assertEquals(MCC_STR, ci.getMccString());
    141         assertEquals(MNC_STR, ci.getMncString());
    142         assertEquals(MCC_STR + MNC_STR, ci.getMobileNetworkOperator());
    143         assertNull(ci.getOperatorAlphaLong());
    144         assertNull(ci.getOperatorAlphaShort());
    145     }
    146 
    147     @SmallTest
    148     public void testEquals() {
    149         CellIdentityGsm ciA = new CellIdentityGsm(
    150                 LAC, CID, ARFCN, BSIC, MCC_STR, MNC_STR, ALPHA_LONG, ALPHA_SHORT);
    151         CellIdentityGsm ciB = new CellIdentityGsm(
    152                 LAC, CID, ARFCN, BSIC,  MCC_STR, MNC_STR, ALPHA_LONG, ALPHA_SHORT);
    153 
    154         assertTrue(ciA.equals(ciB));
    155 
    156         ciA = new CellIdentityGsm(LAC, CID, ARFCN, BSIC, null, MNC_STR, ALPHA_LONG, ALPHA_SHORT);
    157         ciB = new CellIdentityGsm(LAC, CID, ARFCN, BSIC, null, MNC_STR, ALPHA_LONG, ALPHA_SHORT);
    158 
    159         assertTrue(ciA.equals(ciB));
    160 
    161         ciA = new CellIdentityGsm(
    162                 LAC, CID, ARFCN, BSIC, MCC_STR,  MNC_STR, ALPHA_LONG, ALPHA_SHORT);
    163         ciB = new CellIdentityGsm(LAC, CID, ARFCN, BSIC, null, MNC_STR, ALPHA_LONG, ALPHA_SHORT);
    164 
    165         assertFalse(ciA.equals(ciB));
    166     }
    167 
    168     @SmallTest
    169     public void testParcel() {
    170         CellIdentityGsm ci =
    171                 new CellIdentityGsm(LAC, CID, ARFCN, BSIC, MCC_STR, MNC_STR,
    172                         ALPHA_LONG, ALPHA_SHORT);
    173 
    174         Parcel p = Parcel.obtain();
    175         ci.writeToParcel(p, 0);
    176         p.setDataPosition(0);
    177 
    178         CellIdentityGsm newCi = CellIdentityGsm.CREATOR.createFromParcel(p);
    179         assertEquals(ci, newCi);
    180     }
    181 
    182     @SmallTest
    183     public void testParcelWithUnknowMccMnc() {
    184         CellIdentityGsm ci =
    185                 new CellIdentityGsm(LAC, CID, ARFCN, BSIC, null, null, ALPHA_LONG, ALPHA_SHORT);
    186 
    187         Parcel p = Parcel.obtain();
    188         p.writeInt(CellIdentityGsm.TYPE_GSM);
    189         p.writeString(String.valueOf(Integer.MAX_VALUE));
    190         p.writeString(String.valueOf(Integer.MAX_VALUE));
    191         p.writeString(ALPHA_LONG);
    192         p.writeString(ALPHA_SHORT);
    193         p.writeInt(LAC);
    194         p.writeInt(CID);
    195         p.writeInt(ARFCN);
    196         p.writeInt(BSIC);
    197         p.setDataPosition(0);
    198 
    199         CellIdentityGsm newCi = CellIdentityGsm.CREATOR.createFromParcel(p);
    200         assertEquals(ci, newCi);
    201     }
    202 
    203     @SmallTest
    204     public void testParcelWithInvalidMccMnc() {
    205         final String invalidMcc = "randomStuff";
    206         final String invalidMnc = "randomStuff";
    207         CellIdentityGsm ci =
    208                 new CellIdentityGsm(LAC, CID, ARFCN, BSIC, null, null, ALPHA_LONG, ALPHA_SHORT);
    209 
    210         Parcel p = Parcel.obtain();
    211         p.writeInt(CellIdentity.TYPE_GSM);
    212         p.writeString(invalidMcc);
    213         p.writeString(invalidMnc);
    214         p.writeString(ALPHA_LONG);
    215         p.writeString(ALPHA_SHORT);
    216         p.writeInt(LAC);
    217         p.writeInt(CID);
    218         p.writeInt(ARFCN);
    219         p.writeInt(BSIC);
    220         p.setDataPosition(0);
    221 
    222         CellIdentityGsm newCi = CellIdentityGsm.CREATOR.createFromParcel(p);
    223         assertEquals(ci, newCi);
    224     }
    225 }
    226