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