• 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;
18 
19 import static org.mockito.ArgumentMatchers.eq;
20 import static org.mockito.Matchers.anyString;
21 import static org.mockito.Mockito.doReturn;
22 
23 import android.app.Service;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.content.ContextWrapper;
27 import android.content.Intent;
28 import android.content.res.Resources;
29 import android.telephony.CarrierConfigManager;
30 import android.telephony.SubscriptionManager;
31 import android.test.ServiceTestCase;
32 import android.util.Log;
33 
34 import com.android.internal.telephony.ISub;
35 
36 import org.junit.After;
37 import org.junit.Before;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 
41 public abstract class CellBroadcastServiceTestCase<T extends Service> extends ServiceTestCase<T> {
42 
43     @Mock
44     protected CarrierConfigManager mMockedCarrierConfigManager;
45     @Mock
46     Resources mResources;
47     @Mock
48     protected ISub.Stub mSubService;
49 
50     MockedServiceManager mMockedServiceManager;
51 
52     Intent mServiceIntentToVerify;
53 
54     Intent mActivityIntentToVerify;
55 
CellBroadcastServiceTestCase(Class<T> serviceClass)56     CellBroadcastServiceTestCase(Class<T> serviceClass) {
57         super(serviceClass);
58     }
59 
waitForMs(long ms)60     protected static void waitForMs(long ms) {
61         try {
62             Thread.sleep(ms);
63         } catch (InterruptedException e) {
64         }
65     }
66 
67     private class TestContextWrapper extends ContextWrapper {
68 
69         private final String TAG = TestContextWrapper.class.getSimpleName();
70 
TestContextWrapper(Context base)71         public TestContextWrapper(Context base) {
72             super(base);
73         }
74 
75         @Override
startService(Intent service)76         public ComponentName startService(Intent service) {
77             mServiceIntentToVerify = service;
78             return null;
79         }
80 
81         @Override
getResources()82         public Resources getResources() {
83             return mResources;
84         }
85 
86         @Override
startActivity(Intent intent)87         public void startActivity(Intent intent) {
88             mActivityIntentToVerify = intent;
89         }
90 
91         @Override
getApplicationContext()92         public Context getApplicationContext() {
93             return this;
94         }
95 
96         @Override
getSystemService(String name)97         public Object getSystemService(String name) {
98             if (name.equals(Context.CARRIER_CONFIG_SERVICE)) {
99                 Log.d(TAG, "return mocked svc for " + name + ", " + mMockedCarrierConfigManager);
100                 return mMockedCarrierConfigManager;
101             }
102             Log.d(TAG, "return real service " + name);
103             return super.getSystemService(name);
104         }
105     }
106 
107     @Before
setUp()108     public void setUp() throws Exception {
109         MockitoAnnotations.initMocks(this);
110         // A hack to return mResources from static method
111         // CellBroadcastSettings.getResourcesForDefaultSmsSubscriptionId(context).
112         doReturn(mSubService).when(mSubService).queryLocalInterface(anyString());
113         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubService).getDefaultSubId();
114         doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubService).getDefaultSmsSubId();
115         mMockedServiceManager = new MockedServiceManager();
116         mMockedServiceManager.replaceService("isub", mSubService);
117         mContext = new TestContextWrapper(getContext());
118         setContext(mContext);
119     }
120 
121     @After
tearDown()122     public void tearDown() throws Exception {
123         mMockedServiceManager.restoreAllServices();
124     }
125 
putResources(int id, String[] values)126     void putResources(int id, String[] values) {
127         doReturn(values).when(mResources).getStringArray(eq(id));
128     }
129 }
130