1 /*
2  * Copyright 2022 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 androidx.graphics.opengl.egl;
18 
19 import static org.junit.Assert.assertEquals;
20 
21 import android.opengl.EGL14;
22 
23 import androidx.test.ext.junit.runners.AndroidJUnit4;
24 import androidx.test.filters.SmallTest;
25 
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 
29 @SmallTest
30 @RunWith(AndroidJUnit4.class)
31 public class EGLConfigAttributesJavaTest {
32 
33     @Test
testEglConfigAttribute()34     public void testEglConfigAttribute() {
35         EGLConfigAttributes config = new EGLConfigAttributes.Builder()
36                 .setAttribute(1, 2)
37                 .setAttribute(3, 4)
38                 .setAttribute(5, 6)
39                 .build();
40         int[] attrs = config.toArray();
41         assertEquals(7, attrs.length);
42         assertEquals(Integer.valueOf(2), findValueForKey(attrs, 1));
43         assertEquals(Integer.valueOf(4), findValueForKey(attrs, 3));
44         assertEquals(Integer.valueOf(6), findValueForKey(attrs, 5));
45         assertEquals(EGL14.EGL_NONE, attrs[6]);
46     }
47 
48     /**
49      * Helper method that does a linear search of the key in an integer array and returns
50      * the corresponding value for the key.
51      * This assumes the array is structured in an alternating format of key/value pairs and ends
52      * with the value of EGL_NONE
53      * @param attrs Array of ints representing alternating key value pairs, ending with EGL_NONE
54      * @param key Key to search for the corresponding value of
55      * @return Value of the specified key or null if it was not found
56      */
findValueForKey(int[] attrs, int key)57     private Integer findValueForKey(int[] attrs, int key) {
58         for (int i = 0; i < attrs.length; i++) {
59             if (attrs[i] == EGL14.EGL_NONE) {
60                 break;
61             }
62             if (attrs[i] == key) {
63                 if (i < attrs.length - 1) {
64                     return attrs[i + 1];
65                 } else {
66                     return null;
67                 }
68             }
69         }
70         return null;
71     }
72 }
73