Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.glwallpaper; |
| 18 | |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 19 | import static com.google.common.truth.Truth.assertThat; |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 20 | |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 21 | import static org.mockito.ArgumentMatchers.any; |
| 22 | import static org.mockito.ArgumentMatchers.anyInt; |
| 23 | import static org.mockito.ArgumentMatchers.eq; |
| 24 | import static org.mockito.Mockito.atMost; |
| 25 | import static org.mockito.Mockito.doReturn; |
| 26 | import static org.mockito.Mockito.never; |
| 27 | import static org.mockito.Mockito.verify; |
| 28 | import static org.mockito.Mockito.when; |
| 29 | |
| 30 | import android.graphics.PixelFormat; |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 31 | import android.testing.AndroidTestingRunner; |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 32 | import android.view.Surface; |
| 33 | import android.view.SurfaceControl; |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 34 | import android.view.SurfaceHolder; |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 35 | import android.view.SurfaceSession; |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 36 | |
| 37 | import androidx.test.filters.SmallTest; |
| 38 | |
| 39 | import com.android.systemui.SysuiTestCase; |
| 40 | |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 41 | import org.junit.After; |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 42 | import org.junit.Before; |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 43 | import org.junit.Ignore; |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 46 | import org.mockito.ArgumentCaptor; |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 47 | import org.mockito.Mock; |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 48 | import org.mockito.MockitoAnnotations; |
| 49 | import org.mockito.Spy; |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 50 | |
| 51 | @SmallTest |
| 52 | @RunWith(AndroidTestingRunner.class) |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 53 | public class EglHelperTest extends SysuiTestCase { |
| 54 | |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 55 | @Spy |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 56 | private EglHelper mEglHelper; |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 57 | |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 58 | @Mock |
| 59 | private SurfaceHolder mSurfaceHolder; |
| 60 | |
| 61 | @Before |
| 62 | public void setUp() throws Exception { |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 63 | MockitoAnnotations.initMocks(this); |
| 64 | prepareSurface(); |
| 65 | } |
| 66 | |
| 67 | @After |
| 68 | public void tearDown() { |
| 69 | mSurfaceHolder.getSurface().destroy(); |
| 70 | mSurfaceHolder = null; |
| 71 | } |
| 72 | |
| 73 | private void prepareSurface() { |
| 74 | final SurfaceSession session = new SurfaceSession(); |
| 75 | final SurfaceControl control = new SurfaceControl.Builder(session) |
| 76 | .setName("Test") |
| 77 | .setBufferSize(100, 100) |
| 78 | .setFormat(PixelFormat.RGB_888) |
| 79 | .build(); |
| 80 | final Surface surface = new Surface(); |
| 81 | surface.copyFrom(control); |
| 82 | when(mSurfaceHolder.getSurface()).thenReturn(surface); |
| 83 | assertThat(mSurfaceHolder.getSurface()).isNotNull(); |
| 84 | assertThat(mSurfaceHolder.getSurface().isValid()).isTrue(); |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | @Test |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 88 | public void testInit_normal() { |
Ahan Wu | 287d828 | 2019-12-17 16:23:17 +0800 | [diff] [blame] | 89 | mEglHelper.init(mSurfaceHolder, false /* wideColorGamut */); |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 90 | assertThat(mEglHelper.hasEglDisplay()).isTrue(); |
| 91 | assertThat(mEglHelper.hasEglContext()).isTrue(); |
| 92 | assertThat(mEglHelper.hasEglSurface()).isTrue(); |
| 93 | verify(mEglHelper).askCreatingEglWindowSurface( |
| 94 | any(SurfaceHolder.class), eq(null), anyInt()); |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Test |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 98 | public void testInit_wide_gamut() { |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 99 | // In EglHelper, EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT = 0x3490; |
| 100 | doReturn(0x3490).when(mEglHelper).getWcgCapability(); |
| 101 | // In EglHelper, KHR_GL_COLOR_SPACE = "EGL_KHR_gl_colorspace"; |
| 102 | doReturn(true).when(mEglHelper).checkExtensionCapability("EGL_KHR_gl_colorspace"); |
| 103 | ArgumentCaptor<int[]> ac = ArgumentCaptor.forClass(int[].class); |
| 104 | // {EGL_GL_COLORSPACE_KHR, EGL_GL_COLORSPACE_DISPLAY_P3_PASSTHROUGH_EXT, EGL_NONE} |
| 105 | final int[] expectedArgument = new int[] {0x309D, 0x3490, 0x3038}; |
| 106 | |
| 107 | mEglHelper.init(mSurfaceHolder, true /* wideColorGamut */); |
| 108 | verify(mEglHelper) |
| 109 | .askCreatingEglWindowSurface(any(SurfaceHolder.class), ac.capture(), anyInt()); |
| 110 | assertThat(ac.getValue()).isNotNull(); |
| 111 | assertThat(ac.getValue()).isEqualTo(expectedArgument); |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
Ahan Wu | 4a29779 | 2020-04-16 03:00:14 +0800 | [diff] [blame] | 115 | @Ignore |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 116 | public void testFinish_shouldNotCrash() { |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 117 | mEglHelper.terminateEglDisplay(); |
| 118 | assertThat(mEglHelper.hasEglDisplay()).isFalse(); |
| 119 | assertThat(mEglHelper.hasEglSurface()).isFalse(); |
| 120 | assertThat(mEglHelper.hasEglContext()).isFalse(); |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 121 | |
| 122 | mEglHelper.finish(); |
Ahan Wu | d860933 | 2020-01-15 20:08:08 +0800 | [diff] [blame] | 123 | verify(mEglHelper, never()).destroyEglContext(); |
| 124 | verify(mEglHelper, never()).destroyEglSurface(); |
| 125 | verify(mEglHelper, atMost(1)).terminateEglDisplay(); |
Bill Lin | a6fe754 | 2019-08-12 11:45:27 +0800 | [diff] [blame] | 126 | } |
| 127 | } |