1 /*
2  * Copyright (C) 2015 The Android Open Source Project
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 android.cts.util;
18 
19 import android.content.BroadcastReceiver;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.IntentFilter;
24 import android.content.pm.PackageManager;
25 import android.os.Bundle;
26 import android.cts.util.BroadcastTestStartActivity;
27 import android.cts.util.BroadcastUtils;
28 import android.test.ActivityInstrumentationTestCase2;
29 import android.util.Log;
30 
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.TimeUnit;
33 
34 public class BroadcastTestBase extends ActivityInstrumentationTestCase2<
35                                        BroadcastTestStartActivity> {
36     static final String TAG = "BroadcastTestBase";
37     protected static final int TIMEOUT_MS = 20 * 1000;
38 
39     protected Context mContext;
40     protected Bundle mResultExtras;
41     private CountDownLatch mLatch;
42     protected ActivityDoneReceiver mActivityDoneReceiver = null;
43     private BroadcastTestStartActivity mActivity;
44     private BroadcastUtils.TestcaseType mTestCaseType;
45     protected boolean mHasFeature;
46 
BroadcastTestBase()47     public BroadcastTestBase() {
48         super(BroadcastTestStartActivity.class);
49     }
50 
51     @Override
setUp()52     protected void setUp() throws Exception {
53         super.setUp();
54         mHasFeature = false;
55     }
56 
57     @Override
tearDown()58     protected void tearDown() throws Exception {
59         if (mHasFeature && mActivityDoneReceiver != null) {
60             try {
61                 mContext.unregisterReceiver(mActivityDoneReceiver);
62             } catch (IllegalArgumentException e) {
63                 // This exception is thrown if mActivityDoneReceiver in
64                 // the above call to unregisterReceiver is never registered.
65                 // If so, no harm done by ignoring this exception.
66             }
67             mActivityDoneReceiver = null;
68         }
69         super.tearDown();
70     }
71 
isIntentSupported(String intentStr)72     protected boolean isIntentSupported(String intentStr) {
73         Intent intent = new Intent(intentStr);
74         final PackageManager manager = mContext.getPackageManager();
75         assertNotNull(manager);
76         if (manager.resolveActivity(intent, 0) == null) {
77             Log.i(TAG, "No Activity found for the intent: " + intentStr);
78             return false;
79         }
80         return true;
81     }
82 
startTestActivity(String intentSuffix)83     protected void startTestActivity(String intentSuffix) {
84         Intent intent = new Intent();
85         intent.setAction("android.intent.action.TEST_START_ACTIVITY_" + intentSuffix);
86         intent.setComponent(new ComponentName(getInstrumentation().getContext(),
87                 BroadcastTestStartActivity.class));
88         setActivityIntent(intent);
89         mActivity = getActivity();
90     }
91 
registerBroadcastReceiver(BroadcastUtils.TestcaseType testCaseType)92     protected void registerBroadcastReceiver(BroadcastUtils.TestcaseType testCaseType) throws Exception {
93         mTestCaseType = testCaseType;
94         mLatch = new CountDownLatch(1);
95         mActivityDoneReceiver = new ActivityDoneReceiver();
96         mContext.registerReceiver(mActivityDoneReceiver,
97                 new IntentFilter(BroadcastUtils.BROADCAST_INTENT + testCaseType.toString()));
98     }
99 
startTestAndWaitForBroadcast(BroadcastUtils.TestcaseType testCaseType, String pkg, String cls)100     protected boolean startTestAndWaitForBroadcast(BroadcastUtils.TestcaseType testCaseType,
101                                                    String pkg, String cls) throws Exception {
102         Log.i(TAG, "Begin Testing: " + testCaseType);
103         registerBroadcastReceiver(testCaseType);
104         mActivity.startTest(testCaseType.toString(), pkg, cls);
105         if (!mLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
106             fail("Failed to receive broadcast in " + TIMEOUT_MS + "msec");
107             return false;
108         }
109         return true;
110     }
111 
112     class ActivityDoneReceiver extends BroadcastReceiver {
113         @Override
onReceive(Context context, Intent intent)114         public void onReceive(Context context, Intent intent) {
115             if (intent.getAction().equals(
116                     BroadcastUtils.BROADCAST_INTENT +
117                         BroadcastTestBase.this.mTestCaseType.toString())) {
118                 Bundle extras = intent.getExtras();
119                 Log.i(TAG, "received_broadcast for " + BroadcastUtils.toBundleString(extras));
120                 BroadcastTestBase.this.mResultExtras = extras;
121                 mLatch.countDown();
122             }
123         }
124     }
125 }
126