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