blob: 0e19ca84d4331f8b871cf6f0f937e11b18b7d46b [file] [log] [blame]
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -08001/*
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
17package android.view;
18
19import static android.view.ImeInsetsSourceConsumer.areEditorsSimilar;
Tiger Huang332793b2019-10-29 23:21:27 +080020import static android.view.InsetsState.ITYPE_IME;
Jorim Jaggi648e5882019-01-24 13:24:02 +010021import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE;
Tiger Huang332793b2019-10-29 23:21:27 +080022
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -080023import static junit.framework.Assert.assertFalse;
24import static junit.framework.Assert.assertTrue;
25
26import android.content.Context;
27import android.graphics.Insets;
Tarandeep Singh215929b2019-01-11 18:24:37 -080028import android.graphics.Point;
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -080029import android.graphics.Rect;
30import android.os.Bundle;
31import android.platform.test.annotations.Presubmit;
32import android.view.SurfaceControl.Transaction;
33import android.view.WindowManager.BadTokenException;
34import android.view.WindowManager.LayoutParams;
35import android.view.inputmethod.EditorInfo;
36import android.widget.TextView;
37
38import androidx.test.InstrumentationRegistry;
39import androidx.test.filters.FlakyTest;
40import androidx.test.runner.AndroidJUnit4;
41
42import org.junit.Before;
43import org.junit.Test;
44import org.junit.runner.RunWith;
45
46import java.util.ArrayList;
47
48@Presubmit
49@FlakyTest(detail = "Promote once confirmed non-flaky")
50@RunWith(AndroidJUnit4.class)
51public class ImeInsetsSourceConsumerTest {
52
53 Context mContext = InstrumentationRegistry.getTargetContext();
54 ImeInsetsSourceConsumer mImeConsumer;
55 InsetsController mController;
56 SurfaceControl mLeash;
57
58 @Before
59 public void setup() {
60 mLeash = new SurfaceControl.Builder(new SurfaceSession())
61 .setName("testSurface")
62 .build();
63 InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
64 ViewRootImpl viewRootImpl = new ViewRootImpl(mContext, mContext.getDisplay());
65 try {
66 viewRootImpl.setView(new TextView(mContext), new LayoutParams(), null);
67 } catch (BadTokenException e) {
68 // activity isn't running, we will ignore BadTokenException.
69 }
70 mController = new InsetsController(viewRootImpl);
71 final Rect rect = new Rect(5, 5, 5, 5);
72 mController.calculateInsets(
73 false,
74 false,
75 new DisplayCutout(
76 Insets.of(10, 10, 10, 10), rect, rect, rect, rect),
Jorim Jaggi648e5882019-01-24 13:24:02 +010077 rect, rect, SOFT_INPUT_ADJUST_RESIZE);
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -080078 mImeConsumer = new ImeInsetsSourceConsumer(
79 new InsetsState(), Transaction::new, mController);
80 });
81 }
82
83 @Test
84 public void testImeVisibility() {
Tiger Huang332793b2019-10-29 23:21:27 +080085 final InsetsSourceControl ime = new InsetsSourceControl(ITYPE_IME, mLeash, new Point());
Tarandeep Singh2cbcd7f2019-01-25 11:47:57 -080086 mController.onControlsChanged(new InsetsSourceControl[] { ime });
87
88 InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
89 // test if setVisibility can show IME
90 mImeConsumer.onWindowFocusGained();
91 mImeConsumer.applyImeVisibility(true);
92 mController.cancelExistingAnimation();
93 assertTrue(mController.getSourceConsumer(ime.getType()).isVisible());
94
95 // test if setVisibility can hide IME
96 mImeConsumer.applyImeVisibility(false);
97 mController.cancelExistingAnimation();
98 assertFalse(mController.getSourceConsumer(ime.getType()).isVisible());
99 });
100 }
101
102 @Test
103 public void testAreEditorsSimilar() {
104 EditorInfo info1 = new EditorInfo();
105 info1.privateImeOptions = "dummy";
106 EditorInfo info2 = new EditorInfo();
107
108 assertFalse(areEditorsSimilar(info1, info2));
109
110 info1.privateImeOptions = null;
111 assertTrue(areEditorsSimilar(info1, info2));
112
113 info1.inputType = info2.inputType = 3;
114 info1.imeOptions = info2.imeOptions = 0x4;
115 info1.packageName = info2.packageName = "dummy.package";
116 assertTrue(areEditorsSimilar(info1, info2));
117
118 Bundle extras1 = new Bundle();
119 extras1.putByteArray("key1", "value1".getBytes());
120 extras1.putChar("key2", 'c');
121 Bundle extras2 = new Bundle();
122 extras2.putByteArray("key1", "value1".getBytes());
123 extras2.putChar("key2", 'c');
124 info1.extras = extras1;
125 info2.extras = extras2;
126 assertTrue(areEditorsSimilar(info1, info2));
127
128 Bundle extraBundle = new Bundle();
129 ArrayList<Integer> list = new ArrayList<>();
130 list.add(2);
131 list.add(5);
132 extraBundle.putByteArray("key1", "value1".getBytes());
133 extraBundle.putChar("key2", 'c');
134 extraBundle.putIntegerArrayList("key3", list);
135
136 extras1.putAll(extraBundle);
137 extras2.putAll(extraBundle);
138 assertTrue(areEditorsSimilar(info1, info2));
139
140 extras2.putChar("key2", 'd');
141 assertFalse(areEditorsSimilar(info1, info2));
142
143 extras2.putChar("key2", 'c');
144 extras2.putInt("key4", 1);
145 assertFalse(areEditorsSimilar(info1, info2));
146 }
147}