blob: e23507b0a895e712a7176c8575650b39474bfb60 [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;
Ahan Wu4a297792020-04-16 03:00:14 +080043import org.junit.Ignore;
Bill Lina6fe7542019-08-12 11:45:27 +080044import org.junit.Test;
45import org.junit.runner.RunWith;
Ahan Wud8609332020-01-15 20:08:08 +080046import org.mockito.ArgumentCaptor;
Bill Lina6fe7542019-08-12 11:45:27 +080047import org.mockito.Mock;
Ahan Wud8609332020-01-15 20:08:08 +080048import org.mockito.MockitoAnnotations;
49import org.mockito.Spy;
Bill Lina6fe7542019-08-12 11:45:27 +080050
51@SmallTest
52@RunWith(AndroidTestingRunner.class)
Bill Lina6fe7542019-08-12 11:45:27 +080053public class EglHelperTest extends SysuiTestCase {
54
Ahan Wud8609332020-01-15 20:08:08 +080055 @Spy
Bill Lina6fe7542019-08-12 11:45:27 +080056 private EglHelper mEglHelper;
Ahan Wud8609332020-01-15 20:08:08 +080057
Bill Lina6fe7542019-08-12 11:45:27 +080058 @Mock
59 private SurfaceHolder mSurfaceHolder;
60
61 @Before
62 public void setUp() throws Exception {
Ahan Wud8609332020-01-15 20:08:08 +080063 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 Lina6fe7542019-08-12 11:45:27 +080085 }
86
87 @Test
Ahan Wu4a297792020-04-16 03:00:14 +080088 public void testInit_normal() {
Ahan Wu287d8282019-12-17 16:23:17 +080089 mEglHelper.init(mSurfaceHolder, false /* wideColorGamut */);
Ahan Wud8609332020-01-15 20:08:08 +080090 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 Wud8609332020-01-15 20:08:08 +080095 }
96
97 @Test
Ahan Wu4a297792020-04-16 03:00:14 +080098 public void testInit_wide_gamut() {
Ahan Wud8609332020-01-15 20:08:08 +080099 // 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 Lina6fe7542019-08-12 11:45:27 +0800112 }
113
114 @Test
Ahan Wu4a297792020-04-16 03:00:14 +0800115 @Ignore
Bill Lina6fe7542019-08-12 11:45:27 +0800116 public void testFinish_shouldNotCrash() {
Ahan Wud8609332020-01-15 20:08:08 +0800117 mEglHelper.terminateEglDisplay();
118 assertThat(mEglHelper.hasEglDisplay()).isFalse();
119 assertThat(mEglHelper.hasEglSurface()).isFalse();
120 assertThat(mEglHelper.hasEglContext()).isFalse();
Bill Lina6fe7542019-08-12 11:45:27 +0800121
122 mEglHelper.finish();
Ahan Wud8609332020-01-15 20:08:08 +0800123 verify(mEglHelper, never()).destroyEglContext();
124 verify(mEglHelper, never()).destroyEglSurface();
125 verify(mEglHelper, atMost(1)).terminateEglDisplay();
Bill Lina6fe7542019-08-12 11:45:27 +0800126 }
127}