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