blob: f6b90b9e471faf6fd7412313af280cf225f21201 [file] [log] [blame]
Cameron Neale38de8fd2015-09-14 13:22:38 -07001/*
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
17package android.assist.cts;
18
19import android.assist.cts.TestStartActivity;
20import android.assist.common.Utils;
21
22import android.app.Activity;
23import android.content.BroadcastReceiver;
24import android.content.Context;
25import android.content.Intent;
26import android.content.IntentFilter;
27import android.test.ActivityInstrumentationTestCase2;
28import android.util.Log;
29
30import java.lang.Override;
31import java.util.concurrent.CountDownLatch;
32import java.util.concurrent.TimeUnit;
33
34/** Test that triggering the Assistant causes the underlying Activity to lose focus **/
35
36public 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
46 public void setUp() throws Exception {
47 super.setUp();
48 setUpAndRegisterReceiver();
49 startTestActivity(TEST_CASE_TYPE);
50 }
51
52 @Override
53 public void tearDown() throws Exception {
54 super.tearDown();
55 if (mReceiver != null) {
56 mContext.unregisterReceiver(mReceiver);
57 mReceiver = null;
58 }
59 }
60
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
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
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
88 public void testLayerCausesUnderlyingActivityToLoseFocus() throws Exception {
89 mTestActivity.startTest(Utils.FOCUS_CHANGE);
90 waitForAssistantToBeReady(mReadyLatch);
91 mTestActivity.start3pApp(Utils.FOCUS_CHANGE);
92 waitToGainFocus();
93 startSession();
94 waitToLoseFocus();
95 }
96
97 private class FocusChangeTestReceiver extends BroadcastReceiver {
98 @Override
99 public void onReceive(Context context, Intent intent) {
100 String action = intent.getAction();
101 if (action.equals(Utils.GAINED_FOCUS) && mHasGainedFocusLatch != null) {
102 mHasGainedFocusLatch.countDown();
103 } else if (action.equals(Utils.LOST_FOCUS) && mHasLostFocusLatch != null) {
104 mHasLostFocusLatch.countDown();
105 } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
106 if (mReadyLatch != null) {
107 mReadyLatch.countDown();
108 }
109 }
110 }
111 }
112}