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.voicesettings.cts; 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.os.Bundle; 25 import android.test.ActivityInstrumentationTestCase2; 26 import android.util.Log; 27 28 import common.src.android.voicesettings.common.Utils; 29 30 import java.util.concurrent.CountDownLatch; 31 import java.util.concurrent.TimeUnit; 32 33 public class VoiceSettingsTestBase extends ActivityInstrumentationTestCase2<TestStartActivity> { 34 static final String TAG = "VoiceSettingsTestBase"; 35 protected static final int TIMEOUT_MS = 20 * 1000; 36 37 protected Context mContext; 38 protected Bundle mResultExtras; 39 private CountDownLatch mLatch; 40 private ActivityDoneReceiver mActivityDoneReceiver = null; 41 private TestStartActivity mActivity; 42 private Utils.TestcaseType mTestCaseType; 43 VoiceSettingsTestBase()44 public VoiceSettingsTestBase() { 45 super(TestStartActivity.class); 46 } 47 48 @Override setUp()49 protected void setUp() throws Exception { 50 super.setUp(); 51 mContext = getInstrumentation().getTargetContext(); 52 } 53 54 @Override tearDown()55 protected void tearDown() throws Exception { 56 mContext.unregisterReceiver(mActivityDoneReceiver); 57 super.tearDown(); 58 } 59 startTestActivity(String intentSuffix)60 protected void startTestActivity(String intentSuffix) { 61 Intent intent = new Intent(); 62 intent.setAction("android.intent.action.TEST_START_ACTIVITY_" + intentSuffix); 63 intent.setComponent(new ComponentName(getInstrumentation().getContext(), 64 TestStartActivity.class)); 65 setActivityIntent(intent); 66 mActivity = getActivity(); 67 } 68 registerBroadcastReceiver(Utils.TestcaseType testCaseType)69 protected void registerBroadcastReceiver(Utils.TestcaseType testCaseType) throws Exception { 70 mTestCaseType = testCaseType; 71 mLatch = new CountDownLatch(1); 72 if (mActivityDoneReceiver != null) { 73 mContext.unregisterReceiver(mActivityDoneReceiver); 74 } 75 mActivityDoneReceiver = new ActivityDoneReceiver(); 76 mContext.registerReceiver(mActivityDoneReceiver, 77 new IntentFilter(Utils.BROADCAST_INTENT + testCaseType.toString())); 78 } 79 startTestAndWaitForBroadcast(Utils.TestcaseType testCaseType)80 protected boolean startTestAndWaitForBroadcast(Utils.TestcaseType testCaseType) 81 throws Exception { 82 Log.i(TAG, "Begin Testing: " + testCaseType); 83 registerBroadcastReceiver(testCaseType); 84 mActivity.startTest(testCaseType.toString()); 85 if (!mLatch.await(TIMEOUT_MS, TimeUnit.MILLISECONDS)) { 86 fail("Failed to receive broadcast in " + TIMEOUT_MS + "msec"); 87 return false; 88 } 89 return true; 90 } 91 92 class ActivityDoneReceiver extends BroadcastReceiver { 93 @Override onReceive(Context context, Intent intent)94 public void onReceive(Context context, Intent intent) { 95 if (intent.getAction().equals( 96 Utils.BROADCAST_INTENT + 97 VoiceSettingsTestBase.this.mTestCaseType.toString())) { 98 Bundle extras = intent.getExtras(); 99 Log.i(TAG, "received_broadcast for " + Utils.toBundleString(extras)); 100 VoiceSettingsTestBase.this.mResultExtras = extras; 101 mLatch.countDown(); 102 } 103 } 104 } 105 } 106