root/tests/lib/widget/group_init_destroy.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. widget_callback
  2. group_callback
  3. START_TEST
  4. main

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

/* [previous][next][first][last][top][bottom][index][help]  */