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