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.assist.cts; 18 19 import android.assist.cts.TestStartActivity; 20 import android.assist.common.Utils; 21 22 import android.app.Activity; 23 import android.content.BroadcastReceiver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.content.IntentFilter; 27 import android.test.ActivityInstrumentationTestCase2; 28 import android.util.Log; 29 30 import java.lang.Override; 31 import java.util.concurrent.CountDownLatch; 32 import java.util.concurrent.TimeUnit; 33 34 /** Test that triggering the Assistant causes the underlying Activity to lose focus **/ 35 36 public class FocusChangeTest extends AssistTestBase { 37 private static final String TAG = "FocusChangeTest"; 38 private static final String TEST_CASE_TYPE = Utils.FOCUS_CHANGE; 39 40 private BroadcastReceiver mReceiver; 41 private CountDownLatch mHasGainedFocusLatch = new CountDownLatch(1); 42 private CountDownLatch mHasLostFocusLatch = new CountDownLatch(1); 43 private CountDownLatch mReadyLatch = new CountDownLatch(1); 44 45 @Override setUp()46 public void setUp() throws Exception { 47 super.setUp(); 48 setUpAndRegisterReceiver(); 49 startTestActivity(TEST_CASE_TYPE); 50 } 51 52 @Override tearDown()53 public void tearDown() throws Exception { 54 super.tearDown(); 55 if (mReceiver != null) { 56 mContext.unregisterReceiver(mReceiver); 57 mReceiver = null; 58 } 59 } 60 setUpAndRegisterReceiver()61 private void setUpAndRegisterReceiver() { 62 if (mReceiver != null) { 63 mContext.unregisterReceiver(mReceiver); 64 } 65 mReceiver = new FocusChangeTestReceiver(); 66 IntentFilter filter = new IntentFilter(); 67 filter.addAction(Utils.GAINED_FOCUS); 68 filter.addAction(Utils.LOST_FOCUS); 69 filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED); 70 mContext.registerReceiver(mReceiver, filter); 71 } 72 waitToGainFocus()73 private void waitToGainFocus() throws Exception { 74 Log.i(TAG, "Waiting for the underlying activity to gain focus before continuing."); 75 if (!mHasGainedFocusLatch.await(Utils.TIMEOUT_MS, TimeUnit.MILLISECONDS)) { 76 fail("Activity failed to gain focus in " + Utils.TIMEOUT_MS + "msec."); 77 } 78 } 79 waitToLoseFocus()80 private void waitToLoseFocus() throws Exception { 81 Log.i(TAG, "Waiting for the underlying activity to lose focus."); 82 if (!mHasLostFocusLatch.await(Utils.TIMEOUT_MS, TimeUnit.MILLISECONDS)) { 83 fail("Activity maintained focus despite the Assistant Firing" 84 + Utils.TIMEOUT_MS + "msec."); 85 } 86 } 87 testLayerCausesUnderlyingActivityToLoseFocus()88 public void testLayerCausesUnderlyingActivityToLoseFocus() throws Exception { 89 if (mActivityManager.isLowRamDevice()) { 90 Log.d(TAG, "Not running assist tests on low-RAM device."); 91 return; 92 } 93 mTestActivity.startTest(Utils.FOCUS_CHANGE); 94 waitForAssistantToBeReady(mReadyLatch); 95 mTestActivity.start3pApp(Utils.FOCUS_CHANGE); 96 waitToGainFocus(); 97 startSession(); 98 waitToLoseFocus(); 99 } 100 101 private class FocusChangeTestReceiver extends BroadcastReceiver { 102 @Override onReceive(Context context, Intent intent)103 public void onReceive(Context context, Intent intent) { 104 String action = intent.getAction(); 105 if (action.equals(Utils.GAINED_FOCUS) && mHasGainedFocusLatch != null) { 106 mHasGainedFocusLatch.countDown(); 107 } else if (action.equals(Utils.LOST_FOCUS) && mHasLostFocusLatch != null) { 108 mHasLostFocusLatch.countDown(); 109 } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) { 110 if (mReadyLatch != null) { 111 mReadyLatch.countDown(); 112 } 113 } 114 } 115 } 116 } 117