blob: 77ceeedae1acb51435d26bd976b6121922e73ad8 [file] [log] [blame]
Robert Carrf59b8dd2017-10-02 18:58:36 -07001/*
2 * Copyright (C) 2017 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
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070014 * limitations under the License.
Robert Carrf59b8dd2017-10-02 18:58:36 -070015 */
16
17package com.android.server.wm;
18
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090019import static com.android.dx.mockito.inline.extended.ExtendedMockito.mock;
20import static com.android.dx.mockito.inline.extended.ExtendedMockito.never;
21import static com.android.dx.mockito.inline.extended.ExtendedMockito.reset;
22import static com.android.dx.mockito.inline.extended.ExtendedMockito.spy;
23import static com.android.dx.mockito.inline.extended.ExtendedMockito.times;
24import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
Issei Suzuki8b995df2020-01-08 12:23:04 +010025import static com.android.server.wm.SurfaceAnimator.ANIMATION_TYPE_DIMMER;
Tadashi G. Takaokabf0d57b2018-11-19 16:09:58 +090026
chaviw2fb06bc2018-01-19 17:09:15 -080027import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertNotNull;
chaviw87ca63a2018-03-26 14:06:17 -070029import static org.mockito.ArgumentMatchers.any;
30import static org.mockito.ArgumentMatchers.anyBoolean;
Issei Suzuki8b995df2020-01-08 12:23:04 +010031import static org.mockito.ArgumentMatchers.eq;
Issei Suzuki2f541842020-01-09 20:18:29 +010032import static org.mockito.ArgumentMatchers.isNull;
33import static org.mockito.Mockito.never;
Robert Carr024a3492019-04-22 09:21:57 -070034import static org.mockito.Mockito.when;
Robert Carrf59b8dd2017-10-02 18:58:36 -070035
Issei Suzuki2f541842020-01-09 20:18:29 +010036import android.annotation.Nullable;
Robert Carrf59b8dd2017-10-02 18:58:36 -070037import android.graphics.Rect;
38import android.platform.test.annotations.Presubmit;
Robert Carrf59b8dd2017-10-02 18:58:36 -070039import android.view.SurfaceControl;
40import android.view.SurfaceSession;
41
Issei Suzuki8b995df2020-01-08 12:23:04 +010042import com.android.server.wm.SurfaceAnimator.AnimationType;
43import com.android.server.wm.SurfaceAnimator.OnAnimationFinishedCallback;
44
chaviw2fb06bc2018-01-19 17:09:15 -080045import org.junit.Before;
chaviw2fb06bc2018-01-19 17:09:15 -080046import org.junit.Test;
Louis Chang2453d062019-11-19 22:30:48 +080047import org.junit.runner.RunWith;
Robert Carrf59b8dd2017-10-02 18:58:36 -070048
49/**
50 * Build/Install/Run:
Andrii Kulianf5d14112019-10-31 17:37:54 -070051 * atest WmTests:DimmerTests
Robert Carrf59b8dd2017-10-02 18:58:36 -070052 */
53@Presubmit
Louis Chang2453d062019-11-19 22:30:48 +080054@RunWith(WindowTestRunner.class)
Robert Carrf59b8dd2017-10-02 18:58:36 -070055public class DimmerTests extends WindowTestsBase {
chaviw2fb06bc2018-01-19 17:09:15 -080056
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070057 private static class TestWindowContainer extends WindowContainer<TestWindowContainer> {
Robert Carrf59b8dd2017-10-02 18:58:36 -070058 final SurfaceControl mControl = mock(SurfaceControl.class);
chaviw619da692019-06-10 15:39:40 -070059 final SurfaceControl.Transaction mTransaction = spy(StubTransaction.class);
Robert Carrf59b8dd2017-10-02 18:58:36 -070060
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070061 TestWindowContainer(WindowManagerService wm) {
62 super(wm);
Jorim Jaggiffe128d2017-11-30 13:54:36 +010063 }
64
Robert Carrf59b8dd2017-10-02 18:58:36 -070065 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +010066 public SurfaceControl getSurfaceControl() {
Robert Carrf59b8dd2017-10-02 18:58:36 -070067 return mControl;
68 }
69
70 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +010071 public SurfaceControl.Transaction getPendingTransaction() {
Robert Carrf59b8dd2017-10-02 18:58:36 -070072 return mTransaction;
73 }
74 }
75
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070076 private static class MockSurfaceBuildingContainer extends WindowContainer<TestWindowContainer> {
Robert Carrf59b8dd2017-10-02 18:58:36 -070077 final SurfaceSession mSession = new SurfaceSession();
chaviwf20bd222018-02-01 16:06:52 -080078 final SurfaceControl mHostControl = mock(SurfaceControl.class);
chaviw619da692019-06-10 15:39:40 -070079 final SurfaceControl.Transaction mHostTransaction = spy(StubTransaction.class);
Robert Carrf59b8dd2017-10-02 18:58:36 -070080
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070081 MockSurfaceBuildingContainer(WindowManagerService wm) {
82 super(wm);
Jorim Jaggiffe128d2017-11-30 13:54:36 +010083 }
84
Robert Carrf59b8dd2017-10-02 18:58:36 -070085 class MockSurfaceBuilder extends SurfaceControl.Builder {
86 MockSurfaceBuilder(SurfaceSession ss) {
87 super(ss);
88 }
89
90 @Override
91 public SurfaceControl build() {
Robert Carr024a3492019-04-22 09:21:57 -070092 SurfaceControl mSc = mock(SurfaceControl.class);
93 when(mSc.isValid()).thenReturn(true);
94 return mSc;
Robert Carrf59b8dd2017-10-02 18:58:36 -070095 }
96 }
97
chaviw2fb06bc2018-01-19 17:09:15 -080098 @Override
Robert Carrf59b8dd2017-10-02 18:58:36 -070099 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
100 return new MockSurfaceBuilder(mSession);
101 }
102
103 @Override
chaviwf20bd222018-02-01 16:06:52 -0800104 public SurfaceControl getSurfaceControl() {
105 return mHostControl;
106 }
107
108 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +0100109 public SurfaceControl.Transaction getPendingTransaction() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700110 return mHostTransaction;
111 }
112 }
113
chaviwf20bd222018-02-01 16:06:52 -0800114 private MockSurfaceBuildingContainer mHost;
115 private Dimmer mDimmer;
116 private SurfaceControl.Transaction mTransaction;
chaviw87ca63a2018-03-26 14:06:17 -0700117 private Dimmer.SurfaceAnimatorStarter mSurfaceAnimatorStarter;
118
119 private static class SurfaceAnimatorStarterImpl implements Dimmer.SurfaceAnimatorStarter {
120 @Override
121 public void startAnimation(SurfaceAnimator surfaceAnimator, SurfaceControl.Transaction t,
Issei Suzuki8b995df2020-01-08 12:23:04 +0100122 AnimationAdapter anim, boolean hidden, @AnimationType int type,
123 @Nullable OnAnimationFinishedCallback animationFinishedCallback) {
124 surfaceAnimator.mStaticAnimationFinishedCallback.onAnimationFinished(type, anim);
Issei Suzuki2f541842020-01-09 20:18:29 +0100125 if (animationFinishedCallback != null) {
Issei Suzuki8b995df2020-01-08 12:23:04 +0100126 animationFinishedCallback.onAnimationFinished(type, anim);
Issei Suzuki2f541842020-01-09 20:18:29 +0100127 }
chaviw87ca63a2018-03-26 14:06:17 -0700128 }
129 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700130
131 @Before
132 public void setUp() throws Exception {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700133 mHost = new MockSurfaceBuildingContainer(mWm);
chaviw87ca63a2018-03-26 14:06:17 -0700134 mSurfaceAnimatorStarter = spy(new SurfaceAnimatorStarterImpl());
chaviw619da692019-06-10 15:39:40 -0700135 mTransaction = spy(StubTransaction.class);
chaviw87ca63a2018-03-26 14:06:17 -0700136 mDimmer = new Dimmer(mHost, mSurfaceAnimatorStarter);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700137 }
138
139 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700140 public void testDimAboveNoChildCreatesSurface() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700141 final float alpha = 0.8f;
142 mDimmer.dimAbove(mTransaction, alpha);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700143
chaviwf20bd222018-02-01 16:06:52 -0800144 SurfaceControl dimLayer = getDimLayer();
chaviw2fb06bc2018-01-19 17:09:15 -0800145
146 assertNotNull("Dimmer should have created a surface", dimLayer);
147
148 verify(mTransaction).setAlpha(dimLayer, alpha);
149 verify(mTransaction).setLayer(dimLayer, Integer.MAX_VALUE);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700150 }
151
152 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700153 public void testDimAboveNoChildRedundantlyUpdatesAlphaOnExistingSurface() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700154 float alpha = 0.8f;
155 mDimmer.dimAbove(mTransaction, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800156 final SurfaceControl firstSurface = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700157
158 alpha = 0.9f;
159 mDimmer.dimAbove(mTransaction, alpha);
160
chaviwf20bd222018-02-01 16:06:52 -0800161 assertEquals(firstSurface, getDimLayer());
Robert Carrf59b8dd2017-10-02 18:58:36 -0700162 verify(mTransaction).setAlpha(firstSurface, 0.9f);
163 }
164
165 @Test
Vishnu Naire86bd982018-11-28 13:23:17 -0800166 public void testUpdateDimsAppliesCrop() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700167 mDimmer.dimAbove(mTransaction, 0.8f);
168
169 int width = 100;
170 int height = 300;
171 Rect bounds = new Rect(0, 0, width, height);
172 mDimmer.updateDims(mTransaction, bounds);
chaviw2fb06bc2018-01-19 17:09:15 -0800173
Vishnu Naire86bd982018-11-28 13:23:17 -0800174 verify(mTransaction).setWindowCrop(getDimLayer(), width, height);
chaviwf20bd222018-02-01 16:06:52 -0800175 verify(mTransaction).show(getDimLayer());
Robert Carrf59b8dd2017-10-02 18:58:36 -0700176 }
177
178 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700179 public void testDimAboveNoChildNotReset() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700180 mDimmer.dimAbove(mTransaction, 0.8f);
chaviwf20bd222018-02-01 16:06:52 -0800181 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700182 mDimmer.resetDimStates();
183
184 mDimmer.updateDims(mTransaction, new Rect());
chaviwf20bd222018-02-01 16:06:52 -0800185 verify(mTransaction).show(getDimLayer());
chaviw9f6171e2019-06-07 16:33:50 -0700186 verify(mTransaction, never()).remove(dimLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700187 }
188
189 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700190 public void testDimAboveWithChildCreatesSurfaceAboveChild() {
191 TestWindowContainer child = new TestWindowContainer(mWm);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700192 mHost.addChild(child, 0);
193
194 final float alpha = 0.8f;
195 mDimmer.dimAbove(mTransaction, child, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800196 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700197
chaviwf20bd222018-02-01 16:06:52 -0800198 assertNotNull("Dimmer should have created a surface", dimLayer);
chaviw2fb06bc2018-01-19 17:09:15 -0800199
chaviwf20bd222018-02-01 16:06:52 -0800200 verify(mTransaction).setAlpha(dimLayer, alpha);
201 verify(mTransaction).setRelativeLayer(dimLayer, child.mControl, 1);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700202 }
203
204 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700205 public void testDimBelowWithChildSurfaceCreatesSurfaceBelowChild() {
206 TestWindowContainer child = new TestWindowContainer(mWm);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700207 mHost.addChild(child, 0);
208
209 final float alpha = 0.8f;
210 mDimmer.dimBelow(mTransaction, child, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800211 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700212
chaviwf20bd222018-02-01 16:06:52 -0800213 assertNotNull("Dimmer should have created a surface", dimLayer);
chaviw2fb06bc2018-01-19 17:09:15 -0800214
chaviwf20bd222018-02-01 16:06:52 -0800215 verify(mTransaction).setAlpha(dimLayer, alpha);
216 verify(mTransaction).setRelativeLayer(dimLayer, child.mControl, -1);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700217 }
218
219 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700220 public void testDimBelowWithChildSurfaceDestroyedWhenReset() {
221 TestWindowContainer child = new TestWindowContainer(mWm);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700222 mHost.addChild(child, 0);
223
224 final float alpha = 0.8f;
225 mDimmer.dimAbove(mTransaction, child, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800226 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700227 mDimmer.resetDimStates();
chaviw2fb06bc2018-01-19 17:09:15 -0800228
Robert Carrf59b8dd2017-10-02 18:58:36 -0700229 mDimmer.updateDims(mTransaction, new Rect());
chaviw87ca63a2018-03-26 14:06:17 -0700230 verify(mSurfaceAnimatorStarter).startAnimation(any(SurfaceAnimator.class), any(
Issei Suzuki2f541842020-01-09 20:18:29 +0100231 SurfaceControl.Transaction.class), any(AnimationAdapter.class), anyBoolean(),
Issei Suzuki8b995df2020-01-08 12:23:04 +0100232 eq(ANIMATION_TYPE_DIMMER), isNull());
Robert Carr71200f22019-02-05 09:44:53 -0800233 verify(mHost.getPendingTransaction()).remove(dimLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700234 }
235
236 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700237 public void testDimBelowWithChildSurfaceNotDestroyedWhenPersisted() {
238 TestWindowContainer child = new TestWindowContainer(mWm);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700239 mHost.addChild(child, 0);
240
241 final float alpha = 0.8f;
242 mDimmer.dimAbove(mTransaction, child, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800243 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700244 mDimmer.resetDimStates();
245 mDimmer.dimAbove(mTransaction, child, alpha);
246
247 mDimmer.updateDims(mTransaction, new Rect());
chaviw2fb06bc2018-01-19 17:09:15 -0800248 verify(mTransaction).show(dimLayer);
chaviw9f6171e2019-06-07 16:33:50 -0700249 verify(mTransaction, never()).remove(dimLayer);
chaviw2fb06bc2018-01-19 17:09:15 -0800250 }
251
chaviwf20bd222018-02-01 16:06:52 -0800252 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700253 public void testDimUpdateWhileDimming() {
chaviwf20bd222018-02-01 16:06:52 -0800254 Rect bounds = new Rect();
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700255 TestWindowContainer child = new TestWindowContainer(mWm);
chaviwf20bd222018-02-01 16:06:52 -0800256 mHost.addChild(child, 0);
257
258 final float alpha = 0.8f;
259 mDimmer.dimAbove(mTransaction, child, alpha);
260
261 SurfaceControl dimLayer = getDimLayer();
262 bounds.set(0, 0, 10, 10);
263 mDimmer.updateDims(mTransaction, bounds);
Vishnu Naire86bd982018-11-28 13:23:17 -0800264 verify(mTransaction).setWindowCrop(dimLayer, bounds.width(), bounds.height());
chaviwf20bd222018-02-01 16:06:52 -0800265 verify(mTransaction, times(1)).show(dimLayer);
chaviwf20bd222018-02-01 16:06:52 -0800266 verify(mTransaction).setPosition(dimLayer, 0, 0);
267
268 bounds.set(10, 10, 30, 30);
269 mDimmer.updateDims(mTransaction, bounds);
Vishnu Naire86bd982018-11-28 13:23:17 -0800270 verify(mTransaction).setWindowCrop(dimLayer, bounds.width(), bounds.height());
chaviwf20bd222018-02-01 16:06:52 -0800271 verify(mTransaction).setPosition(dimLayer, 10, 10);
272 }
273
chaviw87ca63a2018-03-26 14:06:17 -0700274 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700275 public void testRemoveDimImmediately() {
276 TestWindowContainer child = new TestWindowContainer(mWm);
chaviw87ca63a2018-03-26 14:06:17 -0700277 mHost.addChild(child, 0);
278
279 mDimmer.dimAbove(mTransaction, child, 1);
280 SurfaceControl dimLayer = getDimLayer();
281 mDimmer.updateDims(mTransaction, new Rect());
282 verify(mTransaction, times(1)).show(dimLayer);
283
284 reset(mSurfaceAnimatorStarter);
285 mDimmer.dontAnimateExit();
286 mDimmer.resetDimStates();
287 mDimmer.updateDims(mTransaction, new Rect());
288 verify(mSurfaceAnimatorStarter, never()).startAnimation(any(SurfaceAnimator.class), any(
Issei Suzuki2f541842020-01-09 20:18:29 +0100289 SurfaceControl.Transaction.class), any(AnimationAdapter.class), anyBoolean(),
Issei Suzuki8b995df2020-01-08 12:23:04 +0100290 eq(ANIMATION_TYPE_DIMMER), isNull());
Robert Carr71200f22019-02-05 09:44:53 -0800291 verify(mTransaction).remove(dimLayer);
chaviw87ca63a2018-03-26 14:06:17 -0700292 }
293
chaviwf20bd222018-02-01 16:06:52 -0800294 private SurfaceControl getDimLayer() {
295 return mDimmer.mDimState.mDimLayer;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700296 }
297}