Home | History | Annotate | Download | only in format
      1 /*
      2  * Copyright (C) 2011 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.ide.eclipse.gltrace.format;
     18 
     19 import static org.junit.Assert.*;
     20 
     21 import com.android.ide.eclipse.gltrace.GLEnum;
     22 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage;
     23 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.Builder;
     24 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.DataType;
     25 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.DataType.Type;
     26 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.Function;
     27 import com.google.protobuf.ByteString;
     28 
     29 import org.junit.Test;
     30 
     31 import java.util.Arrays;
     32 import java.util.HashMap;
     33 import java.util.List;
     34 import java.util.Map;
     35 
     36 public class GLMessageFormatterTest {
     37     private static final List<String> API_SPECS = Arrays.asList(
     38             "void, glBindBuffer, GLenum target, GLuint buffer",
     39             "const GLchar*, glGetString, GLenum name",
     40             "void, glMultMatrixf, const GLfloat* m",
     41             "GLenum, eglBindAPI, GLEnum arg",
     42             "void, glGetActiveAttrib, GLenum* type",
     43             "void, glTexImage2D, GLint level, GLsizei width, const GLvoid* pixels");
     44     private static GLMessageFormatter sGLMessageFormatter;
     45 
     46     static {
     47         Map<String, GLAPISpec> specs = new HashMap<String, GLAPISpec>(API_SPECS.size());
     48 
     49         for (String specString: API_SPECS) {
     50             GLAPISpec spec = GLAPISpec.parseLine(specString);
     51             specs.put(spec.getFunction(), spec);
     52         }
     53 
     54         sGLMessageFormatter = new GLMessageFormatter(specs);
     55     }
     56 
     57     @Test
     58     public void testBindBuffer() {
     59         GLEnum arg1 = GLEnum.GL_ELEMENT_ARRAY_BUFFER;
     60         int arg2 = 10;
     61 
     62         GLMessage msg = constructGLMessage(null, Function.glBindBuffer,
     63                 createEnumDataType((int)arg1.value),
     64                 createIntegerDataType(arg2));
     65 
     66         String expected = String.format("glBindBuffer(target = %s, buffer = %s)",
     67                 arg1.toString(),
     68                 Integer.toString(arg2));
     69         String actual = sGLMessageFormatter.formatGLMessage(msg);
     70 
     71         assertEquals(expected, actual);
     72     }
     73 
     74     @Test
     75     public void testGlGetString() {
     76         String retValue = "testString";
     77         GLEnum arg1 = GLEnum.GL_RENDERER;
     78 
     79         GLMessage msg = constructGLMessage(
     80                 createStringDataType(retValue),
     81                 Function.glGetString,
     82                 createEnumDataType((int)arg1.value));
     83         String expected = String.format("%s(name = %s) = (const GLchar*) %s", Function.glGetString,
     84                 arg1.toString(), retValue);
     85         String actual = sGLMessageFormatter.formatGLMessage(msg);
     86 
     87         assertEquals(expected, actual);
     88     }
     89 
     90     @Test
     91     public void testGLEnum0() {
     92         // an enum of value 0 should equal GL_POINTS if it is an argument,
     93         // and GL_NO_ERROR if it is a return value
     94         GLMessage msg = constructGLMessage(
     95                 createEnumDataType(0),
     96                 Function.eglBindAPI,
     97                 createEnumDataType(0));
     98         String expected = "eglBindAPI(arg = GL_POINTS) = (GLenum) GL_NO_ERROR";
     99         String actual = sGLMessageFormatter.formatGLMessage(msg);
    100 
    101         assertEquals(expected, actual);
    102     }
    103 
    104     @Test
    105     public void testMessageWithPointer() {
    106         GLMessage msg = constructGLMessage(null,
    107                 Function.glTexImage2D,
    108                 createIntegerDataType(1),
    109                 createIntegerDataType(2),
    110                 createIntegerPointerDataType(0xbadc0ffe));
    111         String expected = "glTexImage2D(level = 1, width = 2, pixels = 0xbadc0ffe)";
    112         String actual = sGLMessageFormatter.formatGLMessage(msg);
    113 
    114         assertEquals(expected, actual);
    115     }
    116 
    117     @Test
    118     public void testMessageWithMismatchedPointer() {
    119         // "void, glMultMatrixf, const GLfloat* m",
    120         GLMessage msg = constructGLMessage(null,
    121                 Function.glMultMatrixf,
    122                 createIntegerDataType(0xbadc0ffe));
    123 
    124         String expected = "glMultMatrixf(m = 0xbadc0ffe)";
    125         String actual = sGLMessageFormatter.formatGLMessage(msg);
    126 
    127         assertEquals(expected, actual);
    128     }
    129 
    130     @Test
    131     public void testMessageWithEnumPointer() {
    132         //void, glGetActiveAttrib, GLenum* type
    133         GLMessage msg = constructGLMessage(null,
    134                 Function.glGetActiveAttrib,
    135                 createIntegerPointerDataType((int)GLEnum.GL_FLOAT_MAT4.value));
    136 
    137         String expected = "glGetActiveAttrib(type = [GL_FLOAT_MAT4])";
    138         String actual = sGLMessageFormatter.formatGLMessage(msg);
    139 
    140         assertEquals(expected, actual);
    141     }
    142 
    143     private DataType createStringDataType(String retValue) {
    144         return DataType.newBuilder()
    145                 .addCharValue(ByteString.copyFromUtf8(retValue))
    146                 .setIsArray(true)
    147                 .setType(Type.CHAR)
    148                 .build();
    149     }
    150 
    151     private DataType createIntegerDataType(int val) {
    152         return DataType.newBuilder()
    153                 .addIntValue(val)
    154                 .setIsArray(false)
    155                 .setType(Type.INT)
    156                 .build();
    157     }
    158 
    159     private DataType createIntegerPointerDataType(int val) {
    160         return DataType.newBuilder()
    161                 .addIntValue(val)
    162                 .setIsArray(true)
    163                 .setType(Type.INT)
    164                 .build();
    165     }
    166 
    167     private DataType createEnumDataType(int val) {
    168         return DataType.newBuilder()
    169                 .addIntValue(val)
    170                 .setIsArray(false)
    171                 .setType(Type.ENUM)
    172                 .build();
    173     }
    174 
    175     private GLMessage constructGLMessage(DataType retValue, Function func, DataType...args) {
    176         Builder builder = GLMessage.newBuilder();
    177         builder.setFunction(func);
    178 
    179         // set required fields we don't care about in these tests
    180         builder.setContextId(0);
    181         builder.setStartTime(0);
    182         builder.setDuration(0);
    183 
    184         // set return value if any
    185         if (retValue != null) {
    186             builder.setReturnValue(retValue);
    187         }
    188 
    189         for (DataType arg: args) {
    190             builder.addArgs(arg);
    191         }
    192 
    193         return builder.build();
    194     }
    195 }
    196