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