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.R;
19 import android.content.ComponentName;
20 import android.os.Bundle;
21 
22 import org.json.JSONException;
23 import org.json.JSONObject;
24 import java.util.ArrayList;
25 
26 public class Utils {
27     public static final String TESTCASE_TYPE = "testcase_type";
28     public static final String TESTINFO = "testinfo";
29     public static final String ACTION_PREFIX = "android.intent.action.";
30     public static final String BROADCAST_INTENT = ACTION_PREFIX + "ASSIST_TESTAPP";
31     public static final String BROADCAST_ASSIST_DATA_INTENT = ACTION_PREFIX + "ASSIST_DATA";
32     public static final String BROADCAST_INTENT_START_ASSIST = ACTION_PREFIX + "START_ASSIST";
33     public static final String ASSIST_RECEIVER_REGISTERED = ACTION_PREFIX + "ASSIST_READY";
34 
35     public static final String ACTION_INVALIDATE = "invalidate_action";
36     public static final String GET_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "GET_CONTENT_VIEW_HEIGHT";
37     public static final String BROADCAST_CONTENT_VIEW_HEIGHT = ACTION_PREFIX + "VIEW_HEIGHT";
38     public static final String SCROLL_TEXTVIEW_ACTION = ACTION_PREFIX + "TEXTVIEW_SCROLL";
39     public static final String SCROLL_SCROLLVIEW_ACTION = ACTION_PREFIX + "SCROLLVIEW_SCROLL";
40     public static final String TEST_ERROR = "Error In Test:";
41 
42     public static final String ASSIST_STRUCTURE_KEY = "assist_structure";
43     public static final String ASSIST_CONTENT_KEY = "assist_content";
44     public static final String ASSIST_BUNDLE_KEY = "assist_bundle";
45     public static final String ASSIST_SCREENSHOT_KEY = "assist_screenshot";
46     public static final String SCREENSHOT_COLOR_KEY = "set_screenshot_color";
47     public static final String COMPARE_SCREENSHOT_KEY = "compare_screenshot";
48     public static final String DISPLAY_WIDTH_KEY = "display_width";
49     public static final String DISPLAY_HEIGHT_KEY = "dislay_height";
50     public static final String SCROLL_X_POSITION = "scroll_x_position";
51     public static final String SCROLL_Y_POSITION = "scroll_y_position";
52 
53     /** Lifecycle Test intent constants */
54     public static final String LIFECYCLE_PREFIX = ACTION_PREFIX + "lifecycle_";
55     public static final String LIFECYCLE_HASRESUMED = LIFECYCLE_PREFIX + "hasResumed";
56     public static final String LIFECYCLE_ONPAUSE = LIFECYCLE_PREFIX + "onpause";
57     public static final String LIFECYCLE_ONSTOP = LIFECYCLE_PREFIX + "onstop";
58     public static final String LIFECYCLE_ONDESTROY = LIFECYCLE_PREFIX + "ondestroy";
59 
60     /** Focus Change Test intent constants */
61     public static final String GAINED_FOCUS = ACTION_PREFIX + "focus_changed";
62     public static final String LOST_FOCUS = ACTION_PREFIX + "lost_focus";
63 
64     /** Flag Secure Test intent constants */
65     public static final String FLAG_SECURE_HASRESUMED = ACTION_PREFIX + "flag_secure_hasResumed";
66     public static final String APP_3P_HASRESUMED = ACTION_PREFIX + "app_3p_hasResumed";
67     public static final String TEST_ACTIVITY_LOADED = ACTION_PREFIX + "test_activity_hasResumed";
68 
69     /** Two second timeout for getting back assist context */
70     public static final int TIMEOUT_MS = 2 * 1000;
71     /** Four second timeout for an activity to resume */
72     public static final int ACTIVITY_ONRESUME_TIMEOUT_MS = 4000;
73 
74     public static final String EXTRA_REGISTER_RECEIVER = "register_receiver";
75 
76     /** Extras for passing the Assistant's ContentView's dimensions*/
77     public static final String EXTRA_CONTENT_VIEW_HEIGHT = "extra_content_view_height";
78     public static final String EXTRA_CONTENT_VIEW_WIDTH = "extra_content_view_width";
79     public static final String EXTRA_DISPLAY_POINT = "extra_display_point";
80 
81     /** Test name suffixes */
82     public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE";
83     public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT";
84     public static final String FLAG_SECURE = "FLAG_SECURE";
85     public static final String LIFECYCLE = "LIFECYCLE";
86     public static final String SCREENSHOT = "SCREENSHOT";
87     public static final String EXTRA_ASSIST = "EXTRA_ASSIST";
88     public static final String VERIFY_CONTENT_VIEW = "VERIFY_CONTENT_VIEW";
89     public static final String TEXTVIEW = "TEXTVIEW";
90     public static final String LARGE_VIEW_HIERARCHY = "LARGE_VIEW_HIERARCHY";
91     public static final String WEBVIEW = "WEBVIEW";
92     public static final String FOCUS_CHANGE = "FOCUS_CHANGE";
93 
94     /** Session intent constants */
95     public static final String HIDE_SESSION = "android.intent.action.hide_session";
96 
97     /** Stub html view to load into WebView */
98     public static final String WEBVIEW_HTML_GREETING = "Hello WebView!";
99     public static final String WEBVIEW_HTML = "<html><body><div><p>" + WEBVIEW_HTML_GREETING
100             + "</p></div></body></html>";
101 
102     /** Extra data to add to assist data and assist content */
103     private static Bundle EXTRA_ASSIST_BUNDLE;
104     private static String STRUCTURED_JSON;
105 
getStructuredJSON()106     public static final String getStructuredJSON() throws Exception {
107         if (STRUCTURED_JSON == null) {
108             STRUCTURED_JSON = new JSONObject()
109                     .put("@type", "MusicRecording")
110                     .put("@id", "https://example/music/recording")
111                     .put("url", "android-app://com.example/https/music/album")
112                     .put("name", "Album Title")
113                     .put("hello", "hi there")
114                     .put("knownNull", null)
115                     .put("unicode value", "\ud800\udc35")
116                     .put("empty string", "")
117                     .put("LongString",
118                         "lkasdjfalsdkfjalsdjfalskj9i9234jl1w23j4o123j412l3j421l3kj412l3kj1l3k4j32")
119                     .put("\ud800\udc35", "any-value")
120                     .put("key with spaces", "any-value")
121                     .toString();
122         }
123         return STRUCTURED_JSON;
124     }
125 
getExtraAssistBundle()126     public static final Bundle getExtraAssistBundle() {
127         if (EXTRA_ASSIST_BUNDLE == null) {
128             EXTRA_ASSIST_BUNDLE = new Bundle();
129             addExtraAssistDataToBundle(EXTRA_ASSIST_BUNDLE);
130         }
131         return EXTRA_ASSIST_BUNDLE;
132     }
133 
addExtraAssistDataToBundle(Bundle data)134     public static void addExtraAssistDataToBundle(Bundle data) {
135         data.putString("hello", "there");
136         data.putBoolean("isthis_true_or_false", true);
137         data.putInt("number", 123);
138     }
139 
140     /** The shim activity that starts the service associated with each test. */
getTestActivity(String testCaseType)141     public static final String getTestActivity(String testCaseType) {
142         switch (testCaseType) {
143             case DISABLE_CONTEXT:
144                 // doesn't need to wait for activity to resume
145                 // can be activated on top of any non-secure activity.
146                 return "service.DisableContextActivity";
147             case ASSIST_STRUCTURE:
148             case FLAG_SECURE:
149             case LIFECYCLE:
150             case SCREENSHOT:
151             case EXTRA_ASSIST:
152             case VERIFY_CONTENT_VIEW:
153             case TEXTVIEW:
154             case LARGE_VIEW_HIERARCHY:
155             case WEBVIEW:
156             case FOCUS_CHANGE:
157                 return "service.DelayedAssistantActivity";
158             default:
159                 return "";
160         }
161     }
162 
163     /**
164      * The test app associated with each test.
165      */
getTestAppComponent(String testCaseType)166     public static final ComponentName getTestAppComponent(String testCaseType) {
167         switch (testCaseType) {
168             case ASSIST_STRUCTURE:
169             case LARGE_VIEW_HIERARCHY:
170                 return new ComponentName(
171                         "android.assist.testapp", "android.assist.testapp.TestApp");
172             case DISABLE_CONTEXT:
173                 return new ComponentName(
174                         "android.assist.testapp", "android.assist.testapp.TestApp");
175             case FLAG_SECURE:
176                 return new ComponentName(
177                         "android.assist.testapp", "android.assist.testapp.SecureActivity");
178             case LIFECYCLE:
179                 return new ComponentName(
180                         "android.assist.testapp", "android.assist.testapp.LifecycleActivity");
181             case SCREENSHOT:
182                 return new ComponentName(
183                         "android.assist.testapp", "android.assist.testapp.ScreenshotActivity");
184             case EXTRA_ASSIST:
185                 return new ComponentName(
186                         "android.assist.testapp", "android.assist.testapp.ExtraAssistDataActivity");
187             case TEXTVIEW:
188                 return new ComponentName(
189                         "android.assist.testapp", "android.assist.testapp.TextViewActivity");
190             case WEBVIEW:
191                 return new ComponentName(
192                         "android.assist.testapp", "android.assist.testapp.WebViewActivity");
193             case FOCUS_CHANGE:
194                 return new ComponentName(
195                         "android.assist.testapp", "android.assist.testapp.FocusChangeActivity");
196             default:
197                 return new ComponentName("","");
198         }
199     }
200 
201     /**
202      * Returns the amount of time to wait for assist data.
203      */
getAssistDataTimeout(String testCaseType)204     public static final int getAssistDataTimeout(String testCaseType) {
205         switch (testCaseType) {
206             case SCREENSHOT:
207                 // needs to wait for 3p activity to resume before receiving assist data.
208                 return TIMEOUT_MS + ACTIVITY_ONRESUME_TIMEOUT_MS;
209             default:
210                 return TIMEOUT_MS;
211         }
212     }
213 
toBundleString(Bundle bundle)214     public static final String toBundleString(Bundle bundle) {
215         if (bundle == null) {
216             return "*** Bundle is null ****";
217         }
218         StringBuffer buf = new StringBuffer("Bundle is: ");
219         String testType = bundle.getString(TESTCASE_TYPE);
220         if (testType != null) {
221             buf.append("testcase type = " + testType);
222         }
223         ArrayList<String> info = bundle.getStringArrayList(TESTINFO);
224         if (info != null) {
225             for (String s : info) {
226                 buf.append(s + "\n\t\t");
227             }
228         }
229         return buf.toString();
230     }
231 
addErrorResult(final Bundle testinfo, final String msg)232     public static final void addErrorResult(final Bundle testinfo, final String msg) {
233         testinfo.getStringArrayList(testinfo.getString(Utils.TESTCASE_TYPE))
234             .add(TEST_ERROR + " " + msg);
235     }
236 }
237