blob: 568ff55f6d341ede6d8344b16d18c371e5dd9dfe [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.support.test.filters.SmallTest;
30import android.testing.AndroidTestingRunner;
31import android.testing.TestableLooper;
Selim Cinekbdbf4582018-02-22 09:39:41 -080032import android.view.View;
Yohei Yukawa72769462019-01-20 09:28:08 -080033import android.view.inputmethod.EditorInfo;
34import android.view.inputmethod.InputConnection;
Petr Cermak9a3380c2018-01-19 15:00:24 +000035import android.widget.EditText;
36import android.widget.ImageButton;
37
Jason Monk6dceace2018-05-15 20:24:07 -040038import com.android.systemui.Dependency;
Petr Cermak9a3380c2018-01-19 15:00:24 +000039import com.android.systemui.R;
40import com.android.systemui.SysuiTestCase;
Petr Cermak9a3380c2018-01-19 15:00:24 +000041import com.android.systemui.statusbar.NotificationTestHelper;
42import com.android.systemui.statusbar.RemoteInputController;
Jason Monka716bac2018-12-05 15:48:21 -050043import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
44import com.android.systemui.util.Assert;
Petr Cermak9a3380c2018-01-19 15:00:24 +000045
Petr Cermak7ef78122018-02-23 15:54:34 +000046import org.junit.After;
Petr Cermak9a3380c2018-01-19 15:00:24 +000047import org.junit.Before;
48import org.junit.Test;
49import org.junit.runner.RunWith;
50import org.mockito.Mock;
51import org.mockito.MockitoAnnotations;
52
53@RunWith(AndroidTestingRunner.class)
Jason Monka716bac2018-12-05 15:48:21 -050054@TestableLooper.RunWithLooper
Petr Cermak9a3380c2018-01-19 15:00:24 +000055@SmallTest
56public class RemoteInputViewTest extends SysuiTestCase {
57
58 private static final String TEST_RESULT_KEY = "test_result_key";
59 private static final String TEST_REPLY = "hello";
Selim Cinek9a236f72018-02-22 18:35:53 -080060 private static final String TEST_ACTION = "com.android.REMOTE_INPUT_VIEW_ACTION";
Petr Cermak9a3380c2018-01-19 15:00:24 +000061
Yohei Yukawa72769462019-01-20 09:28:08 -080062 private static final String DUMMY_MESSAGE_APP_PKG =
63 "com.android.sysuitest.dummynotificationsender";
64 private static final int DUMMY_MESSAGE_APP_ID = Process.LAST_APPLICATION_UID - 1;
65
Petr Cermak9a3380c2018-01-19 15:00:24 +000066 @Mock private RemoteInputController mController;
67 @Mock private ShortcutManager mShortcutManager;
sanryhuang63787862018-05-18 15:57:43 +080068 @Mock private RemoteInputQuickSettingsDisabler mRemoteInputQuickSettingsDisabler;
Petr Cermak9a3380c2018-01-19 15:00:24 +000069 private BlockingQueueIntentReceiver mReceiver;
70 private RemoteInputView mView;
71
72 @Before
73 public void setUp() throws Exception {
Jason Monka716bac2018-12-05 15:48:21 -050074 Assert.sMainLooper = TestableLooper.get(this).getLooper();
Petr Cermak9a3380c2018-01-19 15:00:24 +000075 MockitoAnnotations.initMocks(this);
76
sanryhuang63787862018-05-18 15:57:43 +080077 mDependency.injectTestDependency(RemoteInputQuickSettingsDisabler.class,
78 mRemoteInputQuickSettingsDisabler);
79
Petr Cermak9a3380c2018-01-19 15:00:24 +000080 mReceiver = new BlockingQueueIntentReceiver();
Jason Monk6dceace2018-05-15 20:24:07 -040081 mContext.registerReceiver(mReceiver, new IntentFilter(TEST_ACTION), null,
82 Handler.createAsync(Dependency.get(Dependency.BG_LOOPER)));
Petr Cermak9a3380c2018-01-19 15:00:24 +000083
84 // Avoid SecurityException RemoteInputView#sendRemoteInput().
85 mContext.addMockSystemService(ShortcutManager.class, mShortcutManager);
Petr Cermak9a3380c2018-01-19 15:00:24 +000086 }
87
Petr Cermak7ef78122018-02-23 15:54:34 +000088 @After
89 public void tearDown() {
90 mContext.unregisterReceiver(mReceiver);
91 }
92
Yohei Yukawa72769462019-01-20 09:28:08 -080093 private void setTestPendingIntent(RemoteInputView view) {
Petr Cermak9a3380c2018-01-19 15:00:24 +000094 PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0,
95 new Intent(TEST_ACTION), 0);
96 RemoteInput input = new RemoteInput.Builder(TEST_RESULT_KEY).build();
97
Yohei Yukawa72769462019-01-20 09:28:08 -080098 view.setPendingIntent(pendingIntent);
Milo Sredkov13d88112019-02-01 12:23:24 +000099 view.setRemoteInput(new RemoteInput[]{input}, input, null /* editedSuggestionInfo */);
Yohei Yukawa72769462019-01-20 09:28:08 -0800100 }
Petr Cermak9a3380c2018-01-19 15:00:24 +0000101
Yohei Yukawa72769462019-01-20 09:28:08 -0800102 @Test
103 public void testSendRemoteInput_intentContainsResultsAndSource() throws Exception {
104 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow();
105 RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
106
107 setTestPendingIntent(view);
108
109 view.focus();
110
111 EditText editText = view.findViewById(R.id.remote_input_text);
Petr Cermak9a3380c2018-01-19 15:00:24 +0000112 editText.setText(TEST_REPLY);
Yohei Yukawa72769462019-01-20 09:28:08 -0800113 ImageButton sendButton = view.findViewById(R.id.remote_input_send);
Petr Cermak9a3380c2018-01-19 15:00:24 +0000114 sendButton.performClick();
115
116 Intent resultIntent = mReceiver.waitForIntent();
117 assertEquals(TEST_REPLY,
118 RemoteInput.getResultsFromIntent(resultIntent).get(TEST_RESULT_KEY));
119 assertEquals(RemoteInput.SOURCE_FREE_FORM_INPUT,
120 RemoteInput.getResultsSource(resultIntent));
121 }
Selim Cinekbdbf4582018-02-22 09:39:41 -0800122
Yohei Yukawa72769462019-01-20 09:28:08 -0800123 private UserHandle getTargetInputMethodUser(UserHandle fromUser, UserHandle toUser)
124 throws Exception {
125 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow(
126 DUMMY_MESSAGE_APP_PKG,
127 UserHandle.getUid(fromUser.getIdentifier(), DUMMY_MESSAGE_APP_ID),
128 toUser);
129 RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
130
131 setTestPendingIntent(view);
132
133 view.focus();
134
135 EditText editText = view.findViewById(R.id.remote_input_text);
136 EditorInfo editorInfo = new EditorInfo();
137 editorInfo.packageName = DUMMY_MESSAGE_APP_PKG;
138 editorInfo.fieldId = editText.getId();
139 InputConnection ic = editText.onCreateInputConnection(editorInfo);
140 assertNotNull(ic);
141 return editorInfo.targetInputMethodUser;
142 }
143
Selim Cinekbdbf4582018-02-22 09:39:41 -0800144 @Test
Yohei Yukawa72769462019-01-20 09:28:08 -0800145 public void testEditorInfoTargetInputMethodUserForCallingUser() throws Exception {
146 UserHandle callingUser = Process.myUserHandle();
147 assertEquals(callingUser, getTargetInputMethodUser(callingUser, callingUser));
148 }
149
150 @Test
151 public void testEditorInfoTargetInputMethodUserForDifferentUser() throws Exception {
152 UserHandle differentUser = UserHandle.of(UserHandle.getCallingUserId() + 1);
153 assertEquals(differentUser, getTargetInputMethodUser(differentUser, differentUser));
154 }
155
156 @Test
157 public void testEditorInfoTargetInputMethodUserForAllUser() throws Exception {
158 // For the special pseudo user UserHandle.ALL, EditorInfo#targetInputMethodUser must be
159 // resolved as the current user.
160 UserHandle callingUser = Process.myUserHandle();
161 assertEquals(UserHandle.of(ActivityManager.getCurrentUser()),
162 getTargetInputMethodUser(callingUser, UserHandle.ALL));
163 }
164
165 @Test
166 public void testNoCrashWithoutVisibilityListener() throws Exception {
167 ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow();
168 RemoteInputView view = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
169
170 view.setOnVisibilityChangedListener(null);
171 view.setVisibility(View.INVISIBLE);
172 view.setVisibility(View.VISIBLE);
Selim Cinekbdbf4582018-02-22 09:39:41 -0800173 }
Petr Cermak9a3380c2018-01-19 15:00:24 +0000174}