blob: cc45dbdd16752121fc736d2fd5a1cd15117c8054 [file] [log] [blame]
Alice Leed71a07b2015-07-29 14:30:56 -07001/*
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 */
16package android.assist.common;
17
Alice Lee78098bf2015-08-05 15:42:47 -070018import android.R;
Alice Lee69f4ec42015-08-05 14:17:57 -070019import android.content.ComponentName;
Alice Leed71a07b2015-07-29 14:30:56 -070020import android.os.Bundle;
Alice Leed71a07b2015-07-29 14:30:56 -070021
Alice Lee474d0e42015-09-02 15:14:00 -070022import org.json.JSONException;
23import org.json.JSONObject;
24
Alice Leed71a07b2015-07-29 14:30:56 -070025import java.util.ArrayList;
Alice Leed71a07b2015-07-29 14:30:56 -070026
27public class Utils {
Alice Leed71a07b2015-07-29 14:30:56 -070028 public static final String TESTCASE_TYPE = "testcase_type";
29 public static final String TESTINFO = "testinfo";
Cameron Nealeff1da6f2015-08-18 14:44:20 -070030 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 Nealec77854e2015-09-04 10:09:27 -070035
Alice Leef4aef2b2015-08-25 14:29:16 -070036 public static final String ACTION_INVALIDATE = "invalidate_action";
Cameron Nealec77854e2015-09-04 10:09:27 -070037 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 Leed71a07b2015-07-29 14:30:56 -070039 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 Lee69f4ec42015-08-05 14:17:57 -070043 public static final String ASSIST_BUNDLE_KEY = "assist_bundle";
44 public static final String ASSIST_SCREENSHOT_KEY = "assist_screenshot";
Alice Leef4aef2b2015-08-25 14:29:16 -070045 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 Nealeff1da6f2015-08-18 14:44:20 -070049
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 Lee474d0e42015-09-02 15:14:00 -070059 public static final String APP_3P_HASRESUMED = ACTION_PREFIX + "screenshot_hasResumed";
Alice Leef6b3e9e2015-08-19 14:54:54 -070060 public static final String ASSIST_STRUCTURE_HASRESUMED = ACTION_PREFIX
61 + "assist_structure_hasResumed";
Cameron Nealeff1da6f2015-08-18 14:44:20 -070062
63 /** Two second timeout for getting back assist context */
Alice Leef6b3e9e2015-08-19 14:54:54 -070064 public static final int TIMEOUT_MS = 2 * 1000;
Alice Leef4aef2b2015-08-25 14:29:16 -070065 /** Four second timeout for an activity to resume */
Cameron Nealeff1da6f2015-08-18 14:44:20 -070066 public static final int ACTIVITY_ONRESUME_TIMEOUT_MS = 4000;
Alice Leef4aef2b2015-08-25 14:29:16 -070067
Cameron Nealeff1da6f2015-08-18 14:44:20 -070068 public static final String EXTRA_REGISTER_RECEIVER = "register_receiver";
Alice Leed71a07b2015-07-29 14:30:56 -070069
Cameron Nealec77854e2015-09-04 10:09:27 -070070 /** 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 Lee78098bf2015-08-05 15:42:47 -070075 /** Test name suffixes */
Alice Lee69f4ec42015-08-05 14:17:57 -070076 public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE";
77 public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT";
Alice Lee78098bf2015-08-05 15:42:47 -070078 public static final String FLAG_SECURE = "FLAG_SECURE";
Cameron Nealeff1da6f2015-08-18 14:44:20 -070079 public static final String LIFECYCLE = "LIFECYCLE";
Alice Leef4aef2b2015-08-25 14:29:16 -070080 public static final String SCREENSHOT = "SCREENSHOT";
Alice Lee474d0e42015-09-02 15:14:00 -070081 public static final String EXTRA_ASSIST = "EXTRA_ASSIST";
Cameron Nealec77854e2015-09-04 10:09:27 -070082 public static final String VERIFY_CONTENT_VIEW = "VERIFY_CONTENT_VIEW";
Cameron Nealeff1da6f2015-08-18 14:44:20 -070083
84 /** Session intent constants */
85 public static final String HIDE_SESSION = "android.intent.action.hide_session";
Alice Lee69f4ec42015-08-05 14:17:57 -070086
Alice Lee474d0e42015-09-02 15:14:00 -070087 /** 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 Leef4aef2b2015-08-25 14:29:16 -0700125 /** The shim activity that starts the service associated with each test. */
Alice Leed71a07b2015-07-29 14:30:56 -0700126 public static final String getTestActivity(String testCaseType) {
Alice Lee69f4ec42015-08-05 14:17:57 -0700127 switch (testCaseType) {
Alice Lee69f4ec42015-08-05 14:17:57 -0700128 case DISABLE_CONTEXT:
Alice Leef4aef2b2015-08-25 14:29:16 -0700129 // doesn't need to wait for activity to resume
130 // can be activated on top of any non-secure activity.
Alice Lee69f4ec42015-08-05 14:17:57 -0700131 return "service.DisableContextActivity";
Alice Leef6b3e9e2015-08-19 14:54:54 -0700132 case ASSIST_STRUCTURE:
Cameron Nealeff1da6f2015-08-18 14:44:20 -0700133 case FLAG_SECURE:
134 case LIFECYCLE:
Alice Leef4aef2b2015-08-25 14:29:16 -0700135 case SCREENSHOT:
Alice Lee474d0e42015-09-02 15:14:00 -0700136 case EXTRA_ASSIST:
Cameron Nealec77854e2015-09-04 10:09:27 -0700137 case VERIFY_CONTENT_VIEW:
Cameron Nealeff1da6f2015-08-18 14:44:20 -0700138 return "service.DelayedAssistantActivity";
Alice Lee69f4ec42015-08-05 14:17:57 -0700139 default:
140 return "";
Alice Leed71a07b2015-07-29 14:30:56 -0700141 }
Alice Lee69f4ec42015-08-05 14:17:57 -0700142 }
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 Leef6b3e9e2015-08-19 14:54:54 -0700150 case DISABLE_CONTEXT:
151 return new ComponentName(
Alice Leef4aef2b2015-08-25 14:29:16 -0700152 "android.assist.testapp", "android.assist.testapp.TestApp");
Alice Lee78098bf2015-08-05 15:42:47 -0700153 case FLAG_SECURE:
154 return new ComponentName(
155 "android.assist.testapp", "android.assist.testapp.SecureActivity");
Cameron Nealeff1da6f2015-08-18 14:44:20 -0700156 case LIFECYCLE:
157 return new ComponentName(
158 "android.assist.testapp", "android.assist.testapp.LifecycleActivity");
Alice Leef4aef2b2015-08-25 14:29:16 -0700159 case SCREENSHOT:
160 return new ComponentName(
161 "android.assist.testapp", "android.assist.testapp.ScreenshotActivity");
Alice Lee474d0e42015-09-02 15:14:00 -0700162 case EXTRA_ASSIST:
163 return new ComponentName(
164 "android.assist.testapp", "android.assist.testapp.ExtraAssistDataActivity");
Alice Lee69f4ec42015-08-05 14:17:57 -0700165 default:
166 return new ComponentName("","");
167 }
Alice Leed71a07b2015-07-29 14:30:56 -0700168 }
169
Alice Leef4aef2b2015-08-25 14:29:16 -0700170 /**
171 * Returns the amount of time to wait for assist data.
172 */
173 public static final int getAssistDataTimeout(String testCaseType) {
174 switch (testCaseType) {
Alice Leef4aef2b2015-08-25 14:29:16 -0700175 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 Lee474d0e42015-09-02 15:14:00 -0700179 return TIMEOUT_MS;
Alice Leef4aef2b2015-08-25 14:29:16 -0700180 }
181 }
182
Alice Leed71a07b2015-07-29 14:30:56 -0700183 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 Leed71a07b2015-07-29 14:30:56 -0700201 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}