Home | History | Annotate | Download | only in common
      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.common;
     17 
     18 import android.content.ComponentName;
     19 import android.os.Bundle;
     20 import android.os.LocaleList;
     21 
     22 import org.json.JSONObject;
     23 
     24 import java.util.ArrayList;
     25 import java.util.Locale;
     26 
     27 public class Utils {
     28     public static final String TESTCASE_TYPE = "testcase_type";
     29     public static final String TESTINFO = "testinfo";
     30     public static final String ACTION_PREFIX = "android.intent.action.";
     31     public static final String BROADCAST_INTENT = ACTION_PREFIX + "ASSIST_TESTAPP";
     32     public static final String BROADCAST_ASSIST_DATA_INTENT = ACTION_PREFIX + "ASSIST_DATA";
     33     public static final String BROADCAST_INTENT_START_ASSIST = ACTION_PREFIX + "START_ASSIST";
     34     public static final String ASSIST_RECEIVER_REGISTERED = ACTION_PREFIX + "ASSIST_READY";
     35 
     36     public static final String ACTION_INVALIDATE = "invalidate_action";
     37     public static final String GET_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "GET_CONTENT_VIEW_HEIGHT";
     38     public static final String BROADCAST_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "VIEW_HEIGHT";
     39     public static final String SCROLL_TEXTVIEW_ACTION = ACTION_PREFIX + "TEXTVIEW_SCROLL";
     40     public static final String SCROLL_SCROLLVIEW_ACTION = ACTION_PREFIX + "SCROLLVIEW_SCROLL";
     41     public static final String TEST_ERROR = "Error In Test:";
     42 
     43     public static final String ASSIST_STRUCTURE_KEY = "assist_structure";
     44     public static final String ASSIST_CONTENT_KEY = "assist_content";
     45     public static final String ASSIST_BUNDLE_KEY = "assist_bundle";
     46     public static final String ASSIST_SCREENSHOT_KEY = "assist_screenshot";
     47     public static final String SCREENSHOT_COLOR_KEY = "set_screenshot_color";
     48     public static final String COMPARE_SCREENSHOT_KEY = "compare_screenshot";
     49     public static final String DISPLAY_WIDTH_KEY = "display_width";
     50     public static final String DISPLAY_HEIGHT_KEY = "dislay_height";
     51     public static final String SCROLL_X_POSITION = "scroll_x_position";
     52     public static final String SCROLL_Y_POSITION = "scroll_y_position";
     53 
     54     /** Lifecycle Test intent constants */
     55     public static final String LIFECYCLE_PREFIX = ACTION_PREFIX + "lifecycle_";
     56     public static final String LIFECYCLE_HASRESUMED = LIFECYCLE_PREFIX + "hasResumed";
     57     public static final String LIFECYCLE_HASFOCUS = LIFECYCLE_PREFIX + "hasFocus";
     58     public static final String LIFECYCLE_LOSTFOCUS = LIFECYCLE_PREFIX + "lostFocus";
     59     public static final String LIFECYCLE_ONPAUSE = LIFECYCLE_PREFIX + "onpause";
     60     public static final String LIFECYCLE_ONSTOP = LIFECYCLE_PREFIX + "onstop";
     61     public static final String LIFECYCLE_ONDESTROY = LIFECYCLE_PREFIX + "ondestroy";
     62 
     63     /** Focus Change Test intent constants */
     64     public static final String GAINED_FOCUS = ACTION_PREFIX + "focus_changed";
     65     public static final String LOST_FOCUS = ACTION_PREFIX + "lost_focus";
     66 
     67     /** Flag Secure Test intent constants */
     68     public static final String FLAG_SECURE_HASRESUMED = ACTION_PREFIX + "flag_secure_hasResumed";
     69     public static final String APP_3P_HASRESUMED = ACTION_PREFIX + "app_3p_hasResumed";
     70     public static final String APP_3P_HASDRAWED = ACTION_PREFIX + "app_3p_hasDrawed";
     71     public static final String TEST_ACTIVITY_LOADED = ACTION_PREFIX + "test_activity_hasResumed";
     72 
     73     /** Two second timeout for getting back assist context */
     74     public static final int TIMEOUT_MS = 2 * 1000;
     75     /** Four second timeout for an activity to resume */
     76     public static final int ACTIVITY_ONRESUME_TIMEOUT_MS = 4000;
     77 
     78     public static final String EXTRA_REGISTER_RECEIVER = "register_receiver";
     79 
     80     /** Extras for passing the Assistant's ContentView's dimensions*/
     81     public static final String EXTRA_CONTENT_VIEW_HEIGHT = "extra_content_view_height";
     82     public static final String EXTRA_CONTENT_VIEW_WIDTH = "extra_content_view_width";
     83     public static final String EXTRA_DISPLAY_POINT = "extra_display_point";
     84 
     85     /** Test name suffixes */
     86     public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE";
     87     public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT";
     88     public static final String FLAG_SECURE = "FLAG_SECURE";
     89     public static final String LIFECYCLE = "LIFECYCLE";
     90     public static final String LIFECYCLE_NOUI = "LIFECYCLE_NOUI";
     91     public static final String SCREENSHOT = "SCREENSHOT";
     92     public static final String EXTRA_ASSIST = "EXTRA_ASSIST";
     93     public static final String VERIFY_CONTENT_VIEW = "VERIFY_CONTENT_VIEW";
     94     public static final String TEXTVIEW = "TEXTVIEW";
     95     public static final String LARGE_VIEW_HIERARCHY = "LARGE_VIEW_HIERARCHY";
     96     public static final String WEBVIEW = "WEBVIEW";
     97     public static final String FOCUS_CHANGE = "FOCUS_CHANGE";
     98 
     99     /** Session intent constants */
    100     public static final String HIDE_SESSION = "android.intent.action.hide_session";
    101 
    102     /** Lifecycle activity intent constants */
    103     /** Session intent constants */
    104     public static final String HIDE_LIFECYCLE_ACTIVITY
    105             = "android.intent.action.hide_lifecycle_activity";
    106 
    107     /** Stub html view to load into WebView */
    108     public static final String WEBVIEW_HTML_URL = "http://dev.null/thou/should?not=pass";
    109     public static final String WEBVIEW_HTML_DOMAIN = "dev.null";
    110     public static final LocaleList WEBVIEW_LOCALE_LIST = new LocaleList(Locale.ROOT, Locale.US);
    111     public static final String WEBVIEW_HTML_GREETING = "Hello WebView!";
    112     public static final String WEBVIEW_HTML = "<html><body><div><p>" + WEBVIEW_HTML_GREETING
    113             + "</p></div></body></html>";
    114 
    115     /** Extra data to add to assist data and assist content */
    116     private static Bundle EXTRA_ASSIST_BUNDLE;
    117     private static String STRUCTURED_JSON;
    118 
    119     public static final String getStructuredJSON() throws Exception {
    120         if (STRUCTURED_JSON == null) {
    121             STRUCTURED_JSON = new JSONObject()
    122                     .put("@type", "MusicRecording")
    123                     .put("@id", "https://example/music/recording")
    124                     .put("url", "android-app://com.example/https/music/album")
    125                     .put("name", "Album Title")
    126                     .put("hello", "hi there")
    127                     .put("knownNull", null)
    128                     .put("unicode value", "\ud800\udc35")
    129                     .put("empty string", "")
    130                     .put("LongString",
    131                         "lkasdjfalsdkfjalsdjfalskj9i9234jl1w23j4o123j412l3j421l3kj412l3kj1l3k4j32")
    132                     .put("\ud800\udc35", "any-value")
    133                     .put("key with spaces", "any-value")
    134                     .toString();
    135         }
    136         return STRUCTURED_JSON;
    137     }
    138 
    139     public static final Bundle getExtraAssistBundle() {
    140         if (EXTRA_ASSIST_BUNDLE == null) {
    141             EXTRA_ASSIST_BUNDLE = new Bundle();
    142             addExtraAssistDataToBundle(EXTRA_ASSIST_BUNDLE);
    143         }
    144         return EXTRA_ASSIST_BUNDLE;
    145     }
    146 
    147     public static void addExtraAssistDataToBundle(Bundle data) {
    148         data.putString("hello", "there");
    149         data.putBoolean("isthis_true_or_false", true);
    150         data.putInt("number", 123);
    151     }
    152 
    153     /** The shim activity that starts the service associated with each test. */
    154     public static final String getTestActivity(String testCaseType) {
    155         switch (testCaseType) {
    156             case DISABLE_CONTEXT:
    157                 // doesn't need to wait for activity to resume
    158                 // can be activated on top of any non-secure activity.
    159                 return "service.DisableContextActivity";
    160             case ASSIST_STRUCTURE:
    161             case FLAG_SECURE:
    162             case LIFECYCLE:
    163             case LIFECYCLE_NOUI:
    164             case SCREENSHOT:
    165             case EXTRA_ASSIST:
    166             case VERIFY_CONTENT_VIEW:
    167             case TEXTVIEW:
    168             case LARGE_VIEW_HIERARCHY:
    169             case WEBVIEW:
    170             case FOCUS_CHANGE:
    171                 return "service.DelayedAssistantActivity";
    172             default:
    173                 return "";
    174         }
    175     }
    176 
    177     /**
    178      * The test app associated with each test.
    179      */
    180     public static final ComponentName getTestAppComponent(String testCaseType) {
    181         switch (testCaseType) {
    182             case ASSIST_STRUCTURE:
    183             case LARGE_VIEW_HIERARCHY:
    184                 return new ComponentName(
    185                         "android.assist.testapp", "android.assist.testapp.TestApp");
    186             case DISABLE_CONTEXT:
    187                 return new ComponentName(
    188                         "android.assist.testapp", "android.assist.testapp.TestApp");
    189             case FLAG_SECURE:
    190                 return new ComponentName(
    191                         "android.assist.testapp", "android.assist.testapp.SecureActivity");
    192             case LIFECYCLE:
    193             case LIFECYCLE_NOUI:
    194                 return new ComponentName(
    195                         "android.assist.testapp", "android.assist.testapp.LifecycleActivity");
    196             case SCREENSHOT:
    197                 return new ComponentName(
    198                         "android.assist.testapp", "android.assist.testapp.ScreenshotActivity");
    199             case EXTRA_ASSIST:
    200                 return new ComponentName(
    201                         "android.assist.testapp", "android.assist.testapp.ExtraAssistDataActivity");
    202             case TEXTVIEW:
    203                 return new ComponentName(
    204                         "android.assist.testapp", "android.assist.testapp.TextViewActivity");
    205             case WEBVIEW:
    206                 return new ComponentName(
    207                         "android.assist.testapp", "android.assist.testapp.WebViewActivity");
    208             case FOCUS_CHANGE:
    209                 return new ComponentName(
    210                         "android.assist.testapp", "android.assist.testapp.FocusChangeActivity");
    211             default:
    212                 return new ComponentName("","");
    213         }
    214     }
    215 
    216     /**
    217      * Returns the amount of time to wait for assist data.
    218      */
    219     public static final int getAssistDataTimeout(String testCaseType) {
    220         switch (testCaseType) {
    221             case SCREENSHOT:
    222                 // needs to wait for 3p activity to resume before receiving assist data.
    223                 return TIMEOUT_MS + ACTIVITY_ONRESUME_TIMEOUT_MS;
    224             default:
    225                 return TIMEOUT_MS;
    226         }
    227     }
    228 
    229     public static final String toBundleString(Bundle bundle) {
    230         if (bundle == null) {
    231             return "*** Bundle is null ****";
    232         }
    233         StringBuffer buf = new StringBuffer("Bundle is: ");
    234         String testType = bundle.getString(TESTCASE_TYPE);
    235         if (testType != null) {
    236             buf.append("testcase type = " + testType);
    237         }
    238         ArrayList<String> info = bundle.getStringArrayList(TESTINFO);
    239         if (info != null) {
    240             for (String s : info) {
    241                 buf.append(s + "\n\t\t");
    242             }
    243         }
    244         return buf.toString();
    245     }
    246 
    247     public static final void addErrorResult(final Bundle testinfo, final String msg) {
    248         testinfo.getStringArrayList(testinfo.getString(Utils.TESTCASE_TYPE))
    249             .add(TEST_ERROR + " " + msg);
    250     }
    251 }
    252