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 */
16package com.android.phone.euicc;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertNotNull;
20import static org.junit.Assert.assertNull;
21import static org.mockito.Mockito.when;
22
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.ActivityInfo;
26import android.support.test.InstrumentationRegistry;
27import android.support.test.runner.AndroidJUnit4;
28import android.telephony.euicc.EuiccManager;
29
30import org.junit.Before;
31import org.junit.Test;
32import org.junit.runner.RunWith;
33import org.mockito.Mock;
34import org.mockito.MockitoAnnotations;
35
36@RunWith(AndroidJUnit4.class)
37public class EuiccUiDispatcherActivityTest {
38    private static final Intent MANAGE_INTENT =
39            new Intent(EuiccManager.ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS);
40    private static final Intent PROVISION_INTENT =
41            new Intent(EuiccManager.ACTION_PROVISION_EMBEDDED_SUBSCRIPTION);
42
43    private static final ActivityInfo ACTIVITY_INFO = new ActivityInfo();
44    static {
45        ACTIVITY_INFO.packageName = "test.package";
46        ACTIVITY_INFO.name = "TestClass";
47    }
48
49    @Mock private Context mMockContext;
50    @Mock private EuiccManager mMockEuiccManager;
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_noImplementation() {
84        mActivityInfo = null;
85        assertNull(mActivity.resolveEuiccUiIntent());
86    }
87
88    @Test
89    public void testResolveEuiccUiIntent_validManage() {
90        assertNotNull(mActivity.resolveEuiccUiIntent());
91    }
92
93    @Test
94    public void testResolveEuiccUiIntent_validProvision() {
95        assertNotNull(mActivity.resolveEuiccUiIntent());
96    }
97
98    @Test
99    public void testExtrasPropagated() {
100        mIntent.putExtra("foo", "bar");
101
102        Intent euiccUiIntent = mActivity.resolveEuiccUiIntent();
103        assertNotNull(euiccUiIntent);
104        assertEquals("bar", euiccUiIntent.getStringExtra("foo"));
105    }
106
107    class TestEuiccUiDispatcherActivity extends EuiccUiDispatcherActivity {
108        public TestEuiccUiDispatcherActivity() {
109            attachBaseContext(mMockContext);
110        }
111
112        @Override
113        public Intent getIntent() {
114            return mIntent;
115        }
116
117        @Override
118        ActivityInfo findBestActivity(Intent euiccUiIntent) {
119            return mActivityInfo;
120        }
121
122        @Override
123        protected void grantDefaultPermissionsToActiveLuiApp(ActivityInfo activityInfo) {}
124
125        @Override
126        protected void revokePermissionFromLuiApps(Intent intent) {}
127    }
128}
129