Home | History | Annotate | Download | only in cts
      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 import android.content.BroadcastReceiver;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.content.IntentFilter;
     24 import android.content.pm.PackageManager;
     25 import android.os.LocaleList;
     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 WebViewTest extends AssistTestBase {
     36     private static final String TAG = "WebViewTest";
     37     private static final String TEST_CASE_TYPE = Utils.WEBVIEW;
     38 
     39     private boolean mWebViewSupported;
     40     private BroadcastReceiver mReceiver;
     41     private CountDownLatch mHasResumedLatch = new CountDownLatch(1);
     42     private CountDownLatch mTestWebViewLatch = new CountDownLatch(1);
     43     private CountDownLatch mReadyLatch = new CountDownLatch(1);
     44 
     45     public WebViewTest() {
     46         super();
     47     }
     48 
     49     @Override
     50     protected void setUp() throws Exception {
     51         super.setUp();
     52         setUpAndRegisterReceiver();
     53         startTestActivity(TEST_CASE_TYPE);
     54     }
     55 
     56     @Override
     57     public void tearDown() throws Exception {
     58         super.tearDown();
     59         if (mReceiver != null) {
     60             mContext.unregisterReceiver(mReceiver);
     61             mReceiver = null;
     62         }
     63     }
     64 
     65     private void setUpAndRegisterReceiver() {
     66         if (mReceiver != null) {
     67             mContext.unregisterReceiver(mReceiver);
     68         }
     69         mReceiver = new WebViewTestBroadcastReceiver();
     70         IntentFilter filter = new IntentFilter();
     71         filter.addAction(Utils.APP_3P_HASRESUMED);
     72         filter.addAction(Utils.ASSIST_RECEIVER_REGISTERED);
     73         filter.addAction(Utils.TEST_ACTIVITY_LOADED);
     74         mContext.registerReceiver(mReceiver, filter);
     75     }
     76 
     77     private void waitForOnResume() throws Exception {
     78         Log.i(TAG, "waiting for onResume() before continuing");
     79         if (!mHasResumedLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
     80             fail("Activity failed to resume in " + Utils.ACTIVITY_ONRESUME_TIMEOUT_MS + "msec");
     81         }
     82     }
     83 
     84     private void waitForTestActivity() throws Exception {
     85         Log.i(TAG, "waiting for webview in test activity to load");
     86         if (!mTestWebViewLatch.await(Utils.ACTIVITY_ONRESUME_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
     87             // wait for webView to load completely.
     88         }
     89     }
     90 
     91     public void testWebView() throws Exception {
     92         if (mActivityManager.isLowRamDevice()) {
     93             Log.d(TAG, "Not running assist tests on low-RAM device.");
     94             return;
     95         }
     96         if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WEBVIEW)) {
     97             return;
     98         }
     99         mTestActivity.start3pApp(TEST_CASE_TYPE);
    100         mTestActivity.startTest(TEST_CASE_TYPE);
    101         waitForAssistantToBeReady(mReadyLatch);
    102         waitForOnResume();
    103         waitForTestActivity();
    104         startSession();
    105         waitForContext();
    106         verifyAssistDataNullness(false, false, false, false);
    107         verifyAssistStructure(Utils.getTestAppComponent(TEST_CASE_TYPE),
    108                 false /*FLAG_SECURE set*/);
    109         verifyAssistStructureHasWebDomain(Utils.WEBVIEW_HTML_DOMAIN);
    110         verifyAssistStructureHasLocaleList(Utils.WEBVIEW_LOCALE_LIST);
    111     }
    112 
    113     private class WebViewTestBroadcastReceiver extends BroadcastReceiver {
    114         @Override
    115         public void onReceive(Context context, Intent intent) {
    116             String action = intent.getAction();
    117             if (action.equals(Utils.APP_3P_HASRESUMED) && mHasResumedLatch != null) {
    118                 mHasResumedLatch.countDown();
    119             } else if (action.equals(Utils.ASSIST_RECEIVER_REGISTERED) && mReadyLatch != null) {
    120                 mReadyLatch.countDown();
    121             } else if (action.equals(Utils.TEST_ACTIVITY_LOADED) && mTestWebViewLatch != null) {
    122                 mTestWebViewLatch.countDown();
    123             }
    124         }
    125     }
    126 }
    127