blob: 6769e40dca783f0d9a1d3f0a0fec1977b98c00e0 [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
14 * limitations under the License
15 */
16
17package com.android.server.wm;
18
chaviw2fb06bc2018-01-19 17:09:15 -080019import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertNotNull;
chaviw87ca63a2018-03-26 14:06:17 -070021import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.ArgumentMatchers.anyBoolean;
chaviw2fb06bc2018-01-19 17:09:15 -080023import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.never;
chaviw87ca63a2018-03-26 14:06:17 -070025import static org.mockito.Mockito.reset;
26import static org.mockito.Mockito.spy;
chaviwf20bd222018-02-01 16:06:52 -080027import static org.mockito.Mockito.times;
chaviw2fb06bc2018-01-19 17:09:15 -080028import static org.mockito.Mockito.verify;
Robert Carrf59b8dd2017-10-02 18:58:36 -070029
30import android.graphics.Rect;
31import android.platform.test.annotations.Presubmit;
32import android.support.test.runner.AndroidJUnit4;
33import android.view.SurfaceControl;
34import android.view.SurfaceSession;
35
chaviw2fb06bc2018-01-19 17:09:15 -080036import org.junit.Before;
chaviw2fb06bc2018-01-19 17:09:15 -080037import org.junit.Test;
38import org.junit.runner.RunWith;
Robert Carrf59b8dd2017-10-02 18:58:36 -070039
40/**
41 * Build/Install/Run:
chaviw2fb06bc2018-01-19 17:09:15 -080042 * atest FrameworksServicesTests:com.android.server.wm.DimmerTests;
Robert Carrf59b8dd2017-10-02 18:58:36 -070043 */
44@Presubmit
45@RunWith(AndroidJUnit4.class)
46public class DimmerTests extends WindowTestsBase {
chaviw2fb06bc2018-01-19 17:09:15 -080047
Robert Carrf59b8dd2017-10-02 18:58:36 -070048 private class TestWindowContainer extends WindowContainer<TestWindowContainer> {
49 final SurfaceControl mControl = mock(SurfaceControl.class);
50 final SurfaceControl.Transaction mTransaction = mock(SurfaceControl.Transaction.class);
51
Jorim Jaggiffe128d2017-11-30 13:54:36 +010052 TestWindowContainer() {
53 super(sWm);
54 }
55
Robert Carrf59b8dd2017-10-02 18:58:36 -070056 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +010057 public SurfaceControl getSurfaceControl() {
Robert Carrf59b8dd2017-10-02 18:58:36 -070058 return mControl;
59 }
60
61 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +010062 public SurfaceControl.Transaction getPendingTransaction() {
Robert Carrf59b8dd2017-10-02 18:58:36 -070063 return mTransaction;
64 }
65 }
66
67 private class MockSurfaceBuildingContainer extends WindowContainer<TestWindowContainer> {
68 final SurfaceSession mSession = new SurfaceSession();
chaviwf20bd222018-02-01 16:06:52 -080069 final SurfaceControl mHostControl = mock(SurfaceControl.class);
Robert Carrf59b8dd2017-10-02 18:58:36 -070070 final SurfaceControl.Transaction mHostTransaction = mock(SurfaceControl.Transaction.class);
71
Jorim Jaggiffe128d2017-11-30 13:54:36 +010072 MockSurfaceBuildingContainer() {
73 super(sWm);
74 }
75
Robert Carrf59b8dd2017-10-02 18:58:36 -070076 class MockSurfaceBuilder extends SurfaceControl.Builder {
77 MockSurfaceBuilder(SurfaceSession ss) {
78 super(ss);
79 }
80
81 @Override
82 public SurfaceControl build() {
chaviwf20bd222018-02-01 16:06:52 -080083 return mock(SurfaceControl.class);
Robert Carrf59b8dd2017-10-02 18:58:36 -070084 }
85 }
86
chaviw2fb06bc2018-01-19 17:09:15 -080087 @Override
Robert Carrf59b8dd2017-10-02 18:58:36 -070088 SurfaceControl.Builder makeChildSurface(WindowContainer child) {
89 return new MockSurfaceBuilder(mSession);
90 }
91
92 @Override
chaviwf20bd222018-02-01 16:06:52 -080093 public SurfaceControl getSurfaceControl() {
94 return mHostControl;
95 }
96
97 @Override
Jorim Jaggia5e10572017-11-15 14:36:26 +010098 public SurfaceControl.Transaction getPendingTransaction() {
Robert Carrf59b8dd2017-10-02 18:58:36 -070099 return mHostTransaction;
100 }
101 }
102
chaviwf20bd222018-02-01 16:06:52 -0800103 private MockSurfaceBuildingContainer mHost;
104 private Dimmer mDimmer;
105 private SurfaceControl.Transaction mTransaction;
chaviw87ca63a2018-03-26 14:06:17 -0700106 private Dimmer.SurfaceAnimatorStarter mSurfaceAnimatorStarter;
107
108 private static class SurfaceAnimatorStarterImpl implements Dimmer.SurfaceAnimatorStarter {
109 @Override
110 public void startAnimation(SurfaceAnimator surfaceAnimator, SurfaceControl.Transaction t,
111 AnimationAdapter anim, boolean hidden) {
112 surfaceAnimator.mAnimationFinishedCallback.run();
113 }
114 }
Robert Carrf59b8dd2017-10-02 18:58:36 -0700115
116 @Before
117 public void setUp() throws Exception {
118 super.setUp();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700119 mHost = new MockSurfaceBuildingContainer();
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
126 public void testDimAboveNoChildCreatesSurface() throws Exception {
127 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
139 public void testDimAboveNoChildRedundantlyUpdatesAlphaOnExistingSurface() throws Exception {
140 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
152 public void testUpdateDimsAppliesSize() throws Exception {
153 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
chaviwf20bd222018-02-01 16:06:52 -0800160 verify(mTransaction).setSize(getDimLayer(), width, height);
161 verify(mTransaction).show(getDimLayer());
Robert Carrf59b8dd2017-10-02 18:58:36 -0700162 }
163
164 @Test
165 public void testDimAboveNoChildNotReset() throws Exception {
166 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());
chaviw2fb06bc2018-01-19 17:09:15 -0800172 verify(dimLayer, never()).destroy();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700173 }
174
175 @Test
176 public void testDimAboveWithChildCreatesSurfaceAboveChild() throws Exception {
177 TestWindowContainer child = new TestWindowContainer();
178 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
191 public void testDimBelowWithChildSurfaceCreatesSurfaceBelowChild() throws Exception {
192 TestWindowContainer child = new TestWindowContainer();
193 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
206 public void testDimBelowWithChildSurfaceDestroyedWhenReset() throws Exception {
207 TestWindowContainer child = new TestWindowContainer();
208 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());
chaviw2fb06bc2018-01-19 17:09:15 -0800218 verify(dimLayer).destroy();
Robert Carrf59b8dd2017-10-02 18:58:36 -0700219 }
220
221 @Test
222 public void testDimBelowWithChildSurfaceNotDestroyedWhenPersisted() throws Exception {
223 TestWindowContainer child = new TestWindowContainer();
224 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);
234 verify(dimLayer, never()).destroy();
235 }
236
chaviwf20bd222018-02-01 16:06:52 -0800237 @Test
238 public void testDimUpdateWhileDimming() throws Exception {
239 Rect bounds = new Rect();
240 TestWindowContainer child = new TestWindowContainer();
241 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);
249 verify(mTransaction, times(1)).show(dimLayer);
250 verify(mTransaction).setSize(dimLayer, bounds.width(), bounds.height());
251 verify(mTransaction).setPosition(dimLayer, 0, 0);
252
253 bounds.set(10, 10, 30, 30);
254 mDimmer.updateDims(mTransaction, bounds);
255 verify(mTransaction).setSize(dimLayer, bounds.width(), bounds.height());
256 verify(mTransaction).setPosition(dimLayer, 10, 10);
257 }
258
chaviw87ca63a2018-03-26 14:06:17 -0700259 @Test
260 public void testRemoveDimImmediately() throws Exception {
261 TestWindowContainer child = new TestWindowContainer();
262 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());
275 verify(mTransaction).destroy(dimLayer);
276 }
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}