Home | History | Annotate | Download | only in cts
      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.common.Utils;
     20 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.util.Log;
     25 
     26 import java.util.concurrent.CountDownLatch;
     27 import java.util.concurrent.TimeUnit;
     28 
     29 /**
     30  * Test we receive proper assist data (root assistStructure with no children) when the assistant is
     31  * invoked on an app with FLAG_SECURE set.
     32  */
     33 public class FlagSecureTest extends AssistTestBase {
     34     static final String TAG = "FlagSecureTest";
     35 
     36     private static final String TEST_CASE_TYPE = Utils.FLAG_SECURE;
     37 
     38     private BroadcastReceiver mReceiver;
     39     private CountDownLatch mHasResumedLatch = new CountDownLatch(1);
     40     private CountDownLatch mReadyLatch = new CountDownLatch(1);
     41     public FlagSecureTest() {
     42         super();
     43     }
     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 FlagSecureTestBroadcastReceiver();
     66         IntentFilter filter = new IntentFilter();
     67         filter.addAction(Utils.FLAG_SECURE_HASRESUMED);
     68         filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED);
     69         mContext.registerReceiver(mReceiver, filter);
     70     }
     71 
     72     private void waitForOnResume() throws Exception {
     73         Log.i(TAG, "waiting for onResume() before continuing");
     74         if (!mHasResumedLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
     75             fail("Activity failed to resume in " + Utils.ACTIVITY_ONRESUME_TIMEOUT_MS + "msec");
     76         }
     77     }
     78 
     79     public void testSecureActivity() throws Exception {
     80         if (mActivityManager.isLowRamDevice()) {
     81             Log.d(TAG, "Not running assist tests on low-RAM device.");
     82             return;
     83         }
     84         mTestActivity.startTest(TEST_CASE_TYPE);
     85         waitForAssistantToBeReady(mReadyLatch);
     86         mTestActivity.start3pApp(TEST_CASE_TYPE);
     87         waitForOnResume();
     88         startSession();
     89         waitForContext();
     90         verifyAssistDataNullness(false, false, false, false);
     91         // verify that we have only the root window and not its children.
     92         verifyAssistStructure(Utils.getTestAppComponent(TEST_CASE_TYPE), true);
     93     }
     94 
     95     private class FlagSecureTestBroadcastReceiver extends BroadcastReceiver {
     96         @Override
     97         public void onReceive(Context context, Intent intent) {
     98             String action = intent.getAction();
     99             if (action.equals(Utils.FLAG_SECURE_HASRESUMED)) {
    100                 if (mHasResumedLatch != null) {
    101                     mHasResumedLatch.countDown();
    102                 }
    103             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
    104                 if (mReadyLatch != null) {
    105                     mReadyLatch.countDown();
    106                 }
    107             }
    108         }
    109     }
    110 }
    111