Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +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 android.view; |
| 18 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 19 | import static android.view.InsetsState.ITYPE_STATUS_BAR; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 20 | |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 21 | import static junit.framework.TestCase.assertFalse; |
| 22 | import static junit.framework.TestCase.assertTrue; |
| 23 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 24 | import static org.mockito.ArgumentMatchers.eq; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 25 | import static org.mockito.Mockito.reset; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.verifyZeroInteractions; |
| 28 | |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 29 | import android.app.Instrumentation; |
| 30 | import android.content.Context; |
Tarandeep Singh | 215929b | 2019-01-11 18:24:37 -0800 | [diff] [blame] | 31 | import android.graphics.Point; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 32 | import android.platform.test.annotations.Presubmit; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 33 | import android.view.SurfaceControl.Transaction; |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 34 | import android.view.WindowManager.BadTokenException; |
| 35 | import android.view.WindowManager.LayoutParams; |
| 36 | import android.widget.TextView; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 37 | |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 38 | import androidx.test.InstrumentationRegistry; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 39 | import androidx.test.runner.AndroidJUnit4; |
| 40 | |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 41 | import org.junit.Before; |
| 42 | import org.junit.Test; |
| 43 | import org.junit.runner.RunWith; |
| 44 | import org.mockito.Mock; |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 45 | import org.mockito.Mockito; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 46 | import org.mockito.MockitoAnnotations; |
| 47 | |
Tadashi G. Takaoka | 82eb7a5 | 2019-03-26 18:22:01 +0900 | [diff] [blame] | 48 | /** |
| 49 | * Tests for {@link InsetsSourceConsumer}. |
| 50 | * |
| 51 | * <p>Build/Install/Run: |
| 52 | * atest FrameworksCoreTests:InsetsSourceConsumerTest |
| 53 | * |
| 54 | * <p>This test class is a part of Window Manager Service tests and specified in |
| 55 | * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}. |
| 56 | */ |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 57 | @Presubmit |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 58 | @RunWith(AndroidJUnit4.class) |
| 59 | public class InsetsSourceConsumerTest { |
| 60 | |
| 61 | private InsetsSourceConsumer mConsumer; |
| 62 | |
| 63 | private SurfaceSession mSession = new SurfaceSession(); |
| 64 | private SurfaceControl mLeash; |
| 65 | @Mock Transaction mMockTransaction; |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 66 | private InsetsSource mSpyInsetsSource; |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 67 | |
| 68 | @Before |
| 69 | public void setup() { |
| 70 | MockitoAnnotations.initMocks(this); |
| 71 | mLeash = new SurfaceControl.Builder(mSession) |
| 72 | .setName("testSurface") |
| 73 | .build(); |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 74 | final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation(); |
| 75 | instrumentation.runOnMainSync(() -> { |
| 76 | final Context context = instrumentation.getTargetContext(); |
| 77 | // cannot mock ViewRootImpl since it's final. |
| 78 | final ViewRootImpl viewRootImpl = new ViewRootImpl(context, context.getDisplay()); |
| 79 | try { |
| 80 | viewRootImpl.setView(new TextView(context), new LayoutParams(), null); |
| 81 | } catch (BadTokenException e) { |
| 82 | // activity isn't running, lets ignore BadTokenException. |
| 83 | } |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 84 | InsetsState state = new InsetsState(); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 85 | mSpyInsetsSource = Mockito.spy(new InsetsSource(ITYPE_STATUS_BAR)); |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 86 | state.addSource(mSpyInsetsSource); |
| 87 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 88 | mConsumer = new InsetsSourceConsumer(ITYPE_STATUS_BAR, state, |
Tarandeep Singh | 5c071ef | 2019-03-18 15:57:53 -0700 | [diff] [blame] | 89 | () -> mMockTransaction, new InsetsController(viewRootImpl)); |
| 90 | }); |
| 91 | instrumentation.waitForIdleSync(); |
| 92 | |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 93 | mConsumer.setControl(new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point())); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Test |
| 97 | public void testHide() { |
| 98 | mConsumer.hide(); |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 99 | assertFalse("Consumer should not be visible", mConsumer.isVisible()); |
| 100 | verify(mSpyInsetsSource).setVisible(eq(false)); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void testShow() { |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 105 | mConsumer.show(); |
Tarandeep Singh | 92d2dd3 | 2019-08-07 14:45:01 -0700 | [diff] [blame] | 106 | assertTrue("Consumer should be visible", mConsumer.isVisible()); |
| 107 | verify(mSpyInsetsSource).setVisible(eq(true)); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Test |
| 111 | public void testRestore() { |
| 112 | mConsumer.setControl(null); |
| 113 | reset(mMockTransaction); |
| 114 | mConsumer.hide(); |
| 115 | verifyZeroInteractions(mMockTransaction); |
Tiger Huang | 332793b | 2019-10-29 23:21:27 +0800 | [diff] [blame] | 116 | mConsumer.setControl(new InsetsSourceControl(ITYPE_STATUS_BAR, mLeash, new Point())); |
Jorim Jaggi | b603095 | 2018-10-23 18:31:52 +0200 | [diff] [blame] | 117 | verify(mMockTransaction).hide(eq(mLeash)); |
| 118 | } |
| 119 | } |