1 /* 2 * Copyright (C) 2021 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.carrierapi.cts; 18 19 import static com.android.compatibility.common.util.UiccUtil.UiccCertificate.CTS_UICC_LEGACY; 20 21 import static com.google.common.truth.Truth.assertWithMessage; 22 23 import static org.junit.Assume.assumeTrue; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.telephony.TelephonyManager; 28 29 import androidx.test.InstrumentationRegistry; 30 31 import com.android.compatibility.common.util.AmUtils; 32 import com.android.compatibility.common.util.FeatureUtil; 33 import com.android.compatibility.common.util.UiccUtil; 34 35 import org.junit.Before; 36 37 /** 38 * Common test base to ensure uniform preconditions checking. This class will check for: 39 * 40 * <ol> 41 * <li>{@link android.content.pm.PackageManager#FEATURE_TELEPHONY} 42 * <li>A SIM that grants us carrier privileges is currently active in the device 43 * </ol> 44 * 45 * Just inherit from this class when writing your test, then you are able to assume in the subclass 46 * {@code Before} method that preconditions have all passed. The setup and test methods will not be 47 * executed if preconditions are not met. 48 */ 49 public abstract class BaseCarrierApiTest { 50 protected static final String NO_CARRIER_PRIVILEGES_FAILURE_MESSAGE = 51 "This test requires a SIM card with carrier privilege rules on it.\n" 52 + "Visit https://source.android.com/devices/tech/config/uicc.html"; 53 // More specific message when the test suite detects an outdated legacy SIM. 54 private static final String DEPRECATED_TEST_SIM_FAILURE_MESSAGE = 55 "This test requires a 2021-compliant SIM card with carrier privilege rules on it.\n" 56 + "The current SIM card appears to be outdated and is not compliant with the 2021" 57 + " CTS SIM specification published with Android 12 (\"S\").\n" 58 + "As of Android 13 (\"T\"), you must use a 2021-compliant SIM card to pass this" 59 + " suite. The 2021-compliant SIM is backward compatible with the legacy" 60 + " specification, so it may also be used to run this suite on older Android" 61 + " releases.\n" 62 + "2021-compliant SIMs received directly from Google have \"2021 CTS\" printed on" 63 + " them.\n" 64 + "Visit https://source.android.com/devices/tech/config/uicc#prepare_uicc"; 65 getContext()66 protected Context getContext() { 67 return InstrumentationRegistry.getInstrumentation().getTargetContext(); 68 } 69 70 private boolean mPreconditionsSatisfied = false; 71 werePreconditionsSatisfied()72 protected boolean werePreconditionsSatisfied() { 73 return mPreconditionsSatisfied; 74 } 75 76 /** 77 * Subclasses do NOT need to explicitly call or override this method. Per the JUnit docs, a 78 * superclass {@code Before} method always executes before a subclass {@code Before} method. 79 * 80 * <p>If preconditions fail, neither the subclass {@code Before} method(s) nor the actual {@code 81 * Test} method will execute, but {@code After} methods will still execute. If a subclass does 82 * work in an {@code After} method, then it should first check {@link 83 * #werePreconditionsSatisfied} and return early without doing any work if it's {@code false}. 84 */ 85 @Before ensurePreconditionsMet()86 public void ensurePreconditionsMet() { 87 mPreconditionsSatisfied = false; 88 // Bail out if no cellular support. 89 assumeTrue( 90 "No cellular support, CarrierAPI." 91 + getClass().getSimpleName() 92 + " cases will be skipped", 93 FeatureUtil.hasTelephony()); 94 95 // Wait for the broadcast queue to be handled completely to make sure 96 // CarrierPrivilegesTracker has received the ACTION_PACKAGE_ADDED for this test package and 97 // updated the carrier privileges in advance to avoid flakiness. 98 AmUtils.waitForBroadcastBarrier(); 99 100 // We must run with carrier privileges. As of 2022, all devices must run CTS with a SIM 101 // compliant with the 2021 spec, which has a new certificate. To make results very clear, we 102 // still explicitly check for the legacy certificate, and if we don't have carrier 103 // privileges but detect the legacy cert, we tell the tester they must upgrade to pass this 104 // test suite. 105 assertWithMessage( 106 UiccUtil.uiccHasCertificate(CTS_UICC_LEGACY) 107 ? DEPRECATED_TEST_SIM_FAILURE_MESSAGE 108 : NO_CARRIER_PRIVILEGES_FAILURE_MESSAGE) 109 .that(getContext().getSystemService(TelephonyManager.class).hasCarrierPrivileges()) 110 .isTrue(); 111 mPreconditionsSatisfied = true; 112 } 113 hasTelephonyCalling()114 protected boolean hasTelephonyCalling() { 115 return getContext().getPackageManager().hasSystemFeature( 116 PackageManager.FEATURE_TELEPHONY_CALLING); 117 } 118 } 119