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