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