• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.UiObject2;
46 import androidx.test.uiautomator.Until;
47 
48 import org.junit.After;
49 import org.junit.Before;
50 import org.junit.Rule;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 
54 import java.util.concurrent.TimeUnit;
55 import java.util.regex.Pattern;
56 
57 @RootPermissionTest
58 @RunWith(AndroidJUnit4.class)
59 public final class NotificationTest {
60 
61     private static final long TIMEOUT = TimeUnit.SECONDS.toMillis(10);
62 
63     private static final String CHANNEL_ID = "TEST_CHANNEL";
64     private static final String CHANNEL_NAME = "Test channel";
65 
66     private static final String REPLY_INPUT_KEY = "REPLY_KEY";
67     private static final String REPLY_INPUT_LABEL = "Test reply label";
68     private static final String ACTION_REPLY = "com.android.inputmethod.stresstest.ACTION_REPLY";
69     private static final String REPLY_ACTION_LABEL = "Test reply";
70     private static final int REPLY_REQUEST_CODE = 1;
71 
72     private static final String NOTIFICATION_TITLE = "Test notification";
73     private static final String NOTIFICATION_CONTENT = "Test notification content";
74     private static final int NOTIFICATION_ID = 2000;
75 
76     // This is for AOSP System UI for phones. When testing customized System UI, please modify here.
77     private static final BySelector REPLY_SEND_BUTTON_SELECTOR =
78             By.res("com.android.systemui", "remote_input_send").enabled(true);
79 
80     @Rule(order = 0) public UnlockScreenRule mUnlockScreenRule = new UnlockScreenRule();
81     @Rule(order = 1) public ImeStressTestRule mImeStressTestRule =
82             new ImeStressTestRule(true /* useSimpleTestIme */);
83     @Rule(order = 2) public ScreenCaptureRule mScreenCaptureRule =
84             new ScreenCaptureRule("/sdcard/InputMethodStressTest");
85 
86     private Context mContext;
87     private NotificationManager mNotificationManager;
88     private UiDevice mUiDevice;
89 
90     @Before
setUp()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
tearDown()103     public void tearDown() {
104         mNotificationManager.cancelAll();
105         mUiDevice.pressHome();
106     }
107 
108     @Test
testDirectReply()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         UiObject2 sendButton = mUiDevice.wait(
123                 Until.findObject(REPLY_SEND_BUTTON_SELECTOR), TIMEOUT);
124         if (sendButton == null) {
125             // If the screen is too small, sendButton may be hidden by IME.
126             // Dismiss IME and try again.
127             mUiDevice.pressBack();
128             sendButton = mUiDevice.wait(Until.findObject(REPLY_SEND_BUTTON_SELECTOR), TIMEOUT);
129         }
130         sendButton.click();
131         // Verify that IME is gone.
132         assertThat(mUiDevice.wait(Until.gone(By.pkg(getImePackage(mContext))), TIMEOUT)).isTrue();
133     }
134 
postMessagingNotification()135     private void postMessagingNotification() {
136         // Register the channel. It's safe to register the same channel again and again.
137         NotificationChannel channel =
138                 new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, IMPORTANCE_HIGH);
139         mNotificationManager.createNotificationChannel(channel);
140 
141         // Post inline reply notification.
142         PendingIntent pendingIntent = PendingIntent.getBroadcast(
143                 mContext, REPLY_REQUEST_CODE,
144                 new Intent().setAction(ACTION_REPLY).setClass(mContext, NotificationTest.class),
145                 PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
146         RemoteInput remoteInput = new RemoteInput.Builder(REPLY_INPUT_KEY)
147                 .setLabel(REPLY_INPUT_LABEL)
148                 .build();
149         Icon icon = Icon.createWithResource(mContext, android.R.drawable.ic_menu_edit);
150         Notification.Action action =
151                 new Notification.Action.Builder(icon, REPLY_ACTION_LABEL, pendingIntent)
152                         .addRemoteInput(remoteInput)
153                         .build();
154         Notification notification = new Notification.Builder(mContext, CHANNEL_ID)
155                 .setSmallIcon(android.R.drawable.ic_menu_edit)
156                 .setContentTitle(NOTIFICATION_TITLE)
157                 .setContentText(NOTIFICATION_CONTENT)
158                 .addAction(action)
159                 .build();
160         mNotificationManager.notify(NOTIFICATION_ID, notification);
161     }
162 
getImePackage(Context context)163     private static String getImePackage(Context context) {
164         String imeId = Settings.Secure.getString(
165                 context.getContentResolver(), Settings.Secure.DEFAULT_INPUT_METHOD);
166         ComponentName cn = ComponentName.unflattenFromString(imeId);
167         assertThat(cn).isNotNull();
168         return cn.getPackageName();
169     }
170 }
171