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