Debian Codegraph @

tests/InputMethodStressTest/src/com/android/inputmethod/stresstest/NotificationTest.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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 com.android.inputmethod.stresstest;
18
19 import static android.app.NotificationManager.IMPORTANCE_HIGH;
20
21 import static com.google.common.truth.Truth.assertThat;
22
23 import static org.junit.Assume.assumeFalse;
24
25 import android.app.Notification;
26 import android.app.NotificationChannel;
27 import android.app.NotificationManager;
28 import android.app.PendingIntent;
29 import android.app.RemoteInput;
30 import android.content.ComponentName;
31 import android.content.Context;
32 import android.content.Intent;
33 import android.content.pm.PackageManager;
34 import android.graphics.drawable.Icon;
35 import android.platform.test.annotations.RootPermissionTest;
36 import android.platform.test.rule.UnlockScreenRule;
37 import android.provider.Settings;
38 import android.view.KeyEvent;
39
40 import androidx.test.ext.junit.runners.AndroidJUnit4;
41 import androidx.test.platform.app.InstrumentationRegistry;
42 import androidx.test.uiautomator.By;
43 import androidx.test.uiautomator.BySelector;
44 import androidx.test.uiautomator.UiDevice;
45 import androidx.test.uiautomator.Until;
46
47 import org.junit.After;
48 import org.junit.Before;
49 import org.junit.Rule;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52
53 import java.util.concurrent.TimeUnit;
54 import java.util.regex.Pattern;
55
56 @RootPermissionTest
57 @RunWith(AndroidJUnit4.class)
58 public final class NotificationTest {
59
60 private static final long TIMEOUT = TimeUnit.SECONDS.toMillis(10);
61
62 private static final String CHANNEL_ID = "TEST_CHANNEL";
63 private static final String CHANNEL_NAME = "Test channel";
64
65 private static final String REPLY_INPUT_KEY = "REPLY_KEY";
66 private static final String REPLY_INPUT_LABEL = "Test reply label";
67 private static final String ACTION_REPLY = "com.android.inputmethod.stresstest.ACTION_REPLY";
68 private static final String REPLY_ACTION_LABEL = "Test reply";
69 private static final int REPLY_REQUEST_CODE = 1;
70
71 private static final String NOTIFICATION_TITLE = "Test notification";
72 private static final String NOTIFICATION_CONTENT = "Test notification content";
73 private static final int NOTIFICATION_ID = 2000;
74
75 // This is for AOSP System UI for phones. When testing customized System UI, please modify here.
76 private static final BySelector REPLY_SEND_BUTTON_SELECTOR =
77 By.res("com.android.systemui", "remote_input_send");
78
79 @Rule
80 public UnlockScreenRule mUnlockScreenRule = new UnlockScreenRule();
81
82 @Rule
83 public ScreenCaptureRule mScreenCaptureRule =
84 new ScreenCaptureRule("/sdcard/InputMethodStressTest");
85
86 private Context mContext;
87 private NotificationManager mNotificationManager;
88 private UiDevice mUiDevice;
89
90 @Before
91 public void setUp() {
92 mContext = InstrumentationRegistry.getInstrumentation().getContext();
93 mUiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
94 mNotificationManager = mContext.getSystemService(NotificationManager.class);
95 PackageManager pm = mContext.getPackageManager();
96 // Do not run on Automotive.
97 assumeFalse(pm.hasSystemFeature(PackageManager.FEATURE_AUTOMOTIVE));
98 // Do not run on TV. Direct Reply isn't supported on TV.
99 assumeFalse(pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK_ONLY));
100 }
101
102 @After
103 public void tearDown() {
104 mNotificationManager.cancelAll();
105 mUiDevice.pressHome();
106 }
107
108 @Test
109 public void testDirectReply() {
110 postMessagingNotification();
111 mUiDevice.openNotification();
112 // The text can be shown as-is, or all-caps, depending on the system.
113 Pattern actionLabelPattern = Pattern.compile(REPLY_ACTION_LABEL, Pattern.CASE_INSENSITIVE);
114 mUiDevice.wait(Until.findObject(By.text(actionLabelPattern)), TIMEOUT).click();
115 // Verify that IME is visible.
116 assertThat(mUiDevice.wait(Until.findObject(By.pkg(getImePackage(mContext))), TIMEOUT))
117 .isNotNull();
118 // Type something, which enables the Send button, then click the Send button.
119 mUiDevice.pressKeyCode(KeyEvent.KEYCODE_A);
120 mUiDevice.pressKeyCode(KeyEvent.KEYCODE_B);
121 mUiDevice.pressKeyCode(KeyEvent.KEYCODE_C);
122 mUiDevice.wait(Until.findObject(REPLY_SEND_BUTTON_SELECTOR.enabled(true)), TIMEOUT).click();
123 // Verify that IME is gone.
124 assertThat(mUiDevice.wait(Until.gone(By.pkg(getImePackage(mContext))), TIMEOUT)).isTrue();
125 }
126
127 private void postMessagingNotification() {
128 // Register the channel. It's safe to register the same channel again and again.
129 NotificationChannel channel =
130 new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, IMPORTANCE_HIGH);
131 mNotificationManager.createNotificationChannel(channel);
132
133 // Post inline reply notification.
134 PendingIntent pendingIntent = PendingIntent.getBroadcast(
135 mContext, REPLY_REQUEST_CODE, new Intent().setAction(ACTION_REPLY),
136 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
137 RemoteInput remoteInput = new RemoteInput.Builder(REPLY_INPUT_KEY)
138 .setLabel(REPLY_INPUT_LABEL)
139 .build();
140 Icon icon = Icon.createWithResource(mContext, android.R.drawable.ic_menu_edit);
141 Notification.Action action =
142 new Notification.Action.Builder(icon, REPLY_ACTION_LABEL, pendingIntent)
143 .addRemoteInput(remoteInput)
144 .build();
145 Notification notification = new Notification.Builder(mContext, CHANNEL_ID)
146 .setSmallIcon(android.R.drawable.ic_menu_edit)
147 .setContentTitle(NOTIFICATION_TITLE)
148 .setContentText(NOTIFICATION_CONTENT)
149 .addAction(action)
150 .build();
151 mNotificationManager.notify(NOTIFICATION_ID, notification);
152 }
153
154 private static String getImePackage(Context context) {
155 String imeId = Settings.Secure.getString(
156 context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
157 ComponentName cn = ComponentName.unflattenFromString(imeId);
158 assertThat(cn).isNotNull();
159 return cn.getPackageName();
160 }
161 }