1 /* 2 * Copyright (C) 2024 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.cts; 18 19 import static com.android.internal.telephony.RILConstants.RIL_REQUEST_RADIO_POWER; 20 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertTrue; 23 import static org.junit.Assume.assumeTrue; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.platform.test.flag.junit.CheckFlagsRule; 28 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 29 import android.telephony.TelephonyManager; 30 import android.telephony.mockmodem.MockModemManager; 31 import android.util.Log; 32 33 import androidx.test.InstrumentationRegistry; 34 35 import org.junit.After; 36 import org.junit.Before; 37 import org.junit.Rule; 38 39 /** 40 * Base class for test cases performed on MockModem. 41 */ 42 public class MockModemTestBase { 43 @Rule 44 public final CheckFlagsRule mCheckFlagsRule = 45 DeviceFlagsValueProvider.createCheckFlagsRule(); 46 47 private static final String TAG = "MockModemTestBase"; 48 private static final String RESOURCE_PACKAGE_NAME = "android"; 49 50 protected static final boolean VDBG = true; 51 52 protected static MockModemManager sMockModemManager; 53 protected static TelephonyManager sTelephonyManager; 54 protected static boolean sIsMultiSimDevice; 55 beforeAllTestsCheck()56 protected static boolean beforeAllTestsCheck() throws Exception { 57 if (VDBG) Log.d(TAG, "beforeAllTests()"); 58 59 if (!hasTelephonyFeature()) { 60 Log.d(TAG, "Skipping test that requires FEATURE_TELEPHONY"); 61 return false; 62 } 63 MockModemManager.enforceMockModemDeveloperSetting(); 64 sTelephonyManager = 65 (TelephonyManager) getContext().getSystemService(Context.TELEPHONY_SERVICE); 66 sIsMultiSimDevice = isMultiSim(sTelephonyManager); 67 68 return true; 69 } 70 createMockModemAndConnectToService()71 protected static void createMockModemAndConnectToService() throws Exception { 72 sMockModemManager = new MockModemManager(); 73 assertNotNull(sMockModemManager); 74 assertTrue(sMockModemManager.connectMockModemService()); 75 } 76 afterAllTestsBase()77 protected static boolean afterAllTestsBase() throws Exception { 78 if (VDBG) Log.d(TAG, "afterAllTestsBase()"); 79 80 if (!hasTelephonyFeature()) { 81 return false; 82 } 83 84 // Rebind all interfaces which is binding to MockModemService to default. 85 assertNotNull(sMockModemManager); 86 // Reset the modified error response of RIL_REQUEST_RADIO_POWER to the original behavior 87 // and -1 means to disable the modifed mechanism in mock modem 88 sMockModemManager.forceErrorResponse(0, RIL_REQUEST_RADIO_POWER, -1); 89 assertTrue(sMockModemManager.disconnectMockModemService()); 90 sMockModemManager = null; 91 92 return true; 93 } 94 95 @Before beforeTest()96 public void beforeTest() { 97 if (VDBG) Log.d(TAG, "beforeTest"); 98 assumeTrue(hasTelephonyFeature()); 99 } 100 101 @After afterTest()102 public void afterTest() { 103 if (VDBG) Log.d(TAG, "afterTest"); 104 } 105 hasTelephonyFeature()106 protected static boolean hasTelephonyFeature() { 107 final PackageManager pm = getContext().getPackageManager(); 108 if (!pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) { 109 return false; 110 } 111 return true; 112 } 113 hasTelephonyFeature(String featureName)114 protected static boolean hasTelephonyFeature(String featureName) { 115 final PackageManager pm = getContext().getPackageManager(); 116 if (!pm.hasSystemFeature(featureName)) { 117 return false; 118 } 119 return true; 120 } 121 getContext()122 protected static Context getContext() { 123 return InstrumentationRegistry.getInstrumentation().getContext(); 124 } 125 isMultiSim(TelephonyManager tm)126 protected static boolean isMultiSim(TelephonyManager tm) { 127 return tm != null && tm.getPhoneCount() > 1; 128 } 129 isSimHotSwapCapable()130 protected static boolean isSimHotSwapCapable() { 131 boolean isSimHotSwapCapable = false; 132 int resourceId = 133 getContext() 134 .getResources() 135 .getIdentifier("config_hotswapCapable", "bool", RESOURCE_PACKAGE_NAME); 136 137 if (resourceId > 0) { 138 isSimHotSwapCapable = getContext().getResources().getBoolean(resourceId); 139 } else { 140 Log.d(TAG, "Fail to get the resource Id, using default."); 141 } 142 143 Log.d(TAG, "isSimHotSwapCapable = " + (isSimHotSwapCapable ? "true" : "false")); 144 145 return isSimHotSwapCapable; 146 } 147 } 148