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.content.pm.PackageManager;
26 import android.provider.Settings;
27 import android.util.Log;
28 
29 import java.util.concurrent.CountDownLatch;
30 import java.util.concurrent.TimeUnit;
31 
32 /**
33  *  Test that the AssistStructure returned is properly formatted.
34  */
35 
36 public class WebViewTest extends AssistTestBase {
37     private static final String TAG = "WebViewTest";
38     private static final String TEST_CASE_TYPE = Utils.WEBVIEW;
39 
40     private boolean mWebViewSupported;
41     private BroadcastReceiver mReceiver;
42     private CountDownLatch mHasResumedLatch = new CountDownLatch(1);
43     private CountDownLatch mTestWebViewLatch = new CountDownLatch(1);
44     private CountDownLatch mReadyLatch = new CountDownLatch(1);
45 
WebViewTest()46     public WebViewTest() {
47         super();
48     }
49 
50     @Override
setUp()51     protected void setUp() throws Exception {
52         super.setUp();
53         setUpAndRegisterReceiver();
54         startTestActivity(TEST_CASE_TYPE);
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 WebViewTestBroadcastReceiver();
71         IntentFilter filter = new IntentFilter();
72         filter.addAction(Utils.APP_3P_HASRESUMED);
73         filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED);
74         filter.addAction(Utils.TEST_ACTIVITY_LOADED);
75         mContext.registerReceiver(mReceiver, filter);
76     }
77 
waitForOnResume()78     private void waitForOnResume() throws Exception {
79         Log.i(TAG, "waiting for onResume() before continuing");
80         if (!mHasResumedLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
81             fail("Activity failed to resume in " + Utils.ACTIVITY_ONRESUME_TIMEOUT_MS + "msec");
82         }
83     }
84 
waitForTestActivity()85     private void waitForTestActivity() throws Exception {
86         Log.i(TAG, "waiting for webview in test activity to load");
87         if (!mTestWebViewLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
88             // wait for webView to load completely.
89         }
90     }
91 
testWebView()92     public void testWebView() throws Exception {
93         if (mActivityManager.isLowRamDevice()) {
94             Log.d(TAG, "Not running assist tests on low-RAM device.");
95             return;
96         }
97         if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WEBVIEW)) {
98             return;
99         }
100         mTestActivity.start3pApp(TEST_CASE_TYPE);
101         mTestActivity.startTest(TEST_CASE_TYPE);
102         waitForAssistantToBeReady(mReadyLatch);
103         waitForOnResume();
104         waitForTestActivity();
105         startSession();
106         waitForContext();
107         verifyAssistDataNullness(false, false, false, false);
108         verifyAssistStructure(Utils.getTestAppComponent(TEST_CASE_TYPE),
109                 false /*FLAG_SECURE set*/);
110     }
111 
112     private class WebViewTestBroadcastReceiver extends BroadcastReceiver {
113         @Override
onReceive(Context context, Intent intent)114         public void onReceive(Context context, Intent intent) {
115             String action = intent.getAction();
116             if (action.equals(Utils.APP_3P_HASRESUMED) && mHasResumedLatch != null) {
117                 mHasResumedLatch.countDown();
118             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED) && mReadyLatch != null) {
119                 mReadyLatch.countDown();
120             } else if (action.equals(Utils.TEST_ACTIVITY_LOADED) && mTestWebViewLatch != null) {
121                 mTestWebViewLatch.countDown();
122             }
123         }
124     }
125 }
126