• 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.Intent;
20 import android.os.Bundle;
21 import android.os.LocaleList;
22 import android.os.Process;
23 import android.util.Log;
24 
25 import org.json.JSONObject;
26 
27 import java.util.ArrayList;
28 import java.util.Locale;
29 
30 public class Utils {
31     private static final String TAG = Utils.class.getSimpleName();
32     public static final String TESTCASE_TYPE = "testcase_type";
33     public static final String TESTINFO = "testinfo";
34     public static final String ACTION_PREFIX = "android.intent.action.";
35     public static final String BROADCAST_INTENT = ACTION_PREFIX + "ASSIST_TESTAPP";
36     public static final String BROADCAST_ASSIST_DATA_INTENT = ACTION_PREFIX + "ASSIST_DATA";
37     public static final String BROADCAST_INTENT_START_ASSIST = ACTION_PREFIX + "START_ASSIST";
38     public static final String ASSIST_RECEIVER_REGISTERED = ACTION_PREFIX + "ASSIST_READY";
39     public static final String ACTION_END_OF_TEST = ACTION_PREFIX + "END_OF_TEST";
40 
41     public static final String ACTION_INVALIDATE = "invalidate_action";
42     public static final String GET_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "GET_CONTENT_VIEW_HEIGHT";
43     public static final String BROADCAST_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "VIEW_HEIGHT";
44     public static final String SCROLL_TEXTVIEW_ACTION = ACTION_PREFIX + "TEXTVIEW_SCROLL";
45     public static final String SCROLL_SCROLLVIEW_ACTION = ACTION_PREFIX + "SCROLLVIEW_SCROLL";
46     public static final String TEST_ERROR = "Error In Test:";
47 
48     public static final String ASSIST_STRUCTURE_KEY = "assist_structure";
49     public static final String ASSIST_CONTENT_KEY = "assist_content";
50     public static final String ASSIST_BUNDLE_KEY = "assist_bundle";
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 DISPLAY_WIDTH_KEY = "display_width";
55     public static final String DISPLAY_HEIGHT_KEY = "dislay_height";
56     public static final String SCROLL_X_POSITION = "scroll_x_position";
57     public static final String SCROLL_Y_POSITION = "scroll_y_position";
58 
59     /** Lifecycle Test intent constants */
60     public static final String LIFECYCLE_PREFIX = ACTION_PREFIX + "lifecycle_";
61     public static final String LIFECYCLE_HASRESUMED = LIFECYCLE_PREFIX + "hasResumed";
62     public static final String LIFECYCLE_HASFOCUS = LIFECYCLE_PREFIX + "hasFocus";
63     public static final String LIFECYCLE_LOSTFOCUS = LIFECYCLE_PREFIX + "lostFocus";
64     public static final String LIFECYCLE_ONPAUSE = LIFECYCLE_PREFIX + "onpause";
65     public static final String LIFECYCLE_ONSTOP = LIFECYCLE_PREFIX + "onstop";
66     public static final String LIFECYCLE_ONDESTROY = LIFECYCLE_PREFIX + "ondestroy";
67 
68     /** Focus Change Test intent constants */
69     public static final String GAINED_FOCUS = ACTION_PREFIX + "focus_changed";
70     public static final String LOST_FOCUS = ACTION_PREFIX + "lost_focus";
71 
72     public static final String APP_3P_HASRESUMED = ACTION_PREFIX + "app_3p_hasResumed";
73     public static final String APP_3P_HASDRAWED = ACTION_PREFIX + "app_3p_hasDrawed";
74     public static final String TEST_ACTIVITY_DESTROY = ACTION_PREFIX + "test_activity_destroy";
75     public static final String TEST_ACTIVITY_WEBVIEW_LOADED = ACTION_PREFIX + "test_activity_webview_hasResumed";
76 
77     // Notice: timeout belows have to be long because some devices / form factors (like car) are
78     // slower.
79 
80     /** Timeout for getting back assist context */
81     public static final int TIMEOUT_MS = 4 * 1_000;
82     /** Timeout for an activity to resume */
83     public static final int ACTIVITY_ONRESUME_TIMEOUT_MS = 8 * 1_000;
84 
85     public static final String EXTRA_REGISTER_RECEIVER = "register_receiver";
86 
87     /** Extras for passing the Assistant's ContentView's dimensions*/
88     public static final String EXTRA_CONTENT_VIEW_HEIGHT = "extra_content_view_height";
89     public static final String EXTRA_CONTENT_VIEW_WIDTH = "extra_content_view_width";
90     public static final String EXTRA_DISPLAY_POINT = "extra_display_point";
91 
92     /*
93      * Extras used to pass RemoteCallback objects responsible for IPC between test, app, and
94      * service.
95      */
96     public static final String EXTRA_REMOTE_CALLBACK = "extra_remote_callback";
97     public static final String EXTRA_REMOTE_CALLBACK_ACTION = "extra_remote_callback_action";
98 
99     public static final String EXTRA_REMOTE_CALLBACK_RECEIVING = "extra_remote_callback_receiving";
100     public static final String EXTRA_REMOTE_CALLBACK_RECEIVING_ACTION = "extra_remote_callback_receiving_action";
101 
102     /** Test name suffixes */
103     public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE";
104     public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT";
105     public static final String FLAG_SECURE = "FLAG_SECURE";
106     public static final String LIFECYCLE = "LIFECYCLE";
107     public static final String LIFECYCLE_NOUI = "LIFECYCLE_NOUI";
108     public static final String SCREENSHOT = "SCREENSHOT";
109     public static final String EXTRA_ASSIST = "EXTRA_ASSIST";
110     public static final String VERIFY_CONTENT_VIEW = "VERIFY_CONTENT_VIEW";
111     public static final String TEXTVIEW = "TEXTVIEW";
112     public static final String LARGE_VIEW_HIERARCHY = "LARGE_VIEW_HIERARCHY";
113     public static final String WEBVIEW = "WEBVIEW";
114     public static final String FOCUS_CHANGE = "FOCUS_CHANGE";
115 
116     /** Session intent constants */
117     public static final String HIDE_SESSION = "android.intent.action.hide_session";
118     public static final String HIDE_SESSION_COMPLETE = "android.intent.action.hide_session_complete";
119 
120     /** Lifecycle activity intent constants */
121     /** Session intent constants */
122     public static final String HIDE_LIFECYCLE_ACTIVITY
123             = "android.intent.action.hide_lifecycle_activity";
124 
125     /** Stub html view to load into WebView */
126     public static final String WEBVIEW_HTML_URL = "http://dev.null/thou/should?not=pass";
127     public static final String WEBVIEW_HTML_DOMAIN = "dev.null";
128     public static final LocaleList WEBVIEW_LOCALE_LIST = new LocaleList(Locale.ROOT, Locale.US);
129     public static final String WEBVIEW_HTML_GREETING = "Hello WebView!";
130     public static final String WEBVIEW_HTML = "<html><body><div><p>" + WEBVIEW_HTML_GREETING
131             + "</p></div></body></html>";
132 
133     /** Extra data to add to assist data and assist content */
134     private static Bundle EXTRA_ASSIST_BUNDLE;
135     private static String STRUCTURED_JSON;
136 
137     private static String MY_UID_EXTRA = "my_uid";
138 
getStructuredJSON()139     public static final String getStructuredJSON() throws Exception {
140         if (STRUCTURED_JSON == null) {
141             STRUCTURED_JSON = new JSONObject()
142                     .put("@type", "MusicRecording")
143                     .put("@id", "https://example/music/recording")
144                     .put("url", "android-app://com.example/https/music/album")
145                     .put("name", "Album Title")
146                     .put("hello", "hi there")
147                     .put("knownNull", null)
148                     .put("unicode value", "\ud800\udc35")
149                     .put("empty string", "")
150                     .put("LongString",
151                         "lkasdjfalsdkfjalsdjfalskj9i9234jl1w23j4o123j412l3j421l3kj412l3kj1l3k4j32")
152                     .put("\ud800\udc35", "any-value")
153                     .put("key with spaces", "any-value")
154                     .toString();
155         }
156         return STRUCTURED_JSON;
157     }
158 
getExtraAssistBundle()159     public static final Bundle getExtraAssistBundle() {
160         if (EXTRA_ASSIST_BUNDLE == null) {
161             EXTRA_ASSIST_BUNDLE = new Bundle();
162             addExtraAssistDataToBundle(EXTRA_ASSIST_BUNDLE, /* addMyUid= */ false);
163         }
164         return EXTRA_ASSIST_BUNDLE;
165     }
166 
addExtraAssistDataToBundle(Bundle data)167     public static void addExtraAssistDataToBundle(Bundle data) {
168         addExtraAssistDataToBundle(data, /* addMyUid= */ true);
169 
170     }
171 
addExtraAssistDataToBundle(Bundle data, boolean addMyUid)172     private static void addExtraAssistDataToBundle(Bundle data, boolean addMyUid) {
173         data.putString("hello", "there");
174         data.putBoolean("isthis_true_or_false", true);
175         data.putInt("number", 123);
176         if (addMyUid) {
177             Log.i(TAG, "adding " + MY_UID_EXTRA + "=" + Process.myUid());
178             data.putInt(MY_UID_EXTRA, Process.myUid());
179         }
180     }
181 
182     /**
183      * The test app associated with each test.
184      */
getTestAppComponent(String testCaseType)185     public static final ComponentName getTestAppComponent(String testCaseType) {
186         switch (testCaseType) {
187             case ASSIST_STRUCTURE:
188             case LARGE_VIEW_HIERARCHY:
189             case DISABLE_CONTEXT:
190                 return new ComponentName(
191                         "android.assist.testapp", "android.assist.testapp.TestApp");
192             case FLAG_SECURE:
193                 return new ComponentName(
194                         "android.assist.testapp", "android.assist.testapp.SecureActivity");
195             case LIFECYCLE:
196             case LIFECYCLE_NOUI:
197                 return new ComponentName(
198                         "android.assist.testapp", "android.assist.testapp.LifecycleActivity");
199             case SCREENSHOT:
200                 return new ComponentName(
201                         "android.assist.testapp", "android.assist.testapp.ScreenshotActivity");
202             case EXTRA_ASSIST:
203                 return new ComponentName(
204                         "android.assist.testapp", "android.assist.testapp.ExtraAssistDataActivity");
205             case TEXTVIEW:
206                 return new ComponentName(
207                         "android.assist.testapp", "android.assist.testapp.TextViewActivity");
208             case WEBVIEW:
209                 return new ComponentName(
210                         "android.assist.testapp", "android.assist.testapp.WebViewActivity");
211             case FOCUS_CHANGE:
212                 return new ComponentName(
213                         "android.assist.testapp", "android.assist.testapp.FocusChangeActivity");
214             default:
215                 return new ComponentName("","");
216         }
217     }
218 
219     /**
220      * Sets the proper action used to launch an activity in the testapp package.
221      */
setTestAppAction(Intent intent, String testCaseName)222     public static void setTestAppAction(Intent intent, String testCaseName) {
223         intent.putExtra(Utils.TESTCASE_TYPE, testCaseName);
224         intent.setAction("android.intent.action.TEST_APP_" + testCaseName);
225     }
226 
227     /**
228      * Returns the amount of time to wait for assist data.
229      */
getAssistDataTimeout(String testCaseType)230     public static final int getAssistDataTimeout(String testCaseType) {
231         switch (testCaseType) {
232             case SCREENSHOT:
233                 // needs to wait for 3p activity to resume before receiving assist data.
234                 return TIMEOUT_MS + ACTIVITY_ONRESUME_TIMEOUT_MS;
235             default:
236                 return TIMEOUT_MS;
237         }
238     }
239 
toBundleString(Bundle bundle)240     public static final String toBundleString(Bundle bundle) {
241         if (bundle == null) {
242             return "*** Bundle is null ****";
243         }
244         StringBuffer buf = new StringBuffer("Bundle is: ");
245         String testType = bundle.getString(TESTCASE_TYPE);
246         if (testType != null) {
247             buf.append("testcase type = " + testType);
248         }
249         ArrayList<String> info = bundle.getStringArrayList(TESTINFO);
250         if (info != null) {
251             for (String s : info) {
252                 buf.append(s + "\n\t\t");
253             }
254         }
255         return buf.toString();
256     }
257 
addErrorResult(final Bundle testinfo, final String msg)258     public static final void addErrorResult(final Bundle testinfo, final String msg) {
259         testinfo.getStringArrayList(testinfo.getString(Utils.TESTCASE_TYPE))
260             .add(TEST_ERROR + " " + msg);
261     }
262 
getExpectedUid(Bundle extras)263     public static int getExpectedUid(Bundle extras) {
264         return extras.getInt(MY_UID_EXTRA);
265     }
266 
bundleOfRemoteAction(String action)267     public static Bundle bundleOfRemoteAction(String action) {
268         Bundle bundle = new Bundle();
269         bundle.putString(Utils.EXTRA_REMOTE_CALLBACK_ACTION, action);
270         return bundle;
271     }
272 }
273