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 static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
20
21import static org.junit.Assert.assertTrue;
22import static org.mockito.Mockito.doReturn;
23import static org.mockito.Mockito.eq;
24import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26
27import android.content.IntentFilter;
28import android.content.pm.ServiceInfo;
29import android.os.RemoteException;
30import android.telephony.AccessNetworkConstants;
31import android.telephony.INetworkService;
32import android.telephony.INetworkServiceCallback;
33import android.telephony.NetworkRegistrationState;
34import android.telephony.NetworkService;
35import android.telephony.NetworkServiceCallback;
36import android.telephony.ServiceState;
37import android.telephony.SubscriptionManager;
38import android.test.suitebuilder.annotation.MediumTest;
39
40import com.android.internal.R;
41
42import org.junit.After;
43import org.junit.Before;
44import org.junit.Test;
45import org.mockito.Mock;
46
47public class CellularNetworkServiceTest extends TelephonyTest {
48    CellularNetworkService mCellularNetworkService;
49
50    @Mock
51    private INetworkServiceCallback mCallback;
52
53    private void addNetworkService() {
54        mCellularNetworkService = new CellularNetworkService();
55        ServiceInfo serviceInfo =  new ServiceInfo();
56        serviceInfo.packageName = "com.android.phone";
57        serviceInfo.permission = "android.permission.BIND_NETWORK_SERVICE";
58        IntentFilter filter = new IntentFilter();
59        mContextFixture.addService(
60                NetworkService.NETWORK_SERVICE_INTERFACE,
61                null,
62                "com.android.phone",
63                mCellularNetworkService.mBinder,
64                serviceInfo,
65                filter);
66    }
67    INetworkService.Stub mBinder;
68
69    @Before
70    public void setUp() throws Exception {
71
72        logd("CellularNetworkServiceTest +Setup!");
73        super.setUp("CellularNetworkServiceTest");
74
75        mContextFixture.putResource(R.string.config_wwan_network_service_package,
76                "com.android.phone");
77        addNetworkService();
78        mBinder = mCellularNetworkService.mBinder;
79        mBinder.createNetworkServiceProvider(0);
80
81        int dds = SubscriptionManager.getDefaultDataSubscriptionId();
82        doReturn(dds).when(mPhone).getSubId();
83
84        logd("CellularNetworkServiceTest -Setup!");
85    }
86
87    @After
88    public void tearDown() throws Exception {
89        super.tearDown();
90    }
91
92    @Test
93    @MediumTest
94    public void testGetNetworkRegistrationState() {
95        int voiceRegState = NetworkRegistrationState.REG_STATE_HOME;
96        int dataRegState = NetworkRegistrationState.REG_STATE_HOME;
97        int voiceRadioTech = ServiceState.RIL_RADIO_TECHNOLOGY_HSPA;
98        int dataRadioTech = ServiceState.RIL_RADIO_TECHNOLOGY_HSPA;
99        int domain = NetworkRegistrationState.DOMAIN_CS;
100
101        boolean cssSupported = true;
102        int roamingIndicator = 1;
103        int systemIsInPrl = 2;
104        int defaultRoamingIndicator = 3;
105        int reasonForDenial = 0;
106        int maxDataCalls = 4;
107        int[] availableServices = new int[] {
108                NetworkRegistrationState.SERVICE_TYPE_VOICE,
109                NetworkRegistrationState.SERVICE_TYPE_SMS,
110                NetworkRegistrationState.SERVICE_TYPE_VIDEO
111        };
112
113        mSimulatedCommands.setVoiceRegState(voiceRegState);
114        mSimulatedCommands.setVoiceRadioTech(voiceRadioTech);
115        mSimulatedCommands.setDataRegState(dataRegState);
116        mSimulatedCommands.setDataRadioTech(dataRadioTech);
117        mSimulatedCommands.mCssSupported = cssSupported;
118        mSimulatedCommands.mRoamingIndicator = roamingIndicator;
119        mSimulatedCommands.mSystemIsInPrl = systemIsInPrl;
120        mSimulatedCommands.mDefaultRoamingIndicator = defaultRoamingIndicator;
121        mSimulatedCommands.mReasonForDenial = reasonForDenial;
122        mSimulatedCommands.mMaxDataCalls = maxDataCalls;
123
124        mSimulatedCommands.notifyNetworkStateChanged();
125
126        try {
127            mBinder.getNetworkRegistrationState(0, domain, mCallback);
128        } catch (RemoteException e) {
129            assertTrue(false);
130        }
131
132        waitForMs(1000);
133
134        NetworkRegistrationState expectedState = new NetworkRegistrationState(
135                AccessNetworkConstants.TransportType.WWAN, domain, voiceRegState,
136                ServiceState.rilRadioTechnologyToNetworkType(voiceRadioTech), reasonForDenial,
137                false, availableServices, null, cssSupported,
138                roamingIndicator, systemIsInPrl, defaultRoamingIndicator);
139
140        try {
141            verify(mCallback, times(1)).onGetNetworkRegistrationStateComplete(
142                    eq(NetworkServiceCallback.RESULT_SUCCESS), eq(expectedState));
143        } catch (RemoteException e) {
144            assertTrue(false);
145        }
146
147        domain = NetworkRegistrationState.DOMAIN_PS;
148        availableServices = new int[] {NetworkRegistrationState.SERVICE_TYPE_DATA};
149        try {
150            mBinder.getNetworkRegistrationState(0, domain, mCallback);
151        } catch (RemoteException e) {
152            assertTrue(false);
153        }
154
155        waitForMs(1000);
156
157        expectedState = new NetworkRegistrationState(
158                AccessNetworkConstants.TransportType.WWAN, domain, voiceRegState,
159                ServiceState.rilRadioTechnologyToNetworkType(voiceRadioTech), reasonForDenial,
160                false, availableServices, null, maxDataCalls);
161
162        try {
163            verify(mCallback, times(1)).onGetNetworkRegistrationStateComplete(
164                    eq(NetworkServiceCallback.RESULT_SUCCESS), eq(expectedState));
165        } catch (RemoteException e) {
166            assertTrue(false);
167        }
168    }
169}
170