1 /*
2  * Copyright (C) 2009 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 android.os.Parcel;
19 import android.telephony.ServiceState;
20 import android.test.AndroidTestCase;
21 
22 public class ServiceStateTest extends AndroidTestCase {
23     private static final String OPERATOR_ALPHA_LONG = "CtsOperatorLong";
24     private static final String OPERATOR_ALPHA_SHORT = "CtsOp";
25     private static final String OPERATOR_NUMERIC = "02871";
26 
testServiceState()27     public void testServiceState() {
28         ServiceState serviceState = new ServiceState();
29 
30         assertEquals(0, serviceState.describeContents());
31 
32         serviceState.setStateOff();
33         assertEquals(ServiceState.STATE_POWER_OFF, serviceState.getState());
34         checkOffStatus(serviceState);
35 
36         serviceState.setStateOutOfService();
37         assertEquals(ServiceState.STATE_OUT_OF_SERVICE, serviceState.getState());
38         checkOffStatus(serviceState);
39 
40         serviceState.setState(ServiceState.STATE_IN_SERVICE);
41         assertEquals(ServiceState.STATE_IN_SERVICE, serviceState.getState());
42 
43         assertFalse(serviceState.getRoaming());
44         serviceState.setRoaming(true);
45         assertTrue(serviceState.getRoaming());
46 
47         assertFalse(serviceState.getIsManualSelection());
48         serviceState.setIsManualSelection(true);
49         assertTrue(serviceState.getIsManualSelection());
50 
51         serviceState.setOperatorName(OPERATOR_ALPHA_LONG, OPERATOR_ALPHA_SHORT, OPERATOR_NUMERIC);
52         assertEquals(OPERATOR_ALPHA_LONG, serviceState.getOperatorAlphaLong());
53         assertEquals(OPERATOR_ALPHA_SHORT, serviceState.getOperatorAlphaShort());
54         assertEquals(OPERATOR_NUMERIC, serviceState.getOperatorNumeric());
55 
56         assertTrue(serviceState.hashCode() > 0);
57         assertNotNull(serviceState.toString());
58 
59         ServiceState tempServiceState = new ServiceState(serviceState);
60         assertTrue(tempServiceState.equals(serviceState));
61 
62         Parcel stateParcel = Parcel.obtain();
63         serviceState.writeToParcel(stateParcel, 0);
64         stateParcel.setDataPosition(0);
65         tempServiceState = new ServiceState(stateParcel);
66         assertTrue(tempServiceState.equals(serviceState));
67 
68         MockServiceState mockServiceState = new MockServiceState();
69         mockServiceState.copyFrom(serviceState);
70         assertTrue(mockServiceState.equals(serviceState));
71     }
72 
73     /**
74      * Check the ServiceState fields in STATE_OUT_OF_SERVICE or STATE_POWER_OFF
75      */
checkOffStatus(ServiceState s)76     private void checkOffStatus(ServiceState s) {
77         assertFalse(s.getRoaming());
78         assertNull(s.getOperatorAlphaLong());
79         assertNull(s.getOperatorAlphaShort());
80         assertNull(s.getOperatorNumeric());
81         assertFalse(s.getIsManualSelection());
82     }
83 
84     private class MockServiceState extends ServiceState {
85         @Override
copyFrom(ServiceState s)86         protected void copyFrom(ServiceState s) {
87             super.copyFrom(s);
88         }
89     }
90 }
91