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.content.Context; 20 import android.content.Intent; 21 import android.content.pm.PackageManager; 22 import android.os.Bundle; 23 import android.os.LocaleList; 24 import android.os.Process; 25 import android.util.Log; 26 27 import org.json.JSONObject; 28 29 import java.util.ArrayList; 30 import java.util.Locale; 31 32 public class Utils { 33 private static final String TAG = Utils.class.getSimpleName(); 34 public static final String TESTCASE_TYPE = "testcase_type"; 35 public static final String TESTINFO = "testinfo"; 36 public static final String ACTION_PREFIX = "android.intent.action."; 37 public static final String BROADCAST_INTENT = ACTION_PREFIX + "ASSIST_TESTAPP"; 38 public static final String BROADCAST_ASSIST_DATA_INTENT = ACTION_PREFIX + "ASSIST_DATA"; 39 public static final String BROADCAST_INTENT_START_ASSIST = ACTION_PREFIX + "START_ASSIST"; 40 public static final String ASSIST_RECEIVER_REGISTERED = ACTION_PREFIX + "ASSIST_READY"; 41 public static final String ACTION_END_OF_TEST = ACTION_PREFIX + "END_OF_TEST"; 42 43 public static final String ACTION_INVALIDATE = "invalidate_action"; 44 public static final String GET_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "GET_CONTENT_VIEW_HEIGHT"; 45 public static final String BROADCAST_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "VIEW_HEIGHT"; 46 public static final String SCROLL_TEXTVIEW_ACTION = ACTION_PREFIX + "TEXTVIEW_SCROLL"; 47 public static final String SCROLL_SCROLLVIEW_ACTION = ACTION_PREFIX + "SCROLLVIEW_SCROLL"; 48 public static final String TEST_ERROR = "Error In Test:"; 49 50 public static final String ASSIST_STRUCTURE_KEY = "assist_structure"; 51 public static final String ASSIST_CONTENT_KEY = "assist_content"; 52 public static final String ASSIST_BUNDLE_KEY = "assist_bundle"; 53 public static final String ASSIST_IS_ACTIVITY_ID_NULL = "assist_is_activity_id_null"; 54 public static final String ASSIST_SCREENSHOT_KEY = "assist_screenshot"; 55 public static final String SCREENSHOT_COLOR_KEY = "set_screenshot_color"; 56 public static final String COMPARE_SCREENSHOT_KEY = "compare_screenshot"; 57 public static final String DISPLAY_WIDTH_KEY = "display_width"; 58 public static final String DISPLAY_HEIGHT_KEY = "dislay_height"; 59 public static final String DISPLAY_AREA_BOUNDS_KEY = "display_area_bounds"; 60 public static final String SCROLL_X_POSITION = "scroll_x_position"; 61 public static final String SCROLL_Y_POSITION = "scroll_y_position"; 62 public static final String SHOW_SESSION_FLAGS_TO_SET = "show_session_flags_to_set"; 63 64 /** Lifecycle Test intent constants */ 65 public static final String LIFECYCLE_PREFIX = ACTION_PREFIX + "lifecycle_"; 66 public static final String LIFECYCLE_HASRESUMED = LIFECYCLE_PREFIX + "hasResumed"; 67 public static final String LIFECYCLE_HASFOCUS = LIFECYCLE_PREFIX + "hasFocus"; 68 public static final String LIFECYCLE_LOSTFOCUS = LIFECYCLE_PREFIX + "lostFocus"; 69 public static final String LIFECYCLE_ONPAUSE = LIFECYCLE_PREFIX + "onpause"; 70 public static final String LIFECYCLE_ONSTOP = LIFECYCLE_PREFIX + "onstop"; 71 public static final String LIFECYCLE_ONDESTROY = LIFECYCLE_PREFIX + "ondestroy"; 72 73 /** Focus Change Test intent constants */ 74 public static final String GAINED_FOCUS = ACTION_PREFIX + "focus_changed"; 75 public static final String LOST_FOCUS = ACTION_PREFIX + "lost_focus"; 76 77 public static final String APP_3P_HASRESUMED = ACTION_PREFIX + "app_3p_hasResumed"; 78 public static final String APP_3P_HASDRAWED = ACTION_PREFIX + "app_3p_hasDrawed"; 79 public static final String TEST_ACTIVITY_DESTROY = ACTION_PREFIX + "test_activity_destroy"; 80 public static final String TEST_ACTIVITY_WEBVIEW_LOADED = ACTION_PREFIX + "test_activity_webview_hasResumed"; 81 82 // Notice: timeout belows have to be long because some devices / form factors (like car) are 83 // slower. 84 85 /** Timeout for getting back assist context */ 86 public static final int TIMEOUT_MS = 6 * 1_000; 87 /** Timeout for an activity to resume */ 88 public static final int ACTIVITY_ONRESUME_TIMEOUT_MS = 12 * 1_000; 89 90 public static final String EXTRA_REGISTER_RECEIVER = "register_receiver"; 91 92 /** Extras for passing the Assistant's ContentView's dimensions*/ 93 public static final String EXTRA_CONTENT_VIEW_HEIGHT = "extra_content_view_height"; 94 public static final String EXTRA_CONTENT_VIEW_WIDTH = "extra_content_view_width"; 95 public static final String EXTRA_DISPLAY_POINT = "extra_display_point"; 96 97 /* 98 * Extras used to pass RemoteCallback objects responsible for IPC between test, app, and 99 * service. 100 */ 101 public static final String EXTRA_REMOTE_CALLBACK = "extra_remote_callback"; 102 public static final String EXTRA_REMOTE_CALLBACK_ACTION = "extra_remote_callback_action"; 103 104 public static final String EXTRA_REMOTE_CALLBACK_RECEIVING = "extra_remote_callback_receiving"; 105 public static final String EXTRA_REMOTE_CALLBACK_RECEIVING_ACTION = "extra_remote_callback_receiving_action"; 106 107 /** Test name suffixes */ 108 public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE"; 109 public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT"; 110 public static final String FLAG_SECURE = "FLAG_SECURE"; 111 public static final String LIFECYCLE = "LIFECYCLE"; 112 public static final String LIFECYCLE_NOUI = "LIFECYCLE_NOUI"; 113 public static final String SCREENSHOT = "SCREENSHOT"; 114 public static final String EXTRA_ASSIST = "EXTRA_ASSIST"; 115 public static final String VERIFY_CONTENT_VIEW = "VERIFY_CONTENT_VIEW"; 116 public static final String TEXTVIEW = "TEXTVIEW"; 117 public static final String LARGE_VIEW_HIERARCHY = "LARGE_VIEW_HIERARCHY"; 118 public static final String WEBVIEW = "WEBVIEW"; 119 public static final String FOCUS_CHANGE = "FOCUS_CHANGE"; 120 121 /** Session intent constants */ 122 public static final String HIDE_SESSION = "android.intent.action.hide_session"; 123 public static final String HIDE_SESSION_COMPLETE = "android.intent.action.hide_session_complete"; 124 125 /** Lifecycle activity intent constants */ 126 /** Session intent constants */ 127 public static final String HIDE_LIFECYCLE_ACTIVITY 128 = "android.intent.action.hide_lifecycle_activity"; 129 130 /** Stub html view to load into WebView */ 131 public static final String WEBVIEW_HTML_URL = "http://dev.null/thou/should?not=pass"; 132 public static final String WEBVIEW_HTML_DOMAIN = "dev.null"; 133 public static final LocaleList WEBVIEW_LOCALE_LIST = new LocaleList(Locale.ROOT, Locale.US); 134 public static final String WEBVIEW_HTML_GREETING = "Hello WebView!"; 135 public static final String WEBVIEW_HTML = "<html><body><div><p>" + WEBVIEW_HTML_GREETING 136 + "</p></div></body></html>"; 137 138 /** Extra data to add to assist data and assist content */ 139 private static Bundle EXTRA_ASSIST_BUNDLE; 140 private static String STRUCTURED_JSON; 141 142 private static String MY_UID_EXTRA = "my_uid"; 143 getStructuredJSON()144 public static final String getStructuredJSON() throws Exception { 145 if (STRUCTURED_JSON == null) { 146 STRUCTURED_JSON = new JSONObject() 147 .put("@type", "MusicRecording") 148 .put("@id", "https://example/music/recording") 149 .put("url", "android-app://com.example/https/music/album") 150 .put("name", "Album Title") 151 .put("hello", "hi there") 152 .put("knownNull", null) 153 .put("unicode value", "\ud800\udc35") 154 .put("empty string", "") 155 .put("LongString", 156 "lkasdjfalsdkfjalsdjfalskj9i9234jl1w23j4o123j412l3j421l3kj412l3kj1l3k4j32") 157 .put("\ud800\udc35", "any-value") 158 .put("key with spaces", "any-value") 159 .toString(); 160 } 161 return STRUCTURED_JSON; 162 } 163 getExtraAssistBundle()164 public static final Bundle getExtraAssistBundle() { 165 if (EXTRA_ASSIST_BUNDLE == null) { 166 EXTRA_ASSIST_BUNDLE = new Bundle(); 167 addExtraAssistDataToBundle(EXTRA_ASSIST_BUNDLE, /* addMyUid= */ false); 168 } 169 return EXTRA_ASSIST_BUNDLE; 170 } 171 addExtraAssistDataToBundle(Bundle data)172 public static void addExtraAssistDataToBundle(Bundle data) { 173 addExtraAssistDataToBundle(data, /* addMyUid= */ true); 174 175 } 176 addExtraAssistDataToBundle(Bundle data, boolean addMyUid)177 private static void addExtraAssistDataToBundle(Bundle data, boolean addMyUid) { 178 data.putString("hello", "there"); 179 data.putBoolean("isthis_true_or_false", true); 180 data.putInt("number", 123); 181 if (addMyUid) { 182 Log.i(TAG, "adding " + MY_UID_EXTRA + "=" + Process.myUid()); 183 data.putInt(MY_UID_EXTRA, Process.myUid()); 184 } 185 } 186 187 /** 188 * The test app associated with each test. 189 */ getTestAppComponent(String testCaseType)190 public static final ComponentName getTestAppComponent(String testCaseType) { 191 switch (testCaseType) { 192 case ASSIST_STRUCTURE: 193 case LARGE_VIEW_HIERARCHY: 194 case DISABLE_CONTEXT: 195 return new ComponentName( 196 "android.assist.testapp", "android.assist.testapp.TestApp"); 197 case FLAG_SECURE: 198 return new ComponentName( 199 "android.assist.testapp", "android.assist.testapp.SecureActivity"); 200 case LIFECYCLE: 201 case LIFECYCLE_NOUI: 202 return new ComponentName( 203 "android.assist.testapp", "android.assist.testapp.LifecycleActivity"); 204 case SCREENSHOT: 205 return new ComponentName( 206 "android.assist.testapp", "android.assist.testapp.ScreenshotActivity"); 207 case EXTRA_ASSIST: 208 return new ComponentName( 209 "android.assist.testapp", "android.assist.testapp.ExtraAssistDataActivity"); 210 case TEXTVIEW: 211 return new ComponentName( 212 "android.assist.testapp", "android.assist.testapp.TextViewActivity"); 213 case WEBVIEW: 214 return new ComponentName( 215 "android.assist.testapp", "android.assist.testapp.WebViewActivity"); 216 case FOCUS_CHANGE: 217 return new ComponentName( 218 "android.assist.testapp", "android.assist.testapp.FocusChangeActivity"); 219 default: 220 return new ComponentName("",""); 221 } 222 } 223 224 /** 225 * Sets the proper action used to launch an activity in the testapp package. 226 */ setTestAppAction(Intent intent, String testCaseName)227 public static void setTestAppAction(Intent intent, String testCaseName) { 228 intent.putExtra(Utils.TESTCASE_TYPE, testCaseName); 229 intent.setAction("android.intent.action.TEST_APP_" + testCaseName); 230 } 231 232 /** 233 * Returns the amount of time to wait for assist data. 234 */ getAssistDataTimeout(String testCaseType)235 public static final int getAssistDataTimeout(String testCaseType) { 236 switch (testCaseType) { 237 case SCREENSHOT: 238 // needs to wait for 3p activity to resume before receiving assist data. 239 return TIMEOUT_MS + ACTIVITY_ONRESUME_TIMEOUT_MS; 240 default: 241 return TIMEOUT_MS; 242 } 243 } 244 toBundleString(Bundle bundle)245 public static final String toBundleString(Bundle bundle) { 246 if (bundle == null) { 247 return "*** Bundle is null ****"; 248 } 249 StringBuffer buf = new StringBuffer("Bundle is: "); 250 String testType = bundle.getString(TESTCASE_TYPE); 251 if (testType != null) { 252 buf.append("testcase type = " + testType); 253 } 254 ArrayList<String> info = bundle.getStringArrayList(TESTINFO); 255 if (info != null) { 256 for (String s : info) { 257 buf.append(s + "\n\t\t"); 258 } 259 } 260 return buf.toString(); 261 } 262 addErrorResult(final Bundle testinfo, final String msg)263 public static final void addErrorResult(final Bundle testinfo, final String msg) { 264 testinfo.getStringArrayList(testinfo.getString(Utils.TESTCASE_TYPE)) 265 .add(TEST_ERROR + " " + msg); 266 } 267 getExpectedUid(Bundle extras)268 public static int getExpectedUid(Bundle extras) { 269 return extras.getInt(MY_UID_EXTRA); 270 } 271 bundleOfRemoteAction(String action)272 public static Bundle bundleOfRemoteAction(String action) { 273 Bundle bundle = new Bundle(); 274 bundle.putString(Utils.EXTRA_REMOTE_CALLBACK_ACTION, action); 275 return bundle; 276 } 277 isAutomotive(Context context)278 public static boolean isAutomotive(Context context) { 279 return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE); 280 } 281 } 282