Home | History | Annotate | Download | only in euicc
      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 package com.android.phone.euicc;
     17 
     18 import static org.junit.Assert.assertNotNull;
     19 import static org.junit.Assert.assertNull;
     20 import static org.mockito.Mockito.when;
     21 
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.content.pm.ActivityInfo;
     25 import android.support.test.InstrumentationRegistry;
     26 import android.support.test.runner.AndroidJUnit4;
     27 import android.telephony.euicc.EuiccManager;
     28 
     29 import org.junit.Before;
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.mockito.Mock;
     33 import org.mockito.MockitoAnnotations;
     34 
     35 @RunWith(AndroidJUnit4.class)
     36 public class EuiccUiDispatcherActivityTest {
     37     private static final Intent MANAGE_INTENT =
     38             new Intent(EuiccManager.ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS);
     39     private static final Intent PROVISION_INTENT =
     40             new Intent(EuiccManager.ACTION_PROVISION_EMBEDDED_SUBSCRIPTION);
     41 
     42     private static final ActivityInfo ACTIVITY_INFO = new ActivityInfo();
     43     static {
     44         ACTIVITY_INFO.packageName = "test.package";
     45         ACTIVITY_INFO.name = "TestClass";
     46     }
     47 
     48     @Mock private Context mMockContext;
     49     @Mock private EuiccManager mMockEuiccManager;
     50     private boolean mIsProvisioned = true;
     51     private ActivityInfo mActivityInfo = ACTIVITY_INFO;
     52     private Intent mIntent = MANAGE_INTENT;
     53     private EuiccUiDispatcherActivity mActivity;
     54 
     55     @Before
     56     public void setUp() {
     57         MockitoAnnotations.initMocks(this);
     58         when(mMockEuiccManager.isEnabled()).thenReturn(true);
     59         when(mMockContext.getSystemService(Context.EUICC_SERVICE)).thenReturn(mMockEuiccManager);
     60         InstrumentationRegistry.getInstrumentation().runOnMainSync(
     61                 new Runnable() {
     62                     @Override
     63                     public void run() {
     64                         mActivity = new TestEuiccUiDispatcherActivity();
     65                     }
     66                 }
     67         );
     68     }
     69 
     70     @Test
     71     public void testResolveEuiccUiIntent_disabled() {
     72         when(mMockEuiccManager.isEnabled()).thenReturn(false);
     73         assertNull(mActivity.resolveEuiccUiIntent());
     74     }
     75 
     76     @Test
     77     public void testResolveEuiccUiIntent_unsupportedAction() {
     78         mIntent = new Intent("fake.action");
     79         assertNull(mActivity.resolveEuiccUiIntent());
     80     }
     81 
     82     @Test
     83     public void testResolveEuiccUiIntent_alreadyProvisioned() {
     84         mIntent = PROVISION_INTENT;
     85         assertNull(mActivity.resolveEuiccUiIntent());
     86     }
     87 
     88     @Test
     89     public void testResolveEuiccUiIntent_noImplementation() {
     90         mActivityInfo = null;
     91         assertNull(mActivity.resolveEuiccUiIntent());
     92     }
     93 
     94     @Test
     95     public void testResolveEuiccUiIntent_validManage() {
     96         assertNotNull(mActivity.resolveEuiccUiIntent());
     97     }
     98 
     99     @Test
    100     public void testResolveEuiccUiIntent_validProvision() {
    101         mIsProvisioned = false;
    102         assertNotNull(mActivity.resolveEuiccUiIntent());
    103     }
    104 
    105     class TestEuiccUiDispatcherActivity extends EuiccUiDispatcherActivity {
    106         public TestEuiccUiDispatcherActivity() {
    107             attachBaseContext(mMockContext);
    108         }
    109 
    110         @Override
    111         public Intent getIntent() {
    112             return mIntent;
    113         }
    114 
    115         @Override
    116         boolean isDeviceProvisioned() {
    117             return mIsProvisioned;
    118         }
    119 
    120         @Override
    121         ActivityInfo findBestActivity(Intent euiccUiIntent) {
    122             return mActivityInfo;
    123         }
    124     }
    125 }
    126