Verify the dimension of the VoiceInteractionSession's
content view
- Also added null checks around CountDownLatches to avoid NPEs that
resulted from acting on broadcasts before the TestCases were ready.

Bug: 21668302

Change-Id: I14bd4b3b85d02d738523e3c9dcb88709baf1ce2f
diff --git a/tests/expectations/knownfailures.txt b/tests/expectations/knownfailures.txt
index 1ea05b3..345279f 100644
--- a/tests/expectations/knownfailures.txt
+++ b/tests/expectations/knownfailures.txt
@@ -300,7 +300,8 @@
   description: "New assist tests that do not yet have a track record.",
   names: [
     "android.assist.cts.ScreenshotTest",
-    "android.assist.cts.ExtraAssistDataTest"
+    "android.assist.cts.ExtraAssistDataTest",
+    "android.assist.cts.AssistantContentViewTest"
   ],
   bug: 21668302
 }
diff --git a/tests/tests/assist/common/src/android/assist/common/Utils.java b/tests/tests/assist/common/src/android/assist/common/Utils.java
index ce3685e..cc45dbd 100644
--- a/tests/tests/assist/common/src/android/assist/common/Utils.java
+++ b/tests/tests/assist/common/src/android/assist/common/Utils.java
@@ -32,7 +32,10 @@
     public static final String BROADCAST_ASSIST_DATA_INTENT = ACTION_PREFIX + "ASSIST_DATA";
     public static final String BROADCAST_INTENT_START_ASSIST = ACTION_PREFIX + "START_ASSIST";
     public static final String ASSIST_RECEIVER_REGISTERED = ACTION_PREFIX + "ASSIST_READY";
+
     public static final String ACTION_INVALIDATE = "invalidate_action";
+    public static final String GET_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "GET_CONTENT_VIEW_HEIGHT";
+    public static final String BROADCAST_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "VIEW_HEIGHT";
     public static final String TEST_ERROR = "Error In Test:";
 
     public static final String ASSIST_STRUCTURE_KEY = "assist_structure";
@@ -64,6 +67,11 @@
 
     public static final String EXTRA_REGISTER_RECEIVER = "register_receiver";
 
+    /** Extras for passing the Assistant's ContentView's dimensions*/
+    public static final String EXTRA_CONTENT_VIEW_HEIGHT = "extra_content_view_height";
+    public static final String EXTRA_CONTENT_VIEW_WIDTH = "extra_content_view_width";
+    public static final String EXTRA_DISPLAY_POINT = "extra_display_point";
+
     /** Test name suffixes */
     public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE";
     public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT";
@@ -71,6 +79,7 @@
     public static final String LIFECYCLE = "LIFECYCLE";
     public static final String SCREENSHOT = "SCREENSHOT";
     public static final String EXTRA_ASSIST = "EXTRA_ASSIST";
+    public static final String VERIFY_CONTENT_VIEW = "VERIFY_CONTENT_VIEW";
 
     /** Session intent constants */
     public static final String HIDE_SESSION = "android.intent.action.hide_session";
@@ -125,6 +134,7 @@
             case LIFECYCLE:
             case SCREENSHOT:
             case EXTRA_ASSIST:
+            case VERIFY_CONTENT_VIEW:
                 return "service.DelayedAssistantActivity";
             default:
                 return "";
diff --git a/tests/tests/assist/service/res/layout/assist_layer.xml b/tests/tests/assist/service/res/layout/assist_layer.xml
index 49f35c9..3677208 100644
--- a/tests/tests/assist/service/res/layout/assist_layer.xml
+++ b/tests/tests/assist/service/res/layout/assist_layer.xml
@@ -1,9 +1,10 @@
 <?xml version="1.0" encoding="utf-8"?>
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
-        android:id="@+id/assist_layer"
-        android:layout_width="match_parent"
-        android:background="@color/assist_layer_background"
-        android:layout_height="match_parent">
+<RelativeLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/assist_layer"
+    android:layout_width="match_parent"
+    android:background="@color/assist_layer_background"
+    android:layout_height="match_parent">
     <TextView
         android:layout_centerInParent="true"
         android:text="@string/test_assistant_text"
diff --git a/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionSession.java b/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionSession.java
index 8fd8271..7bca9be 100644
--- a/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionSession.java
+++ b/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionSession.java
@@ -25,6 +25,7 @@
 import android.content.IntentFilter;
 import android.graphics.Bitmap;
 import android.graphics.Color;
+
 import android.graphics.Point;
 import android.os.Bundle;
 import android.service.voice.VoiceInteractionSession;
@@ -33,6 +34,8 @@
 import android.view.Display;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.Display;
+import android.view.ViewTreeObserver;
 
 import java.io.ByteArrayOutputStream;
 import java.util.Date;
@@ -55,6 +58,7 @@
     private Bitmap mScreenshot;
     private BroadcastReceiver mReceiver;
     private String mTestName;
+    private View mContentView;
 
     MainInteractionSession(Context context) {
         super(context);
@@ -97,6 +101,22 @@
         mDisplayHeight = args.getInt(Utils.DISPLAY_HEIGHT_KEY);
         mDisplayWidth = args.getInt(Utils.DISPLAY_WIDTH_KEY);
         super.onShow(args, showFlags);
+        mContentView.getViewTreeObserver().addOnPreDrawListener(
+                new ViewTreeObserver.OnPreDrawListener() {
+                @Override
+                public boolean onPreDraw() {
+                    mContentView.getViewTreeObserver().removeOnPreDrawListener(this);
+                    Display d = mContentView.getDisplay();
+                    Point displayPoint = new Point();
+                    d.getRealSize(displayPoint);
+                    Intent intent = new Intent(Utils.BROADCAST_CONTENT_VIEW_HEIGHT);
+                    intent.putExtra(Utils.EXTRA_CONTENT_VIEW_HEIGHT, mContentView.getHeight());
+                    intent.putExtra(Utils.EXTRA_CONTENT_VIEW_WIDTH, mContentView.getWidth());
+                    intent.putExtra(Utils.EXTRA_DISPLAY_POINT, displayPoint);
+                    mContext.sendBroadcast(intent);
+                    return true;
+                }
+            });
     }
 
     @Override
@@ -188,6 +208,7 @@
         if (f == null) {
             Log.wtf(TAG, "layout inflater was null");
         }
-        return f.inflate(R.layout.assist_layer,null);
+        mContentView = f.inflate(R.layout.assist_layer,null);
+        return mContentView;
     }
 }
diff --git a/tests/tests/assist/src/android/assist/cts/AssistStructureTest.java b/tests/tests/assist/src/android/assist/cts/AssistStructureTest.java
index baf29fe..b35e43b 100644
--- a/tests/tests/assist/src/android/assist/cts/AssistStructureTest.java
+++ b/tests/tests/assist/src/android/assist/cts/AssistStructureTest.java
@@ -39,6 +39,7 @@
 
     private BroadcastReceiver mReceiver;
     private CountDownLatch mHasResumedLatch = new CountDownLatch(1);
+    private CountDownLatch mReadyLatch = new CountDownLatch(1);
 
     public AssistStructureTest() {
         super();
@@ -81,7 +82,7 @@
     public void testAssistStructure() throws Exception {
         mTestActivity.start3pApp(TEST_CASE_TYPE);
         mTestActivity.startTest(TEST_CASE_TYPE);
-        waitForAssistantToBeReady();
+        waitForAssistantToBeReady(mReadyLatch);
         waitForOnResume();
         startSession();
         waitForContext();
@@ -100,10 +101,10 @@
                     mHasResumedLatch.countDown();
                 }
             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
-                if (mAssistantReadyLatch != null) {
-                    mAssistantReadyLatch.countDown();
+                if (mReadyLatch != null) {
+                    mReadyLatch.countDown();
                 }
             }
         }
     }
-}
\ No newline at end of file
+}
diff --git a/tests/tests/assist/src/android/assist/cts/AssistTestBase.java b/tests/tests/assist/src/android/assist/cts/AssistTestBase.java
index 0f8f74c..ecbd435 100644
--- a/tests/tests/assist/src/android/assist/cts/AssistTestBase.java
+++ b/tests/tests/assist/src/android/assist/cts/AssistTestBase.java
@@ -60,7 +60,7 @@
     protected BroadcastReceiver mReceiver;
     protected Bundle mAssistBundle;
     protected Context mContext;
-    protected CountDownLatch mLatch, mAssistantReadyLatch, mScreenshotLatch, mHasResumedLatch;
+    protected CountDownLatch mLatch, mScreenshotLatch, mHasResumedLatch;
     protected boolean mScreenshotMatches;
     private Point mDisplaySize;
     private String mTestName;
@@ -73,19 +73,26 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-        mAssistantReadyLatch = new CountDownLatch(1);
         mContext = getInstrumentation().getTargetContext();
         SystemUtil.runShellCommand(getInstrumentation(),
                 "settings put secure assist_structure_enabled 1");
         SystemUtil.runShellCommand(getInstrumentation(),
                 "settings put secure assist_screenshot_enabled 1");
         logContextAndScreenshotSetting();
+
         // reset old values
         mScreenshotMatches = false;
         mScreenshot = false;
         mAssistStructure = null;
         mAssistContent = null;
         mAssistBundle = null;
+
+        if (mReceiver != null) {
+            mContext.unregisterReceiver(mReceiver);
+        }
+        mReceiver = new TestResultsReceiver();
+        mContext.registerReceiver(mReceiver,
+            new IntentFilter(Utils.BROADCAST_ASSIST_DATA_INTENT));
     }
 
     @Override
@@ -116,9 +123,9 @@
     /**
      * Called when waiting for Assistant's Broadcast Receiver to be setup
      */
-    public void waitForAssistantToBeReady() throws Exception {
+    public void waitForAssistantToBeReady(CountDownLatch latch) throws Exception {
         Log.i(TAG, "waiting for assistant to be ready before continuing");
-        if (!mAssistantReadyLatch.await(Utils.TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
+        if (!latch.await(Utils.TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
             fail("Assistant was not ready before timeout of: " + Utils.TIMEOUT_MS + "msec");
         }
     }
diff --git a/tests/tests/assist/src/android/assist/cts/AssistantContentViewTest.java b/tests/tests/assist/src/android/assist/cts/AssistantContentViewTest.java
new file mode 100644
index 0000000..557134b
--- /dev/null
+++ b/tests/tests/assist/src/android/assist/cts/AssistantContentViewTest.java
@@ -0,0 +1,113 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.assist.cts;
+
+import android.assist.cts.TestStartActivity;
+import android.assist.common.Utils;
+
+import android.app.Activity;
+import android.app.assist.AssistContent;
+import android.app.assist.AssistStructure;
+import android.content.BroadcastReceiver;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.cts.util.SystemUtil;
+import android.graphics.Point;
+import android.os.Bundle;
+import android.provider.Settings;
+import android.test.ActivityInstrumentationTestCase2;
+import android.util.Log;
+
+import java.lang.Override;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/** Test verifying the Content View of the Assistant */
+public class AssistantContentViewTest extends AssistTestBase {
+    private static final String TAG = "ContentViewTest";
+    private BroadcastReceiver mReceiver;
+    private CountDownLatch mContentViewLatch, mReadyLatch;
+    private Intent mIntent;
+
+    @Override
+    public void setUp() throws Exception {
+        super.setUp();
+        mContentViewLatch = new CountDownLatch(1);
+        mReadyLatch = new CountDownLatch(1);
+        setUpAndRegisterReceiver();
+        startTestActivity(Utils.VERIFY_CONTENT_VIEW);
+    }
+
+    @Override
+    public void tearDown() throws Exception {
+        super.tearDown();
+        if (mReceiver != null) {
+            mContext.unregisterReceiver(mReceiver);
+            mReceiver = null;
+        }
+    }
+
+    private void setUpAndRegisterReceiver() {
+        if (mReceiver != null) {
+            mContext.unregisterReceiver(mReceiver);
+        }
+        mReceiver = new AssistantContentViewReceiver();
+        IntentFilter filter = new IntentFilter();
+        filter.addAction(Utils.BROADCAST_CONTENT_VIEW_HEIGHT);
+        filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED);
+        mContext.registerReceiver(mReceiver, filter);
+
+    }
+
+    private void waitForContentView() throws Exception {
+        Log.i(TAG, "waiting for the Assistant's Content View  before continuing");
+        if (!mContentViewLatch.await(Utils.TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
+            fail("failed to receive content view in " + Utils.TIMEOUT_MS + "msec");
+        }
+    }
+
+    public void testAssistantContentViewDimens() throws Exception {
+        mTestActivity.startTest(Utils.VERIFY_CONTENT_VIEW);
+        waitForAssistantToBeReady(mReadyLatch);
+        startSession();
+        waitForContentView();
+        int height = mIntent.getIntExtra(Utils.EXTRA_CONTENT_VIEW_HEIGHT, 0);
+        int width = mIntent.getIntExtra(Utils.EXTRA_CONTENT_VIEW_WIDTH, 0);
+        Point displayPoint = (Point) mIntent.getParcelableExtra(Utils.EXTRA_DISPLAY_POINT);
+        assertEquals(displayPoint.y, height);
+        assertEquals(displayPoint.x, width);
+    }
+
+    private class AssistantContentViewReceiver extends BroadcastReceiver {
+        @Override
+        public void onReceive(Context context, Intent intent) {
+            String action = intent.getAction();
+            if (action.equals(Utils.BROADCAST_CONTENT_VIEW_HEIGHT)) {
+                mIntent = intent;
+                if (mContentViewLatch != null) {
+                    mContentViewLatch.countDown();
+                }
+            } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
+                if (mReadyLatch != null) {
+                    mReadyLatch.countDown();
+                }
+            }
+        }
+    }
+}
diff --git a/tests/tests/assist/src/android/assist/cts/ExtraAssistDataTest.java b/tests/tests/assist/src/android/assist/cts/ExtraAssistDataTest.java
index 282f7da..28b2af2 100644
--- a/tests/tests/assist/src/android/assist/cts/ExtraAssistDataTest.java
+++ b/tests/tests/assist/src/android/assist/cts/ExtraAssistDataTest.java
@@ -32,6 +32,7 @@
 
     private BroadcastReceiver mReceiver;
     private CountDownLatch mHasResumedLatch = new CountDownLatch(1);
+    private CountDownLatch mReadyLatch = new CountDownLatch(1);
 
     public ExtraAssistDataTest() {
         super();
@@ -66,7 +67,7 @@
 
     public void testAssistContentAndAssistData() throws Exception {
         mTestActivity.startTest(TEST_CASE_TYPE);
-        waitForAssistantToBeReady();
+        waitForAssistantToBeReady(mReadyLatch);
         mTestActivity.start3pApp(TEST_CASE_TYPE);
         waitForOnResume();
         startSession();
@@ -105,10 +106,10 @@
                     mHasResumedLatch.countDown();
                 }
             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
-                if (mAssistantReadyLatch != null) {
-                    mAssistantReadyLatch.countDown();
+                if (mReadyLatch != null) {
+                    mReadyLatch.countDown();
                 }
             }
         }
     }
-}
\ No newline at end of file
+}
diff --git a/tests/tests/assist/src/android/assist/cts/FlagSecureTest.java b/tests/tests/assist/src/android/assist/cts/FlagSecureTest.java
index 2e9932e..35f95a4 100644
--- a/tests/tests/assist/src/android/assist/cts/FlagSecureTest.java
+++ b/tests/tests/assist/src/android/assist/cts/FlagSecureTest.java
@@ -37,7 +37,7 @@
 
     private BroadcastReceiver mReceiver;
     private CountDownLatch mHasResumedLatch = new CountDownLatch(1);
-
+    private CountDownLatch mReadyLatch = new CountDownLatch(1);
     public FlagSecureTest() {
         super();
     }
@@ -78,7 +78,7 @@
 
     public void testSecureActivity() throws Exception {
         mTestActivity.startTest(TEST_CASE_TYPE);
-        waitForAssistantToBeReady();
+        waitForAssistantToBeReady(mReadyLatch);
         mTestActivity.start3pApp(TEST_CASE_TYPE);
         waitForOnResume();
         startSession();
@@ -93,9 +93,13 @@
         public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
             if (action.equals(Utils.FLAG_SECURE_HASRESUMED)) {
-                mHasResumedLatch.countDown();
+                if (mHasResumedLatch != null) {
+                    mHasResumedLatch.countDown();
+                }
             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
-                mAssistantReadyLatch.countDown();
+                if (mReadyLatch != null) {
+                    mReadyLatch.countDown();
+                }
             }
         }
     }
diff --git a/tests/tests/assist/src/android/assist/cts/LifecycleTest.java b/tests/tests/assist/src/android/assist/cts/LifecycleTest.java
index 0b3630a..0a23bfb 100644
--- a/tests/tests/assist/src/android/assist/cts/LifecycleTest.java
+++ b/tests/tests/assist/src/android/assist/cts/LifecycleTest.java
@@ -51,6 +51,7 @@
     private BroadcastReceiver mLifecycleTestBroadcastReceiver;
     private CountDownLatch mHasResumedLatch = new CountDownLatch(1);
     private CountDownLatch mActivityLifecycleLatch = new CountDownLatch(1);
+    private CountDownLatch mReadyLatch = new CountDownLatch(1);
 
     @Override
     public void setUp() throws Exception {
@@ -98,7 +99,7 @@
 
     public void testLayerDoesNotTriggerLifecycleMethods() throws Exception {
         mTestActivity.startTest(Utils.LIFECYCLE);
-        waitForAssistantToBeReady();
+        waitForAssistantToBeReady(mReadyLatch);
         mTestActivity.start3pApp(Utils.LIFECYCLE);
         waitForOnResume();
         startSession();
@@ -119,8 +120,8 @@
             } else if (action.equals(action_onDestroy) && mActivityLifecycleLatch != null) {
                 mActivityLifecycleLatch.countDown();
             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
-                if (mAssistantReadyLatch != null) {
-                    mAssistantReadyLatch.countDown();
+                if (mReadyLatch != null) {
+                    mReadyLatch.countDown();
                 }
             }
         }
diff --git a/tests/tests/assist/src/android/assist/cts/ScreenshotTest.java b/tests/tests/assist/src/android/assist/cts/ScreenshotTest.java
index 45b8b73..45082ae 100644
--- a/tests/tests/assist/src/android/assist/cts/ScreenshotTest.java
+++ b/tests/tests/assist/src/android/assist/cts/ScreenshotTest.java
@@ -40,7 +40,7 @@
     private static final String TEST_CASE_TYPE = Utils.SCREENSHOT;
 
     private BroadcastReceiver mScreenshotActivityReceiver;
-    private CountDownLatch mHasResumedLatch;
+    private CountDownLatch mHasResumedLatch, mReadyLatch;
 
     public ScreenshotTest() {
         super();
@@ -49,7 +49,7 @@
     @Override
     protected void setUp() throws Exception {
         super.setUp();
-
+        mReadyLatch = new CountDownLatch(1);
         // set up receiver
         mScreenshotActivityReceiver = new ScreenshotTestReceiver();
         IntentFilter filter = new IntentFilter();
@@ -73,7 +73,7 @@
         Log.i(TAG, "Starting screenshot test");
         mTestActivity.startTest(TEST_CASE_TYPE);
         Log.i(TAG, "start waitForAssistantToBeReady()");
-        waitForAssistantToBeReady();
+        waitForAssistantToBeReady(mReadyLatch);
 
         waitForActivityResumeAndAssist(Color.RED);
         verifyAssistDataNullness(false, false, false, false);
@@ -84,7 +84,7 @@
         Log.i(TAG, "Starting screenshot test");
         mTestActivity.startTest(TEST_CASE_TYPE);
         Log.i(TAG, "start waitForAssistantToBeReady()");
-        waitForAssistantToBeReady();
+        waitForAssistantToBeReady(mReadyLatch);
 
         waitForActivityResumeAndAssist(Color.GREEN);
         verifyAssistDataNullness(false, false, false, false);
@@ -95,7 +95,7 @@
         Log.i(TAG, "Starting screenshot test");
         mTestActivity.startTest(TEST_CASE_TYPE);
         Log.i(TAG, "start waitForAssistantToBeReady()");
-        waitForAssistantToBeReady();
+        waitForAssistantToBeReady(mReadyLatch);
 
         waitForActivityResumeAndAssist(Color.BLUE);
         verifyAssistDataNullness(false, false, false, false);
@@ -121,7 +121,9 @@
             Log.i(ScreenshotTest.TAG, "Got some broadcast: " + action);
             if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) {
                 Log.i(ScreenshotTest.TAG, "Received assist receiver is registered.");
-                mAssistantReadyLatch.countDown();
+                if (mReadyLatch != null) {
+                    mReadyLatch.countDown();
+                }
             } else if (action.equals(Utils.APP_3P_HASRESUMED)) {
                 if (mHasResumedLatch != null) {
                     mHasResumedLatch.countDown();
@@ -129,4 +131,4 @@
             }
         }
     }
-}
\ No newline at end of file
+}
diff --git a/tools/utils/buildCts.py b/tools/utils/buildCts.py
index 3582848..a048ddc 100755
--- a/tools/utils/buildCts.py
+++ b/tools/utils/buildCts.py
@@ -472,6 +472,7 @@
       ],
       'android.assist' : [
           'android.assist.cts.ExtraAssistDataTest',
+          'android.assist.cts.AssistantContentViewTest',
           'android.assist.cts.ScreenshotTest',
       ],
       'android.calllog' : [