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