group_init_destroy.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. libmc - checks for initialization and deinitialization of WGroup widget
  3. Copyright (C) 2020-2025
  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 <https://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. START_TEST (test_group_init_deinit)
  60. {
  61. WGroup *g, *g0;
  62. Widget *w0;
  63. WRect r;
  64. g = g_new0 (WGroup, 1);
  65. rect_init (&r, 0, 0, 20, 20);
  66. group_init (g, &r, group_callback, NULL);
  67. g0 = g_new0 (WGroup, 1);
  68. rect_init (&r, 0, 0, 10, 10);
  69. group_init (g0, &r, group_callback, NULL);
  70. group_add_widget (g, g0);
  71. w0 = g_new0 (Widget, 1);
  72. rect_init (&r, 0, 0, 5, 5);
  73. widget_init (w0, &r, widget_callback, NULL);
  74. group_add_widget (g0, w0);
  75. w0 = g_new0 (Widget, 1);
  76. rect_init (&r, 5, 5, 5, 5);
  77. widget_init (w0, &r, widget_callback, NULL);
  78. group_add_widget (g0, w0);
  79. g0 = g_new0 (WGroup, 1);
  80. rect_init (&r, 10, 10, 10, 10);
  81. group_init (g0, &r, group_callback, NULL);
  82. group_add_widget (g, g0);
  83. w0 = g_new0 (Widget, 1);
  84. rect_init (&r, 10, 10, 5, 5);
  85. widget_init (w0, &r, widget_callback, NULL);
  86. group_add_widget (g0, w0);
  87. w0 = g_new0 (Widget, 1);
  88. rect_init (&r, 15, 15, 5, 5);
  89. widget_init (w0, &r, widget_callback, NULL);
  90. group_add_widget (g0, w0);
  91. w0 = g_new0 (Widget, 1);
  92. rect_init (&r, 5, 5, 10, 10);
  93. widget_init (w0, &r, widget_callback, NULL);
  94. group_add_widget (g, w0);
  95. ck_assert_msg (w0->id == 7, "last id (%d) != 7", ref);
  96. send_message (g, NULL, MSG_INIT, 0, NULL);
  97. ck_assert_msg (ref == 8, "ref (%d) != 8", ref);
  98. widget_destroy (WIDGET (g));
  99. ck_assert_msg (ref == 0, "ref (%d) != 0", ref);
  100. }
  101. END_TEST
  102. /* --------------------------------------------------------------------------------------------- */
  103. int
  104. main (void)
  105. {
  106. TCase *tc_core;
  107. tc_core = tcase_create ("Core");
  108. // Add new tests here: ***************
  109. tcase_add_test (tc_core, test_group_init_deinit);
  110. // ***********************************
  111. return mctest_run_all (tc_core);
  112. }
  113. /* --------------------------------------------------------------------------------------------- */