summaryrefslogtreecommitdiff
path: root/tests/inputmethod/src/android/view/inputmethod/cts/InputMethodManagerMultiDisplayTest.java
blob: 14a9f1aebc9c189ac73c57978779e2ff73aab9dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.view.inputmethod.cts;

import static android.content.pm.PackageManager.FEATURE_INPUT_METHODS;
import static android.server.wm.WindowManagerState.STATE_RESUMED;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

import static com.android.compatibility.common.util.SystemUtil.runShellCommandOrThrow;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;

import android.content.ComponentName;
import android.hardware.display.DisplayManager;
import android.os.RemoteException;
import android.platform.test.annotations.AppModeFull;
import android.server.wm.MultiDisplayTestBase;
import android.server.wm.WindowManagerState;
import android.view.Display;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.view.inputmethod.cts.util.TestActivity;
import android.widget.LinearLayout;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.MediumTest;
import androidx.test.uiautomator.By;
import androidx.test.uiautomator.UiDevice;
import androidx.test.uiautomator.Until;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.concurrent.TimeUnit;

@MediumTest
@RunWith(AndroidJUnit4.class)
@AppModeFull(reason = "Instant apps cannot query the installed IMEs")
public class InputMethodManagerMultiDisplayTest extends MultiDisplayTestBase {
    private static final String MOCK_IME_ID =
            "com.android.cts.mockimewithsubtypes/.MockImeWithSubtypes";
    private static final String MOCK_IME_SUBTYPE_LABEL = "CTS Subtype 1 Test String";
    private static final String SETTINGS_ACTIVITY_PACKAGE = "com.android.settings";

    private static final long TIMEOUT = TimeUnit.SECONDS.toMillis(5);
    private InputMethodManager mImManager;

    @Before
    public void setUp() throws Exception {
        super.setUp();

        assumeTrue(mContext.getPackageManager().hasSystemFeature(FEATURE_INPUT_METHODS));
        assumeFalse(isWatch());

        mImManager = mContext.getSystemService(InputMethodManager.class);

        enableAndSetIme();
    }

    @After
    public void tearDown() {
        runShellCommandOrThrow("ime reset");
        launchHomeActivity();
    }

    @Test
    public void testShowInputMethodAndSubtypeEnablerOnSingleDisplay() throws RemoteException {
        assumeFalse(isCar());
        final UiDevice uiDevice = UiDevice.getInstance(mInstrumentation);
        uiDevice.setOrientationNatural();

        mImManager.showInputMethodAndSubtypeEnabler(MOCK_IME_ID);
        // Check if new activity was started with subtype settings
        assertThat(uiDevice.wait(Until.hasObject(By.text(MOCK_IME_SUBTYPE_LABEL)),
                TIMEOUT)).isTrue();
    }

    @Test
    public void testShowInputMethodAndSubtypeEnablerOnNonDefaultDisplay() {
        assumeFalse(isCar());
        assumeTrue(supportsMultiDisplay());

        try (MultiDisplayTestBase.VirtualDisplaySession session =
                     new MultiDisplayTestBase.VirtualDisplaySession()) {

            // Set up a simulated display.
            WindowManagerState.DisplayContent dc = session.setSimulateDisplay(true).createDisplay();
            Display simulatedDisplay = mContext.getSystemService(DisplayManager.class)
                    .getDisplay(dc.mId);

            // Launch a test activity on the simulated display.
            TestActivity testActivity = new TestActivity.Starter().withDisplayId(dc.mId)
                    .startSync(activity -> {
                        final View view = new View(activity);
                        view.setLayoutParams(
                                new LinearLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT));
                        return view;
                    }, TestActivity.class);
            waitAndAssertActivityStateOnDisplay(testActivity.getComponentName(), STATE_RESUMED,
                    dc.mId, "Test Activity launched on external display must be resumed");

            // Focus on virtual display, otherwise UI automator cannot detect objects
            tapOnDisplayCenter(dc.mId);

            waitAndAssertTopResumedActivity(testActivity.getComponentName(),
                    dc.mId, "Test Activity launched on external display must be on top");

            // Open settings screen for subtypes from the non-default / currently active screen
            final InputMethodManager im = testActivity.getSystemService(InputMethodManager.class);
            im.showInputMethodAndSubtypeEnabler(MOCK_IME_ID);
            // Verify that settings screen is opened on the second/non-default display
            // TODO(b/274604301): Use UiAutomator to verify the given subtype settings
            // started on the simulated display.
            mWmState.waitForWithAmState(state -> ComponentName.unflattenFromString(
                            state.getResumedActivityOnDisplay(dc.mId)).getPackageName().equals(
                            SETTINGS_ACTIVITY_PACKAGE),
                    "Settings screen must be launched on display where it was started from");
        }
    }

    private void enableAndSetIme() {
        runShellCommandOrThrow("ime enable " + InputMethodManagerMultiDisplayTest.MOCK_IME_ID);
        runShellCommandOrThrow("ime set " + InputMethodManagerMultiDisplayTest.MOCK_IME_ID);
    }

}