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 
19 import static com.google.common.truth.Truth.assertWithMessage;
20 
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;
34 import android.hardware.display.DisplayManagerGlobal;
35 import android.os.Handler;
36 import android.test.suitebuilder.annotation.SmallTest;
37 import android.testing.AndroidTestingRunner;
38 import android.testing.TestableLooper;
39 import android.view.Display;
40 import android.view.DisplayInfo;
41 import android.view.SurfaceHolder;
42 
43 import com.android.systemui.glwallpaper.ImageWallpaperRenderer;
44 
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50 
51 import java.util.concurrent.CountDownLatch;
52 
53 @SmallTest
54 @RunWith(AndroidTestingRunner.class)
55 @TestableLooper.RunWithLooper
56 public class ImageWallpaperTest extends SysuiTestCase {
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
71     private Handler mHandler;
72 
73     private CountDownLatch mEventCountdown;
74 
75     @Before
setUp()76     public void setUp() throws Exception {
77         allowTestableLooperAsMainThread();
78         MockitoAnnotations.initMocks(this);
79         mEventCountdown = new CountDownLatch(1);
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);
97     }
98 
createImageWallpaper()99     private ImageWallpaper createImageWallpaper() {
100         return new ImageWallpaper() {
101             @Override
102             public Engine onCreateEngine() {
103                 return new GLEngine(mHandler) {
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 
126     @Test
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.
130         verifySurfaceSize(DISPLAY_WIDTH /* bmpWidth */,
131                 DISPLAY_WIDTH /* bmpHeight */,
132                 DISPLAY_WIDTH /* surfaceWidth */,
133                 DISPLAY_WIDTH /* surfaceHeight */);
134     }
135 
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.
140         verifySurfaceSize(LOW_BMP_WIDTH /* bmpWidth */,
141                 LOW_BMP_HEIGHT /* bmpHeight */,
142                 LOW_BMP_WIDTH /* surfaceWidth */,
143                 LOW_BMP_HEIGHT /* surfaceHeight */);
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.
150         verifySurfaceSize(INVALID_BMP_WIDTH /* bmpWidth */,
151                 INVALID_BMP_HEIGHT /* bmpHeight */,
152                 ImageWallpaper.GLEngine.MIN_SURFACE_WIDTH /* surfaceWidth */,
153                 ImageWallpaper.GLEngine.MIN_SURFACE_HEIGHT /* surfaceHeight */);
154     }
155 
156     private void verifySurfaceSize(int bmpWidth, int bmpHeight,
157             int surfaceWidth, int surfaceHeight) {
158         ImageWallpaper.GLEngine wallpaperEngine =
159                 (ImageWallpaper.GLEngine) createImageWallpaper().onCreateEngine();
160 
161         ImageWallpaper.GLEngine engineSpy = spy(wallpaperEngine);
162 
163         when(mWallpaperBitmap.getWidth()).thenReturn(bmpWidth);
164         when(mWallpaperBitmap.getHeight()).thenReturn(bmpHeight);
165 
166         ImageWallpaperRenderer renderer = new ImageWallpaperRenderer(mMockContext);
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);
173     }
174 }
175