xref: /aosp_15_r20/external/minigbm/gbm_unittest.cc (revision d95af8df99a05bcb8679a54dc3ab8e5cd312b38e)
1 /* Copyright 2023 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  *
5  * Test gbm.h module code using gtest.
6  */
7 
8 #include <drm/drm_fourcc.h>
9 #include <gmock/gmock.h>
10 #include <gtest/gtest.h>
11 #include <xf86drm.h>
12 
13 #include "gbm.h"
14 
15 class MockDrm
16 {
17       public:
18 	MOCK_METHOD(drmVersionPtr, drmGetVersion, (int fd));
19 	MOCK_METHOD(void, drmFreeVersion, (drmVersionPtr v));
20 };
21 
22 // Define a mock version of drmGetVersion
drmGetVersion(int fd)23 drmVersionPtr drmGetVersion(int fd)
24 {
25 	drmVersionPtr mock_version = new drmVersion();
26 	mock_version->name = "Mock Backend";
27 	return mock_version;
28 }
29 
30 // Define a mock version of drmFreeVersion
drmFreeVersion(drmVersionPtr v)31 void drmFreeVersion(drmVersionPtr v)
32 {
33 	delete (v);
34 }
35 
36 /* TODO : This is a protocol to add unit tests for the public APIs in minigbm.
37  *
38  * The ultimate goal would be cover more APIs and the input combinations.
39  * Set fd to 0 for now, it doesn't have any particular meaning
40  */
41 
TEST(gbm_unit_test,create_device)42 TEST(gbm_unit_test, create_device)
43 {
44 	MockDrm mock_drm; // Create a mock object
45 
46 	EXPECT_CALL(mock_drm, drmGetVersion(testing::_))
47 	    .WillRepeatedly(testing::Invoke(&mock_drm, &MockDrm::drmGetVersion));
48 
49 	struct gbm_device *gbm_device = gbm_create_device(0);
50 
51 	ASSERT_TRUE(gbm_device);
52 
53 	gbm_device_destroy(gbm_device);
54 }
55 
TEST(gbm_unit_test,valid_fd)56 TEST(gbm_unit_test, valid_fd)
57 {
58 	MockDrm mock_drm; // Create a mock object
59 
60 	EXPECT_CALL(mock_drm, drmGetVersion(testing::_))
61 	    .WillRepeatedly(testing::Invoke(&mock_drm, &MockDrm::drmGetVersion));
62 	struct gbm_device *gbm_device = gbm_create_device(99);
63 	int fd = gbm_device_get_fd(gbm_device);
64 
65 	ASSERT_EQ(fd, 99);
66 
67 	gbm_device_destroy(gbm_device);
68 }
69 
TEST(gbm_unit_test,valid_backend_name)70 TEST(gbm_unit_test, valid_backend_name)
71 {
72 	MockDrm mock_drm; // Create a mock object
73 
74 	EXPECT_CALL(mock_drm, drmGetVersion(testing::_))
75 	    .WillRepeatedly(testing::Invoke(&mock_drm, &MockDrm::drmGetVersion));
76 	struct gbm_device *gbm_device = gbm_create_device(0);
77 	const char *backend_name = gbm_device_get_backend_name(gbm_device);
78 
79 	ASSERT_STREQ(backend_name, "Mock Backend");
80 
81 	gbm_device_destroy(gbm_device);
82 }
83