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 17package com.android.internal.telephony; 18 19import android.os.Parcel; 20import android.telephony.CellIdentity; 21import android.telephony.CellIdentityWcdma; 22import android.test.AndroidTestCase; 23import android.test.suitebuilder.annotation.SmallTest; 24 25/** Unit tests for {@link CellIdentityWcdma}. */ 26 27public class CellIdentityWcdmaTest extends AndroidTestCase { 28 29 // Location Area Code ranges from 0 to 65535. 30 private static final int LAC = 65535; 31 // UMTS Cell Identity ranges from 0 to 268435455. 32 private static final int CID = 268435455; 33 // Primary Scrambling Coderanges from 0 to 511. 34 private static final int PSC = 511; 35 // UMTS Absolute RF Channel Number ranges from 0 to 65535. 36 private static final int UARFCN = 65535; 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 @SmallTest 45 public void testDefaultConstructor() { 46 CellIdentityWcdma ci = 47 new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, MNC_STR, 48 ALPHA_LONG, ALPHA_SHORT); 49 50 assertEquals(LAC, ci.getLac()); 51 assertEquals(CID, ci.getCid()); 52 assertEquals(PSC, ci.getPsc()); 53 assertEquals(UARFCN, ci.getUarfcn()); 54 assertEquals(UARFCN, ci.getChannelNumber()); 55 assertEquals(MCC, ci.getMcc()); 56 assertEquals(MNC, ci.getMnc()); 57 assertEquals(MCC_STR, ci.getMccString()); 58 assertEquals(MNC_STR, ci.getMncString()); 59 assertEquals(MCC_STR + MNC_STR, ci.getMobileNetworkOperator()); 60 assertEquals(ALPHA_LONG, ci.getOperatorAlphaLong()); 61 assertEquals(ALPHA_SHORT, ci.getOperatorAlphaShort()); 62 } 63 64 @SmallTest 65 public void testConstructorWithThreeDigitMnc() { 66 final String mncWithThreeDigit = "061"; 67 CellIdentityWcdma ci = 68 new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, mncWithThreeDigit, 69 ALPHA_LONG, ALPHA_SHORT); 70 71 assertEquals(MCC, ci.getMcc()); 72 assertEquals(61, ci.getMnc()); 73 assertEquals(MCC_STR, ci.getMccString()); 74 assertEquals(mncWithThreeDigit, ci.getMncString()); 75 assertEquals(MCC_STR + mncWithThreeDigit, ci.getMobileNetworkOperator()); 76 } 77 78 @SmallTest 79 public void testConstructorWithTwoDigitMnc() { 80 final String mncWithTwoDigit = "61"; 81 CellIdentityWcdma ci = 82 new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, mncWithTwoDigit, 83 ALPHA_LONG, ALPHA_SHORT); 84 85 assertEquals(MCC, ci.getMcc()); 86 assertEquals(61, ci.getMnc()); 87 assertEquals(MCC_STR, ci.getMccString()); 88 assertEquals(mncWithTwoDigit, ci.getMncString()); 89 assertEquals(MCC_STR + mncWithTwoDigit, ci.getMobileNetworkOperator()); 90 } 91 92 @SmallTest 93 public void testConstructorWithEmptyMccMnc() { 94 final String integerMaxValue = String.valueOf(Integer.MAX_VALUE); 95 CellIdentityWcdma ci = 96 new CellIdentityWcdma(LAC, CID, PSC, UARFCN, 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 CellIdentityWcdma(LAC, CID, PSC, UARFCN, 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 CellIdentityWcdma(LAC, CID, PSC, UARFCN, 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 CellIdentityWcdma(LAC, CID, PSC, UARFCN, "", "", 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 CellIdentityWcdma ci = 132 new CellIdentityWcdma(MCC, MNC, LAC, CID, PSC); 133 134 assertEquals(LAC, ci.getLac()); 135 assertEquals(CID, ci.getCid()); 136 assertEquals(PSC, ci.getPsc()); 137 assertEquals(MCC, ci.getMcc()); 138 assertEquals(MNC, ci.getMnc()); 139 assertEquals(MCC_STR, ci.getMccString()); 140 assertEquals(MNC_STR, ci.getMncString()); 141 assertEquals(MCC_STR + MNC_STR, ci.getMobileNetworkOperator()); 142 assertNull(ci.getOperatorAlphaLong()); 143 assertNull(ci.getOperatorAlphaShort()); 144 } 145 146 @SmallTest 147 public void testEquals() { 148 CellIdentityWcdma ciA = new CellIdentityWcdma( 149 LAC, CID, PSC, UARFCN, MCC_STR, MNC_STR, ALPHA_LONG, ALPHA_SHORT); 150 CellIdentityWcdma ciB = new CellIdentityWcdma( 151 LAC, CID, PSC, UARFCN, MCC_STR, MNC_STR, ALPHA_LONG, ALPHA_SHORT); 152 153 assertTrue(ciA.equals(ciB)); 154 155 ciA = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, ALPHA_LONG, ALPHA_SHORT); 156 ciB = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, ALPHA_LONG, ALPHA_SHORT); 157 158 assertTrue(ciA.equals(ciB)); 159 160 ciA = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, null, ALPHA_LONG, ALPHA_SHORT); 161 ciB = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, ALPHA_LONG, ALPHA_SHORT); 162 163 assertFalse(ciA.equals(ciB)); 164 } 165 166 @SmallTest 167 public void testParcel() { 168 CellIdentityWcdma ci = 169 new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, MNC_STR, 170 ALPHA_LONG, ALPHA_SHORT); 171 172 Parcel p = Parcel.obtain(); 173 ci.writeToParcel(p, 0); 174 p.setDataPosition(0); 175 176 CellIdentityWcdma newCi = CellIdentityWcdma.CREATOR.createFromParcel(p); 177 assertEquals(ci, newCi); 178 } 179 180 @SmallTest 181 public void testParcelWithUnknowMccMnc() { 182 CellIdentityWcdma ci = 183 new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, ALPHA_LONG, ALPHA_SHORT); 184 185 Parcel p = Parcel.obtain(); 186 p.writeInt(CellIdentity.TYPE_WCDMA); 187 p.writeString(String.valueOf(Integer.MAX_VALUE)); 188 p.writeString(String.valueOf(Integer.MAX_VALUE)); 189 p.writeString(ALPHA_LONG); 190 p.writeString(ALPHA_SHORT); 191 p.writeInt(LAC); 192 p.writeInt(CID); 193 p.writeInt(PSC); 194 p.writeInt(UARFCN); 195 p.setDataPosition(0); 196 197 CellIdentityWcdma newCi = CellIdentityWcdma.CREATOR.createFromParcel(p); 198 assertEquals(ci, newCi); 199 } 200 201 @SmallTest 202 public void testParcelWithInvalidMccMnc() { 203 final String invalidMcc = "randomStuff"; 204 final String invalidMnc = "randomStuff"; 205 CellIdentityWcdma ci = 206 new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, ALPHA_LONG, ALPHA_SHORT); 207 208 Parcel p = Parcel.obtain(); 209 p.writeInt(CellIdentity.TYPE_WCDMA); 210 p.writeString(invalidMcc); 211 p.writeString(invalidMnc); 212 p.writeString(ALPHA_LONG); 213 p.writeString(ALPHA_SHORT); 214 p.writeInt(LAC); 215 p.writeInt(CID); 216 p.writeInt(PSC); 217 p.writeInt(UARFCN); 218 p.setDataPosition(0); 219 220 CellIdentityWcdma newCi = CellIdentityWcdma.CREATOR.createFromParcel(p); 221 assertEquals(ci, newCi); 222 } 223} 224