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