1/*
2 * Copyright (C) 2016 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.ServiceState;
21import android.telephony.TelephonyManager;
22import android.test.suitebuilder.annotation.SmallTest;
23import android.util.Pair;
24
25import junit.framework.TestCase;
26
27import java.util.ArrayList;
28
29public class ServiceStateTest extends TestCase {
30
31    @SmallTest
32    public void testRoaming() {
33        ServiceState ss = new ServiceState();
34
35        ss.setCdmaDefaultRoamingIndicator(1);
36        assertEquals(1, ss.getCdmaDefaultRoamingIndicator());
37
38        ss.setCdmaEriIconIndex(2);
39        assertEquals(2, ss.getCdmaEriIconIndex());
40
41        ss.setCdmaEriIconMode(3);
42        assertEquals(3, ss.getCdmaEriIconMode());
43
44        ss.setCdmaRoamingIndicator(4);
45        assertEquals(4, ss.getCdmaRoamingIndicator());
46
47        ss.setDataRoamingType(ServiceState.ROAMING_TYPE_DOMESTIC);
48        assertTrue(ss.getDataRoaming());
49        assertEquals(ServiceState.ROAMING_TYPE_DOMESTIC, ss.getDataRoamingType());
50
51        ss.setDataRoamingFromRegistration(true);
52        assertTrue(ss.getDataRoamingFromRegistration());
53
54        ss.setVoiceRoamingType(ServiceState.ROAMING_TYPE_DOMESTIC);
55        assertTrue(ss.getVoiceRoaming());
56        assertEquals(ServiceState.ROAMING_TYPE_DOMESTIC, ss.getVoiceRoamingType());
57    }
58
59    @SmallTest
60    public void testRegState() {
61        ServiceState ss = new ServiceState();
62
63        ss.setDataRegState(ServiceState.STATE_IN_SERVICE);
64        assertEquals(ServiceState.STATE_IN_SERVICE, ss.getDataRegState());
65
66        ss.setVoiceRegState(ServiceState.STATE_IN_SERVICE);
67        assertEquals(ServiceState.STATE_IN_SERVICE, ss.getVoiceRegState());
68    }
69
70    @SmallTest
71    public void testRAT() {
72        ServiceState ss = new ServiceState();
73
74        ss.setRilDataRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
75        assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_LTE, ss.getRilDataRadioTechnology());
76        assertEquals(TelephonyManager.NETWORK_TYPE_LTE, ss.getDataNetworkType());
77
78        ss.setRilVoiceRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT);
79        assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT, ss.getRilVoiceRadioTechnology());
80        assertEquals(TelephonyManager.NETWORK_TYPE_1xRTT, ss.getVoiceNetworkType());
81
82        ArrayList<Pair<Integer, Boolean>> rats = new ArrayList<Pair<Integer, Boolean>>();
83
84        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_IS95A, true));
85        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_IS95B, true));
86        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT, true));
87        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_0, true));
88        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_A, true));
89        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_B, true));
90        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD, true));
91
92        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_GPRS, false));
93        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_EDGE, false));
94        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_UMTS, false));
95        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_HSDPA, false));
96        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_HSUPA, false));
97        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_HSPA, false));
98        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_LTE, false));
99        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_HSPAP, false));
100        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_GSM, false));
101        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_TD_SCDMA, false));
102        rats.add(new Pair<Integer, Boolean>(ServiceState.RIL_RADIO_TECHNOLOGY_IWLAN, false));
103
104        for (Pair<Integer, Boolean> rat : rats) {
105            boolean isCdma = rat.second;
106
107            if (isCdma) {
108                assertTrue("RAT " + rat + " should be CDMA", ServiceState.isCdma(rat.first));
109                assertFalse("RAT " + rat + " should not be GSM", ServiceState.isGsm(rat.first));
110            } else {
111                assertFalse("RAT " + rat + " should not be CDMA", ServiceState.isCdma(rat.first));
112                assertTrue("RAT " + rat + " should be GSM", ServiceState.isGsm(rat.first));
113            }
114        }
115    }
116
117    @SmallTest
118    public void testOperatorName() {
119        ServiceState ss = new ServiceState();
120
121        ss.setDataOperatorAlphaLong("abc");
122        assertEquals("abc", ss.getDataOperatorAlphaLong());
123
124        ss.setDataOperatorName("def", "xyz", "123456");
125        assertEquals("xyz", ss.getDataOperatorAlphaShort());
126
127        ss.setOperatorName("long", "short", "numeric");
128        assertEquals("long", ss.getVoiceOperatorAlphaLong());
129        assertEquals("short", ss.getVoiceOperatorAlphaShort());
130        assertEquals("numeric", ss.getVoiceOperatorNumeric());
131        assertEquals("long", ss.getDataOperatorAlphaLong());
132        assertEquals("short", ss.getDataOperatorAlphaShort());
133        assertEquals("numeric", ss.getDataOperatorNumeric());
134        assertEquals("long", ss.getOperatorAlpha());
135
136        ss.setOperatorName("", "short", "");
137        assertEquals("short", ss.getOperatorAlpha());
138    }
139
140    @SmallTest
141    public void testMisc() {
142        ServiceState ss = new ServiceState();
143
144        ss.setCssIndicator(100);
145        assertEquals(1, ss.getCssIndicator());
146
147        ss.setIsManualSelection(true);
148        assertTrue(ss.getIsManualSelection());
149
150        ss.setSystemAndNetworkId(123, 456);
151        assertEquals(123, ss.getSystemId());
152        assertEquals(456, ss.getNetworkId());
153
154        ss.setEmergencyOnly(true);
155        assertTrue(ss.isEmergencyOnly());
156    }
157
158    @SmallTest
159    public void testParcel() {
160
161        ServiceState ss = new ServiceState();
162        ss.setVoiceRegState(ServiceState.STATE_IN_SERVICE);
163        ss.setDataRegState(ServiceState.STATE_OUT_OF_SERVICE);
164        ss.setVoiceRoamingType(ServiceState.ROAMING_TYPE_INTERNATIONAL);
165        ss.setDataRoamingType(ServiceState.ROAMING_TYPE_UNKNOWN);
166        ss.setOperatorName("long", "short", "numeric");
167        ss.setIsManualSelection(true);
168        ss.setRilVoiceRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT);
169        ss.setRilDataRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_EVDO_0);
170        ss.setCssIndicator(1);
171        ss.setSystemAndNetworkId(2, 3);
172        ss.setCdmaRoamingIndicator(4);
173        ss.setCdmaDefaultRoamingIndicator(5);
174        ss.setCdmaEriIconIndex(6);
175        ss.setCdmaEriIconMode(7);
176        ss.setEmergencyOnly(true);
177        ss.setDataRoamingFromRegistration(true);
178
179        Parcel p = Parcel.obtain();
180        ss.writeToParcel(p, 0);
181        p.setDataPosition(0);
182
183        ServiceState newSs = new ServiceState(p);
184        assertEquals(ss, newSs);
185    }
186}