blob: 0c69ffdef372f6fe335ef91c5e54a307aa1d2954 [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.assertWithMessage;
Valentin Iftime6239e532018-08-24 17:17:26 +020020
Ahan Wu4e404402020-01-27 20:39:57 +080021import static org.mockito.Mockito.doReturn;
22import static org.mockito.Mockito.mock;
23import static org.mockito.Mockito.spy;
24import static org.mockito.Mockito.times;
25import static org.mockito.Mockito.verify;
26import static org.mockito.Mockito.when;
27
28import android.app.WallpaperManager;
29import android.content.Context;
30import android.content.res.Configuration;
31import android.content.res.Resources;
32import android.graphics.Bitmap;
33import android.graphics.ColorSpace;
Ahan Wu4e404402020-01-27 20:39:57 +080034import android.hardware.display.DisplayManagerGlobal;
Beverlyf8f2f162020-02-25 13:47:45 -050035import android.os.Handler;
Ahan Wu4e404402020-01-27 20:39:57 +080036import android.test.suitebuilder.annotation.SmallTest;
37import android.testing.AndroidTestingRunner;
38import android.testing.TestableLooper;
Ahan Wu4e404402020-01-27 20:39:57 +080039import android.view.Display;
40import android.view.DisplayInfo;
41import android.view.SurfaceHolder;
42
43import com.android.systemui.glwallpaper.ImageWallpaperRenderer;
Brett Chabot84151d92019-02-27 15:37:59 -080044
Valentin Iftime6239e532018-08-24 17:17:26 +020045import org.junit.Before;
46import org.junit.Test;
47import org.junit.runner.RunWith;
Ahan Wu4e404402020-01-27 20:39:57 +080048import org.mockito.Mock;
Valentin Iftime6239e532018-08-24 17:17:26 +020049import org.mockito.MockitoAnnotations;
50
51import java.util.concurrent.CountDownLatch;
52
53@SmallTest
Ahan Wu4e404402020-01-27 20:39:57 +080054@RunWith(AndroidTestingRunner.class)
55@TestableLooper.RunWithLooper
Valentin Iftime6239e532018-08-24 17:17:26 +020056public class ImageWallpaperTest extends SysuiTestCase {
Ahan Wu4e404402020-01-27 20:39:57 +080057 private static final int LOW_BMP_WIDTH = 128;
58 private static final int LOW_BMP_HEIGHT = 128;
59 private static final int INVALID_BMP_WIDTH = 1;
60 private static final int INVALID_BMP_HEIGHT = 1;
61 private static final int DISPLAY_WIDTH = 1920;
62 private static final int DISPLAY_HEIGHT = 1080;
63
64 @Mock
65 private SurfaceHolder mSurfaceHolder;
66 @Mock
67 private Context mMockContext;
68 @Mock
69 private Bitmap mWallpaperBitmap;
70 @Mock
Beverlyf8f2f162020-02-25 13:47:45 -050071 private Handler mHandler;
Valentin Iftime6239e532018-08-24 17:17:26 +020072
Ahan Wu723a80e2018-11-07 20:39:32 +080073 private CountDownLatch mEventCountdown;
Valentin Iftime6239e532018-08-24 17:17:26 +020074
75 @Before
76 public void setUp() throws Exception {
Beverly1467c9e2020-02-18 13:31:29 -050077 allowTestableLooperAsMainThread();
Valentin Iftime6239e532018-08-24 17:17:26 +020078 MockitoAnnotations.initMocks(this);
Valentin Iftime6239e532018-08-24 17:17:26 +020079 mEventCountdown = new CountDownLatch(1);
Ahan Wu4e404402020-01-27 20:39:57 +080080
81 WallpaperManager wallpaperManager = mock(WallpaperManager.class);
82 Resources resources = mock(Resources.class);
83
84 when(mMockContext.getSystemService(WallpaperManager.class)).thenReturn(wallpaperManager);
85 when(mMockContext.getResources()).thenReturn(resources);
86 when(resources.getConfiguration()).thenReturn(mock(Configuration.class));
87
88 DisplayInfo displayInfo = new DisplayInfo();
89 displayInfo.logicalWidth = DISPLAY_WIDTH;
90 displayInfo.logicalHeight = DISPLAY_HEIGHT;
91 when(mMockContext.getDisplay()).thenReturn(
92 new Display(mock(DisplayManagerGlobal.class), 0, displayInfo, (Resources) null));
93
94 when(wallpaperManager.getBitmap(false)).thenReturn(mWallpaperBitmap);
95 when(mWallpaperBitmap.getColorSpace()).thenReturn(ColorSpace.get(ColorSpace.Named.SRGB));
96 when(mWallpaperBitmap.getConfig()).thenReturn(Bitmap.Config.ARGB_8888);
Ahan Wu4e404402020-01-27 20:39:57 +080097 }
98
99 private ImageWallpaper createImageWallpaper() {
Ahan Wu4a297792020-04-16 03:00:14 +0800100 return new ImageWallpaper() {
Ahan Wu4e404402020-01-27 20:39:57 +0800101 @Override
102 public Engine onCreateEngine() {
Ahan Wu4a297792020-04-16 03:00:14 +0800103 return new GLEngine(mHandler) {
Ahan Wu4e404402020-01-27 20:39:57 +0800104 @Override
105 public Context getDisplayContext() {
106 return mMockContext;
107 }
108
109 @Override
110 public SurfaceHolder getSurfaceHolder() {
111 return mSurfaceHolder;
112 }
113
114 @Override
115 public void setFixedSizeAllowed(boolean allowed) {
116 super.setFixedSizeAllowed(allowed);
117 assertWithMessage("mFixedSizeAllowed should be true").that(
118 allowed).isTrue();
119 mEventCountdown.countDown();
120 }
121 };
122 }
123 };
124 }
125
Ahan Wu723a80e2018-11-07 20:39:32 +0800126 @Test
Ahan Wu4e404402020-01-27 20:39:57 +0800127 public void testBitmapWallpaper_normal() {
128 // Will use a image wallpaper with dimensions DISPLAY_WIDTH x DISPLAY_WIDTH.
129 // Then we expect the surface size will be also DISPLAY_WIDTH x DISPLAY_WIDTH.
Ahan Wu4a297792020-04-16 03:00:14 +0800130 verifySurfaceSize(DISPLAY_WIDTH /* bmpWidth */,
Ahan Wu4e404402020-01-27 20:39:57 +0800131 DISPLAY_WIDTH /* bmpHeight */,
132 DISPLAY_WIDTH /* surfaceWidth */,
Ahan Wu4a297792020-04-16 03:00:14 +0800133 DISPLAY_WIDTH /* surfaceHeight */);
Ahan Wu723a80e2018-11-07 20:39:32 +0800134 }
Ahan Wub4924522019-02-20 19:15:04 +0800135
Ahan Wu4e404402020-01-27 20:39:57 +0800136 @Test
137 public void testBitmapWallpaper_low_resolution() {
138 // Will use a image wallpaper with dimensions BMP_WIDTH x BMP_HEIGHT.
139 // Then we expect the surface size will be also BMP_WIDTH x BMP_HEIGHT.
Ahan Wu4a297792020-04-16 03:00:14 +0800140 verifySurfaceSize(LOW_BMP_WIDTH /* bmpWidth */,
Ahan Wu4e404402020-01-27 20:39:57 +0800141 LOW_BMP_HEIGHT /* bmpHeight */,
142 LOW_BMP_WIDTH /* surfaceWidth */,
Ahan Wu4a297792020-04-16 03:00:14 +0800143 LOW_BMP_HEIGHT /* surfaceHeight */);
Ahan Wu4e404402020-01-27 20:39:57 +0800144 }
145
146 @Test
147 public void testBitmapWallpaper_too_small() {
148 // Will use a image wallpaper with dimensions INVALID_BMP_WIDTH x INVALID_BMP_HEIGHT.
149 // Then we expect the surface size will be also MIN_SURFACE_WIDTH x MIN_SURFACE_HEIGHT.
Ahan Wu4a297792020-04-16 03:00:14 +0800150 verifySurfaceSize(INVALID_BMP_WIDTH /* bmpWidth */,
Ahan Wu4e404402020-01-27 20:39:57 +0800151 INVALID_BMP_HEIGHT /* bmpHeight */,
152 ImageWallpaper.GLEngine.MIN_SURFACE_WIDTH /* surfaceWidth */,
Ahan Wu4a297792020-04-16 03:00:14 +0800153 ImageWallpaper.GLEngine.MIN_SURFACE_HEIGHT /* surfaceHeight */);
Ahan Wu4e404402020-01-27 20:39:57 +0800154 }
155
Ahan Wu4a297792020-04-16 03:00:14 +0800156 private void verifySurfaceSize(int bmpWidth, int bmpHeight,
157 int surfaceWidth, int surfaceHeight) {
Ahan Wu4e404402020-01-27 20:39:57 +0800158 ImageWallpaper.GLEngine wallpaperEngine =
159 (ImageWallpaper.GLEngine) createImageWallpaper().onCreateEngine();
160
161 ImageWallpaper.GLEngine engineSpy = spy(wallpaperEngine);
Ahan Wu4e404402020-01-27 20:39:57 +0800162
163 when(mWallpaperBitmap.getWidth()).thenReturn(bmpWidth);
164 when(mWallpaperBitmap.getHeight()).thenReturn(bmpHeight);
165
Ahan Wu4a297792020-04-16 03:00:14 +0800166 ImageWallpaperRenderer renderer = new ImageWallpaperRenderer(mMockContext);
Ahan Wu4e404402020-01-27 20:39:57 +0800167 doReturn(renderer).when(engineSpy).getRendererInstance();
168 engineSpy.onCreate(engineSpy.getSurfaceHolder());
169
170 verify(mSurfaceHolder, times(1)).setFixedSize(surfaceWidth, surfaceHeight);
171 assertWithMessage("setFixedSizeAllowed should have been called.").that(
172 mEventCountdown.getCount()).isEqualTo(0);
Ahan Wu4e404402020-01-27 20:39:57 +0800173 }
Valentin Iftime6239e532018-08-24 17:17:26 +0200174}