Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [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 com.android.server.wm; |
| 18 | |
| 19 | import static android.view.InsetsState.TYPE_TOP_BAR; |
| 20 | import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION; |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 21 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 22 | import static junit.framework.Assert.assertFalse; |
| 23 | import static junit.framework.Assert.assertNull; |
| 24 | import static junit.framework.Assert.assertTrue; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 25 | import static org.junit.Assert.assertEquals; |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 26 | import static org.junit.Assert.assertNotNull; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 27 | |
| 28 | import android.graphics.Insets; |
| 29 | import android.graphics.Rect; |
| 30 | import android.platform.test.annotations.Presubmit; |
| 31 | import android.view.InsetsSource; |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 32 | import android.view.InsetsState; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 33 | |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 34 | import org.junit.Before; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 35 | import androidx.test.filters.FlakyTest; |
| 36 | import androidx.test.filters.SmallTest; |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 37 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 38 | import org.junit.Before; |
Tadashi G. Takaoka | bf0d57b | 2018-11-19 16:09:58 +0900 | [diff] [blame] | 39 | import org.junit.Test; |
| 40 | |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 41 | @SmallTest |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 42 | @Presubmit |
| 43 | public class InsetsSourceProviderTest extends WindowTestsBase { |
| 44 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 45 | private InsetsSource mSource = new InsetsSource(TYPE_TOP_BAR); |
| 46 | private InsetsSourceProvider mProvider; |
| 47 | |
| 48 | @Before |
| 49 | public void setUp() throws Exception { |
| 50 | mSource.setVisible(true); |
| 51 | mProvider = new InsetsSourceProvider(mSource, |
| 52 | mDisplayContent.getInsetsStateController(), mDisplayContent); |
| 53 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 54 | |
| 55 | @Test |
| 56 | public void testPostLayout() { |
| 57 | final WindowState topBar = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 58 | topBar.getFrameLw().set(0, 0, 500, 100); |
| 59 | topBar.mHasSurface = true; |
| 60 | mProvider.setWindow(topBar, null); |
| 61 | mProvider.onPostLayout(); |
| 62 | assertEquals(new Rect(0, 0, 500, 100), mProvider.getSource().getFrame()); |
| 63 | assertEquals(Insets.of(0, 100, 0, 0), |
| 64 | mProvider.getSource().calculateInsets(new Rect(0, 0, 500, 500), |
| 65 | false /* ignoreVisibility */)); |
| 66 | } |
| 67 | |
| 68 | @Test |
| 69 | public void testPostLayout_invisible() { |
| 70 | final WindowState topBar = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 71 | topBar.getFrameLw().set(0, 0, 500, 100); |
| 72 | mProvider.setWindow(topBar, null); |
| 73 | mProvider.onPostLayout(); |
| 74 | assertEquals(Insets.NONE, mProvider.getSource().calculateInsets(new Rect(0, 0, 500, 500), |
| 75 | false /* ignoreVisibility */)); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testPostLayout_frameProvider() { |
| 80 | final WindowState topBar = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 81 | topBar.getFrameLw().set(0, 0, 500, 100); |
| 82 | mProvider.setWindow(topBar, |
| 83 | (displayFrames, windowState, rect) -> { |
| 84 | rect.set(10, 10, 20, 20); |
| 85 | }); |
| 86 | mProvider.onPostLayout(); |
| 87 | assertEquals(new Rect(10, 10, 20, 20), mProvider.getSource().getFrame()); |
| 88 | } |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 89 | |
| 90 | @Test |
| 91 | public void testUpdateControlForTarget() { |
| 92 | final WindowState topBar = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 93 | final WindowState target = createWindow(null, TYPE_APPLICATION, "target"); |
| 94 | topBar.getFrameLw().set(0, 0, 500, 100); |
| 95 | mProvider.setWindow(topBar, null); |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 96 | mProvider.updateControlForTarget(target, false /* force */); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 97 | assertNotNull(mProvider.getControl()); |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 98 | mProvider.updateControlForTarget(null, false /* force */); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 99 | assertNull(mProvider.getControl()); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void testInsetsModified() { |
| 104 | final WindowState topBar = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 105 | final WindowState target = createWindow(null, TYPE_APPLICATION, "target"); |
| 106 | topBar.getFrameLw().set(0, 0, 500, 100); |
| 107 | mProvider.setWindow(topBar, null); |
Jorim Jaggi | a2759b2 | 2019-01-24 13:21:40 +0100 | [diff] [blame] | 108 | mProvider.updateControlForTarget(target, false /* force */); |
Jorim Jaggi | e35c059 | 2018-11-06 16:21:08 +0100 | [diff] [blame] | 109 | InsetsState state = new InsetsState(); |
| 110 | state.getSource(TYPE_TOP_BAR).setVisible(false); |
| 111 | mProvider.onInsetsModified(target, state.getSource(TYPE_TOP_BAR)); |
| 112 | assertFalse(mSource.isVisible()); |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testInsetsModified_noControl() { |
| 117 | final WindowState topBar = createWindow(null, TYPE_APPLICATION, "parentWindow"); |
| 118 | final WindowState target = createWindow(null, TYPE_APPLICATION, "target"); |
| 119 | topBar.getFrameLw().set(0, 0, 500, 100); |
| 120 | mProvider.setWindow(topBar, null); |
| 121 | InsetsState state = new InsetsState(); |
| 122 | state.getSource(TYPE_TOP_BAR).setVisible(false); |
| 123 | mProvider.onInsetsModified(target, state.getSource(TYPE_TOP_BAR)); |
| 124 | assertTrue(mSource.isVisible()); |
| 125 | } |
Jorim Jaggi | f96c90a | 2018-09-26 16:55:15 +0200 | [diff] [blame] | 126 | } |