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.R; 19 import android.content.ComponentName; 20 import android.os.Bundle; 21 22 import org.json.JSONException; 23 import org.json.JSONObject; 24 25 import java.util.ArrayList; 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 TEST_ERROR = "Error In Test:"; 40 41 public static final String ASSIST_STRUCTURE_KEY = "assist_structure"; 42 public static final String ASSIST_CONTENT_KEY = "assist_content"; 43 public static final String ASSIST_BUNDLE_KEY = "assist_bundle"; 44 public static final String ASSIST_SCREENSHOT_KEY = "assist_screenshot"; 45 public static final String SCREENSHOT_COLOR_KEY = "set_screenshot_color"; 46 public static final String COMPARE_SCREENSHOT_KEY = "compare_screenshot"; 47 public static final String DISPLAY_WIDTH_KEY = "display_width"; 48 public static final String DISPLAY_HEIGHT_KEY = "dislay_height"; 49 50 /** Lifecycle Test intent constants */ 51 public static final String LIFECYCLE_PREFIX = ACTION_PREFIX + "lifecycle_"; 52 public static final String LIFECYCLE_HASRESUMED = LIFECYCLE_PREFIX + "hasResumed"; 53 public static final String LIFECYCLE_ONPAUSE = LIFECYCLE_PREFIX + "onpause"; 54 public static final String LIFECYCLE_ONSTOP = LIFECYCLE_PREFIX + "onstop"; 55 public static final String LIFECYCLE_ONDESTROY = LIFECYCLE_PREFIX + "ondestroy"; 56 57 /** Flag Secure Test intent constants */ 58 public static final String FLAG_SECURE_HASRESUMED = ACTION_PREFIX + "flag_secure_hasResumed"; 59 public static final String APP_3P_HASRESUMED = ACTION_PREFIX + "screenshot_hasResumed"; 60 public static final String ASSIST_STRUCTURE_HASRESUMED = ACTION_PREFIX 61 + "assist_structure_hasResumed"; 62 63 /** Two second timeout for getting back assist context */ 64 public static final int TIMEOUT_MS = 2 * 1000; 65 /** Four second timeout for an activity to resume */ 66 public static final int ACTIVITY_ONRESUME_TIMEOUT_MS = 4000; 67 68 public static final String EXTRA_REGISTER_RECEIVER = "register_receiver"; 69 70 /** Extras for passing the Assistant's ContentView's dimensions*/ 71 public static final String EXTRA_CONTENT_VIEW_HEIGHT = "extra_content_view_height"; 72 public static final String EXTRA_CONTENT_VIEW_WIDTH = "extra_content_view_width"; 73 public static final String EXTRA_DISPLAY_POINT = "extra_display_point"; 74 75 /** Test name suffixes */ 76 public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE"; 77 public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT"; 78 public static final String FLAG_SECURE = "FLAG_SECURE"; 79 public static final String LIFECYCLE = "LIFECYCLE"; 80 public static final String SCREENSHOT = "SCREENSHOT"; 81 public static final String EXTRA_ASSIST = "EXTRA_ASSIST"; 82 public static final String VERIFY_CONTENT_VIEW = "VERIFY_CONTENT_VIEW"; 83 84 /** Session intent constants */ 85 public static final String HIDE_SESSION = "android.intent.action.hide_session"; 86 87 /** Extra data to add to assist data and assist content */ 88 private static Bundle EXTRA_ASSIST_BUNDLE; 89 private static String STRUCTURED_JSON; 90 getStructuredJSON()91 public static final String getStructuredJSON() throws Exception { 92 if (STRUCTURED_JSON == null) { 93 STRUCTURED_JSON = new JSONObject() 94 .put("@type", "MusicRecording") 95 .put("@id", "https://example/music/recording") 96 .put("url", "android-app://com.example/https/music/album") 97 .put("name", "Album Title") 98 .put("hello", "hi there") 99 .put("knownNull", null) 100 .put("unicode value", "\ud800\udc35") 101 .put("empty string", "") 102 .put("LongString", 103 "lkasdjfalsdkfjalsdjfalskj9i9234jl1w23j4o123j412l3j421l3kj412l3kj1l3k4j32") 104 .put("\ud800\udc35", "any-value") 105 .put("key with spaces", "any-value") 106 .toString(); 107 } 108 return STRUCTURED_JSON; 109 } 110 getExtraAssistBundle()111 public static final Bundle getExtraAssistBundle() { 112 if (EXTRA_ASSIST_BUNDLE == null) { 113 EXTRA_ASSIST_BUNDLE = new Bundle(); 114 addExtraAssistDataToBundle(EXTRA_ASSIST_BUNDLE); 115 } 116 return EXTRA_ASSIST_BUNDLE; 117 } 118 addExtraAssistDataToBundle(Bundle data)119 public static void addExtraAssistDataToBundle(Bundle data) { 120 data.putString("hello", "there"); 121 data.putBoolean("isthis_true_or_false", true); 122 data.putInt("number", 123); 123 } 124 125 /** The shim activity that starts the service associated with each test. */ getTestActivity(String testCaseType)126 public static final String getTestActivity(String testCaseType) { 127 switch (testCaseType) { 128 case DISABLE_CONTEXT: 129 // doesn't need to wait for activity to resume 130 // can be activated on top of any non-secure activity. 131 return "service.DisableContextActivity"; 132 case ASSIST_STRUCTURE: 133 case FLAG_SECURE: 134 case LIFECYCLE: 135 case SCREENSHOT: 136 case EXTRA_ASSIST: 137 case VERIFY_CONTENT_VIEW: 138 return "service.DelayedAssistantActivity"; 139 default: 140 return ""; 141 } 142 } 143 144 /** 145 * The test app associated with each test. 146 */ getTestAppComponent(String testCaseType)147 public static final ComponentName getTestAppComponent(String testCaseType) { 148 switch (testCaseType) { 149 case ASSIST_STRUCTURE: 150 case DISABLE_CONTEXT: 151 return new ComponentName( 152 "android.assist.testapp", "android.assist.testapp.TestApp"); 153 case FLAG_SECURE: 154 return new ComponentName( 155 "android.assist.testapp", "android.assist.testapp.SecureActivity"); 156 case LIFECYCLE: 157 return new ComponentName( 158 "android.assist.testapp", "android.assist.testapp.LifecycleActivity"); 159 case SCREENSHOT: 160 return new ComponentName( 161 "android.assist.testapp", "android.assist.testapp.ScreenshotActivity"); 162 case EXTRA_ASSIST: 163 return new ComponentName( 164 "android.assist.testapp", "android.assist.testapp.ExtraAssistDataActivity"); 165 default: 166 return new ComponentName("",""); 167 } 168 } 169 170 /** 171 * Returns the amount of time to wait for assist data. 172 */ getAssistDataTimeout(String testCaseType)173 public static final int getAssistDataTimeout(String testCaseType) { 174 switch (testCaseType) { 175 case SCREENSHOT: 176 // needs to wait for 3p activity to resume before receiving assist data. 177 return TIMEOUT_MS + ACTIVITY_ONRESUME_TIMEOUT_MS; 178 default: 179 return TIMEOUT_MS; 180 } 181 } 182 toBundleString(Bundle bundle)183 public static final String toBundleString(Bundle bundle) { 184 if (bundle == null) { 185 return "*** Bundle is null ****"; 186 } 187 StringBuffer buf = new StringBuffer("Bundle is: "); 188 String testType = bundle.getString(TESTCASE_TYPE); 189 if (testType != null) { 190 buf.append("testcase type = " + testType); 191 } 192 ArrayList<String> info = bundle.getStringArrayList(TESTINFO); 193 if (info != null) { 194 for (String s : info) { 195 buf.append(s + "\n\t\t"); 196 } 197 } 198 return buf.toString(); 199 } 200 addErrorResult(final Bundle testinfo, final String msg)201 public static final void addErrorResult(final Bundle testinfo, final String msg) { 202 testinfo.getStringArrayList(testinfo.getString(Utils.TESTCASE_TYPE)) 203 .add(TEST_ERROR + " " + msg); 204 } 205 } 206