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 android.telephony.ims;
18
19import static junit.framework.Assert.assertEquals;
20
21import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.ArgumentMatchers.eq;
23import static org.mockito.Mockito.never;
24import static org.mockito.Mockito.verify;
25
26import android.net.Uri;
27import android.os.Parcel;
28import android.os.RemoteException;
29import android.support.test.runner.AndroidJUnit4;
30import android.telephony.ServiceState;
31import android.telephony.ims.aidl.IImsRegistration;
32import android.telephony.ims.aidl.IImsRegistrationCallback;
33import android.telephony.ims.feature.ImsFeature;
34import android.telephony.ims.stub.ImsFeatureConfiguration;
35import android.telephony.ims.stub.ImsRegistrationImplBase;
36import android.test.suitebuilder.annotation.SmallTest;
37
38import org.junit.After;
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.MockitoAnnotations;
43import org.mockito.Spy;
44
45@RunWith(AndroidJUnit4.class)
46public class ImsRegistrationTests {
47
48    @Spy private IImsRegistrationCallback.Stub mCallback;
49    @Spy private IImsRegistrationCallback.Stub mCallback2;
50    private ImsRegistrationImplBase mRegistration;
51    private IImsRegistration mRegBinder;
52
53    @Before
54    public void setup() throws RemoteException {
55        MockitoAnnotations.initMocks(this);
56        mRegistration = new ImsRegistrationImplBase();
57        mRegBinder = mRegistration.getBinder();
58        mRegBinder.addRegistrationCallback(mCallback);
59    }
60
61    @After
62    public void tearDown() {
63        mRegistration = null;
64        mRegBinder = null;
65    }
66
67    @SmallTest
68    @Test
69    public void testRegistrationConfigParcel() {
70        ImsFeatureConfiguration testConfig = new ImsFeatureConfiguration.Builder()
71                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_MMTEL)
72                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_RCS)
73                .build();
74        Parcel p = Parcel.obtain();
75        testConfig.writeToParcel(p, 0);
76        p.setDataPosition(0);
77        ImsFeatureConfiguration result =
78                ImsFeatureConfiguration.CREATOR.createFromParcel(p);
79        p.recycle();
80
81        assertEquals(testConfig, result);
82    }
83
84    @SmallTest
85    @Test
86    public void testRegistrationConfigPermutationEqual() {
87        ImsFeatureConfiguration testConfig = new ImsFeatureConfiguration.Builder()
88                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_MMTEL)
89                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_RCS)
90                .build();
91
92        // Permute field insertion ordering to ensure order doesn't matter.
93        ImsFeatureConfiguration testConfig2 = new ImsFeatureConfiguration.Builder()
94                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_RCS)
95                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_MMTEL)
96                .build();
97
98        assertEquals(testConfig, testConfig2);
99    }
100
101    @SmallTest
102    @Test
103    public void testRegistrationConfigConstructorsEqual() {
104        // Permute field insertion ordering to ensure order doesn't matter.
105        ImsFeatureConfiguration testConfig = new ImsFeatureConfiguration.Builder()
106                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_MMTEL)
107                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_RCS)
108                .build();
109
110        // Permute field insertion ordering to ensure order doesn't matter.
111        ImsFeatureConfiguration testConfig2 = new ImsFeatureConfiguration.Builder()
112                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_RCS)
113                .addFeature(/*slotId*/ 0, ImsFeature.FEATURE_MMTEL)
114                .build();
115
116        assertEquals(testConfig, testConfig2);
117    }
118
119    @SmallTest
120    @Test
121    public void testRegistrationCallbackOnRegistered() throws RemoteException {
122        mRegistration.onRegistered(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
123
124        verify(mCallback).onRegistered(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
125    }
126
127    @SmallTest
128    @Test
129    public void testRegistrationCallbackOnRegistering() throws RemoteException {
130        mRegistration.onRegistering(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
131
132        verify(mCallback).onRegistering(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
133    }
134
135    @SmallTest
136    @Test
137    public void testRegistrationCallbackOnDeregistered() throws RemoteException {
138        ImsReasonInfo info = new ImsReasonInfo();
139        mRegistration.onDeregistered(info);
140
141        verify(mCallback).onDeregistered(eq(info));
142    }
143
144    @SmallTest
145    @Test
146    public void testRegistrationCallbackOnTechChangeFailed() throws RemoteException {
147        ImsReasonInfo info = new ImsReasonInfo();
148        mRegistration.onTechnologyChangeFailed(ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN,
149                info);
150
151        verify(mCallback).onTechnologyChangeFailed(
152                eq(ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN), eq(info));
153    }
154
155    @SmallTest
156    @Test
157    public void testSubscriberUrisChanged() throws RemoteException {
158        Uri[] uris = new Uri[1];
159        uris[0] = Uri.fromParts("tel", "5555551212", null);
160
161        mRegistration.onSubscriberAssociatedUriChanged(uris);
162
163        verify(mCallback).onSubscriberAssociatedUriChanged(eq(uris));
164    }
165
166    @SmallTest
167    @Test
168    public void testRegistrationCallbackAfterUnregistered() throws RemoteException {
169        mRegBinder.removeRegistrationCallback(mCallback);
170
171        mRegistration.onRegistered(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
172
173        verify(mCallback, never()).onRegistered(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
174    }
175
176    @SmallTest
177    @Test
178    public void testRegistrationCallbackSendCurrentState() throws RemoteException {
179        mRegistration.onRegistered(ImsRegistrationImplBase.REGISTRATION_TECH_LTE);
180
181        mRegBinder.addRegistrationCallback(mCallback2);
182
183        verify(mCallback2).onRegistered(eq(ImsRegistrationImplBase.REGISTRATION_TECH_LTE));
184    }
185
186    @SmallTest
187    @Test
188    public void testRegistrationCallbackGetRegistrationTech() throws RemoteException {
189        mRegistration.onRegistered(ImsRegistrationImplBase.REGISTRATION_TECH_LTE);
190
191        assertEquals(ImsRegistrationImplBase.REGISTRATION_TECH_LTE,
192                mRegBinder.getRegistrationTechnology());
193    }
194
195    @SmallTest
196    @Test
197    public void testRegistrationCallbackSendCurrentStateDisconnected() throws RemoteException {
198        ImsReasonInfo info = new ImsReasonInfo(ImsReasonInfo.CODE_LOCAL_NETWORK_NO_LTE_COVERAGE, 0);
199        mRegistration.onDeregistered(info);
200
201        mRegBinder.addRegistrationCallback(mCallback2);
202
203        // The original callback that has been registered should get LTE tech in disconnected
204        // message
205        verify(mCallback).onDeregistered(eq(info));
206        // A callback that has just been registered should get NONE for tech in disconnected
207        // message
208        verify(mCallback2).onDeregistered(eq(info));
209    }
210
211    @SmallTest
212    @Test
213    public void testRegistrationCallbackGetRegistrationTechDisconnected() throws RemoteException {
214        ImsReasonInfo info = new ImsReasonInfo(ImsReasonInfo.CODE_LOCAL_NETWORK_NO_LTE_COVERAGE, 0);
215
216        mRegistration.onDeregistered(info);
217
218        verify(mCallback).onDeregistered(eq(info));
219        assertEquals(ImsRegistrationImplBase.REGISTRATION_TECH_NONE,
220                mRegBinder.getRegistrationTechnology());
221    }
222
223    @SmallTest
224    @Test
225    public void testRegistrationCallbackNoCallbackIfUnknown() throws RemoteException {
226        mRegBinder.addRegistrationCallback(mCallback2);
227        // Verify that if we have never set the registration state, we do not callback immediately
228        // with onDeregistered.
229        verify(mCallback2, never()).onDeregistered(any(ImsReasonInfo.class));
230    }
231}
232