1 /* 2 * Copyright (C) 2020 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 17 package com.android.ims; 18 19 import junit.framework.AssertionFailedError; 20 21 import static org.junit.Assert.assertEquals; 22 import static org.mockito.Mockito.doReturn; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.os.IBinder; 28 import android.os.RemoteException; 29 import android.telephony.ims.aidl.IImsConfig; 30 import android.telephony.ims.aidl.IImsRegistration; 31 import android.telephony.ims.aidl.ISipTransport; 32 import android.telephony.ims.feature.ImsFeature; 33 import android.telephony.ims.stub.ImsRegistrationImplBase; 34 35 import androidx.test.ext.junit.runners.AndroidJUnit4; 36 import androidx.test.filters.SmallTest; 37 38 import org.junit.After; 39 import org.junit.Before; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.Mock; 43 44 45 @RunWith(AndroidJUnit4.class) 46 public class FeatureConnectionTest extends ImsTestBase { 47 48 private class TestFeatureConnection extends FeatureConnection { 49 private Integer mFeatureState = ImsFeature.STATE_READY; 50 51 public boolean isFeatureCreatedCalled = false; 52 public boolean isFeatureRemovedCalled = false; 53 public int mNewStatus = ImsFeature.STATE_UNAVAILABLE; 54 public long mCapabilities; 55 TestFeatureConnection(Context context, int slotId, int subId)56 TestFeatureConnection(Context context, int slotId, int subId) { 57 super(context, slotId, subId, mConfigBinder, mRegistrationBinder, mSipTransportBinder); 58 if (!ImsManager.isImsSupportedOnDevice(context)) { 59 sImsSupportedOnDevice = false; 60 } 61 } 62 63 @Override checkServiceIsReady()64 public void checkServiceIsReady() throws RemoteException { 65 super.checkServiceIsReady(); 66 } 67 68 @Override retrieveFeatureState()69 protected Integer retrieveFeatureState() { 70 return mFeatureState; 71 } 72 73 @Override onFeatureCapabilitiesUpdated(long capabilities)74 protected void onFeatureCapabilitiesUpdated(long capabilities) { 75 mCapabilities = capabilities; 76 } 77 setFeatureState(int state)78 public void setFeatureState(int state) { 79 mFeatureState = state; 80 } 81 }; 82 83 private TestFeatureConnection mTestFeatureConnection; 84 @Mock IBinder mBinder; 85 @Mock IImsRegistration mRegistrationBinder; 86 @Mock IImsConfig mConfigBinder; 87 @Mock ISipTransport mSipTransportBinder; 88 89 public static final int PHONE_ID = 1; 90 public static final int SUB_ID = 2; 91 92 @Before setUp()93 public void setUp() throws Exception { 94 super.setUp(); 95 96 doReturn(null).when(mContext).getMainLooper(); 97 mContextFixture.addSystemFeature(PackageManager.FEATURE_TELEPHONY_IMS); 98 99 mTestFeatureConnection = new TestFeatureConnection(mContext, PHONE_ID, SUB_ID); 100 mTestFeatureConnection.setBinder(mBinder); 101 } 102 103 @After tearDown()104 public void tearDown() throws Exception { 105 super.tearDown(); 106 } 107 108 /** 109 * Test service is ready when binder is alive and IMS status is ready. 110 */ 111 @Test 112 @SmallTest testServiceIsReady()113 public void testServiceIsReady() { 114 when(mBinder.isBinderAlive()).thenReturn(true); 115 mTestFeatureConnection.setFeatureState(ImsFeature.STATE_READY); 116 117 try { 118 mTestFeatureConnection.checkServiceIsReady(); 119 } catch (RemoteException e) { 120 throw new AssertionFailedError("Exception in testServiceIsReady: " + e); 121 } 122 } 123 124 /** 125 * Test service is not ready when binder is not alive or status is not ready. 126 */ 127 @Test 128 @SmallTest testServiceIsNotReady()129 public void testServiceIsNotReady() { 130 // Binder is not alive 131 when(mBinder.isBinderAlive()).thenReturn(false); 132 133 try { 134 mTestFeatureConnection.checkServiceIsReady(); 135 throw new AssertionFailedError("testServiceIsNotReady: binder isn't alive"); 136 } catch (RemoteException e) { 137 // expected result 138 } 139 140 // IMS feature status is unavailable 141 when(mBinder.isBinderAlive()).thenReturn(true); 142 mTestFeatureConnection.setFeatureState(ImsFeature.STATE_UNAVAILABLE); 143 144 try { 145 mTestFeatureConnection.checkServiceIsReady(); 146 throw new AssertionFailedError("testServiceIsNotReady: status unavailable"); 147 } catch (RemoteException e) { 148 // expected result 149 } 150 } 151 152 /** 153 * Test registration tech callbacks. 154 */ 155 @Test 156 @SmallTest testRegistrationTech()157 public void testRegistrationTech() throws Exception { 158 when(mRegistrationBinder.getRegistrationTechnology()).thenReturn( 159 ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN); 160 161 assertEquals(ImsRegistrationImplBase.REGISTRATION_TECH_IWLAN, 162 mTestFeatureConnection.getRegistrationTech()); 163 164 } 165 166 /** 167 * Test registration tech callbacks. 168 */ 169 @Test 170 @SmallTest testUpdateCapabilities()171 public void testUpdateCapabilities() throws Exception { 172 long testCaps = 1; 173 assertEquals(0 /*base state*/, mTestFeatureConnection.mCapabilities); 174 mTestFeatureConnection.updateFeatureCapabilities(testCaps); 175 assertEquals(testCaps, mTestFeatureConnection.mCapabilities); 176 177 } 178 } 179