/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.graphics.opengl.egl;

import static org.junit.Assert.assertEquals;

import android.opengl.EGL14;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import org.junit.Test;
import org.junit.runner.RunWith;

@SmallTest
@RunWith(AndroidJUnit4.class)
public class EGLConfigAttributesJavaTest {

    @Test
    public void testEglConfigAttribute() {
        EGLConfigAttributes config = new EGLConfigAttributes.Builder()
                .setAttribute(1, 2)
                .setAttribute(3, 4)
                .setAttribute(5, 6)
                .build();
        int[] attrs = config.toArray();
        assertEquals(7, attrs.length);
        assertEquals(1, attrs[0]);
        assertEquals(2, attrs[1]);
        assertEquals(3, attrs[2]);
        assertEquals(4, attrs[3]);
        assertEquals(5, attrs[4]);
        assertEquals(6, attrs[5]);
        assertEquals(EGL14.EGL_NONE, attrs[6]);
    }
}

@@ -1324,19 +1324,53 @@
 als(
-1, 
+Integer.valueOf(2), findValueForKey(
 attrs
-%5B0%5D
+, 1)
 );%0A 
@@ -1393,19 +1393,53 @@
 als(
-2, 
+Integer.valueOf(4), findValueForKey(
 attrs
-%5B1%5D
+, 3)
 );%0A 
@@ -1462,19 +1462,53 @@
 als(
-3, 
+Integer.valueOf(6), findValueForKey(
 attrs
-%5B2%5D
+, 5)
 );%0A 
@@ -1531,17 +1531,30 @@
 als(
-4
+EGL14.EGL_NONE
 , attrs%5B
 3%5D);
@@ -1553,17 +1553,17 @@
 trs%5B
-3
+6
 %5D);%0A    
     
@@ -1562,120 +1562,953 @@
     
+%7D%0A%0A
     
-assertEquals(5, attrs%5B4%5D);%0A        assertEquals(6, attrs%5B5%5D);%0A        assertEquals(EGL14.EGL_NONE, attrs%5B6%5D)
+/**%0A     * Helper method that does a linear search of the key in an integer array and returns%0A     * the corresponding value for the key.%0A     * This assumes the array is structured in an alternating format of key/value pairs and ends%0A     * with the value of EGL_NONE%0A     * @param attrs Array of ints representing alternating key value pairs, ending with EGL_NONE%0A     * @param key Key to search for the corresponding value of%0A     * @return Value of the specified key or null if it was not found%0A     */%0A    private Integer findValueForKey(int%5B%5D attrs, int key) %7B%0A        for (int i = 0; i %3C attrs.length; i++) %7B%0A            if (attrs%5Bi%5D == EGL14.EGL_NONE) %7B%0A                break;%0A            %7D%0A            if (attrs%5Bi%5D == key) %7B%0A                if (i %3C attrs.length - 1) %7B%0A                    return attrs%5Bi + 1%5D;%0A                %7D else %7B%0A                    return null;%0A                %7D%0A            %7D%0A        %7D%0A        return null
 ;%0A  

Fix EGLConfigAttributesJavaTest