1 /*
2  * Copyright (C) 2020 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.WindowInsets.Type.navigationBars;
20 import static android.view.WindowInsets.Type.systemBars;
21 import static android.view.WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS;
22 import static android.view.WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE;
23 
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.verifyZeroInteractions;
32 import static org.mockito.Mockito.when;
33 
34 import android.os.CancellationSignal;
35 import android.platform.test.annotations.Presubmit;
36 import android.view.WindowInsetsController.OnControllableInsetsChangedListener;
37 import android.view.animation.LinearInterpolator;
38 
39 import androidx.test.runner.AndroidJUnit4;
40 
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 
45 /**
46  * Tests for {@link PendingInsetsControllerTest}.
47  *
48  * <p>Build/Install/Run:
49  *  atest FrameworksCoreTests:PendingInsetsControllerTest
50  *
51  * <p>This test class is a part of Window Manager Service tests and specified in
52  * {@link com.android.server.wm.test.filters.FrameworksTestsFilter}.
53  */
54 @Presubmit
55 @RunWith(AndroidJUnit4.class)
56 public class PendingInsetsControllerTest {
57 
58     private PendingInsetsController mPendingInsetsController = new PendingInsetsController();
59     private InsetsController mReplayedController;
60 
61     @Before
setUp()62     public void setUp() {
63         mPendingInsetsController = new PendingInsetsController();
64         mReplayedController = mock(InsetsController.class);
65     }
66 
67     @Test
testShow()68     public void testShow() {
69         mPendingInsetsController.show(systemBars());
70         mPendingInsetsController.replayAndAttach(mReplayedController);
71         verify(mReplayedController).show(eq(systemBars()));
72     }
73 
74     @Test
testShow_direct()75     public void testShow_direct() {
76         mPendingInsetsController.replayAndAttach(mReplayedController);
77         mPendingInsetsController.show(systemBars());
78         verify(mReplayedController).show(eq(systemBars()));
79     }
80 
81     @Test
testHide()82     public void testHide() {
83         mPendingInsetsController.hide(systemBars());
84         mPendingInsetsController.replayAndAttach(mReplayedController);
85         verify(mReplayedController).hide(eq(systemBars()));
86     }
87 
88     @Test
testHide_direct()89     public void testHide_direct() {
90         mPendingInsetsController.replayAndAttach(mReplayedController);
91         mPendingInsetsController.hide(systemBars());
92         verify(mReplayedController).hide(eq(systemBars()));
93     }
94 
95     @Test
testControl()96     public void testControl() {
97         WindowInsetsAnimationControlListener listener =
98                 mock(WindowInsetsAnimationControlListener.class);
99         CancellationSignal cancellationSignal = new CancellationSignal();
100         mPendingInsetsController.controlWindowInsetsAnimation(
101                 systemBars(), 0, new LinearInterpolator(), cancellationSignal, listener);
102         verify(listener).onCancelled(null);
103         assertFalse(cancellationSignal.isCanceled());
104     }
105 
106     @Test
testControl_direct()107     public void testControl_direct() {
108         WindowInsetsAnimationControlListener listener =
109                 mock(WindowInsetsAnimationControlListener.class);
110         mPendingInsetsController.replayAndAttach(mReplayedController);
111         CancellationSignal cancellationSignal = new CancellationSignal();
112         mPendingInsetsController.controlWindowInsetsAnimation(systemBars(), 0L,
113                 new LinearInterpolator(), cancellationSignal, listener);
114         verify(mReplayedController).controlWindowInsetsAnimation(eq(systemBars()), eq(0L), any(),
115                 eq(cancellationSignal), eq(listener));
116     }
117 
118     @Test
testBehavior()119     public void testBehavior() {
120         mPendingInsetsController.setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
121         mPendingInsetsController.replayAndAttach(mReplayedController);
122         verify(mReplayedController).setSystemBarsBehavior(
123                 eq(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE));
124     }
125 
126     @Test
testBehavior_direct()127     public void testBehavior_direct() {
128         mPendingInsetsController.replayAndAttach(mReplayedController);
129         mPendingInsetsController.setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
130         verify(mReplayedController).setSystemBarsBehavior(
131                 eq(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE));
132     }
133 
134     @Test
testBehavior_direct_get()135     public void testBehavior_direct_get() {
136         when(mReplayedController.getSystemBarsBehavior())
137                 .thenReturn(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
138         mPendingInsetsController.replayAndAttach(mReplayedController);
139         assertEquals(mPendingInsetsController.getSystemBarsBehavior(),
140                 BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
141     }
142 
143     @Test
testAppearance()144     public void testAppearance() {
145         mPendingInsetsController.setSystemBarsAppearance(
146                 APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS);
147         mPendingInsetsController.replayAndAttach(mReplayedController);
148         verify(mReplayedController).setSystemBarsAppearance(eq(APPEARANCE_LIGHT_STATUS_BARS),
149                 eq(APPEARANCE_LIGHT_STATUS_BARS));
150     }
151 
152     @Test
testAppearance_direct()153     public void testAppearance_direct() {
154         mPendingInsetsController.replayAndAttach(mReplayedController);
155         mPendingInsetsController.setSystemBarsAppearance(
156                 APPEARANCE_LIGHT_STATUS_BARS, APPEARANCE_LIGHT_STATUS_BARS);
157         verify(mReplayedController).setSystemBarsAppearance(eq(APPEARANCE_LIGHT_STATUS_BARS),
158                 eq(APPEARANCE_LIGHT_STATUS_BARS));
159     }
160 
161     @Test
testAppearance_direct_get()162     public void testAppearance_direct_get() {
163         when(mReplayedController.getSystemBarsAppearance())
164                 .thenReturn(APPEARANCE_LIGHT_STATUS_BARS);
165         mPendingInsetsController.replayAndAttach(mReplayedController);
166         assertEquals(mPendingInsetsController.getSystemBarsAppearance(),
167                 APPEARANCE_LIGHT_STATUS_BARS);
168     }
169 
170     @Test
testAddOnControllableInsetsChangedListener()171     public void testAddOnControllableInsetsChangedListener() {
172         OnControllableInsetsChangedListener listener =
173                 mock(OnControllableInsetsChangedListener.class);
174         mPendingInsetsController.addOnControllableInsetsChangedListener(listener);
175         mPendingInsetsController.replayAndAttach(mReplayedController);
176         verify(mReplayedController).addOnControllableInsetsChangedListener(eq(listener));
177         verify(listener).onControllableInsetsChanged(eq(mPendingInsetsController), eq(0));
178     }
179 
180     @Test
testAddRemoveControllableInsetsChangedListener()181     public void testAddRemoveControllableInsetsChangedListener() {
182         OnControllableInsetsChangedListener listener =
183                 mock(OnControllableInsetsChangedListener.class);
184         mPendingInsetsController.addOnControllableInsetsChangedListener(listener);
185         mPendingInsetsController.removeOnControllableInsetsChangedListener(listener);
186         mPendingInsetsController.replayAndAttach(mReplayedController);
187         verify(mReplayedController, never()).addOnControllableInsetsChangedListener(any());
188         verify(listener).onControllableInsetsChanged(eq(mPendingInsetsController), eq(0));
189     }
190 
191     @Test
testAddOnControllableInsetsChangedListener_direct()192     public void testAddOnControllableInsetsChangedListener_direct() {
193         mPendingInsetsController.replayAndAttach(mReplayedController);
194         OnControllableInsetsChangedListener listener =
195                 mock(OnControllableInsetsChangedListener.class);
196         mPendingInsetsController.addOnControllableInsetsChangedListener(listener);
197         verify(mReplayedController).addOnControllableInsetsChangedListener(eq(listener));
198     }
199 
200     @Test
testReplayTwice()201     public void testReplayTwice() {
202         mPendingInsetsController.show(systemBars());
203         mPendingInsetsController.setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
204         mPendingInsetsController.setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS,
205                 APPEARANCE_LIGHT_STATUS_BARS);
206         mPendingInsetsController.addOnControllableInsetsChangedListener(
207                 (controller, typeMask) -> {});
208         mPendingInsetsController.replayAndAttach(mReplayedController);
209         InsetsController secondController = mock(InsetsController.class);
210         mPendingInsetsController.replayAndAttach(secondController);
211         verify(mReplayedController).show(eq(systemBars()));
212         verifyZeroInteractions(secondController);
213     }
214 
215     @Test
testDetachReattach()216     public void testDetachReattach() {
217         mPendingInsetsController.show(systemBars());
218         mPendingInsetsController.setSystemBarsBehavior(BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
219         mPendingInsetsController.setSystemBarsAppearance(APPEARANCE_LIGHT_STATUS_BARS,
220                 APPEARANCE_LIGHT_STATUS_BARS);
221         mPendingInsetsController.replayAndAttach(mReplayedController);
222         mPendingInsetsController.detach();
223         mPendingInsetsController.show(navigationBars());
224         InsetsController secondController = mock(InsetsController.class);
225         mPendingInsetsController.replayAndAttach(secondController);
226 
227         verify(mReplayedController).show(eq(systemBars()));
228         verify(secondController).show(eq(navigationBars()));
229     }
230 }
231