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