Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 15 | package com.android.systemui.statusbar.policy; |
| 16 | |
| 17 | import static junit.framework.Assert.assertEquals; |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 18 | import static junit.framework.Assert.assertNotNull; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 19 | |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 21 | import android.app.PendingIntent; |
| 22 | import android.app.RemoteInput; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.content.pm.ShortcutManager; |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 26 | import android.os.Handler; |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 27 | import android.os.Process; |
| 28 | import android.os.UserHandle; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 29 | import android.testing.AndroidTestingRunner; |
| 30 | import android.testing.TestableLooper; |
Selim Cinek | bdbf458 | 2018-02-22 09:39:41 -0800 | [diff] [blame] | 31 | import android.view.View; |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 32 | import android.view.inputmethod.EditorInfo; |
| 33 | import android.view.inputmethod.InputConnection; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 34 | import android.widget.EditText; |
| 35 | import android.widget.ImageButton; |
| 36 | |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 37 | import androidx.test.filters.SmallTest; |
| 38 | |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 39 | import com.android.systemui.Dependency; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 40 | import com.android.systemui.R; |
| 41 | import com.android.systemui.SysuiTestCase; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 42 | import com.android.systemui.statusbar.NotificationTestHelper; |
| 43 | import com.android.systemui.statusbar.RemoteInputController; |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 44 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 45 | import com.android.systemui.util.Assert; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 46 | |
Petr Cermak | 7ef7812 | 2018-02-23 15:54:34 +0000 | [diff] [blame] | 47 | import org.junit.After; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | import org.mockito.Mock; |
| 52 | import org.mockito.MockitoAnnotations; |
| 53 | |
| 54 | @RunWith(AndroidTestingRunner.class) |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 55 | @TestableLooper.RunWithLooper |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 56 | @SmallTest |
| 57 | public 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 Cinek | 9a236f7 | 2018-02-22 18:35:53 -0800 | [diff] [blame] | 61 | private static final String TEST_ACTION = "com.android.REMOTE_INPUT_VIEW_ACTION"; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 62 | |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 63 | 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 Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 67 | @Mock private RemoteInputController mController; |
| 68 | @Mock private ShortcutManager mShortcutManager; |
sanryhuang | 6378786 | 2018-05-18 15:57:43 +0800 | [diff] [blame] | 69 | @Mock private RemoteInputQuickSettingsDisabler mRemoteInputQuickSettingsDisabler; |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 70 | private BlockingQueueIntentReceiver mReceiver; |
| 71 | private RemoteInputView mView; |
| 72 | |
| 73 | @Before |
| 74 | public void setUp() throws Exception { |
Jason Monk | a716bac | 2018-12-05 15:48:21 -0500 | [diff] [blame] | 75 | Assert.sMainLooper = TestableLooper.get(this).getLooper(); |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 76 | MockitoAnnotations.initMocks(this); |
| 77 | |
sanryhuang | 6378786 | 2018-05-18 15:57:43 +0800 | [diff] [blame] | 78 | mDependency.injectTestDependency(RemoteInputQuickSettingsDisabler.class, |
| 79 | mRemoteInputQuickSettingsDisabler); |
| 80 | |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 81 | mReceiver = new BlockingQueueIntentReceiver(); |
Jason Monk | 6dceace | 2018-05-15 20:24:07 -0400 | [diff] [blame] | 82 | mContext.registerReceiver(mReceiver, new IntentFilter(TEST_ACTION), null, |
| 83 | Handler.createAsync(Dependency.get(Dependency.BG_LOOPER))); |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 84 | |
| 85 | // Avoid SecurityException RemoteInputView#sendRemoteInput(). |
| 86 | mContext.addMockSystemService(ShortcutManager.class, mShortcutManager); |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Petr Cermak | 7ef7812 | 2018-02-23 15:54:34 +0000 | [diff] [blame] | 89 | @After |
| 90 | public void tearDown() { |
| 91 | mContext.unregisterReceiver(mReceiver); |
| 92 | } |
| 93 | |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 94 | private void setTestPendingIntent(RemoteInputView view) { |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 95 | PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, |
| 96 | new Intent(TEST_ACTION), 0); |
| 97 | RemoteInput input = new RemoteInput.Builder(TEST_RESULT_KEY).build(); |
| 98 | |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 99 | view.setPendingIntent(pendingIntent); |
Milo Sredkov | 13d8811 | 2019-02-01 12:23:24 +0000 | [diff] [blame] | 100 | view.setRemoteInput(new RemoteInput[]{input}, input, null /* editedSuggestionInfo */); |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 101 | } |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 102 | |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 103 | @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 Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 113 | editText.setText(TEST_REPLY); |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 114 | ImageButton sendButton = view.findViewById(R.id.remote_input_send); |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 115 | 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 Cinek | bdbf458 | 2018-02-22 09:39:41 -0800 | [diff] [blame] | 123 | |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 124 | 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 Cinek | bdbf458 | 2018-02-22 09:39:41 -0800 | [diff] [blame] | 145 | @Test |
Yohei Yukawa | 7276946 | 2019-01-20 09:28:08 -0800 | [diff] [blame] | 146 | 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 Cinek | bdbf458 | 2018-02-22 09:39:41 -0800 | [diff] [blame] | 174 | } |
Petr Cermak | 9a3380c | 2018-01-19 15:00:24 +0000 | [diff] [blame] | 175 | } |