aboutsummaryrefslogtreecommitdiff
path: root/eclipse/plugins/com.android.ide.eclipse.gldebugger.tests/src/com/android/ide/eclipse/gltrace/format/GLMessageFormatterTest.java
blob: 5d1804102ff6eb18c2f8025f0f3998f672c51f16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * Copyright (C) 2011 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 com.android.ide.eclipse.gltrace.format;

import static org.junit.Assert.*;

import com.android.ide.eclipse.gltrace.GLEnum;
import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage;
import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.Builder;
import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.DataType;
import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.DataType.Type;
import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage.Function;
import com.google.protobuf.ByteString;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GLMessageFormatterTest {
    private static final List<String> API_SPECS = Arrays.asList(
            "void, glBindBuffer, GLenum target, GLuint buffer",
            "const GLchar*, glGetString, GLenum name",
            "void, glMultMatrixf, const GLfloat* m",
            "GLenum, eglBindAPI, GLEnum arg",
            "void, glGetActiveAttrib, GLenum* type",
            "void, glTexImage2D, GLint level, GLsizei width, const GLvoid* pixels");
    private static GLMessageFormatter sGLMessageFormatter;

    static {
        Map<String, GLAPISpec> specs = new HashMap<String, GLAPISpec>(API_SPECS.size());

        for (String specString: API_SPECS) {
            GLAPISpec spec = GLAPISpec.parseLine(specString);
            specs.put(spec.getFunction(), spec);
        }

        sGLMessageFormatter = new GLMessageFormatter(specs);
    }

    @Test
    public void testBindBuffer() {
        GLEnum arg1 = GLEnum.GL_ELEMENT_ARRAY_BUFFER;
        int arg2 = 10;

        GLMessage msg = constructGLMessage(null, Function.glBindBuffer,
                createEnumDataType((int)arg1.value),
                createIntegerDataType(arg2));

        String expected = String.format("glBindBuffer(target = %s, buffer = %s)",
                arg1.toString(),
                Integer.toString(arg2));
        String actual = sGLMessageFormatter.formatGLMessage(msg);

        assertEquals(expected, actual);
    }

    @Test
    public void testGlGetString() {
        String retValue = "testString";
        GLEnum arg1 = GLEnum.GL_RENDERER;

        GLMessage msg = constructGLMessage(
                createStringDataType(retValue),
                Function.glGetString,
                createEnumDataType((int)arg1.value));
        String expected = String.format("%s(name = %s) = (const GLchar*) %s", Function.glGetString,
                arg1.toString(), retValue);
        String actual = sGLMessageFormatter.formatGLMessage(msg);

        assertEquals(expected, actual);
    }

    @Test
    public void testGLEnum0() {
        // an enum of value 0 should equal GL_POINTS if it is an argument,
        // and GL_NO_ERROR if it is a return value
        GLMessage msg = constructGLMessage(
                createEnumDataType(0),
                Function.eglBindAPI,
                createEnumDataType(0));
        String expected = "eglBindAPI(arg = GL_POINTS) = (GLenum) GL_NO_ERROR";
        String actual = sGLMessageFormatter.formatGLMessage(msg);

        assertEquals(expected, actual);
    }

    @Test
    public void testMessageWithPointer() {
        GLMessage msg = constructGLMessage(null,
                Function.glTexImage2D,
                createIntegerDataType(1),
                createIntegerDataType(2),
                createIntegerPointerDataType(0xbadc0ffe));
        String expected = "glTexImage2D(level = 1, width = 2, pixels = 0xbadc0ffe)";
        String actual = sGLMessageFormatter.formatGLMessage(msg);

        assertEquals(expected, actual);
    }

    @Test
    public void testMessageWithMismatchedPointer() {
        // "void, glMultMatrixf, const GLfloat* m",
        GLMessage msg = constructGLMessage(null,
                Function.glMultMatrixf,
                createIntegerDataType(0xbadc0ffe));

        String expected = "glMultMatrixf(m = 0xbadc0ffe)";
        String actual = sGLMessageFormatter.formatGLMessage(msg);

        assertEquals(expected, actual);
    }

    @Test
    public void testMessageWithEnumPointer() {
        //void, glGetActiveAttrib, GLenum* type
        GLMessage msg = constructGLMessage(null,
                Function.glGetActiveAttrib,
                createIntegerPointerDataType((int)GLEnum.GL_FLOAT_MAT4.value));

        String expected = "glGetActiveAttrib(type = [GL_FLOAT_MAT4])";
        String actual = sGLMessageFormatter.formatGLMessage(msg);

        assertEquals(expected, actual);
    }

    private DataType createStringDataType(String retValue) {
        return DataType.newBuilder()
                .addCharValue(ByteString.copyFromUtf8(retValue))
                .setIsArray(true)
                .setType(Type.CHAR)
                .build();
    }

    private DataType createIntegerDataType(int val) {
        return DataType.newBuilder()
                .addIntValue(val)
                .setIsArray(false)
                .setType(Type.INT)
                .build();
    }

    private DataType createIntegerPointerDataType(int val) {
        return DataType.newBuilder()
                .addIntValue(val)
                .setIsArray(true)
                .setType(Type.INT)
                .build();
    }

    private DataType createEnumDataType(int val) {
        return DataType.newBuilder()
                .addIntValue(val)
                .setIsArray(false)
                .setType(Type.ENUM)
                .build();
    }

    private GLMessage constructGLMessage(DataType retValue, Function func, DataType...args) {
        Builder builder = GLMessage.newBuilder();
        builder.setFunction(func);

        // set required fields we don't care about in these tests
        builder.setContextId(0);
        builder.setStartTime(0);
        builder.setDuration(0);

        // set return value if any
        if (retValue != null) {
            builder.setReturnValue(retValue);
        }

        for (DataType arg: args) {
            builder.addArgs(arg);
        }

        return builder.build();
    }
}