1 /*
2  * Copyright (C) 2018 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.server.inputmethod;
18 
19 import static android.view.Display.DEFAULT_DISPLAY;
20 import static android.view.Display.INVALID_DISPLAY;
21 import static android.view.WindowManager.DISPLAY_IME_POLICY_FALLBACK_DISPLAY;
22 import static android.view.WindowManager.DISPLAY_IME_POLICY_LOCAL;
23 import static android.view.WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED;
24 
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27 
28 import android.platform.test.annotations.Presubmit;
29 
30 import androidx.test.filters.SmallTest;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import com.android.internal.inputmethod.SoftInputShowHideReason;
34 
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 
38 import java.io.PrintWriter;
39 import java.io.StringWriter;
40 
41 @Presubmit
42 @SmallTest
43 @RunWith(AndroidJUnit4.class)
44 public class InputMethodManagerServiceTests {
45     static final int SYSTEM_DECORATION_SUPPORT_DISPLAY_ID = 2;
46     static final int NO_SYSTEM_DECORATION_SUPPORT_DISPLAY_ID = 3;
47 
48     static InputMethodManagerService.ImeDisplayValidator sChecker =
49             (displayId) -> {
50                 switch (displayId) {
51                     case SYSTEM_DECORATION_SUPPORT_DISPLAY_ID:
52                         return DISPLAY_IME_POLICY_LOCAL;
53                     case NO_SYSTEM_DECORATION_SUPPORT_DISPLAY_ID:
54                         return DISPLAY_IME_POLICY_FALLBACK_DISPLAY;
55                     default:
56                         throw new IllegalArgumentException("Unknown displayId=" + displayId);
57                 }
58             };
59 
60     static InputMethodManagerService.ImeDisplayValidator sMustNotBeCalledChecker =
61             (displayId) -> {
62                 fail("Should not pass to display config check for this test case.");
63                 return DISPLAY_IME_POLICY_FALLBACK_DISPLAY;
64             };
65 
66     @Test
testComputeImeDisplayId_defaultDisplayId()67     public void testComputeImeDisplayId_defaultDisplayId() {
68         // Make sure that there is a short-circuit for DEFAULT_DISPLAY.
69         assertEquals(DEFAULT_DISPLAY,
70                 InputMethodManagerService.computeImeDisplayIdForTarget(
71                         DEFAULT_DISPLAY, sMustNotBeCalledChecker));
72     }
73 
74     @Test
testComputeImeDisplayId_InvalidDisplayId()75     public void testComputeImeDisplayId_InvalidDisplayId() {
76         // Make sure that there is a short-circuit for INVALID_DISPLAY.
77         assertEquals(DEFAULT_DISPLAY,
78                 InputMethodManagerService.computeImeDisplayIdForTarget(
79                         INVALID_DISPLAY, sMustNotBeCalledChecker));
80     }
81 
82     @Test
testComputeImeDisplayId_noSystemDecorationSupportDisplay()83     public void testComputeImeDisplayId_noSystemDecorationSupportDisplay() {
84         // Presume display didn't support system decoration.
85         // Make sure IME displayId is DEFAULT_DISPLAY.
86         assertEquals(DEFAULT_DISPLAY,
87                 InputMethodManagerService.computeImeDisplayIdForTarget(
88                         NO_SYSTEM_DECORATION_SUPPORT_DISPLAY_ID, sChecker));
89     }
90 
91     @Test
testComputeImeDisplayId_withSystemDecorationSupportDisplay()92     public void testComputeImeDisplayId_withSystemDecorationSupportDisplay() {
93         // Presume display support system decoration.
94         // Make sure IME displayId is the same display.
95         assertEquals(SYSTEM_DECORATION_SUPPORT_DISPLAY_ID,
96                 InputMethodManagerService.computeImeDisplayIdForTarget(
97                         SYSTEM_DECORATION_SUPPORT_DISPLAY_ID, sChecker));
98     }
99 
100     @Test
testSoftInputShowHideHistoryDump_withNulls_doesntThrow()101     public void testSoftInputShowHideHistoryDump_withNulls_doesntThrow() {
102         var writer = new StringWriter();
103         var history = new InputMethodManagerService.SoftInputShowHideHistory();
104         history.addEntry(new InputMethodManagerService.SoftInputShowHideHistory.Entry(
105                 null,
106                 null,
107                 null,
108                 SOFT_INPUT_STATE_UNSPECIFIED,
109                 SoftInputShowHideReason.SHOW_SOFT_INPUT,
110                 false,
111                 null,
112                 null,
113                 null,
114                 null));
115 
116         history.dump(new PrintWriter(writer), "" /* prefix */);
117 
118         // Asserts that dump doesn't throw an NPE.
119     }
120 }
121