Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui; |
| 18 | |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | import static com.google.common.truth.Truth.assertWithMessage; |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 21 | |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 22 | import static org.mockito.Mockito.doReturn; |
| 23 | import static org.mockito.Mockito.mock; |
| 24 | import static org.mockito.Mockito.spy; |
| 25 | import static org.mockito.Mockito.times; |
| 26 | import static org.mockito.Mockito.verify; |
| 27 | import static org.mockito.Mockito.when; |
| 28 | |
| 29 | import android.app.WallpaperManager; |
| 30 | import android.content.Context; |
| 31 | import android.content.res.Configuration; |
| 32 | import android.content.res.Resources; |
| 33 | import android.graphics.Bitmap; |
| 34 | import android.graphics.ColorSpace; |
| 35 | import android.graphics.Rect; |
| 36 | import android.hardware.display.DisplayManagerGlobal; |
Beverly | f8f2f16 | 2020-02-25 13:47:45 -0500 | [diff] [blame] | 37 | import android.os.Handler; |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 38 | import android.test.suitebuilder.annotation.SmallTest; |
| 39 | import android.testing.AndroidTestingRunner; |
| 40 | import android.testing.TestableLooper; |
| 41 | import android.util.Size; |
| 42 | import android.view.Display; |
| 43 | import android.view.DisplayInfo; |
| 44 | import android.view.SurfaceHolder; |
| 45 | |
| 46 | import com.android.systemui.glwallpaper.ImageWallpaperRenderer; |
| 47 | import com.android.systemui.statusbar.phone.DozeParameters; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 48 | |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 49 | import org.junit.Before; |
| 50 | import org.junit.Test; |
| 51 | import org.junit.runner.RunWith; |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 52 | import org.mockito.Mock; |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 53 | import org.mockito.MockitoAnnotations; |
| 54 | |
| 55 | import java.util.concurrent.CountDownLatch; |
| 56 | |
| 57 | @SmallTest |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 58 | @RunWith(AndroidTestingRunner.class) |
| 59 | @TestableLooper.RunWithLooper |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 60 | public class ImageWallpaperTest extends SysuiTestCase { |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 61 | 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; |
Beverly | f8f2f16 | 2020-02-25 13:47:45 -0500 | [diff] [blame] | 76 | @Mock |
| 77 | private Handler mHandler; |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 78 | |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 79 | private CountDownLatch mEventCountdown; |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 80 | |
| 81 | @Before |
| 82 | public void setUp() throws Exception { |
Beverly | 1467c9e | 2020-02-18 13:31:29 -0500 | [diff] [blame] | 83 | allowTestableLooperAsMainThread(); |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 84 | MockitoAnnotations.initMocks(this); |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 85 | mEventCountdown = new CountDownLatch(1); |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 86 | |
| 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() { |
Beverly | f8f2f16 | 2020-02-25 13:47:45 -0500 | [diff] [blame] | 110 | return new GLEngine(mDozeParam, mHandler) { |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 111 | @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 Wu | 3222d3f | 2020-03-11 20:24:00 +0800 | [diff] [blame] | 137 | // No - Op |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 138 | } |
| 139 | }; |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 140 | } |
| 141 | |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 142 | @Test |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 143 | 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 Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 152 | } |
Ahan Wu | b492452 | 2019-02-20 19:15:04 +0800 | [diff] [blame] | 153 | |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 154 | @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 Wu | 765f178 | 2020-04-02 23:08:15 +0800 | [diff] [blame] | 163 | false /* assertion */); |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 164 | } |
| 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 Wu | 765f178 | 2020-04-02 23:08:15 +0800 | [diff] [blame] | 175 | false /* assertion */); |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 176 | } |
| 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); |
Beverly | f8f2f16 | 2020-02-25 13:47:45 -0500 | [diff] [blame] | 202 | // destroy |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 203 | } |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 204 | } |