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