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.Mockito.doReturn; 21 22 import android.app.Service; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.content.ContextWrapper; 26 import android.content.Intent; 27 import android.content.res.Resources; 28 import android.telephony.CarrierConfigManager; 29 import android.test.ServiceTestCase; 30 import android.util.Log; 31 32 import org.junit.Before; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 36 public abstract class CellBroadcastServiceTestCase<T extends Service> extends ServiceTestCase<T> { 37 38 @Mock 39 protected CarrierConfigManager mMockedCarrierConfigManager; 40 @Mock 41 Resources mResources; 42 43 Intent mServiceIntentToVerify; 44 45 Intent mActivityIntentToVerify; 46 CellBroadcastServiceTestCase(Class<T> serviceClass)47 CellBroadcastServiceTestCase(Class<T> serviceClass) { 48 super(serviceClass); 49 } 50 waitForMs(long ms)51 protected static void waitForMs(long ms) { 52 try { 53 Thread.sleep(ms); 54 } catch (InterruptedException e) { 55 } 56 } 57 58 private class TestContextWrapper extends ContextWrapper { 59 60 private final String TAG = TestContextWrapper.class.getSimpleName(); 61 TestContextWrapper(Context base)62 public TestContextWrapper(Context base) { 63 super(base); 64 } 65 66 @Override startService(Intent service)67 public ComponentName startService(Intent service) { 68 mServiceIntentToVerify = service; 69 return null; 70 } 71 72 @Override getResources()73 public Resources getResources() { 74 return mResources; 75 } 76 77 @Override startActivity(Intent intent)78 public void startActivity(Intent intent) { 79 mActivityIntentToVerify = intent; 80 } 81 82 @Override getApplicationContext()83 public Context getApplicationContext() { 84 return this; 85 } 86 87 @Override getSystemService(String name)88 public Object getSystemService(String name) { 89 if (name.equals(Context.CARRIER_CONFIG_SERVICE)) { 90 Log.d(TAG, "return mocked svc for " + name + ", " + mMockedCarrierConfigManager); 91 return mMockedCarrierConfigManager; 92 } 93 Log.d(TAG, "return real service " + name); 94 return super.getSystemService(name); 95 } 96 } 97 98 @Before setUp()99 public void setUp() throws Exception { 100 MockitoAnnotations.initMocks(this); 101 mContext = new TestContextWrapper(getContext()); 102 setContext(mContext); 103 } 104 putResources(int id, String[] values)105 void putResources(int id, String[] values) { 106 doReturn(values).when(mResources).getStringArray(eq(id)); 107 } 108 } 109 110