1 /* 2 * Copyright (C) 2016 Google Inc. 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 android.app.Activity; 20 import android.app.ResourcesManager; 21 import android.content.Context; 22 import android.content.ContextWrapper; 23 import android.content.Intent; 24 import android.test.ActivityUnitTestCase; 25 import android.util.Log; 26 import android.view.Display; 27 28 import java.util.HashMap; 29 30 public class CellBroadcastActivityTestCase<T extends Activity> extends ActivityUnitTestCase<T> { 31 32 protected TestContext mContext; 33 34 private T mActivity; 35 CellBroadcastActivityTestCase(Class<T> activityClass)36 CellBroadcastActivityTestCase(Class<T> activityClass) { 37 super(activityClass); 38 } 39 40 @Override setUp()41 protected void setUp() throws Exception { 42 super.setUp(); 43 mContext = new TestContext(getInstrumentation().getTargetContext()); 44 setActivityContext(mContext); 45 } 46 47 @Override tearDown()48 protected void tearDown() throws Exception { 49 super.tearDown(); 50 } 51 startActivity()52 protected T startActivity() throws Throwable { 53 runTestOnUiThread(new Runnable() { 54 @Override 55 public void run() { 56 mActivity = startActivity(createActivityIntent(), null, null); 57 } 58 }); 59 return mActivity; 60 } 61 stopActivity()62 protected void stopActivity() throws Exception { 63 getInstrumentation().callActivityOnStop(mActivity); 64 } 65 waitForMs(long ms)66 public static void waitForMs(long ms) { 67 try { 68 Thread.sleep(ms); 69 } catch (InterruptedException e) { 70 } 71 } 72 createActivityIntent()73 protected Intent createActivityIntent() { 74 Intent intent = new Intent(); 75 return intent; 76 } 77 injectSystemService(Class<S> cls, S service)78 protected <S> void injectSystemService(Class<S> cls, S service) { 79 mContext.injectSystemService(cls, service); 80 } 81 82 public static class TestContext extends ContextWrapper { 83 84 private static final String TAG = TestContext.class.getSimpleName(); 85 86 private HashMap<String, Object> mInjectedSystemServices = new HashMap<>(); 87 TestContext(Context base)88 public TestContext(Context base) { 89 super(base); 90 } 91 injectSystemService(Class<S> cls, S service)92 public <S> void injectSystemService(Class<S> cls, S service) { 93 final String name = getSystemServiceName(cls); 94 mInjectedSystemServices.put(name, service); 95 } 96 97 @Override getDisplay()98 public Display getDisplay() { 99 return ResourcesManager.getInstance().getAdjustedDisplay(Display.DEFAULT_DISPLAY, 100 null); 101 } 102 103 @Override getApplicationContext()104 public Context getApplicationContext() { 105 return this; 106 } 107 108 @Override getSystemService(String name)109 public Object getSystemService(String name) { 110 if (mInjectedSystemServices.containsKey(name)) { 111 Log.d(TAG, "return mocked system service for " + name); 112 return mInjectedSystemServices.get(name); 113 } 114 Log.d(TAG, "return real system service for " + name); 115 return super.getSystemService(name); 116 } 117 } 118 } 119