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.assertWithMessage; |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 20 | |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 21 | import static org.mockito.Mockito.doReturn; |
| 22 | import static org.mockito.Mockito.mock; |
| 23 | import static org.mockito.Mockito.spy; |
| 24 | import static org.mockito.Mockito.times; |
| 25 | import static org.mockito.Mockito.verify; |
| 26 | import static org.mockito.Mockito.when; |
| 27 | |
| 28 | import android.app.WallpaperManager; |
| 29 | import android.content.Context; |
| 30 | import android.content.res.Configuration; |
| 31 | import android.content.res.Resources; |
| 32 | import android.graphics.Bitmap; |
| 33 | import android.graphics.ColorSpace; |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 34 | import android.hardware.display.DisplayManagerGlobal; |
Beverly | f8f2f16 | 2020-02-25 13:47:45 -0500 | [diff] [blame] | 35 | import android.os.Handler; |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 36 | import android.test.suitebuilder.annotation.SmallTest; |
| 37 | import android.testing.AndroidTestingRunner; |
| 38 | import android.testing.TestableLooper; |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 39 | import android.view.Display; |
| 40 | import android.view.DisplayInfo; |
| 41 | import android.view.SurfaceHolder; |
| 42 | |
| 43 | import com.android.systemui.glwallpaper.ImageWallpaperRenderer; |
Brett Chabot | 84151d9 | 2019-02-27 15:37:59 -0800 | [diff] [blame] | 44 | |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 48 | import org.mockito.Mock; |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 49 | import org.mockito.MockitoAnnotations; |
| 50 | |
| 51 | import java.util.concurrent.CountDownLatch; |
| 52 | |
| 53 | @SmallTest |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 54 | @RunWith(AndroidTestingRunner.class) |
| 55 | @TestableLooper.RunWithLooper |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 56 | public class ImageWallpaperTest extends SysuiTestCase { |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 57 | 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 |
Beverly | f8f2f16 | 2020-02-25 13:47:45 -0500 | [diff] [blame] | 71 | private Handler mHandler; |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 72 | |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 73 | private CountDownLatch mEventCountdown; |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 74 | |
| 75 | @Before |
| 76 | public void setUp() throws Exception { |
Beverly | 1467c9e | 2020-02-18 13:31:29 -0500 | [diff] [blame] | 77 | allowTestableLooperAsMainThread(); |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 78 | MockitoAnnotations.initMocks(this); |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 79 | mEventCountdown = new CountDownLatch(1); |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 80 | |
| 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 Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | private ImageWallpaper createImageWallpaper() { |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 100 | return new ImageWallpaper() { |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 101 | @Override |
| 102 | public Engine onCreateEngine() { |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 103 | return new GLEngine(mHandler) { |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 104 | @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 Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 126 | @Test |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 127 | 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 Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 130 | verifySurfaceSize(DISPLAY_WIDTH /* bmpWidth */, |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 131 | DISPLAY_WIDTH /* bmpHeight */, |
| 132 | DISPLAY_WIDTH /* surfaceWidth */, |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 133 | DISPLAY_WIDTH /* surfaceHeight */); |
Ahan Wu | 723a80e | 2018-11-07 20:39:32 +0800 | [diff] [blame] | 134 | } |
Ahan Wu | b492452 | 2019-02-20 19:15:04 +0800 | [diff] [blame] | 135 | |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 136 | @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 Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 140 | verifySurfaceSize(LOW_BMP_WIDTH /* bmpWidth */, |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 141 | LOW_BMP_HEIGHT /* bmpHeight */, |
| 142 | LOW_BMP_WIDTH /* surfaceWidth */, |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 143 | LOW_BMP_HEIGHT /* surfaceHeight */); |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 144 | } |
| 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 Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 150 | verifySurfaceSize(INVALID_BMP_WIDTH /* bmpWidth */, |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 151 | INVALID_BMP_HEIGHT /* bmpHeight */, |
| 152 | ImageWallpaper.GLEngine.MIN_SURFACE_WIDTH /* surfaceWidth */, |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 153 | ImageWallpaper.GLEngine.MIN_SURFACE_HEIGHT /* surfaceHeight */); |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 154 | } |
| 155 | |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 156 | private void verifySurfaceSize(int bmpWidth, int bmpHeight, |
| 157 | int surfaceWidth, int surfaceHeight) { |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 158 | ImageWallpaper.GLEngine wallpaperEngine = |
| 159 | (ImageWallpaper.GLEngine) createImageWallpaper().onCreateEngine(); |
| 160 | |
| 161 | ImageWallpaper.GLEngine engineSpy = spy(wallpaperEngine); |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 162 | |
| 163 | when(mWallpaperBitmap.getWidth()).thenReturn(bmpWidth); |
| 164 | when(mWallpaperBitmap.getHeight()).thenReturn(bmpHeight); |
| 165 | |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 166 | ImageWallpaperRenderer renderer = new ImageWallpaperRenderer(mMockContext); |
Ahan Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 167 | 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 Wu | 4e40440 | 2020-01-27 20:39:57 +0800 | [diff] [blame] | 173 | } |
Valentin Iftime | 6239e53 | 2018-08-24 17:17:26 +0200 | [diff] [blame] | 174 | } |