blob: 4b47093bb951d0e854172814c7590e72be5d238e [file] [log] [blame]
Bill Lina6fe7542019-08-12 11:45:27 +08001/*
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
17package com.android.systemui.glwallpaper;
18
Ahan Wud8609332020-01-15 20:08:08 +080019import static com.google.common.truth.Truth.assertThat;
Bill Lina6fe7542019-08-12 11:45:27 +080020
Ahan Wud8609332020-01-15 20:08:08 +080021import static org.mockito.ArgumentMatchers.any;
22import static org.mockito.ArgumentMatchers.anyInt;
23import static org.mockito.ArgumentMatchers.eq;
24import static org.mockito.Mockito.atMost;
25import static org.mockito.Mockito.doReturn;
26import static org.mockito.Mockito.never;
27import static org.mockito.Mockito.verify;
28import static org.mockito.Mockito.when;
29
30import android.graphics.PixelFormat;
Bill Lina6fe7542019-08-12 11:45:27 +080031import android.testing.AndroidTestingRunner;
Ahan Wud8609332020-01-15 20:08:08 +080032import android.view.Surface;
33import android.view.SurfaceControl;
Bill Lina6fe7542019-08-12 11:45:27 +080034import android.view.SurfaceHolder;
Ahan Wud8609332020-01-15 20:08:08 +080035import android.view.SurfaceSession;
Bill Lina6fe7542019-08-12 11:45:27 +080036
37import androidx.test.filters.SmallTest;
38
39import com.android.systemui.SysuiTestCase;
40
Ahan Wud8609332020-01-15 20:08:08 +080041import org.junit.After;
Bill Lina6fe7542019-08-12 11:45:27 +080042import org.junit.Before;
43import org.junit.Test;
44import org.junit.runner.RunWith;
Ahan Wud8609332020-01-15 20:08:08 +080045import org.mockito.ArgumentCaptor;
Bill Lina6fe7542019-08-12 11:45:27 +080046import org.mockito.Mock;
Ahan Wud8609332020-01-15 20:08:08 +080047import org.mockito.MockitoAnnotations;
48import org.mockito.Spy;
Bill Lina6fe7542019-08-12 11:45:27 +080049
50@SmallTest
51@RunWith(AndroidTestingRunner.class)
Bill Lina6fe7542019-08-12 11:45:27 +080052public class EglHelperTest extends SysuiTestCase {
53
Ahan Wud8609332020-01-15 20:08:08 +080054 @Spy
Bill Lina6fe7542019-08-12 11:45:27 +080055 private EglHelper mEglHelper;
Ahan Wud8609332020-01-15 20:08:08 +080056
Bill Lina6fe7542019-08-12 11:45:27 +080057 @Mock
58 private SurfaceHolder mSurfaceHolder;
59
60 @Before
61 public void setUp() throws Exception {
Ahan Wud8609332020-01-15 20:08:08 +080062 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 Lina6fe7542019-08-12 11:45:27 +080084 }
85
86 @Test
87 public void testInit_finish() {
Ahan Wu287d8282019-12-17 16:23:17 +080088 mEglHelper.init(mSurfaceHolder, false /* wideColorGamut */);
Ahan Wud8609332020-01-15 20:08:08 +080089 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 Lina6fe7542019-08-12 11:45:27 +0800116 mEglHelper.finish();
117 }
118
119 @Test
120 public void testFinish_shouldNotCrash() {
Ahan Wud8609332020-01-15 20:08:08 +0800121 mEglHelper.terminateEglDisplay();
122 assertThat(mEglHelper.hasEglDisplay()).isFalse();
123 assertThat(mEglHelper.hasEglSurface()).isFalse();
124 assertThat(mEglHelper.hasEglContext()).isFalse();
Bill Lina6fe7542019-08-12 11:45:27 +0800125
126 mEglHelper.finish();
Ahan Wud8609332020-01-15 20:08:08 +0800127 verify(mEglHelper, never()).destroyEglContext();
128 verify(mEglHelper, never()).destroyEglSurface();
129 verify(mEglHelper, atMost(1)).terminateEglDisplay();
Bill Lina6fe7542019-08-12 11:45:27 +0800130 }
131}