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