Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 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 | |
Alice Lee | 78098bf | 2015-08-05 15:42:47 -0700 | [diff] [blame] | 18 | import android.R; |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 19 | import android.content.ComponentName; |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 20 | import android.os.Bundle; |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 21 | |
Alice Lee | 474d0e4 | 2015-09-02 15:14:00 -0700 | [diff] [blame] | 22 | import org.json.JSONException; |
| 23 | import org.json.JSONObject; |
| 24 | |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 25 | import java.util.ArrayList; |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 26 | |
| 27 | public class Utils { |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 28 | public static final String TESTCASE_TYPE = "testcase_type"; |
| 29 | public static final String TESTINFO = "testinfo"; |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 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"; |
Cameron Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 35 | |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 36 | public static final String ACTION_INVALIDATE = "invalidate_action"; |
Cameron Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 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"; |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 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"; |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 43 | public static final String ASSIST_BUNDLE_KEY = "assist_bundle"; |
| 44 | public static final String ASSIST_SCREENSHOT_KEY = "assist_screenshot"; |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 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"; |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 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"; |
Alice Lee | 474d0e4 | 2015-09-02 15:14:00 -0700 | [diff] [blame] | 59 | public static final String APP_3P_HASRESUMED = ACTION_PREFIX + "screenshot_hasResumed"; |
Alice Lee | f6b3e9e | 2015-08-19 14:54:54 -0700 | [diff] [blame] | 60 | public static final String ASSIST_STRUCTURE_HASRESUMED = ACTION_PREFIX |
| 61 | + "assist_structure_hasResumed"; |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 62 | |
| 63 | /** Two second timeout for getting back assist context */ |
Alice Lee | f6b3e9e | 2015-08-19 14:54:54 -0700 | [diff] [blame] | 64 | public static final int TIMEOUT_MS = 2 * 1000; |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 65 | /** Four second timeout for an activity to resume */ |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 66 | public static final int ACTIVITY_ONRESUME_TIMEOUT_MS = 4000; |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 67 | |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 68 | public static final String EXTRA_REGISTER_RECEIVER = "register_receiver"; |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 69 | |
Cameron Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 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 | |
Alice Lee | 78098bf | 2015-08-05 15:42:47 -0700 | [diff] [blame] | 75 | /** Test name suffixes */ |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 76 | public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE"; |
| 77 | public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT"; |
Alice Lee | 78098bf | 2015-08-05 15:42:47 -0700 | [diff] [blame] | 78 | public static final String FLAG_SECURE = "FLAG_SECURE"; |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 79 | public static final String LIFECYCLE = "LIFECYCLE"; |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 80 | public static final String SCREENSHOT = "SCREENSHOT"; |
Alice Lee | 474d0e4 | 2015-09-02 15:14:00 -0700 | [diff] [blame] | 81 | public static final String EXTRA_ASSIST = "EXTRA_ASSIST"; |
Cameron Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 82 | public static final String VERIFY_CONTENT_VIEW = "VERIFY_CONTENT_VIEW"; |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 83 | |
| 84 | /** Session intent constants */ |
| 85 | public static final String HIDE_SESSION = "android.intent.action.hide_session"; |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 86 | |
Alice Lee | 474d0e4 | 2015-09-02 15:14:00 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 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 | |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 125 | /** The shim activity that starts the service associated with each test. */ |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 126 | public static final String getTestActivity(String testCaseType) { |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 127 | switch (testCaseType) { |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 128 | case DISABLE_CONTEXT: |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 129 | // doesn't need to wait for activity to resume |
| 130 | // can be activated on top of any non-secure activity. |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 131 | return "service.DisableContextActivity"; |
Alice Lee | f6b3e9e | 2015-08-19 14:54:54 -0700 | [diff] [blame] | 132 | case ASSIST_STRUCTURE: |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 133 | case FLAG_SECURE: |
| 134 | case LIFECYCLE: |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 135 | case SCREENSHOT: |
Alice Lee | 474d0e4 | 2015-09-02 15:14:00 -0700 | [diff] [blame] | 136 | case EXTRA_ASSIST: |
Cameron Neale | c77854e | 2015-09-04 10:09:27 -0700 | [diff] [blame^] | 137 | case VERIFY_CONTENT_VIEW: |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 138 | return "service.DelayedAssistantActivity"; |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 139 | default: |
| 140 | return ""; |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 141 | } |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * The test app associated with each test. |
| 146 | */ |
| 147 | public static final ComponentName getTestAppComponent(String testCaseType) { |
| 148 | switch (testCaseType) { |
| 149 | case ASSIST_STRUCTURE: |
Alice Lee | f6b3e9e | 2015-08-19 14:54:54 -0700 | [diff] [blame] | 150 | case DISABLE_CONTEXT: |
| 151 | return new ComponentName( |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 152 | "android.assist.testapp", "android.assist.testapp.TestApp"); |
Alice Lee | 78098bf | 2015-08-05 15:42:47 -0700 | [diff] [blame] | 153 | case FLAG_SECURE: |
| 154 | return new ComponentName( |
| 155 | "android.assist.testapp", "android.assist.testapp.SecureActivity"); |
Cameron Neale | ff1da6f | 2015-08-18 14:44:20 -0700 | [diff] [blame] | 156 | case LIFECYCLE: |
| 157 | return new ComponentName( |
| 158 | "android.assist.testapp", "android.assist.testapp.LifecycleActivity"); |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 159 | case SCREENSHOT: |
| 160 | return new ComponentName( |
| 161 | "android.assist.testapp", "android.assist.testapp.ScreenshotActivity"); |
Alice Lee | 474d0e4 | 2015-09-02 15:14:00 -0700 | [diff] [blame] | 162 | case EXTRA_ASSIST: |
| 163 | return new ComponentName( |
| 164 | "android.assist.testapp", "android.assist.testapp.ExtraAssistDataActivity"); |
Alice Lee | 69f4ec4 | 2015-08-05 14:17:57 -0700 | [diff] [blame] | 165 | default: |
| 166 | return new ComponentName("",""); |
| 167 | } |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 170 | /** |
| 171 | * Returns the amount of time to wait for assist data. |
| 172 | */ |
| 173 | public static final int getAssistDataTimeout(String testCaseType) { |
| 174 | switch (testCaseType) { |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 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: |
Alice Lee | 474d0e4 | 2015-09-02 15:14:00 -0700 | [diff] [blame] | 179 | return TIMEOUT_MS; |
Alice Lee | f4aef2b | 2015-08-25 14:29:16 -0700 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 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 | |
Alice Lee | d71a07b | 2015-07-29 14:30:56 -0700 | [diff] [blame] | 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 | } |