group_init_destroy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. libmc - checks for initialization and deinitialization of WGroup widget
  3. Copyright (C) 2020-2021
  4. The Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2020
  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. g = g_new0 (WGroup, 1);
  66. group_init (g, 0, 0, 20, 20, group_callback, NULL);
  67. g0 = g_new0 (WGroup, 1);
  68. group_init (g0, 0, 0, 10, 10, group_callback, NULL);
  69. group_add_widget (g, g0);
  70. w0 = g_new0 (Widget, 1);
  71. widget_init (w0, 0, 0, 5, 5, widget_callback, NULL);
  72. group_add_widget (g0, w0);
  73. w0 = g_new0 (Widget, 1);
  74. widget_init (w0, 5, 5, 5, 5, widget_callback, NULL);
  75. group_add_widget (g0, w0);
  76. g0 = g_new0 (WGroup, 1);
  77. group_init (g0, 10, 10, 10, 10, group_callback, NULL);
  78. group_add_widget (g, g0);
  79. w0 = g_new0 (Widget, 1);
  80. widget_init (w0, 10, 10, 5, 5, widget_callback, NULL);
  81. group_add_widget (g0, w0);
  82. w0 = g_new0 (Widget, 1);
  83. widget_init (w0, 15, 15, 5, 5, widget_callback, NULL);
  84. group_add_widget (g0, w0);
  85. w0 = g_new0 (Widget, 1);
  86. widget_init (w0, 5, 5, 10, 10, widget_callback, NULL);
  87. group_add_widget (g, w0);
  88. ck_assert_msg (w0->id == 7, "last id (%d) != 7", ref);
  89. send_message (g, NULL, MSG_INIT, 0, NULL);
  90. ck_assert_msg (ref == 8, "ref (%d) != 8", ref);
  91. widget_destroy (WIDGET (g));
  92. ck_assert_msg (ref == 0, "ref (%d) != 0", ref);
  93. }
  94. /* *INDENT-OFF* */
  95. END_TEST
  96. /* *INDENT-ON* */
  97. /* --------------------------------------------------------------------------------------------- */
  98. int
  99. main (void)
  100. {
  101. TCase *tc_core;
  102. tc_core = tcase_create ("Core");
  103. /* Add new tests here: *************** */
  104. tcase_add_test (tc_core, test_group_init_deinit);
  105. /* *********************************** */
  106. return mctest_run_all (tc_core);
  107. }
  108. /* --------------------------------------------------------------------------------------------- */