• 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.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 APP_3P_HASDRAWED = ACTION_PREFIX + "app_3p_hasDrawed";
68     public static final String TEST_ACTIVITY_LOADED = ACTION_PREFIX + "test_activity_hasResumed";
69 
70     /** Two second timeout for getting back assist context */
71     public static final int TIMEOUT_MS = 2 * 1000;
72     /** Four second timeout for an activity to resume */
73     public static final int ACTIVITY_ONRESUME_TIMEOUT_MS = 4000;
74 
75     public static final String EXTRA_REGISTER_RECEIVER = "register_receiver";
76 
77     /** Extras for passing the Assistant's ContentView's dimensions*/
78     public static final String EXTRA_CONTENT_VIEW_HEIGHT = "extra_content_view_height";
79     public static final String EXTRA_CONTENT_VIEW_WIDTH = "extra_content_view_width";
80     public static final String EXTRA_DISPLAY_POINT = "extra_display_point";
81 
82     /** Test name suffixes */
83     public static final String ASSIST_STRUCTURE = "ASSIST_STRUCTURE";
84     public static final String DISABLE_CONTEXT = "DISABLE_CONTEXT";
85     public static final String FLAG_SECURE = "FLAG_SECURE";
86     public static final String LIFECYCLE = "LIFECYCLE";
87     public static final String SCREENSHOT = "SCREENSHOT";
88     public static final String EXTRA_ASSIST = "EXTRA_ASSIST";
89     public static final String VERIFY_CONTENT_VIEW = "VERIFY_CONTENT_VIEW";
90     public static final String TEXTVIEW = "TEXTVIEW";
91     public static final String LARGE_VIEW_HIERARCHY = "LARGE_VIEW_HIERARCHY";
92     public static final String WEBVIEW = "WEBVIEW";
93     public static final String FOCUS_CHANGE = "FOCUS_CHANGE";
94 
95     /** Session intent constants */
96     public static final String HIDE_SESSION = "android.intent.action.hide_session";
97 
98     /** Stub html view to load into WebView */
99     public static final String WEBVIEW_HTML_GREETING = "Hello WebView!";
100     public static final String WEBVIEW_HTML = "<html><body><div><p>" + WEBVIEW_HTML_GREETING
101             + "</p></div></body></html>";
102 
103     /** Extra data to add to assist data and assist content */
104     private static Bundle EXTRA_ASSIST_BUNDLE;
105     private static String STRUCTURED_JSON;
106 
getStructuredJSON()107     public static final String getStructuredJSON() throws Exception {
108         if (STRUCTURED_JSON == null) {
109             STRUCTURED_JSON = new JSONObject()
110                     .put("@type", "MusicRecording")
111                     .put("@id", "https://example/music/recording")
112                     .put("url", "android-app://com.example/https/music/album")
113                     .put("name", "Album Title")
114                     .put("hello", "hi there")
115                     .put("knownNull", null)
116                     .put("unicode value", "\ud800\udc35")
117                     .put("empty string", "")
118                     .put("LongString",
119                         "lkasdjfalsdkfjalsdjfalskj9i9234jl1w23j4o123j412l3j421l3kj412l3kj1l3k4j32")
120                     .put("\ud800\udc35", "any-value")
121                     .put("key with spaces", "any-value")
122                     .toString();
123         }
124         return STRUCTURED_JSON;
125     }
126 
getExtraAssistBundle()127     public static final Bundle getExtraAssistBundle() {
128         if (EXTRA_ASSIST_BUNDLE == null) {
129             EXTRA_ASSIST_BUNDLE = new Bundle();
130             addExtraAssistDataToBundle(EXTRA_ASSIST_BUNDLE);
131         }
132         return EXTRA_ASSIST_BUNDLE;
133     }
134 
addExtraAssistDataToBundle(Bundle data)135     public static void addExtraAssistDataToBundle(Bundle data) {
136         data.putString("hello", "there");
137         data.putBoolean("isthis_true_or_false", true);
138         data.putInt("number", 123);
139     }
140 
141     /** The shim activity that starts the service associated with each test. */
getTestActivity(String testCaseType)142     public static final String getTestActivity(String testCaseType) {
143         switch (testCaseType) {
144             case DISABLE_CONTEXT:
145                 // doesn't need to wait for activity to resume
146                 // can be activated on top of any non-secure activity.
147                 return "service.DisableContextActivity";
148             case ASSIST_STRUCTURE:
149             case FLAG_SECURE:
150             case LIFECYCLE:
151             case SCREENSHOT:
152             case EXTRA_ASSIST:
153             case VERIFY_CONTENT_VIEW:
154             case TEXTVIEW:
155             case LARGE_VIEW_HIERARCHY:
156             case WEBVIEW:
157             case FOCUS_CHANGE:
158                 return "service.DelayedAssistantActivity";
159             default:
160                 return "";
161         }
162     }
163 
164     /**
165      * The test app associated with each test.
166      */
getTestAppComponent(String testCaseType)167     public static final ComponentName getTestAppComponent(String testCaseType) {
168         switch (testCaseType) {
169             case ASSIST_STRUCTURE:
170             case LARGE_VIEW_HIERARCHY:
171                 return new ComponentName(
172                         "android.assist.testapp", "android.assist.testapp.TestApp");
173             case DISABLE_CONTEXT:
174                 return new ComponentName(
175                         "android.assist.testapp", "android.assist.testapp.TestApp");
176             case FLAG_SECURE:
177                 return new ComponentName(
178                         "android.assist.testapp", "android.assist.testapp.SecureActivity");
179             case LIFECYCLE:
180                 return new ComponentName(
181                         "android.assist.testapp", "android.assist.testapp.LifecycleActivity");
182             case SCREENSHOT:
183                 return new ComponentName(
184                         "android.assist.testapp", "android.assist.testapp.ScreenshotActivity");
185             case EXTRA_ASSIST:
186                 return new ComponentName(
187                         "android.assist.testapp", "android.assist.testapp.ExtraAssistDataActivity");
188             case TEXTVIEW:
189                 return new ComponentName(
190                         "android.assist.testapp", "android.assist.testapp.TextViewActivity");
191             case WEBVIEW:
192                 return new ComponentName(
193                         "android.assist.testapp", "android.assist.testapp.WebViewActivity");
194             case FOCUS_CHANGE:
195                 return new ComponentName(
196                         "android.assist.testapp", "android.assist.testapp.FocusChangeActivity");
197             default:
198                 return new ComponentName("","");
199         }
200     }
201 
202     /**
203      * Returns the amount of time to wait for assist data.
204      */
getAssistDataTimeout(String testCaseType)205     public static final int getAssistDataTimeout(String testCaseType) {
206         switch (testCaseType) {
207             case SCREENSHOT:
208                 // needs to wait for 3p activity to resume before receiving assist data.
209                 return TIMEOUT_MS + ACTIVITY_ONRESUME_TIMEOUT_MS;
210             default:
211                 return TIMEOUT_MS;
212         }
213     }
214 
toBundleString(Bundle bundle)215     public static final String toBundleString(Bundle bundle) {
216         if (bundle == null) {
217             return "*** Bundle is null ****";
218         }
219         StringBuffer buf = new StringBuffer("Bundle is: ");
220         String testType = bundle.getString(TESTCASE_TYPE);
221         if (testType != null) {
222             buf.append("testcase type = " + testType);
223         }
224         ArrayList<String> info = bundle.getStringArrayList(TESTINFO);
225         if (info != null) {
226             for (String s : info) {
227                 buf.append(s + "\n\t\t");
228             }
229         }
230         return buf.toString();
231     }
232 
addErrorResult(final Bundle testinfo, final String msg)233     public static final void addErrorResult(final Bundle testinfo, final String msg) {
234         testinfo.getStringArrayList(testinfo.getString(Utils.TESTCASE_TYPE))
235             .add(TEST_ERROR + " " + msg);
236     }
237 }
238