group_init_destroy.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. libmc - checks for initialization and deinitialization of WGroup widget
  3. Copyright (C) 2020-2024
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2020-2022
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #define TEST_SUITE_NAME "lib/widget/group"
  20. #include <config.h>
  21. #include <check.h>
  22. #include "lib/widget.h"
  23. #include "tests/mctest.h"
  24. /* --------------------------------------------------------------------------------------------- */
  25. static int ref = 0;
  26. /* --------------------------------------------------------------------------------------------- */
  27. static cb_ret_t
  28. widget_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  29. {
  30. switch (msg)
  31. {
  32. case MSG_INIT:
  33. ref++;
  34. return widget_default_callback (w, NULL, MSG_INIT, 0, NULL);
  35. case MSG_DESTROY:
  36. ref--;
  37. return widget_default_callback (w, NULL, MSG_DESTROY, 0, NULL);
  38. default:
  39. return widget_default_callback (w, sender, msg, parm, data);
  40. }
  41. }
  42. /* --------------------------------------------------------------------------------------------- */
  43. static cb_ret_t
  44. group_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  45. {
  46. switch (msg)
  47. {
  48. case MSG_INIT:
  49. ref++;
  50. return group_default_callback (w, NULL, MSG_INIT, 0, NULL);
  51. case MSG_DESTROY:
  52. ref--;
  53. return group_default_callback (w, NULL, MSG_DESTROY, 0, NULL);
  54. default:
  55. return group_default_callback (w, sender, msg, parm, data);
  56. }
  57. }
  58. /* --------------------------------------------------------------------------------------------- */
  59. /* *INDENT-OFF* */
  60. START_TEST (test_group_init_deinit)
  61. /* *INDENT-ON* */
  62. {
  63. WGroup *g, *g0;
  64. Widget *w0;
  65. WRect r;
  66. g = g_new0 (WGroup, 1);
  67. rect_init (&r, 0, 0, 20, 20);
  68. group_init (g, &r, group_callback, NULL);
  69. g0 = g_new0 (WGroup, 1);
  70. rect_init (&r, 0, 0, 10, 10);
  71. group_init (g0, &r, group_callback, NULL);
  72. group_add_widget (g, g0);
  73. w0 = g_new0 (Widget, 1);
  74. rect_init (&r, 0, 0, 5, 5);
  75. widget_init (w0, &r, widget_callback, NULL);
  76. group_add_widget (g0, w0);
  77. w0 = g_new0 (Widget, 1);
  78. rect_init (&r, 5, 5, 5, 5);
  79. widget_init (w0, &r, widget_callback, NULL);
  80. group_add_widget (g0, w0);
  81. g0 = g_new0 (WGroup, 1);
  82. rect_init (&r, 10, 10, 10, 10);
  83. group_init (g0, &r, group_callback, NULL);
  84. group_add_widget (g, g0);
  85. w0 = g_new0 (Widget, 1);
  86. rect_init (&r, 10, 10, 5, 5);
  87. widget_init (w0, &r, widget_callback, NULL);
  88. group_add_widget (g0, w0);
  89. w0 = g_new0 (Widget, 1);
  90. rect_init (&r, 15, 15, 5, 5);
  91. widget_init (w0, &r, widget_callback, NULL);
  92. group_add_widget (g0, w0);
  93. w0 = g_new0 (Widget, 1);
  94. rect_init (&r, 5, 5, 10, 10);
  95. widget_init (w0, &r, widget_callback, NULL);
  96. group_add_widget (g, w0);
  97. ck_assert_msg (w0->id == 7, "last id (%d) != 7", ref);
  98. send_message (g, NULL, MSG_INIT, 0, NULL);
  99. ck_assert_msg (ref == 8, "ref (%d) != 8", ref);
  100. widget_destroy (WIDGET (g));
  101. ck_assert_msg (ref == 0, "ref (%d) != 0", ref);
  102. }
  103. /* *INDENT-OFF* */
  104. END_TEST
  105. /* *INDENT-ON* */
  106. /* --------------------------------------------------------------------------------------------- */
  107. int
  108. main (void)
  109. {
  110. TCase *tc_core;
  111. tc_core = tcase_create ("Core");
  112. /* Add new tests here: *************** */
  113. tcase_add_test (tc_core, test_group_init_deinit);
  114. /* *********************************** */
  115. return mctest_run_all (tc_core);
  116. }
  117. /* --------------------------------------------------------------------------------------------- */