• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (C) 2016 The Android Open Source Project
3  * <p>
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  * <p>
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * <p>
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.anyBoolean;
20 import static org.mockito.ArgumentMatchers.eq;
21 import static org.mockito.Matchers.anyInt;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.mock;
24 
25 import android.app.NotificationManager;
26 import android.app.Service;
27 import android.content.ComponentName;
28 import android.content.Context;
29 import android.content.ContextWrapper;
30 import android.content.Intent;
31 import android.content.SharedPreferences;
32 import android.content.pm.PackageManager;
33 import android.content.res.Configuration;
34 import android.content.res.Resources;
35 import android.media.AudioManager;
36 import android.os.PowerManager;
37 import android.os.SystemClock;
38 import android.os.Vibrator;
39 import android.telephony.CarrierConfigManager;
40 import android.telephony.SubscriptionInfo;
41 import android.telephony.SubscriptionManager;
42 import android.telephony.TelephonyManager;
43 import android.test.ServiceTestCase;
44 
45 import com.android.cellbroadcastreceiver.CellBroadcastChannelManager;
46 import com.android.cellbroadcastreceiver.CellBroadcastSettings;
47 import com.android.internal.telephony.ISub;
48 
49 import org.junit.After;
50 import org.junit.Before;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53 
54 import java.util.Arrays;
55 import java.util.function.BooleanSupplier;
56 
57 public abstract class CellBroadcastServiceTestCase<T extends Service> extends ServiceTestCase<T> {
58 
59     @Mock
60     protected CarrierConfigManager mMockedCarrierConfigManager;
61     @Mock
62     Resources mResources;
63     @Mock
64     protected ISub.Stub mSubService;
65     @Mock
66     protected AudioManager mMockedAudioManager;
67     @Mock
68     protected SubscriptionManager mMockedSubscriptionManager;
69     @Mock
70     protected SubscriptionInfo mMockSubscriptionInfo;
71     @Mock
72     protected TelephonyManager mMockedTelephonyManager;
73     @Mock
74     protected Vibrator mMockedVibrator;
75     @Mock
76     protected SharedPreferences mMockedSharedPreferences;
77     @Mock
78     protected Context mMockContextForRoaming;
79     @Mock
80     protected NotificationManager mMockedNotificationManager;
81     protected PowerManager mMockedPowerManager;
82 
83     protected Configuration mConfiguration;
84 
85     private PackageManager mPackageManager;
86 
87     MockedServiceManager mMockedServiceManager;
88 
89     Intent mServiceIntentToVerify;
90 
91     Intent mActivityIntentToVerify;
92 
CellBroadcastServiceTestCase(Class<T> serviceClass)93     CellBroadcastServiceTestCase(Class<T> serviceClass) {
94         super(serviceClass);
95     }
96 
waitFor(BooleanSupplier condition)97     protected static void waitFor(BooleanSupplier condition) {
98         if (condition.getAsBoolean()) {
99             return;
100         }
101         for (int i = 0; i < 50; i++) {
102             SystemClock.sleep(100);
103             if (condition.getAsBoolean()) {
104                 return;
105             }
106         }
107     }
108 
enablePreference(String pref)109     protected void enablePreference(String pref) {
110         doReturn(true).when(mMockedSharedPreferences).getBoolean(eq(pref), anyBoolean());
111     }
112 
disablePreference(String pref)113     protected void disablePreference(String pref) {
114         doReturn(false).when(mMockedSharedPreferences).getBoolean(eq(pref), anyBoolean());
115     }
116 
117     public class TestContextWrapper extends ContextWrapper {
118 
119         private final String TAG = TestContextWrapper.class.getSimpleName();
120 
TestContextWrapper(Context base)121         public TestContextWrapper(Context base) {
122             super(base);
123             mMockContextForRoaming = null;
124         }
125 
126         @Override
startService(Intent service)127         public ComponentName startService(Intent service) {
128             mServiceIntentToVerify = service;
129             return null;
130         }
131 
132         @Override
getResources()133         public Resources getResources() {
134             return mResources;
135         }
136 
137         @Override
startActivity(Intent intent)138         public void startActivity(Intent intent) {
139             mActivityIntentToVerify = intent;
140         }
141 
142         @Override
getApplicationContext()143         public Context getApplicationContext() {
144             return this;
145         }
146 
147         @Override
getSystemService(String name)148         public Object getSystemService(String name) {
149             switch (name) {
150                 case Context.CARRIER_CONFIG_SERVICE:
151                     return mMockedCarrierConfigManager;
152                 case Context.AUDIO_SERVICE:
153                     return mMockedAudioManager;
154                 case Context.TELEPHONY_SUBSCRIPTION_SERVICE:
155                     return mMockedSubscriptionManager;
156                 case Context.TELEPHONY_SERVICE:
157                     return mMockedTelephonyManager;
158                 case Context.VIBRATOR_SERVICE:
159                     return mMockedVibrator;
160                 case Context.NOTIFICATION_SERVICE:
161                     return mMockedNotificationManager;
162                 case Context.POWER_SERVICE:
163                     if (mMockedPowerManager != null) {
164                         return mMockedPowerManager;
165                     }
166                     break;
167             }
168             return super.getSystemService(name);
169         }
170 
171         @Override
getSystemServiceName(Class<?> serviceClass)172         public String getSystemServiceName(Class<?> serviceClass) {
173             if (TelephonyManager.class.equals(serviceClass)) {
174                 return Context.TELEPHONY_SERVICE;
175             }
176             return super.getSystemServiceName(serviceClass);
177         }
178 
179         @Override
getSharedPreferences(String name, int mode)180         public SharedPreferences getSharedPreferences(String name, int mode) {
181             return mMockedSharedPreferences;
182         }
183 
184         @Override
createConfigurationContext(Configuration overrideConfiguration)185         public Context createConfigurationContext(Configuration overrideConfiguration) {
186             if (mMockContextForRoaming == null) {
187                 return this;
188             } else {
189                 return mMockContextForRoaming;
190             }
191         }
192 
193         @Override
getPackageManager()194         public PackageManager getPackageManager() {
195             if (mPackageManager != null) {
196                 return mPackageManager;
197             }
198             return super.getPackageManager();
199         }
200 
201 
injectCreateConfigurationContext(Context context)202         public void injectCreateConfigurationContext(Context context) {
203             mMockContextForRoaming = context;
204         }
205 
206     }
207 
injectPackageManager(PackageManager packageManager)208     public void injectPackageManager(PackageManager packageManager) {
209         mPackageManager = packageManager;
210     }
211 
setWatchFeatureEnabled(boolean enabled)212     public void setWatchFeatureEnabled(boolean enabled) {
213         PackageManager mockPackageManager = mock(PackageManager.class);
214         doReturn(enabled).when(mockPackageManager).hasSystemFeature(PackageManager.FEATURE_WATCH);
215         injectPackageManager(mockPackageManager);
216     }
217 
218     @Before
setUp()219     public void setUp() throws Exception {
220         MockitoAnnotations.initMocks(this);
221         // A hack to return mResources from static method
222         // CellBroadcastSettings.getResources(context).
223         //doReturn(mSubService).when(mSubService).queryLocalInterface(anyString());
224         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubService).getDefaultSubId();
225         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(
226                 mSubService).getDefaultSmsSubId();
227 
228         doReturn(new String[]{""}).when(mResources).getStringArray(anyInt());
229 
230         mConfiguration = new Configuration();
231         doReturn(mConfiguration).when(mResources).getConfiguration();
232 
233         doReturn(1).when(mMockSubscriptionInfo).getSubscriptionId();
234         doReturn(Arrays.asList(mMockSubscriptionInfo)).when(mMockedSubscriptionManager)
235                 .getActiveSubscriptionInfoList();
236 
237         doReturn(mMockedTelephonyManager).when(mMockedTelephonyManager)
238                 .createForSubscriptionId(anyInt());
239 
240         mMockedServiceManager = new MockedServiceManager();
241         mMockedServiceManager.replaceService("isub", mSubService);
242 
243         mContext = new TestContextWrapper(getContext());
244         setContext(mContext);
245         CellBroadcastSettings.resetResourcesCache();
246         CellBroadcastChannelManager.clearAllCellBroadcastChannelRanges();
247     }
248 
249     @After
tearDown()250     public void tearDown() throws Exception {
251         mMockedServiceManager.restoreAllServices();
252         CellBroadcastChannelManager.clearAllCellBroadcastChannelRanges();
253     }
254 
putResources(int id, String[] values)255     void putResources(int id, String[] values) {
256         doReturn(values).when(mResources).getStringArray(eq(id));
257     }
258 
putResources(int id, boolean values)259     void putResources(int id, boolean values) {
260         doReturn(values).when(mResources).getBoolean(eq(id));
261     }
262 }
263