Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c4de1cb3 authored by Brad Ebinger's avatar Brad Ebinger Committed by Gerrit Code Review
Browse files

Merge "Initialize TestImsService with mock Context"

parents fe403e0c 014a98a5
Loading
Loading
Loading
Loading
+42 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.telephony.ims;

import android.content.Context;
import android.content.Intent;
import android.os.RemoteException;
import android.support.test.runner.AndroidJUnit4;
@@ -33,11 +34,17 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import static android.Manifest.permission.MODIFY_PHONE_STATE;
import static android.Manifest.permission.READ_PHONE_STATE;
import static com.android.internal.telephony.ims.ImsResolver.SERVICE_INTERFACE;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.fail;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@@ -54,13 +61,15 @@ public class ImsServiceTest {
    private TestImsService mTestImsService;
    private IImsServiceController mTestImsServiceBinder;

    @Mock
    private Context mMockContext;
    @Mock
    private IImsFeatureStatusCallback mTestCallback;

    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mTestImsService = new TestImsService();
        mTestImsService = new TestImsService(mMockContext);
        mTestImsServiceBinder = (IImsServiceController) mTestImsService.onBind(
                new Intent(SERVICE_INTERFACE));
    }
@@ -122,4 +131,36 @@ public class ImsServiceTest {

        verify(mTestImsService.mMockMMTelFeature, never()).isConnected(anyInt(), anyInt());
    }

    @Test
    @SmallTest
    public void testCreateFeatureWithNoPermissions() throws RemoteException {
        doThrow(new SecurityException()).when(mMockContext).enforceCallingOrSelfPermission(
                eq(MODIFY_PHONE_STATE), anyString());

        try {
            mTestImsServiceBinder.createImsFeature(TEST_SLOT_0, ImsFeature.MMTEL, mTestCallback);
            fail();
        } catch (SecurityException e) {
            // Expected
        }
    }

    @Test
    @SmallTest
    public void testMethodWithNoPermissions() throws RemoteException {
        doThrow(new SecurityException()).when(mMockContext).enforceCallingOrSelfPermission(
                eq(READ_PHONE_STATE), anyString());
        mTestImsServiceBinder.createImsFeature(TEST_SLOT_0, ImsFeature.MMTEL, mTestCallback);

        try {
            mTestImsServiceBinder.isConnected(TEST_SLOT_1, ImsFeature.MMTEL, 0 /*callSessionType*/,
                    0 /*callType*/);
            fail();
        } catch (SecurityException e) {
            // Expected
        }

        verify(mTestImsService.mMockMMTelFeature, never()).isConnected(anyInt(), anyInt());
    }
}
+3 −1
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.telephony.ims;

import android.content.Context;
import android.telephony.ims.feature.MMTelFeature;
import android.telephony.ims.feature.RcsFeature;

@@ -28,7 +29,8 @@ import org.mockito.MockitoAnnotations;

public class TestImsService extends ImsService {

    public TestImsService() {
    public TestImsService(Context context) {
        attachBaseContext(context);
        MockitoAnnotations.initMocks(this);
    }