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.google.common.truth.Truth.assertWithMessage;
20 
21 import static org.junit.Assume.assumeTrue;
22 
23 import android.content.Context;
24 import android.telephony.TelephonyManager;
25 
26 import androidx.test.InstrumentationRegistry;
27 
28 import com.android.compatibility.common.util.FeatureUtil;
29 
30 import org.junit.Before;
31 
32 /**
33  * Common test base to ensure uniform preconditions checking. This class will check for:
34  *
35  * <ol>
36  *   <li>{@link android.content.pm.PackageManager#FEATURE_TELEPHONY}
37  *   <li>A SIM that grants us carrier privileges is currently active in the device
38  * </ol>
39  *
40  * Just inherit from this class when writing your test, then you are able to assume in the subclass
41  * {@code Before} method that preconditions have all passed. The setup and test methods will not be
42  * executed if preconditions are not met.
43  */
44 public abstract class BaseCarrierApiTest {
45     protected static final String NO_CARRIER_PRIVILEGES_FAILURE_MESSAGE =
46             "This test requires a SIM card with carrier privilege rules on it.\n"
47                     + "Visit https://source.android.com/devices/tech/config/uicc.html";
48 
getContext()49     protected Context getContext() {
50         return InstrumentationRegistry.getInstrumentation().getTargetContext();
51     }
52 
53     private boolean mPreconditionsSatisfied = false;
54 
werePreconditionsSatisfied()55     protected boolean werePreconditionsSatisfied() {
56         return mPreconditionsSatisfied;
57     }
58 
59     /**
60      * Subclasses do NOT need to explicitly call or override this method. Per the JUnit docs, a
61      * superclass {@code Before} method always executes before a subclass {@code Before} method.
62      *
63      * <p>If preconditions fail, neither the subclass {@code Before} method(s) nor the actual {@code
64      * Test} method will execute, but {@code After} methods will still execute. If a subclass does
65      * work in an {@code After} method, then it should first check {@link
66      * #werePreconditionsSatisfied} and return early without doing any work if it's {@code false}.
67      */
68     @Before
ensurePreconditionsMet()69     public void ensurePreconditionsMet() {
70         mPreconditionsSatisfied = false;
71         // Bail out if no cellular support.
72         assumeTrue(
73                 "No cellular support, CarrierAPI."
74                         + getClass().getSimpleName()
75                         + " cases will be skipped",
76                 FeatureUtil.hasTelephony());
77         // We must run with carrier privileges.
78         assertWithMessage(NO_CARRIER_PRIVILEGES_FAILURE_MESSAGE)
79                 .that(getContext().getSystemService(TelephonyManager.class).hasCarrierPrivileges())
80                 .isTrue();
81         mPreconditionsSatisfied = true;
82     }
83 }
84