Testing a function like draw_array, which involves OpenGL calls, can be challenging with Google Test because these calls depend on GPU hardware and drivers. Additionally, the function is static and involves graphical operations that are not straightforward to verify through traditional unit testing methods.
However, you can still approach this by focusing on the abstraction layer that interacts with OpenGL, and by using mocks or fake objects. Here’s a general strategy on how to write tests for such a function:
-
Abstract OpenGL Calls: Wrap OpenGL calls within your own interfaces or function pointers. This allows you to replace OpenGL operations with mock operations during testing.
-
Introduce Dependency Injection: Modify or wrap your draw_array function or its surrounding context so that it uses injected dependencies for OpenGL operations. This could mean passing function pointers or objects that encapsulate OpenGL calls.
-
Use Mocks for OpenGL Calls: With your dependencies abstracted, use a mocking framework compatible with Google Test (like Google Mock) to create mock versions of your OpenGL functions or abstractions.
Let's break down this approach focusing on what changes might be necessary:
Step 1: Abstract OpenGL Functions
Assume you have an abstraction for OpenGL operations:
class OpenGLWrapper {
public:
virtual void glVertexAttribPointer(GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer) = 0;
virtual void glDrawArrays(GLenum mode, GLint first, GLsizei count) = 0;
virtual ~OpenGLWrapper() = default;
};
Step 2: Modify draw_array to Use Abstractions
Modify draw_array to accept an OpenGLWrapper object or pointer, through which you call OpenGL functions:
void draw_array(struct graphics_priv *gr, struct point *p, int count, GLenum mode, OpenGLWrapper* opengl) {
// Theory remains the same, but use opengl->glVertexAttribPointer and opengl->glDrawArrays
}
Step 3: Create Mocks for OpenGLWrapper
Using Google Mock, create mock methods for the OpenGL functions:
class MockOpenGLWrapper : public OpenGLWrapper {
public:
MOCK_METHOD(void, glVertexAttribPointer, (GLuint index, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void *pointer), (override));
MOCK_METHOD(void, glDrawArrays, (GLenum mode, GLint first, GLsizei count), (override));
};
Step 4: Write Tests Using the Mock Object
Write your Google Test cases using MockOpenGLWrapper to set expectations on OpenGL calls:
TEST(DrawArrayTest, CallsOpenGLFunctionsCorrectly) {
struct graphics_priv gr;
struct point points[2] = {{1, 2}, {3, 4}};
int count = 2;
GLenum mode = GL_TRIANGLES; // Example mode
MockOpenGLWrapper mockOpenGL;
EXPECT_CALL(mockOpenGL, glVertexAttribPointer(_,_,_,_,_,_))
.Times(1); // Expect it to be called once, adapt as needed
EXPECT_CALL(mockOpenGL, glDrawArrays(mode, 0, count))
.Times(1); // Expect it to be called once with specified mode and count
draw_array(&gr, points, count, mode, &mockOpenGL);
}
Conclusion
This approach allows you to verify that draw_array makes the correct OpenGL calls with expected parameters, without requiring an actual OpenGL environment during tests. You've abstracted the OpenGL dependency, allowing your tests to focus on logic and behavior rather than the graphics hardware.
Remember, this refactor and abstraction process might involve significant changes to your existing codebase. The value comes from making your graphics-related code more testable and modular, allowing better isolation of graphics logic from your application logic.