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 package android.assist.cts;
     17 
     18 import android.assist.common.Utils;
     19 import android.content.BroadcastReceiver;
     20 import android.content.Context;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.os.Bundle;
     24 import android.util.Log;
     25 
     26 import java.util.concurrent.CountDownLatch;
     27 import java.util.concurrent.TimeUnit;
     28 
     29 public class ExtraAssistDataTest extends AssistTestBase {
     30     private static final String TAG = "ExtraAssistDataTest";
     31     private static final String TEST_CASE_TYPE = Utils.EXTRA_ASSIST;
     32 
     33     private BroadcastReceiver mReceiver;
     34     private CountDownLatch mHasResumedLatch = new CountDownLatch(1);
     35     private CountDownLatch mReadyLatch = new CountDownLatch(1);
     36 
     37     public ExtraAssistDataTest() {
     38         super();
     39     }
     40 
     41     @Override
     42     public void setUp() throws Exception {
     43         super.setUp();
     44         setUpAndRegisterReceiver();
     45         startTestActivity(TEST_CASE_TYPE);
     46     }
     47 
     48     @Override
     49     public void tearDown() throws Exception {
     50         super.tearDown();
     51         if (mReceiver != null) {
     52             mContext.unregisterReceiver(mReceiver);
     53             mReceiver = null;
     54         }
     55     }
     56 
     57     private void setUpAndRegisterReceiver() {
     58         if (mReceiver != null) {
     59             mContext.unregisterReceiver(mReceiver);
     60         }
     61         mReceiver = new ExtraAssistDataReceiver();
     62         IntentFilter filter = new IntentFilter();
     63         filter.addAction(Utils.APP_3P_HASRESUMED);
     64         filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED);
     65         mContext.registerReceiver(mReceiver, filter);
     66     }
     67 
     68     public void testAssistContentAndAssistData() throws Exception {
     69         if (mActivityManager.isLowRamDevice()) {
     70             Log.d(TAG, "Not running assist tests on low-RAM device.");
     71             return;
     72         }
     73         mTestActivity.startTest(TEST_CASE_TYPE);
     74         waitForAssistantToBeReady(mReadyLatch);
     75         mTestActivity.start3pApp(TEST_CASE_TYPE);
     76         waitForOnResume();
     77         startSession();
     78         waitForContext();
     79         verifyAssistDataNullness(false, false, false, false);
     80 
     81         Log.i(TAG, "assist bundle is: " + Utils.toBundleString(mAssistBundle));
     82 
     83         // tests that the assist content's structured data is the expected
     84         assertEquals("AssistContent structured data did not match data in onProvideAssistContent",
     85                 Utils.getStructuredJSON(), mAssistContent.getStructuredData());
     86         // tests the assist data. EXTRA_ASSIST_CONTEXT is what's expected.
     87         Bundle extraExpectedBundle = Utils.getExtraAssistBundle();
     88         Bundle extraAssistBundle = mAssistBundle.getBundle(Intent.EXTRA_ASSIST_CONTEXT);
     89         for (String key : extraExpectedBundle.keySet()) {
     90             assertTrue("Assist bundle does not contain expected extra context key: " + key,
     91                     extraAssistBundle.containsKey(key));
     92             assertEquals("Extra assist context bundle values do not match for key: " + key,
     93                     extraExpectedBundle.get(key), extraAssistBundle.get(key));
     94         }
     95     }
     96 
     97     private void waitForOnResume() throws Exception {
     98         Log.i(TAG, "waiting for onResume() before continuing");
     99         if (!mHasResumedLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
    100             fail("Activity failed to resume in " + Utils.ACTIVITY_ONRESUME_TIMEOUT_MS + "msec");
    101         }
    102     }
    103 
    104     private class ExtraAssistDataReceiver extends BroadcastReceiver {
    105         @Override
    106         public void onReceive(Context context, Intent intent) {
    107             String action = intent.getAction();
    108             if (action.equals(Utils.APP_3P_HASRESUMED)) {
    109                 if (mHasResumedLatch != null) {
    110                     mHasResumedLatch.countDown();
    111                 }
    112             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
    113                 if (mReadyLatch != null) {
    114                     mReadyLatch.countDown();
    115                 }
    116             }
    117         }
    118     }
    119 }
    120