Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 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 android.view; |
| 18 | |
| 19 | import static android.view.ImeInsetsSourceConsumer.areEditorsSimilar; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 20 | import static android.view.InsetsState.ITYPE_IME; |
Jorim Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 21 | import static android.view.WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 22 | |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 23 | import static junit.framework.Assert.assertFalse; |
| 24 | import static junit.framework.Assert.assertTrue; |
| 25 | |
| 26 | import android.content.Context; |
| 27 | import android.graphics.Insets; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 28 | import android.graphics.Point; |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 29 | import android.graphics.Rect; |
| 30 | import android.os.Bundle; |
| 31 | import android.platform.test.annotations.Presubmit; |
| 32 | import android.view.SurfaceControl.Transaction; |
| 33 | import android.view.WindowManager.BadTokenException; |
| 34 | import android.view.WindowManager.LayoutParams; |
| 35 | import android.view.inputmethod.EditorInfo; |
| 36 | import android.widget.TextView; |
| 37 | |
| 38 | import androidx.test.InstrumentationRegistry; |
| 39 | import androidx.test.filters.FlakyTest; |
| 40 | import androidx.test.runner.AndroidJUnit4; |
| 41 | |
| 42 | import org.junit.Before; |
| 43 | import org.junit.Test; |
| 44 | import org.junit.runner.RunWith; |
| 45 | |
| 46 | import java.util.ArrayList; |
| 47 | |
| 48 | @Presubmit |
| 49 | @FlakyTest(detail = "Promote once confirmed non-flaky") |
| 50 | @RunWith(AndroidJUnit4.class) |
| 51 | public 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 Jaggi | 648e588 | 2019-01-24 13:24:02 +0100 | [diff] [blame] | 77 | rect, rect, SOFT_INPUT_ADJUST_RESIZE); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 78 | mImeConsumer = new ImeInsetsSourceConsumer( |
| 79 | new InsetsState(), Transaction::new, mController); |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | @Test |
| 84 | public void testImeVisibility() { |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 85 | final InsetsSourceControl ime = new InsetsSourceControl(ITYPE_IME, mLeash, new Point()); |
Tarandeep Singh | 2cbcd7f | 2019-01-25 11:47:57 -0800 | [diff] [blame] | 86 | 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 | } |