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