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.carrierdefaultapp;
18 
19 import android.app.Activity;
20 import android.content.ComponentName;
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 
27 import org.mockito.MockitoAnnotations;
28 
29 import java.util.HashMap;
30 
31 public class CarrierDefaultActivityTestCase<T extends Activity> extends ActivityUnitTestCase<T> {
32 
33     protected TestContext mContext;
34 
35     private T mActivity;
36 
CarrierDefaultActivityTestCase(Class<T> activityClass)37     CarrierDefaultActivityTestCase(Class<T> activityClass) {
38         super(activityClass);
39     }
40 
41     @Override
setUp()42     protected void setUp() throws Exception {
43         super.setUp();
44         MockitoAnnotations.initMocks(this);
45         mContext = new TestContext(getInstrumentation().getTargetContext());
46         setActivityContext(mContext);
47     }
48 
49     @Override
tearDown()50     protected void tearDown() throws Exception {
51         super.tearDown();
52     }
53 
startActivity()54     protected T startActivity() throws Throwable {
55         runTestOnUiThread(new Runnable() {
56             @Override
57             public void run() {
58                 mActivity = startActivity(createActivityIntent(), null, null);
59             }
60         });
61         return mActivity;
62     }
63 
stopActivity()64     protected void stopActivity() throws Exception {
65         getInstrumentation().callActivityOnStop(mActivity);
66     }
67 
createActivityIntent()68     protected Intent createActivityIntent() {
69         Intent intent = new Intent();
70         return intent;
71     }
72 
injectSystemService(Class<S> cls, S service)73     protected <S> void injectSystemService(Class<S> cls, S service) {
74         mContext.injectSystemService(cls, service);
75     }
76 }