Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2014 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 android.content.Context;
     19 import android.content.pm.PackageManager;
     20 import android.net.ConnectivityManager;
     21 import android.os.Parcel;
     22 import android.telephony.CellIdentityCdma;
     23 import android.telephony.CellIdentityGsm;
     24 import android.telephony.CellIdentityLte;
     25 import android.telephony.CellIdentityWcdma;
     26 import android.telephony.CellInfo;
     27 import android.telephony.CellInfoCdma;
     28 import android.telephony.CellInfoGsm;
     29 import android.telephony.CellInfoLte;
     30 import android.telephony.CellInfoWcdma;
     31 import android.telephony.CellSignalStrengthCdma;
     32 import android.telephony.CellSignalStrengthGsm;
     33 import android.telephony.CellSignalStrengthLte;
     34 import android.telephony.CellSignalStrengthWcdma;
     35 import android.telephony.TelephonyManager;
     36 import android.test.AndroidTestCase;
     37 import android.util.Log;
     38 
     39 
     40 import java.util.List;
     41 import java.util.Objects;
     42 
     43 /**
     44  * Test TelephonyManager.getAllCellInfo()
     45  * <p>
     46  * TODO(chesnutt): test onCellInfoChanged() once the implementation
     47  * of async callbacks is complete (see http://b/13788638)
     48  */
     49 public class CellInfoTest extends AndroidTestCase{
     50     private final Object mLock = new Object();
     51     private TelephonyManager mTelephonyManager;
     52     private static ConnectivityManager mCm;
     53     private static final String TAG = "android.telephony.cts.CellInfoTest";
     54     // Maximum and minimum possible RSSI values(in dbm).
     55     private static final int MAX_RSSI = -10;
     56     private static final int MIN_RSSI = -150;
     57     // Maximum and minimum possible RSSP values(in dbm).
     58     private static final int MAX_RSRP = -44;
     59     private static final int MIN_RSRP = -140;
     60     // Maximum and minimum possible RSSQ values.
     61     private static final int MAX_RSRQ = -3;
     62     private static final int MIN_RSRQ = -35;
     63     // Maximum and minimum possible RSSNR values.
     64     private static final int MAX_RSSNR = 50;
     65     private static final int MIN_RSSNR = 0;
     66     // Maximum and minimum possible CQI values.
     67     private static final int MAX_CQI = 30;
     68     private static final int MIN_CQI = 0;
     69 
     70     // The followings are parameters for testing CellIdentityCdma
     71     // Network Id ranges from 0 to 65535.
     72     private static final int NETWORK_ID  = 65535;
     73     // CDMA System Id ranges from 0 to 32767
     74     private static final int SYSTEM_ID = 32767;
     75     // Base Station Id ranges from 0 to 65535
     76     private static final int BASESTATION_ID = 65535;
     77     // Longitude ranges from -2592000 to 2592000.
     78     private static final int LONGITUDE = 2592000;
     79     // Latitude ranges from -1296000 to 1296000.
     80     private static final int LATITUDE = 1296000;
     81     // Cell identity ranges from 0 to 268435456.
     82 
     83     // The followings are parameters for testing CellIdentityLte
     84     private static final int CI = 268435456;
     85     // Physical cell id ranges from 0 to 503.
     86     private static final int PCI = 503;
     87     // Tracking area code ranges from 0 to 65535.
     88     private static final int TAC = 65535;
     89     // Absolute RF Channel Number ranges from 0 to 262140.
     90     private static final int EARFCN = 47000;
     91     private static final int BANDWIDTH_LOW = 1400;  // kHz
     92     private static final int BANDWIDTH_HIGH = 20000;  // kHz
     93 
     94     // The followings are parameters for testing CellIdentityWcdma
     95     // Location Area Code ranges from 0 to 65535.
     96     private static final int LAC = 65535;
     97     // UMTS Cell Identity ranges from 0 to 268435455.
     98     private static final int CID_UMTS = 268435455;
     99     // Primary Scrambling Coderanges from 0 to 511.
    100     private static final int PSC = 511;
    101 
    102     // The followings are parameters for testing CellIdentityGsm
    103     // GSM Cell Identity ranges from 0 to 65535.
    104     private static final int CID_GSM = 65535;
    105     // GSM Absolute RF Channel Number ranges from 0 to 65535.
    106     private static final int ARFCN = 1024;
    107 
    108     // 3gpp 36.101 Sec 5.7.2
    109     private static final int CHANNEL_RASTER_EUTRAN = 100; //kHz
    110 
    111     private PackageManager mPm;
    112 
    113     @Override
    114     protected void setUp() throws Exception {
    115         super.setUp();
    116         mTelephonyManager =
    117                 (TelephonyManager)getContext().getSystemService(Context.TELEPHONY_SERVICE);
    118         mCm = (ConnectivityManager)getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    119         mPm = getContext().getPackageManager();
    120     }
    121 
    122     public void testCellInfo() throws Throwable {
    123 
    124         if(! (mPm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY))) {
    125             Log.d(TAG, "Skipping test that requires FEATURE_TELEPHONY");
    126             return;
    127         }
    128 
    129         if (mCm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE) == null) {
    130             Log.d(TAG, "Skipping test that requires ConnectivityManager.TYPE_MOBILE");
    131             return;
    132         }
    133 
    134         // getAllCellInfo should never return null, and there should
    135         // be at least one entry.
    136         List<CellInfo> allCellInfo = mTelephonyManager.getAllCellInfo();
    137         assertNotNull("TelephonyManager.getAllCellInfo() returned NULL!", allCellInfo);
    138         assertTrue("TelephonyManager.getAllCellInfo() returned zero-length list!",
    139             allCellInfo.size() > 0);
    140 
    141         int numRegisteredCells = 0;
    142         for (CellInfo cellInfo : allCellInfo) {
    143             if (cellInfo.isRegistered()) {
    144                 ++numRegisteredCells;
    145             }
    146             if (cellInfo instanceof CellInfoLte) {
    147                 verifyLteInfo((CellInfoLte) cellInfo);
    148             } else if (cellInfo instanceof CellInfoWcdma) {
    149                 verifyWcdmaInfo((CellInfoWcdma) cellInfo);
    150             } else if (cellInfo instanceof CellInfoGsm) {
    151                 verifyGsmInfo((CellInfoGsm) cellInfo);
    152             } else if (cellInfo instanceof CellInfoCdma) {
    153                 verifyCdmaInfo((CellInfoCdma) cellInfo);
    154             }
    155         }
    156 
    157         //FIXME: The maximum needs to be calculated based on the number of
    158         //       radios and the technologies used (ex SRLTE); however, we have
    159         //       not hit any of these cases yet.
    160         assertTrue("None or too many registered cells : " + numRegisteredCells,
    161                 numRegisteredCells > 0 && numRegisteredCells <= 2);
    162     }
    163 
    164     private void verifyCdmaInfo(CellInfoCdma cdma) {
    165         verifyCellConnectionStatus(cdma.getCellConnectionStatus());
    166         verifyCellInfoCdmaParcelandHashcode(cdma);
    167         verifyCellIdentityCdma(cdma.getCellIdentity());
    168         verifyCellIdentityCdmaParcel(cdma.getCellIdentity());
    169         verifyCellSignalStrengthCdma(cdma.getCellSignalStrength());
    170         verifyCellSignalStrengthCdmaParcel(cdma.getCellSignalStrength());
    171     }
    172 
    173     private void verifyCellInfoCdmaParcelandHashcode(CellInfoCdma cdma) {
    174         Parcel p = Parcel.obtain();
    175         cdma.writeToParcel(p, 0);
    176         p.setDataPosition(0);
    177 
    178         CellInfoCdma newCi = CellInfoCdma.CREATOR.createFromParcel(p);
    179         assertTrue(cdma.equals(newCi));
    180         assertEquals("hashCode() did not get right hasdCode", cdma.hashCode(), newCi.hashCode());
    181     }
    182 
    183     private void verifyCellIdentityCdma(CellIdentityCdma cdma) {
    184         String alphaLong = (String) cdma.getOperatorAlphaLong();
    185         assertNotNull("getOperatorAlphaLong() returns NULL!", alphaLong);
    186 
    187         String alphaShort = (String) cdma.getOperatorAlphaShort();
    188         assertNotNull("getOperatorAlphaShort() returns NULL!", alphaShort);
    189 
    190         int networkId = cdma.getNetworkId();
    191         assertTrue("getNetworkId() out of range [0,65535], networkId=" + networkId,
    192                 networkId == Integer.MAX_VALUE || (networkId >= 0 && networkId <= NETWORK_ID));
    193 
    194         int systemId = cdma.getSystemId();
    195         assertTrue("getSystemId() out of range [0,32767], systemId=" + systemId,
    196                 systemId == Integer.MAX_VALUE || (systemId >= 0 && systemId <= SYSTEM_ID));
    197 
    198         int basestationId = cdma.getBasestationId();
    199         assertTrue("getBasestationId() out of range [0,65535], basestationId=" + basestationId,
    200                 basestationId == Integer.MAX_VALUE || (basestationId >= 0 && basestationId <= BASESTATION_ID));
    201 
    202         int longitude = cdma.getLongitude();
    203         assertTrue("getLongitude() out of range [-2592000,2592000], longitude=" + longitude,
    204                 longitude == Integer.MAX_VALUE || (longitude >= -LONGITUDE && longitude <= LONGITUDE));
    205 
    206         int latitude = cdma.getLatitude();
    207         assertTrue("getLatitude() out of range [-1296000,1296000], latitude=" + latitude,
    208                 latitude == Integer.MAX_VALUE || (latitude >= -LATITUDE && latitude <= LATITUDE));
    209     }
    210 
    211     private void verifyCellIdentityCdmaParcel(CellIdentityCdma cdma) {
    212         Parcel p = Parcel.obtain();
    213         cdma.writeToParcel(p, 0);
    214         p.setDataPosition(0);
    215 
    216         CellIdentityCdma newCi = CellIdentityCdma.CREATOR.createFromParcel(p);
    217         assertTrue(cdma.equals(newCi));
    218     }
    219 
    220     private void verifyCellSignalStrengthCdma(CellSignalStrengthCdma cdma) {
    221         int level = cdma.getLevel();
    222         assertTrue("getLevel() out of range [0,4], level=" + level,
    223                 level >= 0 && level <= 4);
    224 
    225         int asuLevel = cdma.getAsuLevel();
    226         assertTrue("getAsuLevel() out of range [0,97] (or 99 is unknown), asuLevel=" + asuLevel,
    227                 asuLevel == 99 || (asuLevel >= 0 && asuLevel <= 97));
    228 
    229         int cdmaLevel = cdma.getCdmaLevel();
    230         assertTrue("getCdmaLevel() out of range [0,4], cdmaLevel=" + cdmaLevel,
    231                 cdmaLevel >= 0 && cdmaLevel <= 4);
    232 
    233         int evdoLevel = cdma.getEvdoLevel();
    234         assertTrue("getEvdoLevel() out of range [0,4], evdoLevel=" + evdoLevel,
    235                 evdoLevel >= 0 && evdoLevel <= 4);
    236 
    237         // The following four fields do not have specific limits. So just calling to verify that
    238         // they don't crash the phone.
    239         int cdmaDbm = cdma.getCdmaDbm();
    240         int evdoDbm = cdma.getEvdoDbm();
    241         cdma.getCdmaEcio();
    242         cdma.getEvdoEcio();
    243 
    244         int dbm = (cdmaDbm < evdoDbm) ? cdmaDbm : evdoDbm;
    245         assertEquals("getDbm() did not get correct value", dbm, cdma.getDbm());
    246 
    247         int evdoSnr = cdma.getEvdoSnr();
    248         assertTrue("getEvdoSnr() out of range [0,8], evdoSnr=" + evdoSnr,
    249                 (evdoSnr == Integer.MAX_VALUE) || (evdoSnr >= 0 && evdoSnr <= 8));
    250     }
    251 
    252     private void verifyCellSignalStrengthCdmaParcel(CellSignalStrengthCdma cdma) {
    253         Parcel p = Parcel.obtain();
    254         cdma.writeToParcel(p, 0);
    255         p.setDataPosition(0);
    256 
    257         CellSignalStrengthCdma newCss = CellSignalStrengthCdma.CREATOR.createFromParcel(p);
    258         assertEquals(cdma, newCss);
    259     }
    260 
    261     // Verify lte cell information is within correct range.
    262     private void verifyLteInfo(CellInfoLte lte) {
    263         verifyCellConnectionStatus(lte.getCellConnectionStatus());
    264         verifyCellInfoLteParcelandHashcode(lte);
    265         verifyCellIdentityLte(lte.getCellIdentity());
    266         verifyCellIdentityLteParcel(lte.getCellIdentity());
    267         verifyCellSignalStrengthLte(lte.getCellSignalStrength());
    268         verifyCellSignalStrengthLteParcel(lte.getCellSignalStrength());
    269     }
    270 
    271     private void verifyCellInfoLteParcelandHashcode(CellInfoLte lte) {
    272         Parcel p = Parcel.obtain();
    273         lte.writeToParcel(p, 0);
    274         p.setDataPosition(0);
    275 
    276         CellInfoLte newCi = CellInfoLte.CREATOR.createFromParcel(p);
    277         assertTrue(lte.equals(newCi));
    278         assertEquals("hashCode() did not get right hasdCode", lte.hashCode(), newCi.hashCode());
    279     }
    280 
    281 
    282     private void verifyCellIdentityLte(CellIdentityLte lte) {
    283         int mcc = lte.getMcc();
    284         // getMcc() returns Integer.MAX_VALUE if mccStr is null.
    285         assertTrue("getMcc() out of range [0, 999], mcc=" + mcc,
    286                 (mcc >= 0 && mcc <= 999) || mcc == Integer.MAX_VALUE);
    287 
    288         int mnc = lte.getMnc();
    289         // getMnc() returns Integer.MAX_VALUE if mccStr is null.
    290         assertTrue("getMnc() out of range [0, 999], mnc=" + mnc,
    291                 (mnc >= 0 && mnc <= 999) || mnc == Integer.MAX_VALUE);
    292 
    293         // Cell identity ranges from 0 to 268435456.
    294         int ci = lte.getCi();
    295         assertTrue("getCi() out of range [0,268435456], ci=" + ci,
    296                 (ci == Integer.MAX_VALUE) || (ci >= 0 && ci <= CI));
    297 
    298         // Verify LTE physical cell id information.
    299         // Only physical cell id is available for LTE neighbor.
    300         int pci = lte.getPci();
    301         // Physical cell id should be within [0, 503].
    302         assertTrue("getPci() out of range [0, 503], pci=" + pci,
    303                 (pci== Integer.MAX_VALUE) || (pci >= 0 && pci <= PCI));
    304 
    305         // Tracking area code ranges from 0 to 65535.
    306         int tac = lte.getTac();
    307         assertTrue("getTac() out of range [0,65535], tac=" + tac,
    308                 (tac == Integer.MAX_VALUE) || (tac >= 0 && tac <= TAC));
    309 
    310         int bw = lte.getBandwidth();
    311         assertTrue("getBandwidth out of range [1400, 20000] | Integer.Max_Value, bw=",
    312                 bw == Integer.MAX_VALUE || bw >= BANDWIDTH_LOW && bw <= BANDWIDTH_HIGH);
    313 
    314         int earfcn = lte.getEarfcn();
    315         // Reference 3GPP 36.101 Table 5.7.3-1
    316         // As per NOTE 1 in the table, although 0-6 are valid channel numbers for
    317         // LTE, the reported EARFCN is the center frequency, rendering these channels
    318         // out of the range of the narrowest 1.4Mhz deployment.
    319         int minEarfcn = 7;
    320         if (bw != Integer.MAX_VALUE) {
    321             // The number of channels used by a cell is equal to the cell bandwidth divided
    322             // by the channel raster (bandwidth of a channel). The center channel is the channel
    323             // the n/2-th channel where n is the number of channels, and since it is the center
    324             // channel that is reported as the channel number for a cell, we can exclude any channel
    325             // numbers within a band that would place the bottom of a cell's bandwidth below the
    326             // edge of the band. For channel numbers in Band 1, the EARFCN numbering starts from
    327             // channel 0, which means that we can exclude from the valid range channels starting
    328             // from 0 and numbered less than half the total number of channels occupied by a cell.
    329             minEarfcn = bw / CHANNEL_RASTER_EUTRAN / 2;
    330         }
    331         assertTrue("getEarfcn() out of range [7,47000], earfcn=" + earfcn,
    332                 earfcn == Integer.MAX_VALUE || (earfcn >= minEarfcn && earfcn <= EARFCN));
    333 
    334         String mccStr = lte.getMccString();
    335         // mccStr is set as NULL if empty, unknown or invalid.
    336         assertTrue("getMccString() out of range [0, 999], mcc=" + mccStr,
    337                 mccStr == null || mccStr.matches("^[0-9]{3}$"));
    338 
    339         String mncStr = lte.getMncString();
    340         // mncStr is set as NULL if empty, unknown or invalid.
    341         assertTrue("getMncString() out of range [0, 999], mnc=" + mncStr,
    342                 mncStr == null || mncStr.matches("^[0-9]{2,3}$"));
    343 
    344         String mobileNetworkOperator = lte.getMobileNetworkOperator();
    345         assertTrue("getMobileNetworkOperator() out of range [0, 999999], mobileNetworkOperator="
    346                         + mobileNetworkOperator,
    347                 mobileNetworkOperator == null
    348                         || mobileNetworkOperator.matches("^[0-9]{5,6}$"));
    349 
    350         String alphaLong = (String) lte.getOperatorAlphaLong();
    351         assertNotNull("getOperatorAlphaLong() returns NULL!", alphaLong);
    352 
    353         String alphaShort = (String) lte.getOperatorAlphaShort();
    354         assertNotNull("getOperatorAlphaShort() returns NULL!", alphaShort);
    355     }
    356 
    357     private void verifyCellIdentityLteParcel(CellIdentityLte lte) {
    358         Parcel p = Parcel.obtain();
    359         lte.writeToParcel(p, 0);
    360         p.setDataPosition(0);
    361 
    362         CellIdentityLte newci = CellIdentityLte.CREATOR.createFromParcel(p);
    363         assertEquals(lte, newci);
    364     }
    365 
    366     private void verifyCellSignalStrengthLte(CellSignalStrengthLte cellSignalStrengthLte) {
    367         verifyRssiDbm(cellSignalStrengthLte.getDbm());
    368 
    369         //Integer.MAX_VALUE indicates an unavailable field
    370         int rsrp = cellSignalStrengthLte.getRsrp();
    371         // RSRP is being treated as RSSI in LTE (they are similar but not quite right)
    372         // so reusing the constants here.
    373         assertTrue("getRsrp() out of range, rsrp=" + rsrp, rsrp >= MIN_RSRP && rsrp <= MAX_RSRP);
    374 
    375         int rsrq = cellSignalStrengthLte.getRsrq();
    376         assertTrue("getRsrq() out of range | Integer.MAX_VALUE, rsrq=" + rsrq,
    377             rsrq == Integer.MAX_VALUE || (rsrq >= MIN_RSRQ && rsrq <= MAX_RSRQ));
    378 
    379         int rssnr = cellSignalStrengthLte.getRssnr();
    380         assertTrue("getRssnr() out of range | Integer.MAX_VALUE, rssnr=" + rssnr,
    381             rssnr == Integer.MAX_VALUE || (rssnr >= MIN_RSSNR && rssnr <= MAX_RSSNR));
    382 
    383         int cqi = cellSignalStrengthLte.getCqi();
    384         assertTrue("getCqi() out of range | Integer.MAX_VALUE, cqi=" + cqi,
    385             cqi == Integer.MAX_VALUE || (cqi >= MIN_CQI && cqi <= MAX_CQI));
    386 
    387         int ta = cellSignalStrengthLte.getTimingAdvance();
    388         assertTrue("getTimingAdvance() invalid [0-1282] | Integer.MAX_VALUE, ta=" + ta,
    389                 ta == Integer.MAX_VALUE || (ta >= 0 && ta <=1282));
    390 
    391         int level = cellSignalStrengthLte.getLevel();
    392         assertTrue("getLevel() out of range [0,4], level=" + level, level >= 0 && level <= 4);
    393 
    394         int asuLevel = cellSignalStrengthLte.getAsuLevel();
    395         assertTrue("getAsuLevel() out of range [0,97] (or 99 is unknown), asuLevel=" + asuLevel,
    396                 (asuLevel == 99) || (asuLevel >= 0 && asuLevel <= 97));
    397 
    398         int timingAdvance = cellSignalStrengthLte.getTimingAdvance();
    399         assertTrue("getTimingAdvance() out of range [0,1282], timingAdvance=" + timingAdvance,
    400                 timingAdvance == Integer.MAX_VALUE || (timingAdvance >= 0 && timingAdvance <= 1282));
    401     }
    402 
    403     private void verifyCellSignalStrengthLteParcel(CellSignalStrengthLte cellSignalStrengthLte) {
    404         Parcel p = Parcel.obtain();
    405         cellSignalStrengthLte.writeToParcel(p, 0);
    406         p.setDataPosition(0);
    407 
    408         CellSignalStrengthLte newCss = CellSignalStrengthLte.CREATOR.createFromParcel(p);
    409         assertEquals(cellSignalStrengthLte, newCss);
    410     }
    411 
    412     // Verify wcdma cell information is within correct range.
    413     private void verifyWcdmaInfo(CellInfoWcdma wcdma) {
    414         verifyCellConnectionStatus(wcdma.getCellConnectionStatus());
    415         verifyCellInfoWcdmaParcelandHashcode(wcdma);
    416         verifyCellIdentityWcdma(wcdma.getCellIdentity());
    417         verifyCellIdentityWcdmaParcel(wcdma.getCellIdentity());
    418         verifyCellSignalStrengthWcdma(wcdma.getCellSignalStrength());
    419         verifyCellSignalStrengthWcdmaParcel(wcdma.getCellSignalStrength());
    420     }
    421 
    422     private void verifyCellInfoWcdmaParcelandHashcode(CellInfoWcdma wcdma) {
    423         Parcel p = Parcel.obtain();
    424         wcdma.writeToParcel(p, 0);
    425         p.setDataPosition(0);
    426 
    427         CellInfoWcdma newCi = CellInfoWcdma.CREATOR.createFromParcel(p);
    428         assertTrue(wcdma.equals(newCi));
    429         assertEquals("hashCode() did not get right hasdCode", wcdma.hashCode(), newCi.hashCode());
    430     }
    431 
    432     private void verifyCellIdentityWcdma(CellIdentityWcdma wcdma) {
    433         int mcc = wcdma.getMcc();
    434         // getMcc() returns Integer.MAX_VALUE if mccStr is null.
    435         assertTrue("getMcc() out of range [0, 999], mcc=" + mcc,
    436                 (mcc >= 0 && mcc <= 999) || mcc == Integer.MAX_VALUE);
    437 
    438         int mnc = wcdma.getMnc();
    439         // getMnc() returns Integer.MAX_VALUE if mccStr is null.
    440         assertTrue("getMnc() out of range [0, 999], mnc=" + mnc,
    441                 (mnc >= 0 && mnc <= 999) || mnc == Integer.MAX_VALUE);
    442 
    443         int lac = wcdma.getLac();
    444         assertTrue("getLac() out of range [0, 65535], lac=" + lac,
    445                 (lac >= 0 && lac <= LAC) || lac == Integer.MAX_VALUE);
    446 
    447         int cid = wcdma.getCid();
    448         assertTrue("getCid() out of range [0, 268435455], cid=" + cid,
    449                 (cid >= 0 && cid <= CID_UMTS) || cid == Integer.MAX_VALUE);
    450 
    451         // Verify wcdma primary scrambling code information.
    452         // Primary scrambling code should be within [0, 511].
    453         int psc = wcdma.getPsc();
    454         assertTrue("getPsc() out of range [0, 511], psc=" + psc,
    455                 (psc >= 0 && psc <= PSC) || psc == Integer.MAX_VALUE);
    456 
    457         String mccStr = wcdma.getMccString();
    458         // mccStr is set as NULL if empty, unknown or invalid.
    459         assertTrue("getMccString() out of range [0, 999], mcc=" + mccStr,
    460                 mccStr == null || mccStr.matches("^[0-9]{3}$"));
    461 
    462         String mncStr = wcdma.getMncString();
    463         // mncStr is set as NULL if empty, unknown or invalid.
    464         assertTrue("getMncString() out of range [0, 999], mnc=" + mncStr,
    465                 mncStr == null || mncStr.matches("^[0-9]{2,3}$"));
    466 
    467         String mobileNetworkOperator = wcdma.getMobileNetworkOperator();
    468         assertTrue("getMobileNetworkOperator() out of range [0, 999999], mobileNetworkOperator="
    469                         + mobileNetworkOperator,
    470                 mobileNetworkOperator == null
    471                         || mobileNetworkOperator.matches("^[0-9]{5,6}$"));
    472 
    473         int uarfcn = wcdma.getUarfcn();
    474         // Reference 3GPP 25.101 Table 5.2
    475         // From Appendix E.1, even though UARFCN is numbered from 400, the minumum
    476         // usable channel is 412 due to the fixed bandwidth of 5Mhz
    477         assertTrue("getUarfcn() out of range [412,11000], uarfcn=" + uarfcn,
    478                 uarfcn >= 412 && uarfcn <= 11000);
    479 
    480         String alphaLong = (String) wcdma.getOperatorAlphaLong();
    481         assertNotNull("getOperatorAlphaLong() returns NULL!", alphaLong);
    482 
    483         String alphaShort = (String) wcdma.getOperatorAlphaShort();
    484         assertNotNull("getOperatorAlphaShort() returns NULL!", alphaShort);
    485     }
    486 
    487     private void verifyCellIdentityWcdmaParcel(CellIdentityWcdma wcdma) {
    488         Parcel p = Parcel.obtain();
    489         wcdma.writeToParcel(p, 0);
    490         p.setDataPosition(0);
    491 
    492         CellIdentityWcdma newci = CellIdentityWcdma.CREATOR.createFromParcel(p);
    493         assertEquals(wcdma, newci);
    494     }
    495 
    496     private void verifyCellSignalStrengthWcdma(CellSignalStrengthWcdma wcdma) {
    497         verifyRssiDbm(wcdma.getDbm());
    498 
    499         // Dbm here does not have specific limits. So just calling to verify that it does not crash
    500         // the phone
    501         wcdma.getDbm();
    502 
    503         int asuLevel = wcdma.getAsuLevel();
    504         assertTrue("getLevel() out of range [0,31] (or 99 is unknown), level=" + asuLevel,
    505                 asuLevel == 99 || (asuLevel >= 0 && asuLevel <= 31));
    506 
    507         int level = wcdma.getLevel();
    508         assertTrue("getLevel() out of range [0,4], level=" + level, level >= 0 && level <= 4);
    509     }
    510 
    511     private void verifyCellSignalStrengthWcdmaParcel(CellSignalStrengthWcdma wcdma) {
    512         Parcel p = Parcel.obtain();
    513         wcdma.writeToParcel(p, 0);
    514         p.setDataPosition(0);
    515 
    516         CellSignalStrengthWcdma newCss = CellSignalStrengthWcdma.CREATOR.createFromParcel(p);
    517         assertEquals(wcdma, newCss);
    518     }
    519 
    520     // Verify gsm cell information is within correct range.
    521     private void verifyGsmInfo(CellInfoGsm gsm) {
    522         verifyCellConnectionStatus(gsm.getCellConnectionStatus());
    523         verifyCellInfoWcdmaParcelandHashcode(gsm);
    524         verifyCellIdentityGsm(gsm.getCellIdentity());
    525         verifyCellIdentityGsmParcel(gsm.getCellIdentity());
    526         verifyCellSignalStrengthGsm(gsm.getCellSignalStrength());
    527         verifyCellSignalStrengthGsmParcel(gsm.getCellSignalStrength());
    528     }
    529 
    530     private void verifyCellInfoWcdmaParcelandHashcode(CellInfoGsm gsm) {
    531         Parcel p = Parcel.obtain();
    532         gsm.writeToParcel(p, 0);
    533         p.setDataPosition(0);
    534 
    535         CellInfoGsm newCi = CellInfoGsm.CREATOR.createFromParcel(p);
    536         assertTrue(gsm.equals(newCi));
    537         assertEquals("hashCode() did not get right hasdCode", gsm.hashCode(), newCi.hashCode());
    538     }
    539 
    540     private void verifyCellIdentityGsm(CellIdentityGsm gsm) {
    541         // Local area code and cellid should be with [0, 65535].
    542         int lac = gsm.getLac();
    543         assertTrue("getLac() out of range [0, 65535], lac=" + lac,
    544                 lac == Integer.MAX_VALUE || (lac >= 0 && lac <= LAC));
    545         int cid = gsm.getCid();
    546         assertTrue("getCid() out range [0, 65535], cid=" + cid,
    547                 cid== Integer.MAX_VALUE || (cid >= 0 && cid <= CID_GSM));
    548 
    549         int arfcn = gsm.getArfcn();
    550         // Reference 3GPP 45.005 Table 2-2
    551         assertTrue("getArfcn() out of range [0,1024], arfcn=" + arfcn,
    552                 arfcn == Integer.MAX_VALUE || (arfcn >= 0 && arfcn <= ARFCN));
    553 
    554         String alphaLong = (String) gsm.getOperatorAlphaLong();
    555         assertNotNull("getOperatorAlphaLong() returns NULL!", alphaLong);
    556 
    557         String alphaShort = (String) gsm.getOperatorAlphaShort();
    558         assertNotNull("getOperatorAlphaShort() returns NULL!", alphaShort);
    559 
    560         String mccStr = gsm.getMccString();
    561         // mccStr is set as NULL if empty, unknown or invalid.
    562         assertTrue("getMccString() out of range [0, 999], mcc=" + mccStr,
    563                 mccStr == null || mccStr.matches("^[0-9]{3}$"));
    564         String mncStr = gsm.getMncString();
    565         // mncStr is set as NULL if empty, unknown or invalid.
    566         assertTrue("getMncString() out of range [0, 999], mnc=" + mncStr,
    567                 mncStr == null || mncStr.matches("^[0-9]{2,3}$"));
    568 
    569         String mobileNetworkOperator = gsm.getMobileNetworkOperator();
    570         assertTrue("getMobileNetworkOperator() out of range [0, 999999], mobileNetworkOperator="
    571                         + mobileNetworkOperator,
    572                 mobileNetworkOperator == null
    573                         || mobileNetworkOperator.matches("^[0-9]{5,6}$"));
    574 
    575         int mcc = gsm.getMcc();
    576         // getMcc() returns Integer.MAX_VALUE if mccStr is null.
    577         assertTrue("getMcc() out of range [0, 999], mcc=" + mcc,
    578                 (mcc >= 0 && mcc <= 999) || mcc == Integer.MAX_VALUE);
    579         int mnc = gsm.getMnc();
    580         // getMnc() returns Integer.MAX_VALUE if mccStr is null.
    581         assertTrue("getMnc() out of range [0, 999], mnc=" + mnc,
    582                 (mnc >= 0 && mnc <= 999) || mnc == Integer.MAX_VALUE);
    583 
    584         int bsic = gsm.getBsic();
    585         // TODO(b/32774471) - Bsic should always be valid
    586         //assertTrue("getBsic() out of range [0,63]", bsic >= 0 && bsic <=63);
    587     }
    588 
    589     private void verifyCellIdentityGsmParcel(CellIdentityGsm gsm) {
    590         Parcel p = Parcel.obtain();
    591         gsm.writeToParcel(p, 0);
    592         p.setDataPosition(0);
    593 
    594         CellIdentityGsm newci = CellIdentityGsm.CREATOR.createFromParcel(p);
    595         assertEquals(gsm, newci);
    596     }
    597 
    598     private void verifyCellSignalStrengthGsm(CellSignalStrengthGsm gsm) {
    599         verifyRssiDbm(gsm.getDbm());
    600 
    601         int level = gsm.getLevel();
    602         assertTrue("getLevel() out of range [0,4], level=" + level, level >= 0 && level <= 4);
    603 
    604         int ta = gsm.getTimingAdvance();
    605         assertTrue("getTimingAdvance() out of range [0,219] | Integer.MAX_VALUE, ta=" + ta,
    606                 ta == Integer.MAX_VALUE || (ta >= 0 && ta <= 219));
    607 
    608         // Dbm here does not have specific limits. So just calling to verify that it does not
    609         // crash the phone
    610         gsm.getDbm();
    611 
    612         int asuLevel = gsm.getAsuLevel();
    613         assertTrue("getLevel() out of range [0,31] (or 99 is unknown), level=" + asuLevel,
    614                 asuLevel == 99 || (asuLevel >=0 && asuLevel <= 31));
    615     }
    616 
    617     private void verifyCellSignalStrengthGsmParcel(CellSignalStrengthGsm gsm) {
    618         Parcel p = Parcel.obtain();
    619         gsm.writeToParcel(p, 0);
    620         p.setDataPosition(0);
    621 
    622         CellSignalStrengthGsm newCss = CellSignalStrengthGsm.CREATOR.createFromParcel(p);
    623         assertEquals(gsm, newCss);
    624     }
    625 
    626     // Rssi(in dbm) should be within [MIN_RSSI, MAX_RSSI].
    627     private void verifyRssiDbm(int dbm) {
    628         assertTrue("getCellSignalStrength().getDbm() out of range, dbm=" + dbm,
    629                 dbm >= MIN_RSSI && dbm <= MAX_RSSI);
    630     }
    631 
    632     private void verifyCellConnectionStatus(int status) {
    633         assertTrue("getCellConnectionStatus() invalid [0,2] | Integer.MAX_VALUE, status=",
    634             status == CellInfo.CONNECTION_NONE
    635                 || status == CellInfo.CONNECTION_PRIMARY_SERVING
    636                 || status == CellInfo.CONNECTION_SECONDARY_SERVING
    637                 || status == CellInfo.CONNECTION_UNKNOWN);
    638     }
    639 }
    640