blob: 8bf1606d5b7b85e4d2a3435526c83de6841e9cfc [file] [log] [blame]
Petr Cermak9a3380c2018-01-19 15:00:24 +00001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the
10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11 * KIND, either express or implied. See the License for the specific language governing
12 * permissions and limitations under the License.
13 */
14
15package com.android.systemui.statusbar.policy;
16
17import static junit.framework.Assert.assertEquals;
Yohei Yukawa72769462019-01-20 09:28:08 -080018import static junit.framework.Assert.assertNotNull;
Petr Cermak9a3380c2018-01-19 15:00:24 +000019
Yohei Yukawa72769462019-01-20 09:28:08 -080020import android.app.ActivityManager;
Petr Cermak9a3380c2018-01-19 15:00:24 +000021import android.app.PendingIntent;
22import android.app.RemoteInput;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.content.pm.ShortcutManager;
Jason Monk6dceace2018-05-15 20:24:07 -040026import android.os.Handler;
Yohei Yukawa72769462019-01-20 09:28:08 -080027import android.os.Process;
28import android.os.UserHandle;
Petr Cermak9a3380c2018-01-19 15:00:24 +000029import android.testing.AndroidTestingRunner;
30import android.testing.TestableLooper;
Selim Cinekbdbf4582018-02-22 09:39:41 -080031import android.view.View;
Yohei Yukawa72769462019-01-20 09:28:08 -080032import android.view.inputmethod.EditorInfo;
33import android.view.inputmethod.InputConnection;
Petr Cermak9a3380c2018-01-19 15:00:24 +000034import android.widget.EditText;
35import android.widget.ImageButton;
36
Brett Chabot84151d92019-02-27 15:37:59 -080037import androidx.test.filters.SmallTest;
38
Jason Monk6dceace2018-05-15 20:24:07 -040039import com.android.systemui.Dependency;
Petr Cermak9a3380c2018-01-19 15:00:24 +000040import com.android.systemui.R;
41import com.android.systemui.SysuiTestCase;
Petr Cermak9a3380c2018-01-19 15:00:24 +000042import com.android.systemui.statusbar.NotificationTestHelper;
43import com.android.systemui.statusbar.RemoteInputController;
Jason Monka716bac2018-12-05 15:48:21 -050044import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
45import com.android.systemui.util.Assert;
Petr Cermak9a3380c2018-01-19 15:00:24 +000046
Petr Cermak7ef78122018-02-23 15:54:34 +000047import org.junit.After;
Petr Cermak9a3380c2018-01-19 15:00:24 +000048import org.junit.Before;
49import org.junit.Test;
50import org.junit.runner.RunWith;
51import org.mockito.Mock;
52import org.mockito.MockitoAnnotations;
53
54@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050055@TestableLooper.RunWithLooper
Petr Cermak9a3380c2018-01-19 15:00:24 +000056@SmallTest
57public class RemoteInputViewTest extends SysuiTestCase {
58
59 private static final String TEST_RESULT_KEY = "test_result_key";
60 private static final String TEST_REPLY = "hello";
Selim Cinek9a236f72018-02-22 18:35:53 -080061 private static final String TEST_ACTION = "com.android.REMOTE_INPUT_VIEW_ACTION";
Petr Cermak9a3380c2018-01-19 15:00:24 +000062
Yohei Yukawa72769462019-01-20 09:28:08 -080063 private static final String DUMMY_MESSAGE_APP_PKG =
64 "com.android.sysuitest.dummynotificationsender";
65 private static final int DUMMY_MESSAGE_APP_ID = Process.LAST_APPLICATION_UID - 1;
66
Petr Cermak9a3380c2018-01-19 15:00:24 +000067 @Mock private RemoteInputController mController;
68 @Mock private ShortcutManager mShortcutManager;
sanryhuang63787862018-05-18 15:57:43 +080069 @Mock private RemoteInputQuickSettingsDisabler mRemoteInputQuickSettingsDisabler;
Petr Cermak9a3380c2018-01-19 15:00:24 +000070 private BlockingQueueIntentReceiver mReceiver;
71 private RemoteInputView mView;
72
73 @Before
74 public void setUp() throws Exception {
Jason Monka716bac2018-12-05 15:48:21 -050075 Assert.sMainLooper = TestableLooper.get(this).getLooper();
Petr Cermak9a3380c2018-01-19 15:00:24 +000076 MockitoAnnotations.initMocks(this);
77
sanryhuang63787862018-05-18 15:57:43 +080078 mDependency.injectTestDependency(RemoteInputQuickSettingsDisabler.class,
79 mRemoteInputQuickSettingsDisabler);
80
Petr Cermak9a3380c2018-01-19 15:00:24 +000081 mReceiver = new BlockingQueueIntentReceiver();
Jason Monk6dceace2018-05-15 20:24:07 -040082 mContext.registerReceiver(mReceiver, new IntentFilter(TEST_ACTION), null,
83 Handler.createAsync(Dependency.get(Dependency.BG_LOOPER)));
Petr Cermak9a3380c2018-01-19 15:00:24 +000084
85 // Avoid SecurityException RemoteInputView#sendRemoteInput().
86 mContext.addMockSystemService(ShortcutManager.class, mShortcutManager);
Petr Cermak9a3380c2018-01-19 15:00:24 +000087 }
88
Petr Cermak7ef78122018-02-23 15:54:34 +000089 @After
90 public void tearDown() {
91 mContext.unregisterReceiver(mReceiver);
92 }
93
Yohei Yukawa72769462019-01-20 09:28:08 -080094 private void setTestPendingIntent(RemoteInputView view) {
Petr Cermak9a3380c2018-01-19 15:00:24 +000095 PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0,
96 new Intent(TEST_ACTION), 0);
97 RemoteInput input = new RemoteInput.Builder(TEST_RESULT_KEY).build();
98
Yohei Yukawa72769462019-01-20 09:28:08 -080099 view.setPendingIntent(pendingIntent);
Milo Sredkov13d88112019-02-01 12:23:24 +0000100 view.setRemoteInput(new RemoteInput[]{input}, input, null /* editedSuggestionInfo */);
Yohei Yukawa72769462019-01-20 09:28:08 -0800101 }
Petr Cermak9a3380c2018-01-19 15:00:24 +0000102
Yohei Yukawa72769462019-01-20 09:28:08 -0800103 @Test
104 public void testSendRemoteInput_intentContainsResultsAndSource() throws Exception {
105 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow();
106 RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
107
108 setTestPendingIntent(view);
109
110 view.focus();
111
112 EditText editText = view.findViewById(R.id.remote_input_text);
Petr Cermak9a3380c2018-01-19 15:00:24 +0000113 editText.setText(TEST_REPLY);
Yohei Yukawa72769462019-01-20 09:28:08 -0800114 ImageButton sendButton = view.findViewById(R.id.remote_input_send);
Petr Cermak9a3380c2018-01-19 15:00:24 +0000115 sendButton.performClick();
116
117 Intent resultIntent = mReceiver.waitForIntent();
118 assertEquals(TEST_REPLY,
119 RemoteInput.getResultsFromIntent(resultIntent).get(TEST_RESULT_KEY));
120 assertEquals(RemoteInput.SOURCE_FREE_FORM_INPUT,
121 RemoteInput.getResultsSource(resultIntent));
122 }
Selim Cinekbdbf4582018-02-22 09:39:41 -0800123
Yohei Yukawa72769462019-01-20 09:28:08 -0800124 private UserHandle getTargetInputMethodUser(UserHandle fromUser, UserHandle toUser)
125 throws Exception {
126 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow(
127 DUMMY_MESSAGE_APP_PKG,
128 UserHandle.getUid(fromUser.getIdentifier(), DUMMY_MESSAGE_APP_ID),
129 toUser);
130 RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
131
132 setTestPendingIntent(view);
133
134 view.focus();
135
136 EditText editText = view.findViewById(R.id.remote_input_text);
137 EditorInfo editorInfo = new EditorInfo();
138 editorInfo.packageName = DUMMY_MESSAGE_APP_PKG;
139 editorInfo.fieldId = editText.getId();
140 InputConnection ic = editText.onCreateInputConnection(editorInfo);
141 assertNotNull(ic);
142 return editorInfo.targetInputMethodUser;
143 }
144
Selim Cinekbdbf4582018-02-22 09:39:41 -0800145 @Test
Yohei Yukawa72769462019-01-20 09:28:08 -0800146 public void testEditorInfoTargetInputMethodUserForCallingUser() throws Exception {
147 UserHandle callingUser = Process.myUserHandle();
148 assertEquals(callingUser, getTargetInputMethodUser(callingUser, callingUser));
149 }
150
151 @Test
152 public void testEditorInfoTargetInputMethodUserForDifferentUser() throws Exception {
153 UserHandle differentUser = UserHandle.of(UserHandle.getCallingUserId() + 1);
154 assertEquals(differentUser, getTargetInputMethodUser(differentUser, differentUser));
155 }
156
157 @Test
158 public void testEditorInfoTargetInputMethodUserForAllUser() throws Exception {
159 // For the special pseudo user UserHandle.ALL, EditorInfo#targetInputMethodUser must be
160 // resolved as the current user.
161 UserHandle callingUser = Process.myUserHandle();
162 assertEquals(UserHandle.of(ActivityManager.getCurrentUser()),
163 getTargetInputMethodUser(callingUser, UserHandle.ALL));
164 }
165
166 @Test
167 public void testNoCrashWithoutVisibilityListener() throws Exception {
168 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow();
169 RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
170
171 view.setOnVisibilityChangedListener(null);
172 view.setVisibility(View.INVISIBLE);
173 view.setVisibility(View.VISIBLE);
Selim Cinekbdbf4582018-02-22 09:39:41 -0800174 }
Petr Cermak9a3380c2018-01-19 15:00:24 +0000175}