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 
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.provider.Settings;
26 import android.util.Log;
27 
28 import java.util.concurrent.CountDownLatch;
29 import java.util.concurrent.TimeUnit;
30 
31 /**
32  *  Test that the AssistStructure returned is properly formatted.
33  */
34 
35 public class TextViewTest extends AssistTestBase {
36     private static final String TAG = "TextViewTest";
37     private static final String TEST_CASE_TYPE = Utils.TEXTVIEW;
38 
39     private BroadcastReceiver mReceiver;
40     private CountDownLatch mHasResumedLatch = new CountDownLatch(1);
41     private CountDownLatch mReadyLatch = new CountDownLatch(1);
42 
TextViewTest()43     public TextViewTest() {
44         super();
45     }
46 
47     @Override
setUp()48     protected void setUp() throws Exception {
49         super.setUp();
50         setUpAndRegisterReceiver();
51         startTestActivity(TEST_CASE_TYPE);
52     }
53 
54     @Override
tearDown()55     public void tearDown() throws Exception {
56         super.tearDown();
57         if (mReceiver != null) {
58             mContext.unregisterReceiver(mReceiver);
59             mReceiver = null;
60         }
61     }
62 
setUpAndRegisterReceiver()63     private void setUpAndRegisterReceiver() {
64         if (mReceiver != null) {
65             mContext.unregisterReceiver(mReceiver);
66         }
67         mReceiver = new TextViewTestBroadcastReceiver();
68         IntentFilter filter = new IntentFilter();
69         filter.addAction(Utils.APP_3P_HASRESUMED);
70         filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED);
71         mContext.registerReceiver(mReceiver, filter);
72     }
73 
waitForOnResume()74     private void waitForOnResume() throws Exception {
75         Log.i(TAG, "waiting for onResume() before continuing");
76         if (!mHasResumedLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
77             fail("Activity failed to resume in " + Utils.ACTIVITY_ONRESUME_TIMEOUT_MS + "msec");
78         }
79     }
80 
testTextView()81     public void testTextView() throws Exception {
82         if (mActivityManager.isLowRamDevice()) {
83             Log.d(TAG, "Not running assist tests on low-RAM device.");
84             return;
85         }
86         mTestActivity.start3pApp(TEST_CASE_TYPE);
87         scrollTestApp(0, 0, true, false);
88 
89         // Verify that the text view contains the right text
90         mTestActivity.startTest(TEST_CASE_TYPE);
91         waitForAssistantToBeReady(mReadyLatch);
92         waitForOnResume();
93         startSession();
94         waitForContext();
95         verifyAssistDataNullness(false, false, false, false);
96 
97         verifyAssistStructure(Utils.getTestAppComponent(TEST_CASE_TYPE),
98                 false /*FLAG_SECURE set*/);
99 
100         // Verify that the scroll position of the text view is accurate after scrolling.
101         scrollTestApp(10, 50, true /* scrollTextView */, false /* scrollScrollView */);
102         waitForOnResume();
103         startSession();
104         waitForContext();
105         verifyAssistStructure(Utils.getTestAppComponent(TEST_CASE_TYPE), false);
106 
107         scrollTestApp(-1, -1, true, false);
108         waitForOnResume();
109         startSession();
110         waitForContext();
111         verifyAssistStructure(Utils.getTestAppComponent(TEST_CASE_TYPE), false);
112 
113         scrollTestApp(0, 0, true, true);
114         waitForOnResume();
115         startSession();
116         waitForContext();
117         verifyAssistStructure(Utils.getTestAppComponent(TEST_CASE_TYPE), false);
118 
119         scrollTestApp(10, 50, false, true);
120         waitForOnResume();
121         startSession();
122         waitForContext();
123         verifyAssistStructure(Utils.getTestAppComponent(TEST_CASE_TYPE), false);
124     }
125 
126     private class TextViewTestBroadcastReceiver extends BroadcastReceiver {
127         @Override
onReceive(Context context, Intent intent)128         public void onReceive(Context context, Intent intent) {
129             String action = intent.getAction();
130             if (action.equals(Utils.APP_3P_HASRESUMED) && mHasResumedLatch != null) {
131                 mHasResumedLatch.countDown();
132             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED) &&  mReadyLatch != null) {
133                 mReadyLatch.countDown();
134             }
135         }
136     }
137 }
138