Use test app in separate package from assistant.

Send screenshot with data and added test for disabled context.

Change-Id: I262b85b80aff5cbe0d7d64eb2fcf5e7d393dec39
diff --git a/tests/tests/assist/common/src/android/voiceinteraction/common/Utils.java b/tests/tests/assist/common/src/android/assist/common/Utils.java
similarity index 72%
rename from tests/tests/assist/common/src/android/voiceinteraction/common/Utils.java
rename to tests/tests/assist/common/src/android/assist/common/Utils.java
index b9fb7cf..1831182 100644
--- a/tests/tests/assist/common/src/android/voiceinteraction/common/Utils.java
+++ b/tests/tests/assist/common/src/android/assist/common/Utils.java
@@ -18,6 +18,7 @@
 import android.app.VoiceInteractor;
 import android.app.VoiceInteractor.PickOptionRequest.Option;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
@@ -28,10 +29,6 @@
 import java.util.concurrent.CountDownLatch;
 
 public class Utils {
-    public enum TestCaseType {
-        ASSIST_STRUCTURE,
-        DISABLE_CONTEXT,
-    }
     public static final String TESTCASE_TYPE = "testcase_type";
     public static final String TESTINFO = "testinfo";
     public static final String BROADCAST_INTENT = "android.intent.action.ASSIST_TESTAPP";
@@ -40,17 +37,39 @@
 
     public static final String ASSIST_STRUCTURE_KEY = "assist_structure";
     public static final String ASSIST_CONTENT_KEY = "assist_content";
-    public static final String ASSIST_BUNDLE = "assist_bundle";
+    public static final String ASSIST_BUNDLE_KEY = "assist_bundle";
+    public static final String ASSIST_SCREENSHOT_KEY = "assist_screenshot";
 
     public static final int TIMEOUT_MS = 2 * 1000; // TODO(awlee): what is the timeout
 
+    public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE";
+    public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT";
+
+    /**
+     * The shim activity that starts the service associated with each test.
+     */
     public static final String getTestActivity(String testCaseType) {
-        if (testCaseType.equals("ASSIST_STRUCTURE")) {
-            return "service.AssistStructureActivity";
-        } else if (testCaseType.equals("DISABLE_CONTEXT")) {
-            return "service.DisableContextActivity";
+        switch (testCaseType) {
+            case ASSIST_STRUCTURE:
+                return "service.AssistStructureActivity";
+            case DISABLE_CONTEXT:
+                return "service.DisableContextActivity";
+            default:
+                return "";
         }
-        return "";
+    }
+
+    /**
+     * The test app associated with each test.
+     */
+    public static final ComponentName getTestAppComponent(String testCaseType) {
+        switch (testCaseType) {
+            case ASSIST_STRUCTURE:
+                return new ComponentName(
+                        "android.assist.testapp", "android.assist.testapp.TestApp");
+            default:
+                return new ComponentName("","");
+        }
     }
 
     public static final String toBundleString(Bundle bundle) {
diff --git a/tests/tests/assist/service/src/android/voiceinteraction/service/AssistStructureActivity.java b/tests/tests/assist/service/src/android/voiceinteraction/service/AssistStructureActivity.java
index 8b63b25..784d63b 100644
--- a/tests/tests/assist/service/src/android/voiceinteraction/service/AssistStructureActivity.java
+++ b/tests/tests/assist/service/src/android/voiceinteraction/service/AssistStructureActivity.java
@@ -35,8 +35,8 @@
         super.onStart();
         Intent intent = new Intent();
         intent.setComponent(new ComponentName(this, MainInteractionService.class));
-        ComponentName serviceName = startService(intent);
-        Log.i(TAG, "Started service: " + serviceName);
-        getLayoutInflater().inflate(R.layout.voice_interaction_main, null);
+        Log.i(TAG, "Starting service.");
+        finish();
+        startService(intent);
     }
 }
diff --git a/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionService.java b/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionService.java
index 56017ce..684c817 100644
--- a/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionService.java
+++ b/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionService.java
@@ -48,14 +48,9 @@
             Log.wtf(TAG, "Can't start session because either intent is null or onReady() "
                     + "has not been called yet. mIntent = " + mIntent + ", mReady = " + mReady);
         } else {
-            Log.i(TAG, "Yay! about to start session with TestApp");
+            Log.i(TAG, "Yay! about to start session");
             if (isActiveService(this, new ComponentName(this, getClass()))) {
-                Bundle args = new Bundle();
-                Intent intent = new Intent();
-                intent.setComponent(new ComponentName("android.assist.testapp",
-                        "android.assist.testapp.TestApp"));
-                args.putParcelable("intent", intent);
-                showSession(args, VoiceInteractionSession.SHOW_WITH_ASSIST |
+                showSession(new Bundle(), VoiceInteractionSession.SHOW_WITH_ASSIST |
                         VoiceInteractionSession.SHOW_WITH_SCREENSHOT);
             } else {
                 Log.wtf(TAG, "**** Not starting MainInteractionService because" +
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 d19c6f8..7b77ea0 100644
--- a/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionSession.java
+++ b/tests/tests/assist/service/src/android/voiceinteraction/service/MainInteractionSession.java
@@ -31,6 +31,7 @@
 import android.service.voice.VoiceInteractionSession.PickOptionRequest;
 import android.util.Log;
 
+import java.io.ByteArrayOutputStream;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -44,6 +45,9 @@
     Context mContext;
     Bundle mAssistData = new Bundle();
 
+    private boolean hasReceivedAssistData = false;
+    private boolean hasReceivedScreenshot = false;
+
     MainInteractionSession(Context context) {
         super(context);
         mContext = context;
@@ -64,8 +68,6 @@
     public void onShow(Bundle args, int showFlags) {
         // set some content view.
         // TODO: check that the view takes up the whole screen.
-        mStartIntent = args.getParcelable("intent");
-        startVoiceActivity(mStartIntent); // remove
         // check that interactor mode is for assist
         if ((showFlags & SHOW_WITH_ASSIST) == 0) {
             return;
@@ -73,32 +75,52 @@
         super.onShow(args, showFlags);
     }
 
-
     @Override
     public void onHandleAssist(/*@Nullable */Bundle data, /*@Nullable*/ AssistStructure structure,
         /*@Nullable*/ AssistContent content) {
         Log.i(TAG, "onHandleAssist");
-        Log.i(TAG, String.format("Bundle: %s, Structure: %s, Content: %s", data, structure, content));
+        Log.i(TAG,
+            String.format("Bundle: %s, Structure: %s, Content: %s", data, structure, content));
         super.onHandleAssist(data, structure, content);
 
         // send to test to verify that this is accurate.
         mAssistData.putParcelable(Utils.ASSIST_STRUCTURE_KEY, structure);
         mAssistData.putParcelable(Utils.ASSIST_CONTENT_KEY, content);
-        mAssistData.putBundle(Utils.ASSIST_BUNDLE, data);
-        broadcastResults();
+        mAssistData.putBundle(Utils.ASSIST_BUNDLE_KEY, data);
+        hasReceivedAssistData = true;
+        maybeBroadcastResults();
     }
 
     @Override
     public void onHandleScreenshot(/*@Nullable*/ Bitmap screenshot) {
+        Log.i(TAG, "onHandleScreenshot");
         super.onHandleScreenshot(screenshot);
-        // add this to mAssistData?
+        ByteArrayOutputStream bs = new ByteArrayOutputStream();
+        if (screenshot != null) {
+            screenshot.compress(Bitmap.CompressFormat.PNG, 50, bs);
+            mAssistData.putByteArray(Utils.ASSIST_SCREENSHOT_KEY, bs.toByteArray());
+        } else {
+            mAssistData.putByteArray(Utils.ASSIST_SCREENSHOT_KEY, null);
+        }
+        hasReceivedScreenshot = true;
+        maybeBroadcastResults();
     }
 
-    private void broadcastResults() {
-        Intent intent = new Intent(Utils.BROADCAST_ASSIST_DATA_INTENT);
-        intent.putExtras(mAssistData);
-        Log.i(TAG, "broadcasting: " + intent.toString() + ", Bundle = " + mAssistData.toString());
-        mContext.sendBroadcast(intent);
+    private void maybeBroadcastResults() {
+        if (!hasReceivedAssistData) {
+            Log.i(TAG, "waiting for assist data before broadcasting results");
+        } else if (!hasReceivedScreenshot) {
+            Log.i(TAG, "waiting for screenshot before broadcasting results");
+        } else {
+            Intent intent = new Intent(Utils.BROADCAST_ASSIST_DATA_INTENT);
+            intent.putExtras(mAssistData);
+            Log.i(TAG,
+                    "broadcasting: " + intent.toString() + ", Bundle = " + mAssistData.toString());
+            mContext.sendBroadcast(intent);
+
+            hasReceivedAssistData = false;
+            hasReceivedScreenshot = false;
+        }
     }
 
     class DoneReceiver extends BroadcastReceiver {
diff --git a/tests/tests/assist/src/android/assist/TestStartActivity.java b/tests/tests/assist/src/android/assist/TestStartActivity.java
index 06bf5aa..c0549c6 100644
--- a/tests/tests/assist/src/android/assist/TestStartActivity.java
+++ b/tests/tests/assist/src/android/assist/TestStartActivity.java
@@ -52,6 +52,14 @@
         startActivity(intent);
     }
 
+    public void start3pApp() {
+        Intent intent = new Intent();
+        intent.setComponent(new ComponentName("android.assist.testapp",
+                "android.assist.testapp.TestApp"));
+        startActivity(intent);
+
+    }
+
     @Override protected void onPause() {
         Log.i(TAG, " in onPause");
         super.onPause();
diff --git a/tests/tests/assist/src/android/assist/cts/AssistStructureTest.java b/tests/tests/assist/src/android/assist/cts/AssistStructureTest.java
index 3c93c8a..d113774 100644
--- a/tests/tests/assist/src/android/assist/cts/AssistStructureTest.java
+++ b/tests/tests/assist/src/android/assist/cts/AssistStructureTest.java
@@ -26,6 +26,7 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.cts.util.SystemUtil;
 import android.os.Bundle;
 import android.test.ActivityInstrumentationTestCase2;
 import android.util.Log;
@@ -42,7 +43,7 @@
 public class AssistStructureTest extends AssistTestBase {
     static final String TAG = "AssistStructureTest";
 
-    private static final String TEST_CASE_TYPE = "ASSIST_STRUCTURE";
+    private static final String TEST_CASE_TYPE = Utils.ASSIST_STRUCTURE;
 
     public AssistStructureTest() {
         super();
@@ -52,38 +53,26 @@
     protected void setUp() throws Exception {
         super.setUp();
         startTestActivity(TEST_CASE_TYPE);
-        waitForBroadcast(Utils.TestCaseType.ASSIST_STRUCTURE);
+        waitForBroadcast();
     }
 
     public void testAssistStructure() throws Exception {
-        // verify assist data contains what we want.
-        // go through all things in the bundle, verify not null, verify contains what we want.
-
-        // TODO(awlee): verify that the context is not off by default.
-        if (mAssistContent == null || mAssistBundle == null) {
+        // TODO(awlee): verify that the context/screenshot setting is on if appropriate
+        if (mAssistContent == null) {
             fail("Received null assistBundle or assistContent.");
             return;
         }
+        if (mAssistBundle == null) {
+            fail("Received null assistBundle.");
+            return;
+        }
 
         if (mAssistStructure == null) {
             fail("Received null assistStructure");
             return;
         } else {
-            verifyAssistStructure(new ComponentName("android.assist.service",
-                    "android.assist." + Utils.getTestActivity(TEST_CASE_TYPE)), false /*FLAG_SECURE set*/);
-        }
-    }
-
-    private void verifyAssistStructure(ComponentName backgroundApp,
-            boolean isSecureWindow) {
-        // Check component name matches
-        assertEquals(backgroundApp.flattenToString(),
-                mAssistStructure.getActivityComponent().flattenToString());
-
-        int numWindows = mAssistStructure.getWindowNodeCount();
-        assertEquals(1, numWindows);
-        for (int i = 0; i < numWindows; i++) {
-            AssistStructure.ViewNode node = mAssistStructure.getWindowNodeAt(i).getRootViewNode();
+            verifyAssistStructure(Utils.getTestAppComponent(TEST_CASE_TYPE),
+                    false /*FLAG_SECURE set*/);
         }
     }
 }
\ 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 a4f4cbc..b2ec739 100644
--- a/tests/tests/assist/src/android/assist/cts/AssistTestBase.java
+++ b/tests/tests/assist/src/android/assist/cts/AssistTestBase.java
@@ -27,11 +27,12 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
 import android.os.Bundle;
 import android.test.ActivityInstrumentationTestCase2;
 import android.util.Log;
 
-import java.lang.Override;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -41,11 +42,11 @@
     protected TestStartActivity mTestActivity;
     protected AssistContent mAssistContent;
     protected AssistStructure mAssistStructure;
+    protected Bitmap mScreenshot;
     protected BroadcastReceiver mReceiver;
     protected Bundle mAssistBundle;
     protected Context mContext;
     protected CountDownLatch mLatch;
-    protected Utils.TestCaseType mTestCaseType;
     private String mTestName;
 
     public AssistTestBase() {
@@ -78,8 +79,7 @@
     /**
      * Called after startTestActivity
      */
-    protected boolean waitForBroadcast(Utils.TestCaseType testCaseType) throws Exception {
-        mTestCaseType = testCaseType;
+    protected boolean waitForBroadcast() throws Exception {
         mLatch = new CountDownLatch(1);
         if (mReceiver != null) {
             mContext.unregisterReceiver(mReceiver);
@@ -88,6 +88,7 @@
         mContext.registerReceiver(mReceiver,
                 new IntentFilter(Utils.BROADCAST_ASSIST_DATA_INTENT));
 
+        mTestActivity.start3pApp();
         mTestActivity.startTest(mTestName);
         if (!mLatch.await(Utils.TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
             fail("Failed to receive broadcast in " + Utils.TIMEOUT_MS + "msec");
@@ -96,18 +97,39 @@
         return true;
     }
 
+    protected void verifyAssistStructure(ComponentName backgroundApp, boolean isSecureWindow) {
+        // Check component name matches
+        assertEquals(backgroundApp.flattenToString(),
+            mAssistStructure.getActivityComponent().flattenToString());
+
+        int numWindows = mAssistStructure.getWindowNodeCount();
+        assertEquals(1, numWindows);
+        for (int i = 0; i < numWindows; i++) {
+            AssistStructure.ViewNode node = mAssistStructure.getWindowNodeAt(i).getRootViewNode();
+            // TODO: traverse view heirarchy and verify it matches what we expect
+        }
+    }
+
     class TestResultsReceiver extends BroadcastReceiver {
         @Override
         public void onReceive(Context context, Intent intent) {
             if (intent.getAction().equalsIgnoreCase(Utils.BROADCAST_ASSIST_DATA_INTENT)) { // not necessary?
-                Log.i(TAG, "received broadcast with results ");
+                Log.i(TAG, "Received broadcast with assist data.");
                 Bundle assistData = intent.getExtras();
+                AssistTestBase.this.mAssistBundle = assistData.getBundle(Utils.ASSIST_BUNDLE_KEY);
                 AssistTestBase.this.mAssistStructure = assistData.getParcelable(
                         Utils.ASSIST_STRUCTURE_KEY);
-                AssistTestBase.this.mAssistBundle = assistData.getBundle(Utils.ASSIST_BUNDLE);
                 AssistTestBase.this.mAssistContent = assistData.getParcelable(
                         Utils.ASSIST_CONTENT_KEY);
 
+                byte[] bitmapArray = assistData.getByteArray(Utils.ASSIST_SCREENSHOT_KEY);
+                if (bitmapArray != null) {
+                    AssistTestBase.this.mScreenshot = BitmapFactory.decodeByteArray(
+                            bitmapArray, 0, bitmapArray.length);
+                } else {
+                    AssistTestBase.this.mScreenshot = null;
+                }
+
                 if (mLatch != null) {
                     mLatch.countDown();
                 }
diff --git a/tests/tests/assist/src/android/assist/cts/DisableContextTest.java b/tests/tests/assist/src/android/assist/cts/DisableContextTest.java
index f3da32b..6801737 100644
--- a/tests/tests/assist/src/android/assist/cts/DisableContextTest.java
+++ b/tests/tests/assist/src/android/assist/cts/DisableContextTest.java
@@ -27,7 +27,9 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.cts.util.SystemUtil;
 import android.os.Bundle;
+import android.provider.Settings;
 import android.test.ActivityInstrumentationTestCase2;
 import android.util.Log;
 
@@ -39,7 +41,7 @@
 public class DisableContextTest extends AssistTestBase {
     static final String TAG = "DisableContextTest";
 
-    private static final String TEST_CASE_TYPE = "DISABLE_CONTEXT";
+    private static final String TEST_CASE_TYPE = Utils.DISABLE_CONTEXT;
 
     public DisableContextTest() {
         super();
@@ -48,43 +50,65 @@
     @Override
     public void setUp() throws Exception {
         super.setUp();
-        // need to set action/component/activityintent for the test activity?
+        SystemUtil.runShellCommand(getInstrumentation(),
+                "settings put secure assist_structure_enabled 0");
+        SystemUtil.runShellCommand(getInstrumentation(),
+                "settings put secure assist_screenshot_enabled 0");
+
         startTestActivity(TEST_CASE_TYPE);
-        waitForBroadcast(Utils.TestCaseType.DISABLE_CONTEXT);
+        waitForBroadcast();
     }
 
-    public void testContextOnAndOff() throws Exception {
-        // filler
+    @Override
+    public void tearDown() throws Exception {
+        SystemUtil.runShellCommand(getInstrumentation(),
+                "settings put secure assist_structure_enabled 1");
+        SystemUtil.runShellCommand(getInstrumentation(),
+                "settings put secure assist_screenshot_enabled 1");
+        super.tearDown();
+    }
 
+    public void testContextAndScreenshotOff() throws Exception {
+        // Both settings off
+        if (mAssistContent != null || mAssistBundle != null || mAssistStructure != null) {
+            fail(String.format("Should have all been null - Bundle: %s, Structure: %s, Content: %s",
+                    mAssistBundle, mAssistStructure, mAssistContent));
+        }
 
-        // verify assist data contains what we want.
-        // go through all things in the bundle, verify not null, verify contains what we want.
+        if (mScreenshot != null) {
+            fail(String.format("Should have been null - Screenshot: %s", mScreenshot));
+        }
 
-        // TODO(awlee): verify that the context is not off by default.
+        // Screenshot off, context on
+        SystemUtil.runShellCommand(getInstrumentation(),
+            "settings put secure assist_structure_enabled 1");
+        SystemUtil.runShellCommand(getInstrumentation(),
+            "settings put secure assist_screenshot_enabled 0");
+        waitForBroadcast();
+
+        if (mScreenshot != null) {
+            fail(String.format("Should have been null - Screenshot: %s", mScreenshot));
+        }
+
         if (mAssistContent == null || mAssistBundle == null) {
-            fail("Received null assistBundle or assistContent.");
-            return;
+            fail(String.format("Should not have been null - Bundle: %s, Content: %s",
+                mAssistBundle, mAssistContent));
         }
 
-        if (mAssistStructure == null) {
-            fail("Received null assistStructure");
-            return;
-        } else {
-            verifyAssistStructure(new ComponentName("android.assist.service",
-                    "android.assist." + Utils.getTestActivity(TEST_CASE_TYPE)), false /*FLAG_SECURE set*/);
+        // Context off, screenshot on
+        SystemUtil.runShellCommand(getInstrumentation(),
+            "settings put secure assist_screenshot_enabled 1");
+        SystemUtil.runShellCommand(getInstrumentation(),
+            "settings put secure assist_structure_enabled 0");
+        waitForBroadcast();
+
+        if (mScreenshot == null) {
+            fail(String.format("Should not have been null - Screenshot: %s", mScreenshot));
         }
-    }
 
-    private void verifyAssistStructure(ComponentName backgroundApp,
-            boolean isSecureWindow) {
-        // Check component name matches
-        assertEquals(backgroundApp.flattenToString(),
-                mAssistStructure.getActivityComponent().flattenToString());
-
-        int numWindows = mAssistStructure.getWindowNodeCount();
-        assertEquals(1, numWindows);
-        for (int i = 0; i < numWindows; i++) {
-            AssistStructure.ViewNode node = mAssistStructure.getWindowNodeAt(i).getRootViewNode();
+        if (mAssistContent != null || mAssistBundle != null || mAssistStructure != null) {
+            fail(String.format("Should have all been null - Bundle: %s, Structure: %s, Content: %s",
+                mAssistBundle, mAssistStructure, mAssistContent));
         }
     }
 }
\ No newline at end of file
diff --git a/tests/tests/assist/service/res/layout/voice_interaction_main.xml b/tests/tests/assist/testapp/res/layout/voice_interaction_main.xml
similarity index 100%
rename from tests/tests/assist/service/res/layout/voice_interaction_main.xml
rename to tests/tests/assist/testapp/res/layout/voice_interaction_main.xml
diff --git a/tests/tests/assist/service/res/values/strings.xml b/tests/tests/assist/testapp/res/values/strings.xml
similarity index 100%
rename from tests/tests/assist/service/res/values/strings.xml
rename to tests/tests/assist/testapp/res/values/strings.xml
diff --git a/tests/tests/assist/testapp/src/android/voiceinteraction/testapp/TestApp.java b/tests/tests/assist/testapp/src/android/voiceinteraction/testapp/TestApp.java
index 9083cda..7fa9653 100644
--- a/tests/tests/assist/testapp/src/android/voiceinteraction/testapp/TestApp.java
+++ b/tests/tests/assist/testapp/src/android/voiceinteraction/testapp/TestApp.java
@@ -17,20 +17,11 @@
 package android.assist.testapp;
 
 import android.app.Activity;
-import android.app.VoiceInteractor;
-import android.app.VoiceInteractor.AbortVoiceRequest;
-import android.app.VoiceInteractor.CommandRequest;
-import android.app.VoiceInteractor.CompleteVoiceRequest;
-import android.app.VoiceInteractor.ConfirmationRequest;
-import android.app.VoiceInteractor.PickOptionRequest;
-import android.app.VoiceInteractor.PickOptionRequest.Option;
-import android.app.VoiceInteractor.Prompt;
 import android.content.BroadcastReceiver;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.os.Bundle;
-import android.service.voice.VoiceInteractionService;
 import android.util.Log;
 
 import java.util.ArrayList;
@@ -38,5 +29,20 @@
 import android.assist.common.Utils;
 
 public class TestApp extends Activity {
+    static final String TAG = "TestApp";
 
-}
+    Bundle mTestinfo = new Bundle();
+    Bundle mTotalInfo = new Bundle();
+
+    @Override
+    public void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        Log.i(TAG, "TestApp created");
+        getLayoutInflater().inflate(R.layout.voice_interaction_main, null);
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+    }
+}
\ No newline at end of file