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 ExtraAssistDataTest()37 public ExtraAssistDataTest() { 38 super(); 39 } 40 41 @Override setUp()42 public void setUp() throws Exception { 43 super.setUp(); 44 setUpAndRegisterReceiver(); 45 startTestActivity(TEST_CASE_TYPE); 46 } 47 48 @Override tearDown()49 public void tearDown() throws Exception { 50 super.tearDown(); 51 if (mReceiver != null) { 52 mContext.unregisterReceiver(mReceiver); 53 mReceiver = null; 54 } 55 } 56 setUpAndRegisterReceiver()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 testAssistContentAndAssistData()68 public void testAssistContentAndAssistData() throws Exception { 69 mTestActivity.startTest(TEST_CASE_TYPE); 70 waitForAssistantToBeReady(mReadyLatch); 71 mTestActivity.start3pApp(TEST_CASE_TYPE); 72 waitForOnResume(); 73 startSession(); 74 waitForContext(); 75 verifyAssistDataNullness(false, false, false, false); 76 77 Log.i(TAG, "assist bundle is: " + Utils.toBundleString(mAssistBundle)); 78 79 // tests that the assist content's structured data is the expected 80 assertEquals("AssistContent structured data did not match data in onProvideAssistContent", 81 Utils.getStructuredJSON(), mAssistContent.getStructuredData()); 82 // tests the assist data. EXTRA_ASSIST_CONTEXT is what's expected. 83 Bundle extraExpectedBundle = Utils.getExtraAssistBundle(); 84 Bundle extraAssistBundle = mAssistBundle.getBundle(Intent.EXTRA_ASSIST_CONTEXT); 85 for (String key : extraExpectedBundle.keySet()) { 86 assertTrue("Assist bundle does not contain expected extra context key: " + key, 87 extraAssistBundle.containsKey(key)); 88 assertEquals("Extra assist context bundle values do not match for key: " + key, 89 extraExpectedBundle.get(key), extraAssistBundle.get(key)); 90 } 91 } 92 waitForOnResume()93 private void waitForOnResume() throws Exception { 94 Log.i(TAG, "waiting for onResume() before continuing"); 95 if (!mHasResumedLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) { 96 fail("Activity failed to resume in " + Utils.ACTIVITY_ONRESUME_TIMEOUT_MS + "msec"); 97 } 98 } 99 100 private class ExtraAssistDataReceiver extends BroadcastReceiver { 101 @Override onReceive(Context context, Intent intent)102 public void onReceive(Context context, Intent intent) { 103 String action = intent.getAction(); 104 if (action.equals(Utils.APP_3P_HASRESUMED)) { 105 if (mHasResumedLatch != null) { 106 mHasResumedLatch.countDown(); 107 } 108 } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED)) { 109 if (mReadyLatch != null) { 110 mReadyLatch.countDown(); 111 } 112 } 113 } 114 } 115 } 116