1 /* 2 * Copyright (C) 2016 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 com.android.cellbroadcastreceiver.unit; 18 19 import static org.mockito.ArgumentMatchers.anyInt; 20 import static org.mockito.ArgumentMatchers.anyString; 21 import static org.mockito.Mockito.doReturn; 22 import static org.mockito.Mockito.eq; 23 24 import android.content.Context; 25 import android.content.SharedPreferences; 26 import android.content.res.Resources; 27 import android.os.PersistableBundle; 28 import android.os.RemoteException; 29 import android.telephony.CarrierConfigManager; 30 import android.telephony.SubscriptionManager; 31 import android.telephony.TelephonyManager; 32 import android.util.Log; 33 import android.util.SparseArray; 34 35 import com.android.cellbroadcastreceiver.CellBroadcastChannelManager; 36 import com.android.internal.telephony.ISub; 37 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 41 public abstract class CellBroadcastTest { 42 43 protected static String TAG; 44 45 private SparseArray<PersistableBundle> mBundles = new SparseArray<>(); 46 47 MockedServiceManager mMockedServiceManager; 48 49 @Mock 50 Context mContext; 51 @Mock 52 CarrierConfigManager mCarrierConfigManager; 53 @Mock 54 TelephonyManager mTelephonyManager; 55 @Mock 56 Resources mResources; 57 @Mock 58 ISub.Stub mSubService; 59 @Mock 60 SharedPreferences mSharedPreferences; 61 setUp(String tag)62 protected void setUp(String tag) throws Exception { 63 TAG = tag; 64 MockitoAnnotations.initMocks(this); 65 doReturn(mSharedPreferences).when(mContext).getSharedPreferences(anyString(), anyInt()); 66 doReturn(null).when(mSharedPreferences).getString(anyString(), anyString()); 67 // A hack to return mResources from static method 68 // CellBroadcastSettings.getResources(context). 69 doReturn(mSubService).when(mSubService).queryLocalInterface(anyString()); 70 doReturn(mSubService).when(mSubService).asBinder(); 71 72 mockDefaultSubId(SubscriptionManager.INVALID_SUBSCRIPTION_ID); 73 mMockedServiceManager = new MockedServiceManager(); 74 mMockedServiceManager.replaceService("isub", mSubService); 75 TelephonyManager.disableServiceHandleCaching(); 76 SubscriptionManager.clearCaches(); 77 SubscriptionManager.disableCaching(); 78 79 initContext(); 80 CellBroadcastChannelManager.clearAllCellBroadcastChannelRanges(); 81 } 82 initContext()83 private void initContext() { 84 doReturn(mCarrierConfigManager).when(mContext) 85 .getSystemService(eq(Context.CARRIER_CONFIG_SERVICE)); 86 doReturn(Context.TELEPHONY_SERVICE).when(mContext).getSystemServiceName( 87 TelephonyManager.class); 88 doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE); 89 doReturn(mResources).when(mContext).getResources(); 90 doReturn(mContext).when(mContext).getApplicationContext(); 91 doReturn(new String[]{""}).when(mResources).getStringArray(anyInt()); 92 } 93 mockDefaultSubId(int subId)94 protected void mockDefaultSubId(int subId) throws RemoteException { 95 try { 96 // Only exist after U-QPR2, so the reflection amounts to a QPR version check. 97 ISub.Stub.class.getMethod("getDefaultSubIdAsUser", int.class); 98 doReturn(subId).when(mSubService).getDefaultSubIdAsUser(anyInt()); 99 doReturn(subId).when(mSubService).getDefaultSmsSubIdAsUser(anyInt()); 100 } catch (Exception methodNotFound) { 101 logd("need to check if SubscriptionManagerService.getDefaultSubIdAsUser exist"); 102 doReturn(subId).when(mSubService).getDefaultSubId(); 103 doReturn(subId).when(mSubService).getDefaultSmsSubId(); 104 } 105 } 106 carrierConfigSetStringArray(int subId, String key, String[] values)107 void carrierConfigSetStringArray(int subId, String key, String[] values) { 108 if (mBundles.get(subId) == null) { 109 mBundles.put(subId, new PersistableBundle()); 110 } 111 mBundles.get(subId).putStringArray(key, values); 112 doReturn(mBundles.get(subId)).when(mCarrierConfigManager).getConfigForSubId(eq(subId)); 113 } 114 putResources(int id, String[] values)115 void putResources(int id, String[] values) { 116 doReturn(values).when(mResources).getStringArray(eq(id)); 117 } 118 putResources(int id, boolean values)119 void putResources(int id, boolean values) { 120 doReturn(values).when(mResources).getBoolean(eq(id)); 121 } 122 tearDown()123 protected void tearDown() throws Exception { 124 mMockedServiceManager.restoreAllServices(); 125 CellBroadcastChannelManager.clearAllCellBroadcastChannelRanges(); 126 } 127 logd(String s)128 protected static void logd(String s) { 129 Log.d(TAG, s); 130 } 131 } 132