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.graphics.Point;
     25 import android.util.Log;
     26 
     27 import java.util.concurrent.CountDownLatch;
     28 import java.util.concurrent.TimeUnit;
     29 
     30 /** Test verifying the Content View of the Assistant */
     31 public class AssistantContentViewTest extends AssistTestBase {
     32     private static final String TAG = "ContentViewTest";
     33     private BroadcastReceiver mReceiver;
     34     private CountDownLatch mContentViewLatch, mReadyLatch;
     35     private Intent mIntent;
     36 
     37     @Override
     38     public void setUp() throws Exception {
     39         super.setUp();
     40         mContentViewLatch = new CountDownLatch(1);
     41         mReadyLatch = new CountDownLatch(1);
     42         setUpAndRegisterReceiver();
     43         startTestActivity(Utils.VERIFY_CONTENT_VIEW);
     44     }
     45 
     46     @Override
     47     public void tearDown() throws Exception {
     48         super.tearDown();
     49         if (mReceiver != null) {
     50             mContext.unregisterReceiver(mReceiver);
     51             mReceiver = null;
     52         }
     53     }
     54 
     55     private void setUpAndRegisterReceiver() {
     56         if (mReceiver != null) {
     57             mContext.unregisterReceiver(mReceiver);
     58         }
     59         mReceiver = new AssistantContentViewReceiver();
     60         IntentFilter filter = new IntentFilter();
     61         filter.addAction(Utils.BROADCAST_CONTENT_VIEW_HEIGHT);
     62         filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED);
     63         mContext.registerReceiver(mReceiver, filter);
     64 
     65     }
     66 
     67     private void waitForContentView() throws Exception {
     68         Log.i(TAG, "waiting for the Assistant's Content View  before continuing");
     69         if (!mContentViewLatch.await(Utils.TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
     70             fail("failed to receive content view in " + Utils.TIMEOUT_MS + "msec");
     71         }
     72     }
     73 
     74     public void testAssistantContentViewDimens() throws Exception {
     75         if (mActivityManager.isLowRamDevice()) {
     76           Log.d(TAG, "Not running assist tests on low-RAM device.");
     77           return;
     78         }
     79         mTestActivity.startTest(Utils.VERIFY_CONTENT_VIEW);
     80         waitForAssistantToBeReady(mReadyLatch);
     81         startSession();
     82         waitForContentView();
     83         int height = mIntent.getIntExtra(Utils.EXTRA_CONTENT_VIEW_HEIGHT, 0);
     84         int width = mIntent.getIntExtra(Utils.EXTRA_CONTENT_VIEW_WIDTH, 0);
     85         Point displayPoint = (Point) mIntent.getParcelableExtra(Utils.EXTRA_DISPLAY_POINT);
     86         assertEquals(displayPoint.y, height);
     87         assertEquals(displayPoint.x, width);
     88     }
     89 
     90     private class AssistantContentViewReceiver extends BroadcastReceiver {
     91         @Override
     92         public void onReceive(Context context, Intent intent) {
     93             String action = intent.getAction();
     94             if (action.equals(Utils.BROADCAST_CONTENT_VIEW_HEIGHT)) {
     95                 mIntent = intent;
     96                 if (mContentViewLatch != null) {
     97                     mContentViewLatch.countDown();
     98                 }
     99             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
    100                 if (mReadyLatch != null) {
    101                     mReadyLatch.countDown();
    102                 }
    103             }
    104         }
    105     }
    106 }
    107