Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2018 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 
     17 package android.telephony.euicc.cts;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertNull;
     21 import static org.junit.Assert.fail;
     22 
     23 import android.app.PendingIntent;
     24 import android.content.BroadcastReceiver;
     25 import android.content.Context;
     26 import android.content.Intent;
     27 import android.content.IntentFilter;
     28 import android.telephony.TelephonyManager;
     29 import android.telephony.euicc.DownloadableSubscription;
     30 import android.telephony.euicc.EuiccInfo;
     31 import android.telephony.euicc.EuiccManager;
     32 
     33 import androidx.test.InstrumentationRegistry;
     34 import androidx.test.runner.AndroidJUnit4;
     35 
     36 import org.junit.After;
     37 import org.junit.Before;
     38 import org.junit.Test;
     39 import org.junit.runner.RunWith;
     40 
     41 import java.util.concurrent.CountDownLatch;
     42 import java.util.concurrent.TimeUnit;
     43 
     44 @RunWith(AndroidJUnit4.class)
     45 public class EuiccManagerTest {
     46 
     47     private static final int REQUEST_CODE = 0;
     48     private static final int CALLBACK_TIMEOUT_MILLIS = 2000;
     49     // starting activities might take extra time
     50     private static final int ACTIVITY_CALLBACK_TIMEOUT_MILLIS = 5000;
     51     private static final String ACTION_DOWNLOAD_SUBSCRIPTION = "cts_download_subscription";
     52     private static final String ACTION_DELETE_SUBSCRIPTION = "cts_delete_subscription";
     53     private static final String ACTION_SWITCH_TO_SUBSCRIPTION = "cts_switch_to_subscription";
     54     private static final String ACTION_START_TEST_RESOLUTION_ACTIVITY =
     55             "cts_start_test_resolution_activity";
     56     private static final String ACTIVATION_CODE = "1$LOCALHOST$04386-AGYFT-A74Y8-3F815";
     57 
     58     private static final String[] sCallbackActions =
     59             new String[]{
     60                     ACTION_DOWNLOAD_SUBSCRIPTION,
     61                     ACTION_DELETE_SUBSCRIPTION,
     62                     ACTION_SWITCH_TO_SUBSCRIPTION,
     63                     ACTION_START_TEST_RESOLUTION_ACTIVITY,
     64             };
     65 
     66     private EuiccManager mEuiccManager;
     67     private CallbackReceiver mCallbackReceiver;
     68 
     69     @Before
     70     public void setUp() throws Exception {
     71         mEuiccManager = (EuiccManager) getContext().getSystemService(Context.EUICC_SERVICE);
     72     }
     73 
     74     @After
     75     public void tearDown() throws Exception {
     76         if (mCallbackReceiver != null) {
     77             getContext().unregisterReceiver(mCallbackReceiver);
     78         }
     79     }
     80 
     81     @Test
     82     public void testGetEid() {
     83         // test disabled state only for now
     84         if (mEuiccManager.isEnabled()) {
     85             return;
     86         }
     87 
     88         // call getEid()
     89         String eid = mEuiccManager.getEid();
     90 
     91         // verify result is null
     92         assertNull(eid);
     93     }
     94 
     95     @Test
     96     public void testCreateForCardId() {
     97         // just verify that this does not crash
     98         mEuiccManager = mEuiccManager.createForCardId(TelephonyManager.UNINITIALIZED_CARD_ID);
     99         mEuiccManager = mEuiccManager.createForCardId(TelephonyManager.UNSUPPORTED_CARD_ID);
    100     }
    101 
    102     @Test
    103     public void testDownloadSubscription() {
    104         // test disabled state only for now
    105         if (mEuiccManager.isEnabled()) {
    106             return;
    107         }
    108 
    109         // set up CountDownLatch and receiver
    110         CountDownLatch countDownLatch = new CountDownLatch(1);
    111         mCallbackReceiver = new CallbackReceiver(countDownLatch);
    112         getContext()
    113                 .registerReceiver(
    114                         mCallbackReceiver, new IntentFilter(ACTION_DOWNLOAD_SUBSCRIPTION));
    115 
    116         // call downloadSubscription()
    117         DownloadableSubscription subscription = createDownloadableSubscription();
    118         PendingIntent callbackIntent = createCallbackIntent(ACTION_DOWNLOAD_SUBSCRIPTION);
    119         mEuiccManager.downloadSubscription(
    120                 subscription, false /* switchAfterDownload */, callbackIntent);
    121 
    122         // wait for callback
    123         try {
    124             countDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    125         } catch (InterruptedException e) {
    126             fail(e.toString());
    127         }
    128 
    129         // verify correct result code is received
    130         assertEquals(
    131                 EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, mCallbackReceiver.getResultCode());
    132     }
    133 
    134     @Test
    135     public void testGetEuiccInfo() {
    136         // test disabled state only for now
    137         if (mEuiccManager.isEnabled()) {
    138             return;
    139         }
    140 
    141         // call getEuiccInfo()
    142         EuiccInfo euiccInfo = mEuiccManager.getEuiccInfo();
    143 
    144         // verify result is null
    145         assertNull(euiccInfo);
    146     }
    147 
    148     @Test
    149     public void testDeleteSubscription() {
    150         // test disabled state only for now
    151         if (mEuiccManager.isEnabled()) {
    152             return;
    153         }
    154 
    155         // set up CountDownLatch and receiver
    156         CountDownLatch countDownLatch = new CountDownLatch(1);
    157         mCallbackReceiver = new CallbackReceiver(countDownLatch);
    158         getContext()
    159                 .registerReceiver(mCallbackReceiver, new IntentFilter(ACTION_DELETE_SUBSCRIPTION));
    160 
    161         // call deleteSubscription()
    162         PendingIntent callbackIntent = createCallbackIntent(ACTION_DELETE_SUBSCRIPTION);
    163         mEuiccManager.deleteSubscription(3, callbackIntent);
    164 
    165         // wait for callback
    166         try {
    167             countDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    168         } catch (InterruptedException e) {
    169             fail(e.toString());
    170         }
    171 
    172         // verify correct result code is received
    173         assertEquals(
    174                 EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, mCallbackReceiver.getResultCode());
    175     }
    176 
    177     @Test
    178     public void testSwitchToSubscription() {
    179         // test disabled state only for now
    180         if (mEuiccManager.isEnabled()) {
    181             return;
    182         }
    183 
    184         // set up CountDownLatch and receiver
    185         CountDownLatch countDownLatch = new CountDownLatch(1);
    186         mCallbackReceiver = new CallbackReceiver(countDownLatch);
    187         getContext()
    188                 .registerReceiver(
    189                         mCallbackReceiver, new IntentFilter(ACTION_SWITCH_TO_SUBSCRIPTION));
    190 
    191         // call deleteSubscription()
    192         PendingIntent callbackIntent = createCallbackIntent(ACTION_SWITCH_TO_SUBSCRIPTION);
    193         mEuiccManager.switchToSubscription(4, callbackIntent);
    194 
    195         // wait for callback
    196         try {
    197             countDownLatch.await(CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    198         } catch (InterruptedException e) {
    199             fail(e.toString());
    200         }
    201 
    202         // verify correct result code is received
    203         assertEquals(
    204                 EuiccManager.EMBEDDED_SUBSCRIPTION_RESULT_ERROR, mCallbackReceiver.getResultCode());
    205     }
    206 
    207     @Test
    208     public void testStartResolutionActivity() {
    209         // set up CountDownLatch and receiver
    210         CountDownLatch countDownLatch = new CountDownLatch(1);
    211         mCallbackReceiver = new CallbackReceiver(countDownLatch);
    212         getContext()
    213                 .registerReceiver(
    214                         mCallbackReceiver, new IntentFilter(ACTION_START_TEST_RESOLUTION_ACTIVITY));
    215 
    216         /*
    217          * Start EuiccTestResolutionActivity to test EuiccManager#startResolutionActivity(), since
    218          * it requires a foreground activity. EuiccTestResolutionActivity will report the test
    219          * result to the callback receiver.
    220          */
    221         Intent testResolutionActivityIntent =
    222                 new Intent(getContext(), EuiccTestResolutionActivity.class);
    223         PendingIntent callbackIntent = createCallbackIntent(ACTION_START_TEST_RESOLUTION_ACTIVITY);
    224         testResolutionActivityIntent.putExtra(
    225                 EuiccTestResolutionActivity.EXTRA_ACTIVITY_CALLBACK_INTENT, callbackIntent);
    226         testResolutionActivityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    227         getContext().startActivity(testResolutionActivityIntent);
    228 
    229         // wait for callback
    230         try {
    231             countDownLatch.await(ACTIVITY_CALLBACK_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
    232         } catch (InterruptedException e) {
    233             fail(e.toString());
    234         }
    235 
    236         // verify test result reported by EuiccTestResolutionActivity
    237         assertEquals(
    238                 EuiccTestResolutionActivity.RESULT_CODE_TEST_PASSED,
    239                 mCallbackReceiver.getResultCode());
    240     }
    241 
    242     private Context getContext() {
    243         return InstrumentationRegistry.getContext();
    244     }
    245 
    246     private DownloadableSubscription createDownloadableSubscription() {
    247         return DownloadableSubscription.forActivationCode(ACTIVATION_CODE);
    248     }
    249 
    250     private PendingIntent createCallbackIntent(String action) {
    251         Intent intent = new Intent(action);
    252         return PendingIntent.getBroadcast(
    253                 getContext(), REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    254     }
    255 
    256     private static class CallbackReceiver extends BroadcastReceiver {
    257 
    258         private CountDownLatch mCountDownLatch;
    259 
    260         public CallbackReceiver(CountDownLatch latch) {
    261             mCountDownLatch = latch;
    262         }
    263 
    264         @Override
    265         public void onReceive(Context context, Intent intent) {
    266             for (String callbackAction : sCallbackActions) {
    267                 if (callbackAction.equals(intent.getAction())) {
    268                     int resultCode = getResultCode();
    269                     mCountDownLatch.countDown();
    270                     break;
    271                 }
    272             }
    273         }
    274     }
    275 }
    276