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
15package com.android.systemui.statusbar.policy;
16
17import static junit.framework.Assert.assertEquals;
18
19import android.app.PendingIntent;
20import android.app.RemoteInput;
21import android.content.Intent;
22import android.content.IntentFilter;
23import android.content.pm.ShortcutManager;
24import android.os.Handler;
25import android.support.test.filters.SmallTest;
26import android.testing.AndroidTestingRunner;
27import android.testing.TestableLooper;
28import android.view.View;
29import android.widget.EditText;
30import android.widget.ImageButton;
31
32import com.android.systemui.Dependency;
33import com.android.systemui.R;
34import com.android.systemui.SysuiTestCase;
35import com.android.systemui.statusbar.ExpandableNotificationRow;
36import com.android.systemui.statusbar.NotificationTestHelper;
37import com.android.systemui.statusbar.RemoteInputController;
38
39import org.junit.After;
40import org.junit.Before;
41import org.junit.Test;
42import org.junit.runner.RunWith;
43import org.mockito.Mock;
44import org.mockito.MockitoAnnotations;
45
46@RunWith(AndroidTestingRunner.class)
47@TestableLooper.RunWithLooper(setAsMainLooper = true)
48@SmallTest
49public class RemoteInputViewTest extends SysuiTestCase {
50
51    private static final String TEST_RESULT_KEY = "test_result_key";
52    private static final String TEST_REPLY = "hello";
53    private static final String TEST_ACTION = "com.android.REMOTE_INPUT_VIEW_ACTION";
54
55    @Mock private RemoteInputController mController;
56    @Mock private ShortcutManager mShortcutManager;
57    @Mock private RemoteInputQuickSettingsDisabler mRemoteInputQuickSettingsDisabler;
58    private BlockingQueueIntentReceiver mReceiver;
59    private RemoteInputView mView;
60
61    @Before
62    public void setUp() throws Exception {
63        MockitoAnnotations.initMocks(this);
64
65        mDependency.injectTestDependency(RemoteInputQuickSettingsDisabler.class,
66                mRemoteInputQuickSettingsDisabler);
67
68        mReceiver = new BlockingQueueIntentReceiver();
69        mContext.registerReceiver(mReceiver, new IntentFilter(TEST_ACTION), null,
70                Handler.createAsync(Dependency.get(Dependency.BG_LOOPER)));
71
72        // Avoid SecurityException RemoteInputView#sendRemoteInput().
73        mContext.addMockSystemService(ShortcutManager.class, mShortcutManager);
74
75        ExpandableNotificationRow row = new NotificationTestHelper(mContext).createRow();
76        mView = RemoteInputView.inflate(mContext, null, row.getEntry(), mController);
77    }
78
79    @After
80    public void tearDown() {
81        mContext.unregisterReceiver(mReceiver);
82    }
83
84    @Test
85    public void testSendRemoteInput_intentContainsResultsAndSource() throws InterruptedException {
86        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0,
87                new Intent(TEST_ACTION), 0);
88        RemoteInput input = new RemoteInput.Builder(TEST_RESULT_KEY).build();
89
90        mView.setPendingIntent(pendingIntent);
91        mView.setRemoteInput(new RemoteInput[]{input}, input);
92        mView.focus();
93
94        EditText editText = mView.findViewById(R.id.remote_input_text);
95        editText.setText(TEST_REPLY);
96        ImageButton sendButton = mView.findViewById(R.id.remote_input_send);
97        sendButton.performClick();
98
99        Intent resultIntent = mReceiver.waitForIntent();
100        assertEquals(TEST_REPLY,
101                RemoteInput.getResultsFromIntent(resultIntent).get(TEST_RESULT_KEY));
102        assertEquals(RemoteInput.SOURCE_FREE_FORM_INPUT,
103                RemoteInput.getResultsSource(resultIntent));
104    }
105
106    @Test
107    public void testNoCrashWithoutVisibilityListener() {
108        mView.setOnVisibilityChangedListener(null);
109        mView.setVisibility(View.INVISIBLE);
110        mView.setVisibility(View.VISIBLE);
111    }
112}
113