blob: 292a05bd966a6092a2b77fc502158c8482824b76 [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;
25
chaviw2fb06bc2018-01-19 17:09:15 -080026import static org.junit.Assert.assertEquals;
27import static org.junit.Assert.assertNotNull;
chaviw87ca63a2018-03-26 14:06:17 -070028import static org.mockito.ArgumentMatchers.any;
29import static org.mockito.ArgumentMatchers.anyBoolean;
Robert Carr024a3492019-04-22 09:21:57 -070030import static org.mockito.Mockito.when;
Robert Carrf59b8dd2017-10-02 18:58:36 -070031
32import android.graphics.Rect;
33import android.platform.test.annotations.Presubmit;
Robert Carrf59b8dd2017-10-02 18:58:36 -070034import android.view.SurfaceControl;
35import android.view.SurfaceSession;
36
chaviw2fb06bc2018-01-19 17:09:15 -080037import org.junit.Before;
chaviw2fb06bc2018-01-19 17:09:15 -080038import org.junit.Test;
Robert Carrf59b8dd2017-10-02 18:58:36 -070039
40/**
41 * Build/Install/Run:
chaviw78a3d4692018-11-13 18:06:56 -080042 * atest FrameworksServicesTests:DimmerTests
Robert Carrf59b8dd2017-10-02 18:58:36 -070043 */
44@Presubmit
Robert Carrf59b8dd2017-10-02 18:58:36 -070045public class DimmerTests extends WindowTestsBase {
chaviw2fb06bc2018-01-19 17:09:15 -080046
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070047 private static class TestWindowContainer extends WindowContainer<TestWindowContainer> {
Robert Carrf59b8dd2017-10-02 18:58:36 -070048 final SurfaceControl mControl = mock(SurfaceControl.class);
49 final SurfaceControl.Transaction mTransaction = mock(SurfaceControl.Transaction.class);
50
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070051 TestWindowContainer(WindowManagerService wm) {
52 super(wm);
Jorim Jaggiffe128d2017-11-30 13:54:36 +010053 }
54
Robert Carrf59b8dd2017-10-02 18:58:36 -070055 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +010056 public SurfaceControl getSurfaceControl() {
Robert Carrf59b8dd2017-10-02 18:58:36 -070057 return mControl;
58 }
59
60 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +010061 public SurfaceControl.Transaction getPendingTransaction() {
Robert Carrf59b8dd2017-10-02 18:58:36 -070062 return mTransaction;
63 }
64 }
65
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070066 private static class MockSurfaceBuildingContainer extends WindowContainer<TestWindowContainer> {
Robert Carrf59b8dd2017-10-02 18:58:36 -070067 final SurfaceSession mSession = new SurfaceSession();
chaviwf20bd222018-02-01 16:06:52 -080068 final SurfaceControl mHostControl = mock(SurfaceControl.class);
Robert Carrf59b8dd2017-10-02 18:58:36 -070069 final SurfaceControl.Transaction mHostTransaction = mock(SurfaceControl.Transaction.class);
70
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -070071 MockSurfaceBuildingContainer(WindowManagerService wm) {
72 super(wm);
Jorim Jaggiffe128d2017-11-30 13:54:36 +010073 }
74
Robert Carrf59b8dd2017-10-02 18:58:36 -070075 class MockSurfaceBuilder extends SurfaceControl.Builder {
76 MockSurfaceBuilder(SurfaceSession ss) {
77 super(ss);
78 }
79
80 @Override
81 public SurfaceControl build() {
Robert Carr024a3492019-04-22 09:21:57 -070082 SurfaceControl mSc = mock(SurfaceControl.class);
83 when(mSc.isValid()).thenReturn(true);
84 return mSc;
Robert Carrf59b8dd2017-10-02 18:58:36 -070085 }
86 }
87
chaviw2fb06bc2018-01-19 17:09:15 -080088 @Override
Robert Carrf59b8dd2017-10-02 18:58:36 -070089 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
90 return new MockSurfaceBuilder(mSession);
91 }
92
93 @Override
chaviwf20bd222018-02-01 16:06:52 -080094 public SurfaceControl getSurfaceControl() {
95 return mHostControl;
96 }
97
98 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +010099 public SurfaceControl.Transaction getPendingTransaction() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700100 return mHostTransaction;
101 }
102 }
103
chaviwf20bd222018-02-01 16:06:52 -0800104 private MockSurfaceBuildingContainer mHost;
105 private Dimmer mDimmer;
106 private SurfaceControl.Transaction mTransaction;
chaviw87ca63a2018-03-26 14:06:17 -0700107 private Dimmer.SurfaceAnimatorStarter mSurfaceAnimatorStarter;
108
109 private static class SurfaceAnimatorStarterImpl implements Dimmer.SurfaceAnimatorStarter {
110 @Override
111 public void startAnimation(SurfaceAnimator surfaceAnimator, SurfaceControl.Transaction t,
112 AnimationAdapter anim, boolean hidden) {
113 surfaceAnimator.mAnimationFinishedCallback.run();
114 }
115 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700116
117 @Before
118 public void setUp() throws Exception {
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700119 mHost = new MockSurfaceBuildingContainer(mWm);
chaviw87ca63a2018-03-26 14:06:17 -0700120 mSurfaceAnimatorStarter = spy(new SurfaceAnimatorStarterImpl());
Robert Carrf59b8dd2017-10-02 18:58:36 -0700121 mTransaction = mock(SurfaceControl.Transaction.class);
chaviw87ca63a2018-03-26 14:06:17 -0700122 mDimmer = new Dimmer(mHost, mSurfaceAnimatorStarter);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700123 }
124
125 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700126 public void testDimAboveNoChildCreatesSurface() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700127 final float alpha = 0.8f;
128 mDimmer.dimAbove(mTransaction, alpha);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700129
chaviwf20bd222018-02-01 16:06:52 -0800130 SurfaceControl dimLayer = getDimLayer();
chaviw2fb06bc2018-01-19 17:09:15 -0800131
132 assertNotNull("Dimmer should have created a surface", dimLayer);
133
134 verify(mTransaction).setAlpha(dimLayer, alpha);
135 verify(mTransaction).setLayer(dimLayer, Integer.MAX_VALUE);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700136 }
137
138 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700139 public void testDimAboveNoChildRedundantlyUpdatesAlphaOnExistingSurface() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700140 float alpha = 0.8f;
141 mDimmer.dimAbove(mTransaction, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800142 final SurfaceControl firstSurface = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700143
144 alpha = 0.9f;
145 mDimmer.dimAbove(mTransaction, alpha);
146
chaviwf20bd222018-02-01 16:06:52 -0800147 assertEquals(firstSurface, getDimLayer());
Robert Carrf59b8dd2017-10-02 18:58:36 -0700148 verify(mTransaction).setAlpha(firstSurface, 0.9f);
149 }
150
151 @Test
Vishnu Naire86bd982018-11-28 13:23:17 -0800152 public void testUpdateDimsAppliesCrop() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700153 mDimmer.dimAbove(mTransaction, 0.8f);
154
155 int width = 100;
156 int height = 300;
157 Rect bounds = new Rect(0, 0, width, height);
158 mDimmer.updateDims(mTransaction, bounds);
chaviw2fb06bc2018-01-19 17:09:15 -0800159
Vishnu Naire86bd982018-11-28 13:23:17 -0800160 verify(mTransaction).setWindowCrop(getDimLayer(), width, height);
chaviwf20bd222018-02-01 16:06:52 -0800161 verify(mTransaction).show(getDimLayer());
Robert Carrf59b8dd2017-10-02 18:58:36 -0700162 }
163
164 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700165 public void testDimAboveNoChildNotReset() {
Robert Carrf59b8dd2017-10-02 18:58:36 -0700166 mDimmer.dimAbove(mTransaction, 0.8f);
chaviwf20bd222018-02-01 16:06:52 -0800167 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700168 mDimmer.resetDimStates();
169
170 mDimmer.updateDims(mTransaction, new Rect());
chaviwf20bd222018-02-01 16:06:52 -0800171 verify(mTransaction).show(getDimLayer());
Robert Carr5ea304d2019-02-04 16:04:55 -0800172 verify(dimLayer, never()).remove();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700173 }
174
175 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700176 public void testDimAboveWithChildCreatesSurfaceAboveChild() {
177 TestWindowContainer child = new TestWindowContainer(mWm);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700178 mHost.addChild(child, 0);
179
180 final float alpha = 0.8f;
181 mDimmer.dimAbove(mTransaction, child, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800182 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700183
chaviwf20bd222018-02-01 16:06:52 -0800184 assertNotNull("Dimmer should have created a surface", dimLayer);
chaviw2fb06bc2018-01-19 17:09:15 -0800185
chaviwf20bd222018-02-01 16:06:52 -0800186 verify(mTransaction).setAlpha(dimLayer, alpha);
187 verify(mTransaction).setRelativeLayer(dimLayer, child.mControl, 1);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700188 }
189
190 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700191 public void testDimBelowWithChildSurfaceCreatesSurfaceBelowChild() {
192 TestWindowContainer child = new TestWindowContainer(mWm);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700193 mHost.addChild(child, 0);
194
195 final float alpha = 0.8f;
196 mDimmer.dimBelow(mTransaction, child, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800197 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700198
chaviwf20bd222018-02-01 16:06:52 -0800199 assertNotNull("Dimmer should have created a surface", dimLayer);
chaviw2fb06bc2018-01-19 17:09:15 -0800200
chaviwf20bd222018-02-01 16:06:52 -0800201 verify(mTransaction).setAlpha(dimLayer, alpha);
202 verify(mTransaction).setRelativeLayer(dimLayer, child.mControl, -1);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700203 }
204
205 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700206 public void testDimBelowWithChildSurfaceDestroyedWhenReset() {
207 TestWindowContainer child = new TestWindowContainer(mWm);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700208 mHost.addChild(child, 0);
209
210 final float alpha = 0.8f;
211 mDimmer.dimAbove(mTransaction, child, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800212 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700213 mDimmer.resetDimStates();
chaviw2fb06bc2018-01-19 17:09:15 -0800214
Robert Carrf59b8dd2017-10-02 18:58:36 -0700215 mDimmer.updateDims(mTransaction, new Rect());
chaviw87ca63a2018-03-26 14:06:17 -0700216 verify(mSurfaceAnimatorStarter).startAnimation(any(SurfaceAnimator.class), any(
217 SurfaceControl.Transaction.class), any(AnimationAdapter.class), anyBoolean());
Robert Carr71200f22019-02-05 09:44:53 -0800218 verify(mHost.getPendingTransaction()).remove(dimLayer);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700219 }
220
221 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700222 public void testDimBelowWithChildSurfaceNotDestroyedWhenPersisted() {
223 TestWindowContainer child = new TestWindowContainer(mWm);
Robert Carrf59b8dd2017-10-02 18:58:36 -0700224 mHost.addChild(child, 0);
225
226 final float alpha = 0.8f;
227 mDimmer.dimAbove(mTransaction, child, alpha);
chaviwf20bd222018-02-01 16:06:52 -0800228 SurfaceControl dimLayer = getDimLayer();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700229 mDimmer.resetDimStates();
230 mDimmer.dimAbove(mTransaction, child, alpha);
231
232 mDimmer.updateDims(mTransaction, new Rect());
chaviw2fb06bc2018-01-19 17:09:15 -0800233 verify(mTransaction).show(dimLayer);
Robert Carr5ea304d2019-02-04 16:04:55 -0800234 verify(dimLayer, never()).remove();
chaviw2fb06bc2018-01-19 17:09:15 -0800235 }
236
chaviwf20bd222018-02-01 16:06:52 -0800237 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700238 public void testDimUpdateWhileDimming() {
chaviwf20bd222018-02-01 16:06:52 -0800239 Rect bounds = new Rect();
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700240 TestWindowContainer child = new TestWindowContainer(mWm);
chaviwf20bd222018-02-01 16:06:52 -0800241 mHost.addChild(child, 0);
242
243 final float alpha = 0.8f;
244 mDimmer.dimAbove(mTransaction, child, alpha);
245
246 SurfaceControl dimLayer = getDimLayer();
247 bounds.set(0, 0, 10, 10);
248 mDimmer.updateDims(mTransaction, bounds);
Vishnu Naire86bd982018-11-28 13:23:17 -0800249 verify(mTransaction).setWindowCrop(dimLayer, bounds.width(), bounds.height());
chaviwf20bd222018-02-01 16:06:52 -0800250 verify(mTransaction, times(1)).show(dimLayer);
chaviwf20bd222018-02-01 16:06:52 -0800251 verify(mTransaction).setPosition(dimLayer, 0, 0);
252
253 bounds.set(10, 10, 30, 30);
254 mDimmer.updateDims(mTransaction, bounds);
Vishnu Naire86bd982018-11-28 13:23:17 -0800255 verify(mTransaction).setWindowCrop(dimLayer, bounds.width(), bounds.height());
chaviwf20bd222018-02-01 16:06:52 -0800256 verify(mTransaction).setPosition(dimLayer, 10, 10);
257 }
258
chaviw87ca63a2018-03-26 14:06:17 -0700259 @Test
Tadashi G. Takaokab6e148c2018-11-03 02:59:06 -0700260 public void testRemoveDimImmediately() {
261 TestWindowContainer child = new TestWindowContainer(mWm);
chaviw87ca63a2018-03-26 14:06:17 -0700262 mHost.addChild(child, 0);
263
264 mDimmer.dimAbove(mTransaction, child, 1);
265 SurfaceControl dimLayer = getDimLayer();
266 mDimmer.updateDims(mTransaction, new Rect());
267 verify(mTransaction, times(1)).show(dimLayer);
268
269 reset(mSurfaceAnimatorStarter);
270 mDimmer.dontAnimateExit();
271 mDimmer.resetDimStates();
272 mDimmer.updateDims(mTransaction, new Rect());
273 verify(mSurfaceAnimatorStarter, never()).startAnimation(any(SurfaceAnimator.class), any(
274 SurfaceControl.Transaction.class), any(AnimationAdapter.class), anyBoolean());
Robert Carr71200f22019-02-05 09:44:53 -0800275 verify(mTransaction).remove(dimLayer);
chaviw87ca63a2018-03-26 14:06:17 -0700276 }
277
chaviwf20bd222018-02-01 16:06:52 -0800278 private SurfaceControl getDimLayer() {
279 return mDimmer.mDimState.mDimLayer;
Robert Carrf59b8dd2017-10-02 18:58:36 -0700280 }
281}