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 SCROLL_X_POSITION = "scroll_x_position";
60     public static final String SCROLL_Y_POSITION = "scroll_y_position";
61     public static final String SHOW_SESSION_FLAGS_TO_SET = "show_session_flags_to_set";
62 
63     /** Lifecycle Test intent constants */
64     public static final String LIFECYCLE_PREFIX = ACTION_PREFIX + "lifecycle_";
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     public static final String LOST_FOCUS = ACTION_PREFIX + "lost_focus";
75 
76     public static final String APP_3P_HASRESUMED = ACTION_PREFIX + "app_3p_hasResumed";
77     public static final String APP_3P_HASDRAWED = ACTION_PREFIX + "app_3p_hasDrawed";
78     public static final String TEST_ACTIVITY_DESTROY = ACTION_PREFIX + "test_activity_destroy";
79     public static final String TEST_ACTIVITY_WEBVIEW_LOADED = ACTION_PREFIX + "test_activity_webview_hasResumed";
80 
81     // Notice: timeout belows have to be long because some devices / form factors (like car) are
82     // slower.
83 
84     /** Timeout for getting back assist context */
85     public static final int TIMEOUT_MS = 4 * 1_000;
86     /** Timeout for an activity to resume */
87     public static final int ACTIVITY_ONRESUME_TIMEOUT_MS = 8 * 1_000;
88 
89     public static final String EXTRA_REGISTER_RECEIVER = "register_receiver";
90 
91     /** Extras for passing the Assistant's ContentView's dimensions*/
92     public static final String EXTRA_CONTENT_VIEW_HEIGHT = "extra_content_view_height";
93     public static final String EXTRA_CONTENT_VIEW_WIDTH = "extra_content_view_width";
94     public static final String EXTRA_DISPLAY_POINT = "extra_display_point";
95 
96     /*
97      * Extras used to pass RemoteCallback objects responsible for IPC between test, app, and
98      * service.
99      */
100     public static final String EXTRA_REMOTE_CALLBACK = "extra_remote_callback";
101     public static final String EXTRA_REMOTE_CALLBACK_ACTION = "extra_remote_callback_action";
102 
103     public static final String EXTRA_REMOTE_CALLBACK_RECEIVING = "extra_remote_callback_receiving";
104     public static final String EXTRA_REMOTE_CALLBACK_RECEIVING_ACTION = "extra_remote_callback_receiving_action";
105 
106     /** Test name suffixes */
107     public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE";
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     public static final String HIDE_SESSION_COMPLETE = "android.intent.action.hide_session_complete";
123 
124     /** Lifecycle activity intent constants */
125     /** Session intent constants */
126     public static final String HIDE_LIFECYCLE_ACTIVITY
127             = "android.intent.action.hide_lifecycle_activity";
128 
129     /** Stub html view to load into WebView */
130     public static final String WEBVIEW_HTML_URL = "http://dev.null/thou/should?not=pass";
131     public static final String WEBVIEW_HTML_DOMAIN = "dev.null";
132     public static final LocaleList WEBVIEW_LOCALE_LIST = new LocaleList(Locale.ROOT, Locale.US);
133     public static final String WEBVIEW_HTML_GREETING = "Hello WebView!";
134     public static final String WEBVIEW_HTML = "<html><body><div><p>" + WEBVIEW_HTML_GREETING
135             + "</p></div></body></html>";
136 
137     /** Extra data to add to assist data and assist content */
138     private static Bundle EXTRA_ASSIST_BUNDLE;
139     private static String STRUCTURED_JSON;
140 
141     private static String MY_UID_EXTRA = "my_uid";
142 
getStructuredJSON()143     public static final String getStructuredJSON() throws Exception {
144         if (STRUCTURED_JSON == null) {
145             STRUCTURED_JSON = new JSONObject()
146                     .put("@type", "MusicRecording")
147                     .put("@id", "https://example/music/recording")
148                     .put("url", "android-app://com.example/https/music/album")
149                     .put("name", "Album Title")
150                     .put("hello", "hi there")
151                     .put("knownNull", null)
152                     .put("unicode value", "\ud800\udc35")
153                     .put("empty string", "")
154                     .put("LongString",
155                         "lkasdjfalsdkfjalsdjfalskj9i9234jl1w23j4o123j412l3j421l3kj412l3kj1l3k4j32")
156                     .put("\ud800\udc35", "any-value")
157                     .put("key with spaces", "any-value")
158                     .toString();
159         }
160         return STRUCTURED_JSON;
161     }
162 
getExtraAssistBundle()163     public static final Bundle getExtraAssistBundle() {
164         if (EXTRA_ASSIST_BUNDLE == null) {
165             EXTRA_ASSIST_BUNDLE = new Bundle();
166             addExtraAssistDataToBundle(EXTRA_ASSIST_BUNDLE, /* addMyUid= */ false);
167         }
168         return EXTRA_ASSIST_BUNDLE;
169     }
170 
addExtraAssistDataToBundle(Bundle data)171     public static void addExtraAssistDataToBundle(Bundle data) {
172         addExtraAssistDataToBundle(data, /* addMyUid= */ true);
173 
174     }
175 
addExtraAssistDataToBundle(Bundle data, boolean addMyUid)176     private static void addExtraAssistDataToBundle(Bundle data, boolean addMyUid) {
177         data.putString("hello", "there");
178         data.putBoolean("isthis_true_or_false", true);
179         data.putInt("number", 123);
180         if (addMyUid) {
181             Log.i(TAG, "adding " + MY_UID_EXTRA + "=" + Process.myUid());
182             data.putInt(MY_UID_EXTRA, Process.myUid());
183         }
184     }
185 
186     /**
187      * The test app associated with each test.
188      */
getTestAppComponent(String testCaseType)189     public static final ComponentName getTestAppComponent(String testCaseType) {
190         switch (testCaseType) {
191             case ASSIST_STRUCTURE:
192             case LARGE_VIEW_HIERARCHY:
193             case DISABLE_CONTEXT:
194                 return new ComponentName(
195                         "android.assist.testapp", "android.assist.testapp.TestApp");
196             case FLAG_SECURE:
197                 return new ComponentName(
198                         "android.assist.testapp", "android.assist.testapp.SecureActivity");
199             case LIFECYCLE:
200             case LIFECYCLE_NOUI:
201                 return new ComponentName(
202                         "android.assist.testapp", "android.assist.testapp.LifecycleActivity");
203             case SCREENSHOT:
204                 return new ComponentName(
205                         "android.assist.testapp", "android.assist.testapp.ScreenshotActivity");
206             case EXTRA_ASSIST:
207                 return new ComponentName(
208                         "android.assist.testapp", "android.assist.testapp.ExtraAssistDataActivity");
209             case TEXTVIEW:
210                 return new ComponentName(
211                         "android.assist.testapp", "android.assist.testapp.TextViewActivity");
212             case WEBVIEW:
213                 return new ComponentName(
214                         "android.assist.testapp", "android.assist.testapp.WebViewActivity");
215             case FOCUS_CHANGE:
216                 return new ComponentName(
217                         "android.assist.testapp", "android.assist.testapp.FocusChangeActivity");
218             default:
219                 return new ComponentName("","");
220         }
221     }
222 
223     /**
224      * Sets the proper action used to launch an activity in the testapp package.
225      */
setTestAppAction(Intent intent, String testCaseName)226     public static void setTestAppAction(Intent intent, String testCaseName) {
227         intent.putExtra(Utils.TESTCASE_TYPE, testCaseName);
228         intent.setAction("android.intent.action.TEST_APP_" + testCaseName);
229     }
230 
231     /**
232      * Returns the amount of time to wait for assist data.
233      */
getAssistDataTimeout(String testCaseType)234     public static final int getAssistDataTimeout(String testCaseType) {
235         switch (testCaseType) {
236             case SCREENSHOT:
237                 // needs to wait for 3p activity to resume before receiving assist data.
238                 return TIMEOUT_MS + ACTIVITY_ONRESUME_TIMEOUT_MS;
239             default:
240                 return TIMEOUT_MS;
241         }
242     }
243 
toBundleString(Bundle bundle)244     public static final String toBundleString(Bundle bundle) {
245         if (bundle == null) {
246             return "*** Bundle is null ****";
247         }
248         StringBuffer buf = new StringBuffer("Bundle is: ");
249         String testType = bundle.getString(TESTCASE_TYPE);
250         if (testType != null) {
251             buf.append("testcase type = " + testType);
252         }
253         ArrayList<String> info = bundle.getStringArrayList(TESTINFO);
254         if (info != null) {
255             for (String s : info) {
256                 buf.append(s + "\n\t\t");
257             }
258         }
259         return buf.toString();
260     }
261 
addErrorResult(final Bundle testinfo, final String msg)262     public static final void addErrorResult(final Bundle testinfo, final String msg) {
263         testinfo.getStringArrayList(testinfo.getString(Utils.TESTCASE_TYPE))
264             .add(TEST_ERROR + " " + msg);
265     }
266 
getExpectedUid(Bundle extras)267     public static int getExpectedUid(Bundle extras) {
268         return extras.getInt(MY_UID_EXTRA);
269     }
270 
bundleOfRemoteAction(String action)271     public static Bundle bundleOfRemoteAction(String action) {
272         Bundle bundle = new Bundle();
273         bundle.putString(Utils.EXTRA_REMOTE_CALLBACK_ACTION, action);
274         return bundle;
275     }
276 
isAutomotive(Context context)277     public static boolean isAutomotive(Context context) {
278         return context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE);
279     }
280 }
281