Adrian Roos | b849386 | 2018-11-14 06:50:55 -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 | |
Jorim Jaggi | 297985a | 2018-11-30 17:24:58 +0100 | [diff] [blame] | 19 | import static android.view.WindowInsets.Type.ime; |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 20 | import static android.view.WindowInsets.Type.navigationBars; |
| 21 | import static android.view.WindowInsets.Type.statusBars; |
| 22 | |
Jorim Jaggi | 297985a | 2018-11-30 17:24:58 +0100 | [diff] [blame] | 23 | import static org.junit.Assert.assertEquals; |
Jorim Jaggi | 9099079 | 2019-01-21 23:00:20 +0100 | [diff] [blame] | 24 | import static org.junit.Assert.assertFalse; |
Adrian Roos | b849386 | 2018-11-14 06:50:55 -0800 | [diff] [blame] | 25 | import static org.junit.Assert.assertTrue; |
| 26 | |
Jorim Jaggi | 297985a | 2018-11-30 17:24:58 +0100 | [diff] [blame] | 27 | import android.graphics.Insets; |
Adrian Roos | b849386 | 2018-11-14 06:50:55 -0800 | [diff] [blame] | 28 | import android.graphics.Rect; |
| 29 | import android.platform.test.annotations.Presubmit; |
Jorim Jaggi | 297985a | 2018-11-30 17:24:58 +0100 | [diff] [blame] | 30 | import android.view.WindowInsets.Builder; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 31 | |
| 32 | import androidx.test.filters.SmallTest; |
| 33 | import androidx.test.runner.AndroidJUnit4; |
Adrian Roos | b849386 | 2018-11-14 06:50:55 -0800 | [diff] [blame] | 34 | |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | |
| 38 | @RunWith(AndroidJUnit4.class) |
| 39 | @SmallTest |
| 40 | @Presubmit |
| 41 | public class WindowInsetsTest { |
| 42 | |
| 43 | @Test |
| 44 | public void systemWindowInsets_afterConsuming_isConsumed() { |
Jorim Jaggi | 297985a | 2018-11-30 17:24:58 +0100 | [diff] [blame] | 45 | assertTrue(new WindowInsets(new Rect(1, 2, 3, 4), null, false, false, null) |
Adrian Roos | b849386 | 2018-11-14 06:50:55 -0800 | [diff] [blame] | 46 | .consumeSystemWindowInsets().isConsumed()); |
| 47 | } |
| 48 | |
| 49 | @Test |
| 50 | public void multiNullConstructor_isConsumed() { |
Jorim Jaggi | 297985a | 2018-11-30 17:24:58 +0100 | [diff] [blame] | 51 | assertTrue(new WindowInsets((Rect) null, null, false, false, null).isConsumed()); |
Adrian Roos | b849386 | 2018-11-14 06:50:55 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Test |
| 55 | public void singleNullConstructor_isConsumed() { |
| 56 | assertTrue(new WindowInsets((Rect) null).isConsumed()); |
| 57 | } |
| 58 | |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 59 | // TODO: Move this to CTS once API made public |
Jorim Jaggi | 297985a | 2018-11-30 17:24:58 +0100 | [diff] [blame] | 60 | @Test |
| 61 | public void typeMap() { |
| 62 | Builder b = new WindowInsets.Builder(); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 63 | b.setInsets(navigationBars(), Insets.of(0, 0, 0, 100)); |
Jorim Jaggi | 297985a | 2018-11-30 17:24:58 +0100 | [diff] [blame] | 64 | b.setInsets(ime(), Insets.of(0, 0, 0, 300)); |
| 65 | WindowInsets insets = b.build(); |
| 66 | assertEquals(300, insets.getSystemWindowInsets().bottom); |
| 67 | } |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 68 | |
| 69 | // TODO: Move this to CTS once API made public |
| 70 | @Test |
| 71 | public void compatInsets() { |
| 72 | Builder b = new WindowInsets.Builder(); |
| 73 | b.setSystemWindowInsets(Insets.of(0, 50, 30, 10)); |
| 74 | WindowInsets insets = b.build(); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 75 | assertEquals(Insets.of(0, 50, 0, 0), insets.getInsets(statusBars())); |
| 76 | assertEquals(Insets.of(0, 0, 30, 10), insets.getInsets(navigationBars())); |
Jorim Jaggi | 73f3e8a | 2019-01-14 13:06:23 +0100 | [diff] [blame] | 77 | } |
Jorim Jaggi | 9099079 | 2019-01-21 23:00:20 +0100 | [diff] [blame] | 78 | |
| 79 | // TODO: Move this to CTS once API made public |
| 80 | @Test |
| 81 | public void visibility() { |
| 82 | Builder b = new WindowInsets.Builder(); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 83 | b.setInsets(navigationBars(), Insets.of(0, 0, 0, 100)); |
Jorim Jaggi | 9099079 | 2019-01-21 23:00:20 +0100 | [diff] [blame] | 84 | b.setInsets(ime(), Insets.of(0, 0, 0, 300)); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 85 | b.setVisible(navigationBars(), true); |
Jorim Jaggi | 9099079 | 2019-01-21 23:00:20 +0100 | [diff] [blame] | 86 | b.setVisible(ime(), true); |
| 87 | WindowInsets insets = b.build(); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 88 | assertTrue(insets.isVisible(navigationBars())); |
| 89 | assertTrue(insets.isVisible(navigationBars() | ime())); |
| 90 | assertFalse(insets.isVisible(navigationBars() | statusBars())); |
Jorim Jaggi | 9099079 | 2019-01-21 23:00:20 +0100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | // TODO: Move this to CTS once API made public |
| 94 | @Test |
| 95 | public void consume_doesntChangeVisibility() { |
| 96 | Builder b = new WindowInsets.Builder(); |
| 97 | b.setInsets(ime(), Insets.of(0, 0, 0, 300)); |
| 98 | b.setVisible(ime(), true); |
| 99 | WindowInsets insets = b.build(); |
| 100 | insets = insets.consumeSystemWindowInsets(); |
| 101 | assertTrue(insets.isVisible(ime())); |
| 102 | } |
Adrian Roos | b849386 | 2018-11-14 06:50:55 -0800 | [diff] [blame] | 103 | } |