ut: improve code coverage of tdm_buffer.c 30/158230/4
authorKonstantin Drabeniuk <k.drabeniuk@samsung.com>
Wed, 14 Dec 2016 14:30:15 +0000 (16:30 +0200)
committerSooChan Lim <sc1.lim@samsung.com>
Mon, 30 Oct 2017 11:29:32 +0000 (11:29 +0000)
Change-Id: Ie003d457b800029c280952303c8a0d4b1a97edd6
Signed-off-by: Konstantin Drabeniuk <k.drabeniuk@samsung.com>
Signed-off-by: SooChan Lim <sc1.lim@samsung.com>
ut/src/ut_tdm_buffer.cpp
ut/stubs/stub_tdm.h
ut/stubs/tbm_stubs.cpp
ut/stubs/tbm_stubs.h

index 2eab3d961a0080e3c72f616c9c8295ad1fdc4dac..4ebac1072846adf5ed6bf2bc5c3bec80588a6522 100644 (file)
@@ -30,8 +30,8 @@
 
 #include "tbm_stubs.h"
 #include "stub_pthread.h"
-#include "tdm_list_stubs.h"
 #include "stub_stdlib.h"
+#include "stub_tdm.h"
 
 #include "tdm_buffer.c"
 
@@ -43,9 +43,11 @@ void tdm_buffer_release_handler_f(tbm_surface_h buffer, void *user_data)
 
 static void _init_test()
 {
+       stub_tdm_init();
        stub_pthread_init();
        stub_tbm_init();
        stub_stdlib_init();
+       tdm_debug_module = TDM_DEBUG_BUFFER;
 }
 
 /* tdm_buffer_list_get_first_entry */
@@ -62,6 +64,39 @@ TEST(tdm_buffer_list_get_first_entry, null_ptr_fail_1)
        ASSERT_TRUE(actual == expected);
 }
 
+TEST(tdm_buffer_list_get_first_entry, success_list_is_empty)
+{
+       tbm_surface_h actual;
+       struct list_head buff_list;
+
+       _init_test();
+
+       LIST_INITHEAD(&buff_list);
+
+       actual = tdm_buffer_list_get_first_entry(&buff_list);
+
+       ASSERT_TRUE(actual == NULL);
+}
+
+TEST(tdm_buffer_list_get_first_entry, success)
+{
+       tbm_surface_h actual;
+       struct list_head buff_list;
+       tdm_buffer_info buf_info;
+       struct _tbm_surface buffer;
+
+       _init_test();
+
+       buf_info.buffer = &buffer;
+
+       LIST_INITHEAD(&buff_list);
+       LIST_ADD(&buf_info.link, &buff_list);
+
+       actual = tdm_buffer_list_get_first_entry(&buff_list);
+
+       ASSERT_TRUE(actual == &buffer);
+}
+
 /* tdm_buffer_ref_backend */
 
 TEST(tdm_buffer_ref_backend, work_flow_success_2)
@@ -216,6 +251,36 @@ TEST(tdm_buffer_add_release_handler, null_ptr_fail_1)
 
 /* tdm_buffer_get_info */
 
+TEST(tdm_buffer_get_info, work_flow_success_5)
+{
+       struct _tbm_surface buffer;
+       tdm_buffer_info *actual;
+
+       _init_test();
+
+       TBM_BO_GET_USER_DATA_NULL = 1;
+       tbm_surface_internal_set_user_data_error = 1;
+
+       actual = tdm_buffer_get_info(&buffer);
+
+       ASSERT_TRUE(actual == NULL);
+}
+
+TEST(tdm_buffer_get_info, work_flow_success_4)
+{
+       struct _tbm_surface buffer;
+       tdm_buffer_info *actual;
+
+       _init_test();
+
+       TBM_BO_GET_USER_DATA_NULL = 1;
+       tbm_surface_internal_add_user_data_error = 1;
+
+       actual = tdm_buffer_get_info(&buffer);
+
+       ASSERT_TRUE(actual == NULL);
+}
+
 TEST(tdm_buffer_get_info, work_flow_success_3)
 {
        struct _tbm_surface buffer;
@@ -259,3 +324,232 @@ TEST(tdm_buffer_get_info, work_flow_success_1)
 
        ASSERT_TRUE(actual != not_expected);
 }
+
+/* tdm_buffer_remove_release_handler() */
+
+TEST(tdm_buffer_remove_release_handler, success)
+{
+       struct _tbm_surface buffer;
+       int user_data;
+       tdm_buffer_func_info *func_info_1;
+       tdm_buffer_func_info *func_info_2;
+       tdm_buffer_info buf_info;
+       tdm_buffer_func_info *func_info = NULL, *next = NULL;
+
+       _init_test();
+
+       func_info_1 = (tdm_buffer_func_info *)calloc(1, sizeof * func_info_1);
+       func_info_2 = (tdm_buffer_func_info *)calloc(1, sizeof * func_info_2);
+
+       func_info_1->release_func = stub_tdm_buffer_release_handler;
+       func_info_1->user_data = &user_data;
+
+       func_info_2->release_func = NULL;
+       func_info_2->user_data = NULL;
+
+       tbm_surface_internal_get_user_data_ret = &buf_info;
+
+       LIST_INITHEAD(&buf_info.release_funcs);
+       LIST_ADD(&func_info_1->link, &buf_info.release_funcs);
+       LIST_ADD(&func_info_2->link, &buf_info.release_funcs);
+
+       tdm_buffer_remove_release_handler(&buffer, stub_tdm_buffer_release_handler, &user_data);
+
+       ASSERT_EQ(FREE_CALLED, 1);
+       LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info.release_funcs, link) {
+               if (func_info->release_func != stub_tdm_buffer_release_handler || func_info->user_data != &user_data)
+                       continue;
+
+               ASSERT_TRUE(0);
+       }
+       free(func_info_2);
+}
+
+/* tdm_buffer_remove_release_handler_internal() */
+
+TEST(tdm_buffer_remove_release_handler_internal, success)
+{
+       struct _tbm_surface buffer;
+       tdm_buffer_func_info *func_info_1;
+       tdm_buffer_func_info *func_info_2;
+       tdm_buffer_info buf_info;
+
+       _init_test();
+
+       func_info_1 = (tdm_buffer_func_info *)calloc(1, sizeof * func_info_1);
+       func_info_2 = (tdm_buffer_func_info *)calloc(1, sizeof * func_info_2);
+
+       tbm_surface_internal_get_user_data_ret = &buf_info;
+
+       LIST_INITHEAD(&buf_info.release_funcs);
+       LIST_ADD(&func_info_1->link, &buf_info.release_funcs);
+       LIST_ADD(&func_info_2->link, &buf_info.release_funcs);
+
+       tdm_buffer_remove_release_handler_internal(&buffer);
+
+       ASSERT_EQ(FREE_CALLED, 1);
+       free(func_info_1);
+}
+
+/* tdm_buffer_unref_backend() */
+
+TEST(tdm_buffer_unref_backend, success_ref_count_is_3)
+{
+       struct _tbm_surface buffer;
+       tdm_buffer_info buf_info;
+
+       _init_test();
+
+       tbm_surface_internal_get_user_data_ret = &buf_info;
+       buf_info.backend_ref_count = 3;
+
+       tdm_buffer_unref_backend(&buffer);
+
+       ASSERT_EQ(buf_info.backend_ref_count, 2);
+}
+
+TEST(tdm_buffer_unref_backend, success)
+{
+       struct _tbm_surface buffer;
+       tdm_buffer_info buf_info;
+       tdm_buffer_func_info func_info;
+
+       _init_test();
+
+       tbm_surface_internal_get_user_data_ret = &buf_info;
+       buf_info.backend_ref_count = 1;
+       func_info.release_func = stub_tdm_buffer_release_handler;
+
+       LIST_INITHEAD(&buf_info.release_funcs);
+       LIST_ADD(&func_info.link, &buf_info.release_funcs);
+
+       tdm_buffer_unref_backend(&buffer);
+
+       ASSERT_EQ(stub_tdm_buffer_release_handler_count, 1);
+       ASSERT_EQ(tbm_surface_internal_ref_count, 1);
+       ASSERT_EQ(tbm_surface_internal_unref_count, 2);
+}
+
+/* tdm_buffer_add_destroy_handler() */
+
+TEST(tdm_buffer_add_destroy_handler, error_buffer_is_null)
+{
+       tdm_error error;
+
+       _init_test();
+
+       error = tdm_buffer_add_destroy_handler(NULL, stub_tdm_buffer_destroy_handler, NULL);
+
+       ASSERT_EQ(error, TDM_ERROR_INVALID_PARAMETER);
+}
+
+TEST(tdm_buffer_add_destroy_handler, error_func_is_null)
+{
+       tdm_error error;
+       struct _tbm_surface buffer;
+
+       _init_test();
+
+       error = tdm_buffer_add_destroy_handler(&buffer, NULL, NULL);
+
+       ASSERT_EQ(error, TDM_ERROR_INVALID_PARAMETER);
+}
+
+TEST(tdm_buffer_add_destroy_handler, error_buffer_get_info)
+{
+       tdm_error error;
+       struct _tbm_surface buffer;
+
+       _init_test();
+
+       tbm_surface_internal_get_user_data_ret = NULL;
+       tbm_surface_internal_add_user_data_error = 1;
+
+       error = tdm_buffer_add_destroy_handler(&buffer, stub_tdm_buffer_destroy_handler, NULL);
+
+       ASSERT_EQ(error, TDM_ERROR_OUT_OF_MEMORY);
+}
+
+TEST(tdm_buffer_add_destroy_handler, error_calloc)
+{
+       tdm_error error;
+       struct _tbm_surface buffer;
+       tdm_buffer_info buf_info;
+
+       _init_test();
+
+       tbm_surface_internal_get_user_data_ret = &buf_info;
+
+       CALLOC_ERROR = 1;
+
+       error = tdm_buffer_add_destroy_handler(&buffer, stub_tdm_buffer_destroy_handler, NULL);
+
+       ASSERT_EQ(error, TDM_ERROR_OUT_OF_MEMORY);
+}
+
+TEST(tdm_buffer_add_destroy_handler, success)
+{
+       tdm_error error;
+       struct _tbm_surface buffer;
+       tdm_buffer_info buf_info;
+       int user_data;
+       tdm_buffer_func_info *func_info = NULL, *f = NULL;
+
+       _init_test();
+
+       tbm_surface_internal_get_user_data_ret = &buf_info;
+
+       LIST_INITHEAD(&buf_info.destroy_funcs);
+
+       error = tdm_buffer_add_destroy_handler(&buffer, stub_tdm_buffer_destroy_handler, &user_data);
+
+       ASSERT_EQ(error, TDM_ERROR_NONE);
+       ASSERT_TRUE(!LIST_IS_EMPTY(&buf_info.destroy_funcs));
+       LIST_FOR_EACH_ENTRY(f, &buf_info.destroy_funcs, link) {
+               if (f->destroy_func != stub_tdm_buffer_destroy_handler || f->user_data != &user_data)
+                       continue;
+               func_info = f;
+       }
+       ASSERT_TRUE(func_info);
+       free(func_info);
+}
+
+/* tdm_buffer_remove_release_handler() */
+
+TEST(tdm_buffer_remove_destroy_handler, success)
+{
+       struct _tbm_surface buffer;
+       int user_data;
+       tdm_buffer_func_info *func_info_1;
+       tdm_buffer_func_info *func_info_2;
+       tdm_buffer_info buf_info;
+       tdm_buffer_func_info *func_info = NULL, *next = NULL;
+
+       _init_test();
+
+       func_info_1 = (tdm_buffer_func_info *)calloc(1, sizeof * func_info_1);
+       func_info_2 = (tdm_buffer_func_info *)calloc(1, sizeof * func_info_2);
+
+       func_info_1->destroy_func = stub_tdm_buffer_destroy_handler;
+       func_info_1->user_data = &user_data;
+
+       func_info_2->release_func = NULL;
+       func_info_2->user_data = NULL;
+
+       tbm_surface_internal_get_user_data_ret = &buf_info;
+
+       LIST_INITHEAD(&buf_info.destroy_funcs);
+       LIST_ADD(&func_info_1->link, &buf_info.destroy_funcs);
+       LIST_ADD(&func_info_2->link, &buf_info.destroy_funcs);
+
+       tdm_buffer_remove_destroy_handler(&buffer, stub_tdm_buffer_destroy_handler, &user_data);
+
+       ASSERT_EQ(FREE_CALLED, 1);
+       LIST_FOR_EACH_ENTRY_SAFE(func_info, next, &buf_info.destroy_funcs, link) {
+               if (func_info->release_func != stub_tdm_buffer_destroy_handler || func_info->user_data != &user_data)
+                       continue;
+
+               ASSERT_TRUE(0);
+       }
+       free(func_info_2);
+}
index b8c05279fd8e0a7b4afd9ef1a09c1abd9df50850..d30fd30374fa8613b7cd315093fe34fad1945091 100644 (file)
@@ -7,6 +7,7 @@ static tdm_private_output stub_private_output;
 static int stub_tdm_vblank_handler_call_count;
 static int stub_tdm_output_change_handler_count;
 static int stub_tdm_output_commit_handler_count;
+static int stub_tdm_buffer_release_handler_count;
 
 static void
 stub_tdm_init()
@@ -14,6 +15,7 @@ stub_tdm_init()
        stub_tdm_vblank_handler_call_count = 0;
        stub_tdm_output_change_handler_count = 0;
        stub_tdm_output_commit_handler_count = 0;
+       stub_tdm_buffer_release_handler_count = 0;
 }
 
 static tdm_private_output*
@@ -29,6 +31,13 @@ static void
 stub_tdm_vblank_handler(tdm_vblank *vblank, tdm_error error, unsigned int sequence,
                                                unsigned int tv_sec, unsigned int tv_usec, void *user_data)
 {
+       (void)vblank;
+       (void)error;
+       (void)sequence;
+       (void)tv_sec;
+       (void)tv_usec;
+       (void)user_data;
+
        stub_tdm_vblank_handler_call_count++;
 }
 
@@ -36,6 +45,10 @@ static tdm_error
 stub_tdm_display_update_output(tdm_private_display *private_display,
                                                  tdm_output *output_backend, int pipe)
 {
+       (void)private_display;
+       (void)output_backend;
+       (void)pipe;
+
        return TDM_ERROR_NONE;
 }
 
@@ -45,6 +58,11 @@ stub_tdm_output_change_handler_cb(tdm_output *output,
                                                                  tdm_value value,
                                                                  void *user_data)
 {
+       (void)output;
+       (void)type;
+       (void)value;
+       (void)user_data;
+
        stub_tdm_output_change_handler_count++;
 }
 
@@ -53,9 +71,33 @@ stub_tdm_output_commit_handler(tdm_output *output, unsigned int sequence,
                                                           unsigned int tv_sec, unsigned int tv_usec,
                                                           void *user_data)
 {
+       (void)output;
+       (void)sequence;
+       (void)tv_sec;
+       (void)tv_usec;
+       (void)tv_usec;
+       (void)user_data;
+
        stub_tdm_output_commit_handler_count++;
 }
 
+static void
+stub_tdm_buffer_release_handler(tbm_surface_h buffer, void *user_data)
+{
+       (void)buffer;
+       (void)user_data;
+
+       stub_tdm_buffer_release_handler_count++;
+}
+
+static void
+stub_tdm_buffer_destroy_handler(tbm_surface_h buffer,
+                                                               void *user_data)
+{
+       (void)buffer;
+       (void)user_data;
+}
+
 #define tdm_display_find_output_stamp stub_tdm_display_find_output_stamp
 #define tdm_display_update_output stub_tdm_display_update_output
 
index bc5d19bd13949f42b2bc56040eb845bdc66f4377..c86fabc645c11017e5d490b123b4f809d3d4277d 100644 (file)
@@ -5,6 +5,11 @@
 int TBM_BUFMGR_DEINIT_CALLED = 0;
 int TBM_BO_GET_USER_DATA_NULL = 0;
 int tbm_surface_queue_release_count;
+int tbm_surface_internal_add_user_data_error;
+int tbm_surface_internal_set_user_data_error;
+void *tbm_surface_internal_get_user_data_ret;
+int tbm_surface_internal_ref_count;
+int tbm_surface_internal_unref_count;
 
 tbm_bufmgr tbm_bufmgr_init(int fd)
 {
@@ -70,11 +75,15 @@ int tbm_bo_set_user_data(tbm_bo bo, unsigned long key, void *data)
 void tbm_surface_internal_ref(tbm_surface_h surface)
 {
        (void)surface;
+
+       tbm_surface_internal_ref_count++;
 }
 
 void tbm_surface_internal_unref(tbm_surface_h surface)
 {
        (void)surface;
+
+       tbm_surface_internal_unref_count++;
 }
 
 tbm_surface_queue_error_e tbm_surface_queue_acquire(
@@ -146,7 +155,13 @@ int tbm_surface_internal_get_user_data(tbm_surface_h surface, unsigned long key,
 {
        (void)surface;
        (void)key;
-       (void)data;
+
+       if (tbm_surface_internal_get_user_data_ret) {
+               *data = tbm_surface_internal_get_user_data_ret;
+               return 1;
+       }
+
+       *data = NULL;
 
        return 0;
 }
@@ -158,6 +173,9 @@ int tbm_surface_internal_add_user_data(tbm_surface_h surface, unsigned long key,
        (void)key;
        (void)data_free_func;
 
+       if (tbm_surface_internal_add_user_data_error)
+               return 0;
+
        return 1;
 }
 
@@ -168,6 +186,9 @@ int tbm_surface_internal_set_user_data(tbm_surface_h surface, unsigned long key,
        (void)key;
        (void)data;
 
+       if (tbm_surface_internal_set_user_data_error)
+               return 0;
+
        return 1;
 }
 
@@ -241,4 +262,9 @@ void stub_tbm_init()
        TBM_BUFMGR_DEINIT_CALLED = 0;
        TBM_BO_GET_USER_DATA_NULL = 0;
        tbm_surface_queue_release_count = 0;
+       tbm_surface_internal_add_user_data_error = 0;
+       tbm_surface_internal_set_user_data_error = 0;
+       tbm_surface_internal_get_user_data_ret = NULL;
+       tbm_surface_internal_ref_count = 0;
+       tbm_surface_internal_unref_count = 0;
 }
index 42219416b1f2b7b1a83f83f827bc2ae7b794ea27..e8e1f13f5b4ee452fa1a2ec995cbb5848be3df6b 100644 (file)
@@ -8,6 +8,11 @@
 extern int TBM_BUFMGR_DEINIT_CALLED;
 extern int TBM_BO_GET_USER_DATA_NULL;
 extern int tbm_surface_queue_release_count;
+extern int tbm_surface_internal_add_user_data_error;
+extern int tbm_surface_internal_set_user_data_error;
+extern void *tbm_surface_internal_get_user_data_ret;
+extern int tbm_surface_internal_ref_count;
+extern int tbm_surface_internal_unref_count;
 
 struct _tbm_surface {
        int temp;