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.Matchers.anyInt;
20 import static org.mockito.Matchers.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.res.Resources;
26 import android.os.PersistableBundle;
27 import android.telephony.CarrierConfigManager;
28 import android.telephony.SubscriptionManager;
29 import android.telephony.TelephonyManager;
30 import android.util.Log;
31 import android.util.SparseArray;
32 
33 import com.android.internal.telephony.ISub;
34 
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 
38 public abstract class CellBroadcastTest {
39 
40     protected static String TAG;
41 
42     private SparseArray<PersistableBundle> mBundles = new SparseArray<>();
43 
44     MockedServiceManager mMockedServiceManager;
45 
46     @Mock
47     Context mContext;
48     @Mock
49     CarrierConfigManager mCarrierConfigManager;
50     @Mock
51     TelephonyManager mTelephonyManager;
52     @Mock
53     Resources mResources;
54     @Mock
55     ISub.Stub mSubService;
56 
setUp(String tag)57     protected void setUp(String tag) throws Exception {
58         TAG = tag;
59         MockitoAnnotations.initMocks(this);
60         // A hack to return mResources from static method
61         // CellBroadcastSettings.getResources(context).
62         doReturn(mSubService).when(mSubService).queryLocalInterface(anyString());
63         doReturn(mSubService).when(mSubService).asBinder();
64         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubService).getDefaultSubId();
65         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubService).getDefaultSmsSubId();
66         mMockedServiceManager = new MockedServiceManager();
67         mMockedServiceManager.replaceService("isub", mSubService);
68         TelephonyManager.disableServiceHandleCaching();
69         SubscriptionManager.clearCaches();
70         SubscriptionManager.disableCaching();
71 
72         initContext();
73     }
74 
initContext()75     private void initContext() {
76         doReturn(mCarrierConfigManager).when(mContext)
77                 .getSystemService(eq(Context.CARRIER_CONFIG_SERVICE));
78         doReturn(Context.TELEPHONY_SERVICE).when(mContext).getSystemServiceName(
79                 TelephonyManager.class);
80         doReturn(mTelephonyManager).when(mContext).getSystemService(Context.TELEPHONY_SERVICE);
81         doReturn(mResources).when(mContext).getResources();
82         doReturn(mContext).when(mContext).getApplicationContext();
83         doReturn(new String[]{""}).when(mResources).getStringArray(anyInt());
84         doReturn(TelephonyManager.SIM_STATE_LOADED).when(mTelephonyManager)
85                 .getSimApplicationState(anyInt());
86     }
87 
carrierConfigSetStringArray(int subId, String key, String[] values)88     void carrierConfigSetStringArray(int subId, String key, String[] values) {
89         if (mBundles.get(subId) == null) {
90             mBundles.put(subId, new PersistableBundle());
91         }
92         mBundles.get(subId).putStringArray(key, values);
93         doReturn(mBundles.get(subId)).when(mCarrierConfigManager).getConfigForSubId(eq(subId));
94     }
95 
putResources(int id, String[] values)96     void putResources(int id, String[] values) {
97         doReturn(values).when(mResources).getStringArray(eq(id));
98     }
99 
tearDown()100     protected void tearDown() throws Exception {
101         mMockedServiceManager.restoreAllServices();
102     }
103 
logd(String s)104     protected static void logd(String s) {
105         Log.d(TAG, s);
106     }
107 }
108