1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.inputmethod.stresstest;
18 
19 import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
20 import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN;
21 
22 import static com.android.compatibility.common.util.SystemUtil.eventually;
23 import static com.android.inputmethod.stresstest.ImeStressTestUtil.REQUEST_FOCUS_ON_CREATE;
24 import static com.android.inputmethod.stresstest.ImeStressTestUtil.TestActivity.createIntent;
25 import static com.android.inputmethod.stresstest.ImeStressTestUtil.callOnMainSync;
26 import static com.android.inputmethod.stresstest.ImeStressTestUtil.requestFocusAndVerify;
27 import static com.android.inputmethod.stresstest.ImeStressTestUtil.verifyWindowAndViewFocus;
28 import static com.android.inputmethod.stresstest.ImeStressTestUtil.waitOnMainUntilImeIsHidden;
29 import static com.android.inputmethod.stresstest.ImeStressTestUtil.waitOnMainUntilImeIsShown;
30 
31 import static com.google.common.truth.Truth.assertWithMessage;
32 
33 import android.content.Intent;
34 import android.platform.test.annotations.RootPermissionTest;
35 import android.platform.test.rule.UnlockScreenRule;
36 import android.support.test.uiautomator.UiDevice;
37 import android.widget.EditText;
38 
39 import androidx.test.platform.app.InstrumentationRegistry;
40 
41 import org.junit.Rule;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.junit.runners.Parameterized;
45 
46 import java.util.Arrays;
47 import java.util.Collections;
48 import java.util.List;
49 import java.util.concurrent.TimeUnit;
50 
51 /**
52  * Test IME visibility by using system default IME to ensure the behavior is consistent
53  * across Android platform versions.
54  */
55 @RootPermissionTest
56 @RunWith(Parameterized.class)
57 public final class DefaultImeVisibilityTest {
58 
59     @Rule(order = 0)
60     public UnlockScreenRule mUnlockScreenRule = new UnlockScreenRule();
61     // Use system default IME for test.
62     @Rule(order = 1)
63     public ImeStressTestRule mImeStressTestRule =
64             new ImeStressTestRule(false /* useSimpleTestIme */);
65 
66     @Rule(order = 2)
67     public ScreenCaptureRule mScreenCaptureRule =
68             new ScreenCaptureRule("/sdcard/InputMethodStressTest");
69 
70     private static final long TIMEOUT = TimeUnit.SECONDS.toMillis(3);
71 
72     private static final int NUM_TEST_ITERATIONS = 10;
73 
74     private final boolean mIsPortrait;
75 
76     @Parameterized.Parameters(name = "isPortrait={0}")
isPortraitCases()77     public static List<Boolean> isPortraitCases() {
78         // Test in both portrait and landscape mode.
79         return Arrays.asList(true, false);
80     }
81 
DefaultImeVisibilityTest(boolean isPortrait)82     public DefaultImeVisibilityTest(boolean isPortrait) {
83         mIsPortrait = isPortrait;
84         mImeStressTestRule.setIsPortrait(isPortrait);
85     }
86 
87     @Test
showHideDefaultIme()88     public void showHideDefaultIme() {
89         Intent intent =
90                 createIntent(
91                         0x0 /* No window focus flags */,
92                         SOFT_INPUT_STATE_HIDDEN | SOFT_INPUT_ADJUST_RESIZE,
93                         Collections.singletonList(REQUEST_FOCUS_ON_CREATE));
94         ImeStressTestUtil.TestActivity activity = ImeStressTestUtil.TestActivity.start(intent);
95         EditText editText = activity.getEditText();
96 
97         UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
98         eventually(
99                 () ->
100                         assertWithMessage("Display rotation should have been updated")
101                                 .that(uiDevice.getDisplayRotation())
102                                 .isEqualTo(mIsPortrait ? 0 : 1),
103                 TIMEOUT);
104 
105         for (int i = 0; i < NUM_TEST_ITERATIONS; i++) {
106             // TODO(b/291752364): Remove the explicit focus request once the issue with view focus
107             //  change between fullscreen IME and actual editText is fixed.
108             requestFocusAndVerify(activity);
109             verifyWindowAndViewFocus(editText, true, true);
110             callOnMainSync(activity::showImeWithInputMethodManager);
111             waitOnMainUntilImeIsShown(editText);
112 
113             callOnMainSync(activity::hideImeWithInputMethodManager);
114             waitOnMainUntilImeIsHidden(editText);
115         }
116     }
117 }
118