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; 18 19 import static org.mockito.Mockito.doReturn; 20 import static org.mockito.Mockito.eq; 21 22 import android.content.Context; 23 import android.content.res.Resources; 24 import android.os.PersistableBundle; 25 import android.telephony.CarrierConfigManager; 26 import android.util.Log; 27 import android.util.SparseArray; 28 29 import org.mockito.Mock; 30 import org.mockito.MockitoAnnotations; 31 32 public abstract class CellBroadcastTest { 33 34 protected static String TAG; 35 36 private SparseArray<PersistableBundle> mBundles = new SparseArray<>(); 37 38 MockedServiceManager mMockedServiceManager; 39 40 @Mock 41 Context mContext; 42 @Mock 43 CarrierConfigManager mCarrierConfigManager; 44 @Mock 45 Resources mResources; 46 47 protected void setUp(String tag) throws Exception { 48 TAG = tag; 49 MockitoAnnotations.initMocks(this); 50 mMockedServiceManager = new MockedServiceManager(); 51 initContext(); 52 } 53 54 private void initContext() { 55 doReturn(mCarrierConfigManager).when(mContext) 56 .getSystemService(eq(Context.CARRIER_CONFIG_SERVICE)); 57 doReturn(mResources).when(mContext).getResources(); 58 } 59 60 void carrierConfigSetStringArray(int subId, String key, String[] values) { 61 if (mBundles.get(subId) == null) { 62 mBundles.put(subId, new PersistableBundle()); 63 } 64 mBundles.get(subId).putStringArray(key, values); 65 doReturn(mBundles.get(subId)).when(mCarrierConfigManager).getConfigForSubId(eq(subId)); 66 } 67 68 void putResources(int id, String[] values) { 69 doReturn(values).when(mResources).getStringArray(eq(id)); 70 } 71 72 protected void tearDown() throws Exception { 73 mMockedServiceManager.restoreAllServices(); 74 } 75 76 protected static void logd(String s) { 77 Log.d(TAG, s); 78 } 79 } 80