• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 
17 package android.view.inputmethod.cts.util;
18 
19 import static android.content.Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS;
20 
21 import static com.android.compatibility.common.util.SystemUtil.runShellCommand;
22 import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;
23 
24 import android.content.ComponentName;
25 import android.content.Intent;
26 import android.net.Uri;
27 
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 import androidx.test.platform.app.InstrumentationRegistry;
31 import androidx.test.uiautomator.By;
32 import androidx.test.uiautomator.BySelector;
33 import androidx.test.uiautomator.UiDevice;
34 import androidx.test.uiautomator.Until;
35 
36 import java.util.Map;
37 
38 /**
39  * Provides constants and utility methods to interact with
40  * {@link android.view.inputmethod.ctstestapp.MainActivity}.
41  */
42 public final class MockTestActivityUtil {
43     private static final ComponentName TEST_ACTIVITY = new ComponentName(
44             "android.view.inputmethod.ctstestapp",
45             "android.view.inputmethod.ctstestapp.MainActivity");
46     private static final Uri TEST_ACTIVITY_URI =
47             Uri.parse("https://example.com/android/view/inputmethod/ctstestapp");
48 
49     private static final String ACTION_TRIGGER = "broadcast_action_trigger";
50 
51     /**
52      * A key to be used as the {@code key} of {@link Map} passed as {@code extras} parameter of
53      * {@link #launchSync(boolean, long, Map)}.
54      *
55      * <p>A valid {@code value} is either {@code "true"} or {@code "false"}.</p>
56      */
57     public static final String EXTRA_KEY_SHOW_DIALOG =
58             "android.view.inputmethod.ctstestapp.EXTRA_KEY_SHOW_DIALOG";
59 
60     /**
61      * A key to be used as the {@code key} of {@link Map} passed as {@code extras} parameter of
62      * {@link #launchSync(boolean, long, Map)}.
63      *
64      * <p>The specified {@code value} will be set to
65      * {@link android.view.inputmethod.EditorInfo#privateImeOptions}.</p>
66      */
67     public static final String EXTRA_KEY_PRIVATE_IME_OPTIONS =
68             "android.view.inputmethod.ctstestapp.EXTRA_KEY_PRIVATE_IME_OPTIONS";
69 
70     /**
71      * Can be passed to {@link #sendBroadcastAction(String)} to dismiss the dialog box if exists.
72      */
73     public static final String EXTRA_DISMISS_DIALOG = "extra_dismiss_dialog";
74 
75     /**
76      * Can be passed to {@link #sendBroadcastAction(String)} call
77      * {@link android.view.inputmethod.InputMethodManager#showSoftInput(android.view.View, int)}.
78      */
79     public static final String EXTRA_SHOW_SOFT_INPUT = "extra_show_soft_input";
80 
81     /**
82      * Can be passed to {@link #sendBroadcastAction(String)} to declare editor as
83      * {@link android.view.View#setIsHandwritingDelegate(boolean) handwriting delegator}.
84      */
85     public static final String EXTRA_HANDWRITING_DELEGATE = "extra_handwriting_delegator";
86 
87     @NonNull
formatStringIntentParam(@onNull Uri uri, Map<String, String> extras)88     private static Uri formatStringIntentParam(@NonNull Uri uri, Map<String, String> extras) {
89         if (extras == null) {
90             return uri;
91         }
92         final Uri.Builder builder = uri.buildUpon();
93         extras.forEach(builder::appendQueryParameter);
94         return builder.build();
95     }
96 
97     /**
98      * Launches {@link "android.view.inputmethod.ctstestapp.MainActivity"}.
99      *
100      * @param instant {@code true} when the Activity is installed as an instant app.
101      * @param timeout the timeout to wait until the Activity becomes ready.
102      * @return {@link AutoCloseable} object to automatically stop the test Activity package.
103      */
launchSync(boolean instant, long timeout)104     public static AutoCloseable launchSync(boolean instant, long timeout) {
105         return launchSync(instant, timeout, null);
106     }
107 
108     /**
109      * Launches {@link "android.view.inputmethod.ctstestapp.MainActivity"}.
110      *
111      * @param instant {@code true} when the Activity is installed as an instant app.
112      * @param timeout the timeout to wait until the Activity becomes ready.
113      * @param extras extra parameters to be passed to the Activity.
114      * @return {@link AutoCloseable} object to automatically stop the test Activity package.
115      */
launchSync(boolean instant, long timeout, @Nullable Map<String, String> extras)116     public static AutoCloseable launchSync(boolean instant, long timeout,
117             @Nullable Map<String, String> extras) {
118         final StringBuilder commandBuilder = new StringBuilder();
119         if (instant) {
120             // Override app-links domain verification.
121             runShellCommand(
122                     String.format("pm set-app-links-user-selection --user cur --package %s true %s",
123                             TEST_ACTIVITY.getPackageName(), TEST_ACTIVITY_URI.getHost()));
124             final Uri uri = formatStringIntentParam(TEST_ACTIVITY_URI, extras);
125             commandBuilder.append(String.format("am start -a %s -c %s --activity-clear-task %s",
126                     Intent.ACTION_VIEW, Intent.CATEGORY_BROWSABLE, uri.toString()));
127         } else {
128             commandBuilder.append(String.format("am start -a %s -n %s --activity-clear-task",
129                     Intent.ACTION_MAIN, TEST_ACTIVITY.flattenToShortString()));
130             if (extras != null) {
131                 extras.forEach((key, value) -> commandBuilder.append(" --es ")
132                         .append(key).append(" ").append(value));
133             }
134         }
135 
136         runWithShellPermissionIdentity(() -> {
137             runShellCommand(commandBuilder.toString());
138         });
139         UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
140         BySelector activitySelector = By.pkg(TEST_ACTIVITY.getPackageName()).depth(0);
141         uiDevice.wait(Until.hasObject(activitySelector), timeout);
142 
143         // Make sure to stop package after test finished for resource reclaim.
144         return () -> TestUtils.forceStopPackage(TEST_ACTIVITY.getPackageName());
145     }
146 
147     /**
148      * Sends a broadcast to {@link "android.view.inputmethod.ctstestapp.MainActivity"}.
149      *
150      * @param extra {@link #EXTRA_DISMISS_DIALOG} or {@link #EXTRA_SHOW_SOFT_INPUT}.
151      */
sendBroadcastAction(String extra)152     public static void sendBroadcastAction(String extra) {
153         final StringBuilder commandBuilder = new StringBuilder();
154         commandBuilder.append("am broadcast -a ").append(ACTION_TRIGGER).append(" -p ").append(
155                 TEST_ACTIVITY.getPackageName());
156         commandBuilder.append(" -f 0x").append(
157                 Integer.toHexString(FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS));
158         commandBuilder.append(" --receiver-registered-only");
159         commandBuilder.append(" --ez " + extra + " true");
160         runWithShellPermissionIdentity(() -> {
161             runShellCommand(commandBuilder.toString());
162         });
163     }
164 
165     /**
166      * Force-stops {@link "android.view.inputmethod.ctstestapp"} package.
167      */
forceStopPackage()168     public static void forceStopPackage() {
169         TestUtils.forceStopPackage(TEST_ACTIVITY.getPackageName());
170     }
171 
172     /**
173      * @return {@code "android.view.inputmethod.ctstestapp"}.
174      */
getPackageName()175     public static String getPackageName() {
176         return TEST_ACTIVITY.getPackageName();
177     }
178 }
179