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