1 /*
2  * Copyright (C) 2018 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 static android.telephony.ServiceState.DUPLEX_MODE_FDD;
19 import static android.telephony.ServiceState.DUPLEX_MODE_TDD;
20 import static android.telephony.ServiceState.DUPLEX_MODE_UNKNOWN;
21 
22 import android.os.Parcel;
23 import android.telephony.AccessNetworkConstants;
24 import android.telephony.ServiceState;
25 import android.test.AndroidTestCase;
26 
27 public class ServiceStateTest extends AndroidTestCase {
28     private static final String OPERATOR_ALPHA_LONG = "CtsOperatorLong";
29     private static final String OPERATOR_ALPHA_SHORT = "CtsOp";
30     private static final String OPERATOR_NUMERIC = "02871";
31     private static final int SYSTEM_ID = 123;
32     private static final int NETWORK_ID = 456;
33     private static final int CHANNEL_NUMBER_BAND_66 = 66436;
34     private static final int CHANNEL_NUMBER_BAND_33 = 36000;
35     private static final int[] CELL_BANDWIDTH = {1, 2, 3};
36 
testServiceState()37     public void testServiceState() {
38         ServiceState serviceState = new ServiceState();
39 
40         assertEquals(0, serviceState.describeContents());
41 
42         serviceState.setStateOff();
43         assertEquals(ServiceState.STATE_POWER_OFF, serviceState.getState());
44         checkOffStatus(serviceState);
45 
46         serviceState.setStateOutOfService();
47         assertEquals(ServiceState.STATE_OUT_OF_SERVICE, serviceState.getState());
48         checkOffStatus(serviceState);
49 
50         serviceState.setState(ServiceState.STATE_IN_SERVICE);
51         assertEquals(ServiceState.STATE_IN_SERVICE, serviceState.getState());
52 
53         assertFalse(serviceState.getRoaming());
54         serviceState.setRoaming(true);
55         assertTrue(serviceState.getRoaming());
56 
57         assertFalse(serviceState.getIsManualSelection());
58         serviceState.setIsManualSelection(true);
59         assertTrue(serviceState.getIsManualSelection());
60 
61         serviceState.setOperatorName(OPERATOR_ALPHA_LONG, OPERATOR_ALPHA_SHORT, OPERATOR_NUMERIC);
62         assertEquals(OPERATOR_ALPHA_LONG, serviceState.getOperatorAlphaLong());
63         assertEquals(OPERATOR_ALPHA_SHORT, serviceState.getOperatorAlphaShort());
64         assertEquals(OPERATOR_NUMERIC, serviceState.getOperatorNumeric());
65 
66         serviceState.setCdmaSystemAndNetworkId(SYSTEM_ID, NETWORK_ID);
67         assertEquals(SYSTEM_ID, serviceState.getCdmaSystemId());
68         assertEquals(NETWORK_ID, serviceState.getCdmaNetworkId());
69 
70         serviceState.setChannelNumber(CHANNEL_NUMBER_BAND_66);
71         assertEquals(CHANNEL_NUMBER_BAND_66, serviceState.getChannelNumber());
72 
73         serviceState.setCellBandwidths(CELL_BANDWIDTH);
74         assertEquals(CELL_BANDWIDTH, serviceState.getCellBandwidths());
75 
76         serviceState.setRilDataRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_GSM);
77         assertEquals(DUPLEX_MODE_UNKNOWN, serviceState.getDuplexMode());
78 
79         serviceState.setRilDataRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
80         assertEquals(DUPLEX_MODE_FDD, serviceState.getDuplexMode());
81 
82         serviceState.setChannelNumber(CHANNEL_NUMBER_BAND_33);
83         assertEquals(DUPLEX_MODE_TDD, serviceState.getDuplexMode());
84 
85         assertTrue(serviceState.hashCode() > 0);
86         assertNotNull(serviceState.toString());
87 
88         ServiceState tempServiceState = new ServiceState(serviceState);
89         assertTrue(tempServiceState.equals(serviceState));
90 
91         Parcel stateParcel = Parcel.obtain();
92         serviceState.writeToParcel(stateParcel, 0);
93         stateParcel.setDataPosition(0);
94         tempServiceState = new ServiceState(stateParcel);
95         assertTrue(tempServiceState.equals(serviceState));
96 
97         MockServiceState mockServiceState = new MockServiceState();
98         mockServiceState.copyFrom(serviceState);
99         assertTrue(mockServiceState.equals(serviceState));
100     }
101 
102     /**
103      * Check the ServiceState fields in STATE_OUT_OF_SERVICE or STATE_POWER_OFF
104      */
checkOffStatus(ServiceState s)105     private void checkOffStatus(ServiceState s) {
106         assertFalse(s.getRoaming());
107         assertNull(s.getOperatorAlphaLong());
108         assertNull(s.getOperatorAlphaShort());
109         assertNull(s.getOperatorNumeric());
110         assertFalse(s.getIsManualSelection());
111     }
112 
113     private class MockServiceState extends ServiceState {
114         @Override
copyFrom(ServiceState s)115         protected void copyFrom(ServiceState s) {
116             super.copyFrom(s);
117         }
118     }
119 }
120