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.Ignore;
47 import org.junit.Test;
48 import org.junit.runner.RunWith;
49 import org.mockito.Mock;
50 import org.mockito.MockitoAnnotations;
51 
52 import java.util.concurrent.CountDownLatch;
53 
54 @SmallTest
55 @RunWith(AndroidTestingRunner.class)
56 @TestableLooper.RunWithLooper
57 @Ignore
58 public class ImageWallpaperTest extends SysuiTestCase {
59     private static final int LOW_BMP_WIDTH = 128;
60     private static final int LOW_BMP_HEIGHT = 128;
61     private static final int INVALID_BMP_WIDTH = 1;
62     private static final int INVALID_BMP_HEIGHT = 1;
63     private static final int DISPLAY_WIDTH = 1920;
64     private static final int DISPLAY_HEIGHT = 1080;
65 
66     @Mock
67     private SurfaceHolder mSurfaceHolder;
68     @Mock
69     private Context mMockContext;
70     @Mock
71     private Bitmap mWallpaperBitmap;
72     @Mock
73     private Handler mHandler;
74 
75     private CountDownLatch mEventCountdown;
76 
77     @Before
setUp()78     public void setUp() throws Exception {
79         allowTestableLooperAsMainThread();
80         MockitoAnnotations.initMocks(this);
81         mEventCountdown = new CountDownLatch(1);
82 
83         WallpaperManager wallpaperManager = mock(WallpaperManager.class);
84         Resources resources = mock(Resources.class);
85 
86         when(mMockContext.getSystemService(WallpaperManager.class)).thenReturn(wallpaperManager);
87         when(mMockContext.getResources()).thenReturn(resources);
88         when(resources.getConfiguration()).thenReturn(mock(Configuration.class));
89 
90         DisplayInfo displayInfo = new DisplayInfo();
91         displayInfo.logicalWidth = DISPLAY_WIDTH;
92         displayInfo.logicalHeight = DISPLAY_HEIGHT;
93         when(mMockContext.getDisplay()).thenReturn(
94                 new Display(mock(DisplayManagerGlobal.class), 0, displayInfo, (Resources) null));
95 
96         when(wallpaperManager.getBitmap(false)).thenReturn(mWallpaperBitmap);
97         when(mWallpaperBitmap.getColorSpace()).thenReturn(ColorSpace.get(ColorSpace.Named.SRGB));
98         when(mWallpaperBitmap.getConfig()).thenReturn(Bitmap.Config.ARGB_8888);
99     }
100 
createImageWallpaper()101     private ImageWallpaper createImageWallpaper() {
102         return new ImageWallpaper() {
103             @Override
104             public Engine onCreateEngine() {
105                 return new GLEngine(mHandler) {
106                     @Override
107                     public Context getDisplayContext() {
108                         return mMockContext;
109                     }
110 
111                     @Override
112                     public SurfaceHolder getSurfaceHolder() {
113                         return mSurfaceHolder;
114                     }
115 
116                     @Override
117                     public void setFixedSizeAllowed(boolean allowed) {
118                         super.setFixedSizeAllowed(allowed);
119                         assertWithMessage("mFixedSizeAllowed should be true").that(
120                                 allowed).isTrue();
121                         mEventCountdown.countDown();
122                     }
123                 };
124             }
125         };
126     }
127 
128     @Test
129     public void testBitmapWallpaper_normal() {
130         // Will use a image wallpaper with dimensions DISPLAY_WIDTH x DISPLAY_WIDTH.
131         // Then we expect the surface size will be also DISPLAY_WIDTH x DISPLAY_WIDTH.
132         verifySurfaceSize(DISPLAY_WIDTH /* bmpWidth */,
133                 DISPLAY_WIDTH /* bmpHeight */,
134                 DISPLAY_WIDTH /* surfaceWidth */,
135                 DISPLAY_WIDTH /* surfaceHeight */);
136     }
137 
138     @Test
139     public void testBitmapWallpaper_low_resolution() {
140         // Will use a image wallpaper with dimensions BMP_WIDTH x BMP_HEIGHT.
141         // Then we expect the surface size will be also BMP_WIDTH x BMP_HEIGHT.
142         verifySurfaceSize(LOW_BMP_WIDTH /* bmpWidth */,
143                 LOW_BMP_HEIGHT /* bmpHeight */,
144                 LOW_BMP_WIDTH /* surfaceWidth */,
145                 LOW_BMP_HEIGHT /* surfaceHeight */);
146     }
147 
148     @Test
149     public void testBitmapWallpaper_too_small() {
150         // Will use a image wallpaper with dimensions INVALID_BMP_WIDTH x INVALID_BMP_HEIGHT.
151         // Then we expect the surface size will be also MIN_SURFACE_WIDTH x MIN_SURFACE_HEIGHT.
152         verifySurfaceSize(INVALID_BMP_WIDTH /* bmpWidth */,
153                 INVALID_BMP_HEIGHT /* bmpHeight */,
154                 ImageWallpaper.GLEngine.MIN_SURFACE_WIDTH /* surfaceWidth */,
155                 ImageWallpaper.GLEngine.MIN_SURFACE_HEIGHT /* surfaceHeight */);
156     }
157 
158     private void verifySurfaceSize(int bmpWidth, int bmpHeight,
159             int surfaceWidth, int surfaceHeight) {
160         ImageWallpaper.GLEngine wallpaperEngine =
161                 (ImageWallpaper.GLEngine) createImageWallpaper().onCreateEngine();
162 
163         ImageWallpaper.GLEngine engineSpy = spy(wallpaperEngine);
164 
165         when(mWallpaperBitmap.getWidth()).thenReturn(bmpWidth);
166         when(mWallpaperBitmap.getHeight()).thenReturn(bmpHeight);
167 
168         ImageWallpaperRenderer renderer = new ImageWallpaperRenderer(mMockContext);
169         doReturn(renderer).when(engineSpy).getRendererInstance();
170         engineSpy.onCreate(engineSpy.getSurfaceHolder());
171 
172         verify(mSurfaceHolder, times(1)).setFixedSize(surfaceWidth, surfaceHeight);
173         assertWithMessage("setFixedSizeAllowed should have been called.").that(
174                 mEventCountdown.getCount()).isEqualTo(0);
175     }
176 }
177