blob: 5227aaf0124912e79d7e311c428f51c24c09cf31 [file] [log] [blame]
Valentin Iftime6239e532018-08-24 17:17:26 +02001/*
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
17package com.android.systemui;
18
Ahan Wu4e404402020-01-27 20:39:57 +080019import static com.google.common.truth.Truth.assertThat;
20import static com.google.common.truth.Truth.assertWithMessage;
Valentin Iftime6239e532018-08-24 17:17:26 +020021
Ahan Wu4e404402020-01-27 20:39:57 +080022import static org.mockito.Mockito.doReturn;
23import static org.mockito.Mockito.mock;
24import static org.mockito.Mockito.spy;
25import static org.mockito.Mockito.times;
26import static org.mockito.Mockito.verify;
27import static org.mockito.Mockito.when;
28
29import android.app.WallpaperManager;
30import android.content.Context;
31import android.content.res.Configuration;
32import android.content.res.Resources;
33import android.graphics.Bitmap;
34import android.graphics.ColorSpace;
35import android.graphics.Rect;
36import android.hardware.display.DisplayManagerGlobal;
Beverlyf8f2f162020-02-25 13:47:45 -050037import android.os.Handler;
Ahan Wu4e404402020-01-27 20:39:57 +080038import android.test.suitebuilder.annotation.SmallTest;
39import android.testing.AndroidTestingRunner;
40import android.testing.TestableLooper;
41import android.util.Size;
42import android.view.Display;
43import android.view.DisplayInfo;
44import android.view.SurfaceHolder;
45
46import com.android.systemui.glwallpaper.ImageWallpaperRenderer;
47import com.android.systemui.statusbar.phone.DozeParameters;
Brett Chabot84151d92019-02-27 15:37:59 -080048
Valentin Iftime6239e532018-08-24 17:17:26 +020049import org.junit.Before;
50import org.junit.Test;
51import org.junit.runner.RunWith;
Ahan Wu4e404402020-01-27 20:39:57 +080052import org.mockito.Mock;
Valentin Iftime6239e532018-08-24 17:17:26 +020053import org.mockito.MockitoAnnotations;
54
55import java.util.concurrent.CountDownLatch;
56
57@SmallTest
Ahan Wu4e404402020-01-27 20:39:57 +080058@RunWith(AndroidTestingRunner.class)
59@TestableLooper.RunWithLooper
Valentin Iftime6239e532018-08-24 17:17:26 +020060public class ImageWallpaperTest extends SysuiTestCase {
Ahan Wu4e404402020-01-27 20:39:57 +080061 private static final int LOW_BMP_WIDTH = 128;
62 private static final int LOW_BMP_HEIGHT = 128;
63 private static final int INVALID_BMP_WIDTH = 1;
64 private static final int INVALID_BMP_HEIGHT = 1;
65 private static final int DISPLAY_WIDTH = 1920;
66 private static final int DISPLAY_HEIGHT = 1080;
67
68 @Mock
69 private SurfaceHolder mSurfaceHolder;
70 @Mock
71 private Context mMockContext;
72 @Mock
73 private Bitmap mWallpaperBitmap;
74 @Mock
75 private DozeParameters mDozeParam;
Beverlyf8f2f162020-02-25 13:47:45 -050076 @Mock
77 private Handler mHandler;
Valentin Iftime6239e532018-08-24 17:17:26 +020078
Ahan Wu723a80e2018-11-07 20:39:32 +080079 private CountDownLatch mEventCountdown;
Valentin Iftime6239e532018-08-24 17:17:26 +020080
81 @Before
82 public void setUp() throws Exception {
Beverly1467c9e2020-02-18 13:31:29 -050083 allowTestableLooperAsMainThread();
Valentin Iftime6239e532018-08-24 17:17:26 +020084 MockitoAnnotations.initMocks(this);
Valentin Iftime6239e532018-08-24 17:17:26 +020085 mEventCountdown = new CountDownLatch(1);
Ahan Wu4e404402020-01-27 20:39:57 +080086
87 WallpaperManager wallpaperManager = mock(WallpaperManager.class);
88 Resources resources = mock(Resources.class);
89
90 when(mMockContext.getSystemService(WallpaperManager.class)).thenReturn(wallpaperManager);
91 when(mMockContext.getResources()).thenReturn(resources);
92 when(resources.getConfiguration()).thenReturn(mock(Configuration.class));
93
94 DisplayInfo displayInfo = new DisplayInfo();
95 displayInfo.logicalWidth = DISPLAY_WIDTH;
96 displayInfo.logicalHeight = DISPLAY_HEIGHT;
97 when(mMockContext.getDisplay()).thenReturn(
98 new Display(mock(DisplayManagerGlobal.class), 0, displayInfo, (Resources) null));
99
100 when(wallpaperManager.getBitmap(false)).thenReturn(mWallpaperBitmap);
101 when(mWallpaperBitmap.getColorSpace()).thenReturn(ColorSpace.get(ColorSpace.Named.SRGB));
102 when(mWallpaperBitmap.getConfig()).thenReturn(Bitmap.Config.ARGB_8888);
103 when(mDozeParam.getDisplayNeedsBlanking()).thenReturn(false);
104 }
105
106 private ImageWallpaper createImageWallpaper() {
107 return new ImageWallpaper(mDozeParam) {
108 @Override
109 public Engine onCreateEngine() {
Beverlyf8f2f162020-02-25 13:47:45 -0500110 return new GLEngine(mDozeParam, mHandler) {
Ahan Wu4e404402020-01-27 20:39:57 +0800111 @Override
112 public Context getDisplayContext() {
113 return mMockContext;
114 }
115
116 @Override
117 public SurfaceHolder getSurfaceHolder() {
118 return mSurfaceHolder;
119 }
120
121 @Override
122 public void setFixedSizeAllowed(boolean allowed) {
123 super.setFixedSizeAllowed(allowed);
124 assertWithMessage("mFixedSizeAllowed should be true").that(
125 allowed).isTrue();
126 mEventCountdown.countDown();
127 }
128 };
129 }
130 };
131 }
132
133 private ImageWallpaperRenderer createImageWallpaperRenderer(ImageWallpaper.GLEngine engine) {
134 return new ImageWallpaperRenderer(mMockContext, engine) {
135 @Override
136 public void startProcessingImage() {
Ahan Wu3222d3f2020-03-11 20:24:00 +0800137 // No - Op
Ahan Wu4e404402020-01-27 20:39:57 +0800138 }
139 };
Valentin Iftime6239e532018-08-24 17:17:26 +0200140 }
141
Ahan Wu723a80e2018-11-07 20:39:32 +0800142 @Test
Ahan Wu4e404402020-01-27 20:39:57 +0800143 public void testBitmapWallpaper_normal() {
144 // Will use a image wallpaper with dimensions DISPLAY_WIDTH x DISPLAY_WIDTH.
145 // Then we expect the surface size will be also DISPLAY_WIDTH x DISPLAY_WIDTH.
146 // Finally, we assert the transition will not be stopped.
147 verifySurfaceSizeAndAssertTransition(DISPLAY_WIDTH /* bmpWidth */,
148 DISPLAY_WIDTH /* bmpHeight */,
149 DISPLAY_WIDTH /* surfaceWidth */,
150 DISPLAY_WIDTH /* surfaceHeight */,
151 false /* assertion */);
Ahan Wu723a80e2018-11-07 20:39:32 +0800152 }
Ahan Wub4924522019-02-20 19:15:04 +0800153
Ahan Wu4e404402020-01-27 20:39:57 +0800154 @Test
155 public void testBitmapWallpaper_low_resolution() {
156 // Will use a image wallpaper with dimensions BMP_WIDTH x BMP_HEIGHT.
157 // Then we expect the surface size will be also BMP_WIDTH x BMP_HEIGHT.
158 // Finally, we assert the transition will be stopped.
159 verifySurfaceSizeAndAssertTransition(LOW_BMP_WIDTH /* bmpWidth */,
160 LOW_BMP_HEIGHT /* bmpHeight */,
161 LOW_BMP_WIDTH /* surfaceWidth */,
162 LOW_BMP_HEIGHT /* surfaceHeight */,
Ahan Wu765f1782020-04-02 23:08:15 +0800163 false /* assertion */);
Ahan Wu4e404402020-01-27 20:39:57 +0800164 }
165
166 @Test
167 public void testBitmapWallpaper_too_small() {
168 // Will use a image wallpaper with dimensions INVALID_BMP_WIDTH x INVALID_BMP_HEIGHT.
169 // Then we expect the surface size will be also MIN_SURFACE_WIDTH x MIN_SURFACE_HEIGHT.
170 // Finally, we assert the transition will be stopped.
171 verifySurfaceSizeAndAssertTransition(INVALID_BMP_WIDTH /* bmpWidth */,
172 INVALID_BMP_HEIGHT /* bmpHeight */,
173 ImageWallpaper.GLEngine.MIN_SURFACE_WIDTH /* surfaceWidth */,
174 ImageWallpaper.GLEngine.MIN_SURFACE_HEIGHT /* surfaceHeight */,
Ahan Wu765f1782020-04-02 23:08:15 +0800175 false /* assertion */);
Ahan Wu4e404402020-01-27 20:39:57 +0800176 }
177
178 private void verifySurfaceSizeAndAssertTransition(int bmpWidth, int bmpHeight,
179 int surfaceWidth, int surfaceHeight, boolean assertion) {
180 ImageWallpaper.GLEngine wallpaperEngine =
181 (ImageWallpaper.GLEngine) createImageWallpaper().onCreateEngine();
182
183 ImageWallpaper.GLEngine engineSpy = spy(wallpaperEngine);
184 when(engineSpy.mIsHighEndGfx).thenReturn(true);
185
186 when(mWallpaperBitmap.getWidth()).thenReturn(bmpWidth);
187 when(mWallpaperBitmap.getHeight()).thenReturn(bmpHeight);
188
189 ImageWallpaperRenderer renderer = createImageWallpaperRenderer(engineSpy);
190 doReturn(renderer).when(engineSpy).getRendererInstance();
191 engineSpy.onCreate(engineSpy.getSurfaceHolder());
192
193 verify(mSurfaceHolder, times(1)).setFixedSize(surfaceWidth, surfaceHeight);
194 assertWithMessage("setFixedSizeAllowed should have been called.").that(
195 mEventCountdown.getCount()).isEqualTo(0);
196
197 Size frameSize = renderer.reportSurfaceSize();
198 Rect frame = new Rect(0, 0, frameSize.getWidth(), frameSize.getHeight());
199 when(mSurfaceHolder.getSurfaceFrame()).thenReturn(frame);
200
201 assertThat(engineSpy.checkIfShouldStopTransition()).isEqualTo(assertion);
Beverlyf8f2f162020-02-25 13:47:45 -0500202 // destroy
Ahan Wu4e404402020-01-27 20:39:57 +0800203 }
Valentin Iftime6239e532018-08-24 17:17:26 +0200204}