test/drm-lease: Create helper functions to reduce boilerplate 26/27326/1
authorDamian Hobson-Garcia <dhobsong@igel.co.jp>
Tue, 28 Sep 2021 08:07:32 +0000 (17:07 +0900)
committerDamian Hobson-Garcia <dhobsong@igel.co.jp>
Mon, 4 Apr 2022 06:45:01 +0000 (15:45 +0900)
Most of the test cases use very similar setup code to configure
the test DRM device and leases.  Refactor this out into separate
functions to help keep the focus on the relevant parts of each
test.

The helpers set up a basic DRM device with one 1:1 mapping of
encoders, CRTCs, and connectors, with support for planes.

Tests that don't adhere to this pattern are likely testing for a
specific configuration, so should be open coding their setup.

Bug-AGL: SPEC-3815

Change-Id: I0adbed04246ff6af0060692fa4c1e5897b903237
Signed-off-by: Damian Hobson-Garcia <dhobsong@igel.co.jp>
drm-lease-manager/test/lease-manager-test.c
drm-lease-manager/test/test-drm-device.c
drm-lease-manager/test/test-drm-device.h

index c741cb3..5d19992 100644 (file)
@@ -33,7 +33,7 @@
 
 #define CHECK_LEASE_OBJECTS(lease, ...)                                     \
        do {                                                                \
-               lm_lease_grant(lm, lease);                                  \
+               lm_lease_grant(g_lm, lease);                                \
                uint32_t objs[] = {__VA_ARGS__};                            \
                int nobjs = ARRAY_LEN(objs);                                \
                ck_assert_int_eq(drmModeCreateLease_fake.arg2_val, nobjs);  \
@@ -61,6 +61,7 @@ FAKE_VALUE_FUNC(int, drmModeCreateLease, int, const uint32_t *, int, int,
 FAKE_VALUE_FUNC(int, drmModeRevokeLease, int, uint32_t);
 
 /************** Test fixutre functions *************************/
+struct lm *g_lm = NULL;
 
 static void test_setup(void)
 {
@@ -86,11 +87,34 @@ static void test_setup(void)
        drmModeGetConnector_fake.custom_fake = get_connector;
        drmModeGetEncoder_fake.custom_fake = get_encoder;
        drmModeCreateLease_fake.custom_fake = create_lease;
+
+       ck_assert_msg(g_lm == NULL,
+                     "Lease manager context not clear at start of test");
 }
 
 static void test_shutdown(void)
 {
        reset_drm_test_device();
+       lm_destroy(g_lm);
+       g_lm = NULL;
+}
+
+static struct lease_handle **create_leases(int num_leases,
+                                          struct lease_config *configs)
+{
+       if (configs)
+               g_lm =
+                   lm_create_with_config(TEST_DRM_DEVICE, num_leases, configs);
+       else
+               g_lm = lm_create(TEST_DRM_DEVICE);
+
+       ck_assert_ptr_ne(g_lm, NULL);
+
+       struct lease_handle **handles;
+       ck_assert_int_eq(num_leases, lm_get_lease_handles(g_lm, &handles));
+       ck_assert_ptr_ne(handles, NULL);
+
+       return handles;
 }
 
 /************** Resource enumeration tests *************/
@@ -114,32 +138,12 @@ START_TEST(all_outputs_connected)
 {
        int out_cnt = 2, plane_cnt = 0;
 
-       ck_assert_int_eq(
-           setup_drm_test_device(out_cnt, out_cnt, out_cnt, plane_cnt), true);
-
-       drmModeConnector connectors[] = {
-           CONNECTOR(CONNECTOR_ID(0), ENCODER_ID(0), &ENCODER_ID(0), 1),
-           CONNECTOR(CONNECTOR_ID(1), ENCODER_ID(1), &ENCODER_ID(1), 1),
-       };
+       setup_layout_simple_test_device(out_cnt, plane_cnt);
 
-       drmModeEncoder encoders[] = {
-           ENCODER(ENCODER_ID(0), CRTC_ID(0), 0x3),
-           ENCODER(ENCODER_ID(1), CRTC_ID(1), 0x2),
-       };
-
-       setup_test_device_layout(connectors, encoders, NULL);
-
-       struct lm *lm = lm_create(TEST_DRM_DEVICE);
-       ck_assert_ptr_ne(lm, NULL);
-
-       struct lease_handle **handles;
-       ck_assert_int_eq(out_cnt, lm_get_lease_handles(lm, &handles));
-       ck_assert_ptr_ne(handles, NULL);
+       struct lease_handle **handles = create_leases(out_cnt, NULL);
 
        CHECK_LEASE_OBJECTS(handles[0], CRTC_ID(0), CONNECTOR_ID(0));
        CHECK_LEASE_OBJECTS(handles[1], CRTC_ID(1), CONNECTOR_ID(1));
-
-       lm_destroy(lm);
 }
 END_TEST
 
@@ -170,17 +174,13 @@ START_TEST(no_outputs_connected)
 
        setup_test_device_layout(connectors, encoders, NULL);
 
-       struct lm *lm = lm_create(TEST_DRM_DEVICE);
-       ck_assert_ptr_ne(lm, NULL);
+       g_lm = lm_create(TEST_DRM_DEVICE);
+       ck_assert_ptr_ne(g_lm, NULL);
 
-       struct lease_handle **handles;
-       ck_assert_int_eq(out_cnt, lm_get_lease_handles(lm, &handles));
-       ck_assert_ptr_ne(handles, NULL);
+       struct lease_handle **handles = create_leases(out_cnt, NULL);
 
        CHECK_LEASE_OBJECTS(handles[0], CRTC_ID(1), CONNECTOR_ID(0));
        CHECK_LEASE_OBJECTS(handles[1], CRTC_ID(0), CONNECTOR_ID(1));
-
-       lm_destroy(lm);
 }
 END_TEST
 
@@ -210,17 +210,10 @@ START_TEST(some_outputs_connected)
 
        setup_test_device_layout(connectors, encoders, NULL);
 
-       struct lm *lm = lm_create(TEST_DRM_DEVICE);
-       ck_assert_ptr_ne(lm, NULL);
-
-       struct lease_handle **handles;
-       ck_assert_int_eq(out_cnt, lm_get_lease_handles(lm, &handles));
-       ck_assert_ptr_ne(handles, NULL);
+       struct lease_handle **handles = create_leases(out_cnt, NULL);
 
        CHECK_LEASE_OBJECTS(handles[0], CRTC_ID(0), CONNECTOR_ID(0));
        CHECK_LEASE_OBJECTS(handles[1], CRTC_ID(1), CONNECTOR_ID(1));
-
-       lm_destroy(lm);
 }
 END_TEST
 
@@ -251,16 +244,10 @@ START_TEST(fewer_crtcs_than_connectors)
 
        setup_test_device_layout(connectors, encoders, NULL);
 
-       struct lm *lm = lm_create(TEST_DRM_DEVICE);
-       ck_assert_ptr_ne(lm, NULL);
-
-       struct lease_handle **handles;
-       ck_assert_int_eq(lm_get_lease_handles(lm, &handles), crtc_cnt);
-       ck_assert_ptr_ne(handles, NULL);
+       struct lease_handle **handles = create_leases(crtc_cnt, NULL);
 
        CHECK_LEASE_OBJECTS(handles[0], CRTC_ID(0), CONNECTOR_ID(0));
        CHECK_LEASE_OBJECTS(handles[1], CRTC_ID(1), CONNECTOR_ID(2));
-       lm_destroy(lm);
 }
 END_TEST
 
@@ -275,39 +262,14 @@ START_TEST(separate_overlay_planes_by_crtc)
 
        int out_cnt = 2, plane_cnt = 3;
 
-       ck_assert_int_eq(
-           setup_drm_test_device(out_cnt, out_cnt, out_cnt, plane_cnt), true);
+       setup_layout_simple_test_device(out_cnt, plane_cnt);
 
-       drmModeConnector connectors[] = {
-           CONNECTOR(CONNECTOR_ID(0), ENCODER_ID(0), &ENCODER_ID(0), 1),
-           CONNECTOR(CONNECTOR_ID(1), ENCODER_ID(1), &ENCODER_ID(1), 1),
-       };
-
-       drmModeEncoder encoders[] = {
-           ENCODER(ENCODER_ID(0), CRTC_ID(0), 0x1),
-           ENCODER(ENCODER_ID(1), CRTC_ID(1), 0x2),
-       };
-
-       drmModePlane planes[] = {
-           PLANE(PLANE_ID(0), 0x2),
-           PLANE(PLANE_ID(1), 0x1),
-           PLANE(PLANE_ID(2), 0x2),
-       };
-
-       setup_test_device_layout(connectors, encoders, planes);
-
-       struct lm *lm = lm_create(TEST_DRM_DEVICE);
-       ck_assert_ptr_ne(lm, NULL);
-
-       struct lease_handle **handles;
-       ck_assert_int_eq(out_cnt, lm_get_lease_handles(lm, &handles));
-       ck_assert_ptr_ne(handles, NULL);
+       struct lease_handle **handles = create_leases(out_cnt, NULL);
 
-       CHECK_LEASE_OBJECTS(handles[0], PLANE_ID(1), CRTC_ID(0),
+       CHECK_LEASE_OBJECTS(handles[0], PLANE_ID(0), PLANE_ID(2), CRTC_ID(0),
                            CONNECTOR_ID(0));
-       CHECK_LEASE_OBJECTS(handles[1], PLANE_ID(0), PLANE_ID(2), CRTC_ID(1),
+       CHECK_LEASE_OBJECTS(handles[1], PLANE_ID(1), CRTC_ID(1),
                            CONNECTOR_ID(1));
-       lm_destroy(lm);
 }
 END_TEST
 
@@ -344,18 +306,12 @@ START_TEST(reject_planes_shared_between_multiple_crtcs)
 
        setup_test_device_layout(connectors, encoders, planes);
 
-       struct lm *lm = lm_create(TEST_DRM_DEVICE);
-       ck_assert_ptr_ne(lm, NULL);
-
-       struct lease_handle **handles;
-       ck_assert_int_eq(out_cnt, lm_get_lease_handles(lm, &handles));
-       ck_assert_ptr_ne(handles, NULL);
+       struct lease_handle **handles = create_leases(out_cnt, NULL);
 
        CHECK_LEASE_OBJECTS(handles[0], PLANE_ID(1), CRTC_ID(0),
                            CONNECTOR_ID(0));
        CHECK_LEASE_OBJECTS(handles[1], PLANE_ID(0), CRTC_ID(1),
                            CONNECTOR_ID(1));
-       lm_destroy(lm);
 }
 END_TEST
 
@@ -382,32 +338,15 @@ static void add_connector_enum_tests(Suite *s)
  */
 START_TEST(create_and_revoke_lease)
 {
-       int lease_cnt = 2;
-       bool res = setup_drm_test_device(lease_cnt, lease_cnt, lease_cnt, 0);
-       ck_assert_int_eq(res, true);
+       int lease_cnt = 2, plane_cnt = 0;
 
-       drmModeConnector connectors[] = {
-           CONNECTOR(CONNECTOR_ID(0), ENCODER_ID(0), &ENCODER_ID(0), 1),
-           CONNECTOR(CONNECTOR_ID(1), ENCODER_ID(1), &ENCODER_ID(1), 1),
-       };
+       setup_layout_simple_test_device(lease_cnt, plane_cnt);
 
-       drmModeEncoder encoders[] = {
-           ENCODER(ENCODER_ID(0), CRTC_ID(0), 0x1),
-           ENCODER(ENCODER_ID(1), CRTC_ID(1), 0x2),
-       };
-
-       setup_test_device_layout(connectors, encoders, NULL);
-
-       struct lm *lm = lm_create(TEST_DRM_DEVICE);
-       ck_assert_ptr_ne(lm, NULL);
-
-       struct lease_handle **handles;
-       ck_assert_int_eq(lease_cnt, lm_get_lease_handles(lm, &handles));
-       ck_assert_ptr_ne(handles, NULL);
+       struct lease_handle **handles = create_leases(lease_cnt, NULL);
 
        for (int i = 0; i < lease_cnt; i++) {
-               ck_assert_int_ge(lm_lease_grant(lm, handles[i]), 0);
-               lm_lease_revoke(lm, handles[i]);
+               ck_assert_int_ge(lm_lease_grant(g_lm, handles[i]), 0);
+               lm_lease_revoke(g_lm, handles[i]);
        }
 
        ck_assert_int_eq(drmModeRevokeLease_fake.call_count, lease_cnt);
index c024d6e..1d1e096 100644 (file)
@@ -76,6 +76,13 @@ void reset_drm_test_device(void)
        free(test_device.resources.encoders);
        free(test_device.plane_resources.planes);
        free(test_device.leases.lessee_ids);
+
+       if (test_device.layout.free_on_reset) {
+               free(test_device.layout.connectors);
+               free(test_device.layout.encoders);
+               free(test_device.layout.planes);
+       }
+
        memset(&test_device, 0, sizeof(test_device));
 }
 
@@ -87,6 +94,42 @@ void setup_test_device_layout(drmModeConnector *connectors,
        test_device.layout.planes = planes;
 }
 
+void setup_layout_simple_test_device(int conn_cnt, int plane_cnt)
+{
+       drmModeConnector *connectors;
+       drmModeEncoder *encoders;
+       drmModePlane *planes = NULL;
+
+       ck_assert_int_ge(conn_cnt, 1);
+
+       setup_drm_test_device(conn_cnt, conn_cnt, conn_cnt, plane_cnt);
+
+       ck_assert_ptr_ne(
+           connectors = calloc(sizeof(drmModeConnector), conn_cnt), NULL);
+       ck_assert_ptr_ne(encoders = calloc(sizeof(drmModeEncoder), conn_cnt),
+                        NULL);
+
+       if (plane_cnt > 0)
+               ck_assert_ptr_ne(
+                   planes = calloc(sizeof(drmModePlane), plane_cnt), NULL);
+
+       int crtc_mask = (1 << conn_cnt) - 1;
+       for (int i = 0; i < conn_cnt; i++) {
+               connectors[i] = (drmModeConnector)CONNECTOR(
+                   CONNECTOR_ID(i), ENCODER_ID(i), &ENCODER_ID(i), 1);
+               encoders[i] = (drmModeEncoder)ENCODER(ENCODER_ID(i), CRTC_ID(i),
+                                                     crtc_mask);
+       }
+
+       for (int i = 0; i < plane_cnt; i++) {
+               planes[i] =
+                   (drmModePlane)PLANE(PLANE_ID(i), 1 << (i % conn_cnt));
+       }
+
+       setup_test_device_layout(connectors, encoders, planes);
+       test_device.layout.free_on_reset = true;
+}
+
 #define GET_DRM_RESOURCE_FN(Res, res, RES, container)                       \
        drmMode##Res##Ptr get_##res(int fd, uint32_t id)                    \
        {                                                                   \
index 1d5b683..e2e5f52 100644 (file)
@@ -30,6 +30,7 @@ struct drm_device {
                drmModeConnector *connectors;
                drmModeEncoder *encoders;
                drmModePlane *planes;
+               bool free_on_reset;
        } layout;
 
        struct {
@@ -44,6 +45,7 @@ extern struct drm_device test_device;
 bool setup_drm_test_device(int crtcs, int connectors, int encoders, int planes);
 void setup_test_device_layout(drmModeConnector *connectors,
                              drmModeEncoder *encoders, drmModePlane *planes);
+void setup_layout_simple_test_device(int connectors, int planes);
 void reset_drm_test_device(void);
 
 drmModeConnectorPtr get_connector(int fd, uint32_t id);