CellIdentityWcdmaTest.java revision 7aa0254d5d8f08163b3702a2a1c9602366f568d8
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(MCC, ci.getMcc());
54        assertEquals(MNC, ci.getMnc());
55        assertEquals(MCC_STR, ci.getMccStr());
56        assertEquals(MNC_STR, ci.getMncStr());
57        assertEquals(MCC_STR + MNC_STR, ci.getMobileNetworkOperator());
58        assertEquals(ALPHA_LONG, ci.getOperatorAlphaLong());
59        assertEquals(ALPHA_SHORT, ci.getOperatorAlphaShort());
60    }
61
62    @SmallTest
63    public void testConstructorWithThreeDigitMnc() {
64        final String mncWithThreeDigit = "061";
65        CellIdentityWcdma ci =
66                new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, mncWithThreeDigit,
67                        ALPHA_LONG, ALPHA_SHORT);
68
69        assertEquals(MCC, ci.getMcc());
70        assertEquals(61, ci.getMnc());
71        assertEquals(MCC_STR, ci.getMccStr());
72        assertEquals(mncWithThreeDigit, ci.getMncStr());
73        assertEquals(MCC_STR + mncWithThreeDigit, ci.getMobileNetworkOperator());
74    }
75
76    @SmallTest
77    public void testConstructorWithTwoDigitMnc() {
78        final String mncWithTwoDigit = "61";
79        CellIdentityWcdma ci =
80                new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, mncWithTwoDigit,
81                        ALPHA_LONG, ALPHA_SHORT);
82
83        assertEquals(MCC, ci.getMcc());
84        assertEquals(61, ci.getMnc());
85        assertEquals(MCC_STR, ci.getMccStr());
86        assertEquals(mncWithTwoDigit, ci.getMncStr());
87        assertEquals(MCC_STR + mncWithTwoDigit, ci.getMobileNetworkOperator());
88    }
89
90    @SmallTest
91    public void testConstructorWithEmptyMccMnc() {
92        final String integerMaxValue = String.valueOf(Integer.MAX_VALUE);
93        CellIdentityWcdma ci =
94                new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, ALPHA_LONG, ALPHA_SHORT);
95
96        assertEquals(Integer.MAX_VALUE, ci.getMcc());
97        assertEquals(Integer.MAX_VALUE, ci.getMnc());
98        assertNull(ci.getMccStr());
99        assertNull(ci.getMncStr());
100        assertNull(ci.getMobileNetworkOperator());
101
102        ci = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, null, ALPHA_LONG, ALPHA_SHORT);
103
104        assertEquals(MCC, ci.getMcc());
105        assertEquals(Integer.MAX_VALUE, ci.getMnc());
106        assertEquals(MCC_STR, ci.getMccStr());
107        assertNull(ci.getMncStr());
108        assertNull(ci.getMobileNetworkOperator());
109
110        ci = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, MNC_STR, ALPHA_LONG, ALPHA_SHORT);
111
112        assertEquals(MNC, ci.getMnc());
113        assertEquals(Integer.MAX_VALUE, ci.getMcc());
114        assertEquals(MNC_STR, ci.getMncStr());
115        assertNull(ci.getMccStr());
116        assertNull(ci.getMobileNetworkOperator());
117
118        ci = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, "", "", ALPHA_LONG, ALPHA_SHORT);
119
120        assertEquals(Integer.MAX_VALUE, ci.getMcc());
121        assertEquals(Integer.MAX_VALUE, ci.getMnc());
122        assertNull(ci.getMccStr());
123        assertNull(ci.getMncStr());
124        assertNull(ci.getMobileNetworkOperator());
125    }
126
127    @SmallTest
128    public void testFormerConstructor() {
129        CellIdentityWcdma ci =
130                new CellIdentityWcdma(MCC, MNC, LAC, CID, PSC);
131
132        assertEquals(LAC, ci.getLac());
133        assertEquals(CID, ci.getCid());
134        assertEquals(PSC, ci.getPsc());
135        assertEquals(MCC, ci.getMcc());
136        assertEquals(MNC, ci.getMnc());
137        assertEquals(MCC_STR, ci.getMccStr());
138        assertEquals(MNC_STR, ci.getMncStr());
139        assertEquals(MCC_STR + MNC_STR, ci.getMobileNetworkOperator());
140        assertNull(ci.getOperatorAlphaLong());
141        assertNull(ci.getOperatorAlphaShort());
142    }
143
144    @SmallTest
145    public void testEquals() {
146        CellIdentityWcdma ciA = new CellIdentityWcdma(
147                LAC, CID, PSC, UARFCN, MCC_STR, MNC_STR, ALPHA_LONG, ALPHA_SHORT);
148        CellIdentityWcdma ciB = new CellIdentityWcdma(
149                LAC, CID, PSC, UARFCN, MCC_STR, MNC_STR, ALPHA_LONG, ALPHA_SHORT);
150
151        assertTrue(ciA.equals(ciB));
152
153        ciA = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, ALPHA_LONG, ALPHA_SHORT);
154        ciB = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, ALPHA_LONG, ALPHA_SHORT);
155
156        assertTrue(ciA.equals(ciB));
157
158        ciA = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, null, ALPHA_LONG, ALPHA_SHORT);
159        ciB = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, ALPHA_LONG, ALPHA_SHORT);
160
161        assertFalse(ciA.equals(ciB));
162    }
163
164    @SmallTest
165    public void testParcel() {
166        CellIdentityWcdma ci =
167                new CellIdentityWcdma(LAC, CID, PSC, UARFCN, MCC_STR, MNC_STR,
168                        ALPHA_LONG, ALPHA_SHORT);
169
170        Parcel p = Parcel.obtain();
171        ci.writeToParcel(p, 0);
172        p.setDataPosition(0);
173
174        CellIdentityWcdma newCi = CellIdentityWcdma.CREATOR.createFromParcel(p);
175        assertEquals(ci, newCi);
176    }
177
178    @SmallTest
179    public void testParcelWithUnknowMccMnc() {
180        CellIdentityWcdma ci = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, null, null);
181
182        Parcel p = Parcel.obtain();
183        p.writeInt(CellIdentity.TYPE_WCDMA);
184        p.writeString(String.valueOf(Integer.MAX_VALUE));
185        p.writeString(String.valueOf(Integer.MAX_VALUE));
186        p.writeInt(LAC);
187        p.writeInt(CID);
188        p.writeInt(PSC);
189        p.writeInt(UARFCN);
190        p.setDataPosition(0);
191
192        CellIdentityWcdma newCi = CellIdentityWcdma.CREATOR.createFromParcel(p);
193        assertEquals(ci, newCi);
194    }
195
196    @SmallTest
197    public void testParcelWithInvalidMccMnc() {
198        final String invalidMcc = "randomStuff";
199        final String invalidMnc = "randomStuff";
200        CellIdentityWcdma ci = new CellIdentityWcdma(LAC, CID, PSC, UARFCN, null, null, null, null);
201
202        Parcel p = Parcel.obtain();
203        p.writeInt(CellIdentity.TYPE_WCDMA);
204        p.writeString(invalidMcc);
205        p.writeString(invalidMnc);
206        p.writeInt(LAC);
207        p.writeInt(CID);
208        p.writeInt(PSC);
209        p.writeInt(UARFCN);
210        p.setDataPosition(0);
211
212        CellIdentityWcdma newCi = CellIdentityWcdma.CREATOR.createFromParcel(p);
213        assertEquals(ci, newCi);
214    }
215}
216