summaryrefslogtreecommitdiff
path: root/src/tests/efl/efl_test_model_composite.c
blob: 4c6eef99c4f10c2576c51a2a6367c9a23daf557c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/* EFL - EFL library
 * Copyright (C) 2013 Cedric Bail
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library;
 * if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "efl_suite.h"

#include <Efl.h>
#include <Ecore.h>

typedef struct _Test_Data
{
   Eina_Bool success_flag;
   unsigned int child_count;
} Test_Data;

typedef struct _Test_Child_Data
{
   Test_Data *tdata;
   unsigned int idx;
} Test_Child_Data;

typedef struct _Selection_Test_Child_Data
{
   Test_Data *tdata;
   unsigned int idx;
} Selection_Test_Child_Data;

const int child_number = 3;
const int base_ints[] = { 41, 42, 43 };
const Eina_Bool base_selections[] = { EINA_FALSE, EINA_FALSE, EINA_TRUE };

static void
_future_error_then(void *data EINA_UNUSED, Efl_Event const* event EINA_UNUSED)
{
   fprintf(stderr, "Promise failed!\n"); fflush(stderr);
   ck_abort_msg("Promise failed");
}

static void
_property_get_then(void *data, Efl_Event const *event)
{
   Test_Child_Data *t = data;
   Eina_Accessor *value_itt = (Eina_Accessor*)((Efl_Future_Event_Success*)event->info)->value;
   int v_int = 0;
   Eina_Bool v_bool = EINA_FALSE;

#define _value_get_and_compare(it, i, var, cmp)  \
   do { \
      Eina_Value *vvv = NULL; \
      if (!eina_accessor_data_get(it, i, (void **)&vvv) || !vvv || \
          !eina_value_get(vvv, &var) || var != cmp) \
        { \
           fprintf(stderr, "Could not get value!\n"); fflush(stderr); \
           ck_abort_msg("Could not get value"); \
           return; \
        } \
   } while(0)

   _value_get_and_compare(value_itt, 0, v_int, base_ints[t->idx]);
   _value_get_and_compare(value_itt, 1, v_bool, EINA_TRUE);
   _value_get_and_compare(value_itt, 2, v_bool, EINA_FALSE);

   t->tdata->child_count++;
   if (t->tdata->child_count == 3)
     t->tdata->success_flag = EINA_TRUE;

#undef _value_get_and_compare
}

static void
_selection_property_get_then(void *data, Efl_Event const *event)
{
   Selection_Test_Child_Data *t = data;
   Eina_Accessor *value_itt = (Eina_Accessor*)((Efl_Future_Event_Success*)event->info)->value;
   int v_int = 0;
   Eina_Bool v_bool = EINA_FALSE;

   fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); fflush(stderr);

#define EFL_TEST_value_get_and_compare(it, i, var, cmp)  \
   do { \
      Eina_Value *vvv = NULL; \
      if (!eina_accessor_data_get(it, i, (void **)&vvv) || !vvv || \
          !eina_value_get(vvv, &var) || var != cmp) \
        { \
           fprintf(stderr, "Could not get value!\n"); fflush(stderr); \
           ck_abort_msg("Could not get value"); \
           return; \
        } \
   } while(0)

   EFL_TEST_value_get_and_compare(value_itt, 0, v_int, base_ints[t->idx]);
   EFL_TEST_value_get_and_compare(value_itt, 1, v_bool, base_selections[t->idx]);

   t->tdata->child_count++;
   if (t->tdata->child_count == 3)
     t->tdata->success_flag = EINA_TRUE;

#undef EFL_TEST_value_get_and_compare
}

static void
_children_slice_get_then(void *data, Efl_Event const* event)
{
   Eina_Accessor *children = (Eina_Accessor *)((Efl_Future_Event_Success*)event->info)->value;
   Efl_Model *child;
   Test_Child_Data *t;
   unsigned int i = 0;

   EINA_ACCESSOR_FOREACH(children, i, child)
     {
        Efl_Future *futures[3] = {NULL,};
        Efl_Future *future_all = NULL;

        futures[0] = efl_model_property_get(child, "test_p_int");
        futures[1] = efl_model_property_get(child, "test_p_true");
        futures[2] = efl_model_property_get(child, "test_p_false");

        future_all = efl_future_all(futures[0], futures[1], futures[2]);

        t = calloc(1, sizeof(Test_Child_Data));
        t->tdata = data;
        t->idx = i;
        efl_future_then(future_all, _property_get_then, _future_error_then, NULL, t);
     }
}

static void
_selection_children_slice_get_then(void *data, Efl_Event const* event)
{
   Eina_Accessor *children = (Eina_Accessor *)((Efl_Future_Event_Success*)event->info)->value;
   Efl_Model *child;
   Test_Child_Data *t;
   unsigned int i = 0;

   fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); fflush(stderr);

   EINA_ACCESSOR_FOREACH(children, i, child)
     {
        Efl_Future *futures[3] = {NULL,};
        Efl_Future *future_all = NULL;

        fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); fflush(stderr);
        futures[0] = efl_model_property_get(child, "test_p_int");
        futures[1] = efl_model_property_get(child, "selected");
        fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); fflush(stderr);

        future_all = efl_future_all(futures[0], futures[1]);
        fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); fflush(stderr);

        t = calloc(1, sizeof(Selection_Test_Child_Data));
        t->tdata = data;
        t->idx = i;
        fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); fflush(stderr);
        efl_future_then(future_all, _selection_property_get_then, _future_error_then, NULL, t);
     }
}

START_TEST(efl_test_model_composite_boolean)
{
   Efl_Model_Item *base_model, *child;
   int i;
   Eina_Value v;
   Efl_Model_Composite_Boolean *model;
   Test_Data *tdata;
   Efl_Future *future;

   fail_if(!ecore_init(), "ERROR: Cannot init Ecore!\n");
   fail_if(!efl_object_init(), "ERROR: Cannot init EO!\n");

   eina_value_setup(&v, EINA_VALUE_TYPE_INT);

   base_model = efl_add(EFL_MODEL_ITEM_CLASS, NULL);
   ck_assert(!!base_model);

   for (i = 0; i < child_number; ++i)
     {
        child = efl_model_child_add(base_model);
        ck_assert(!!child);
        ck_assert(eina_value_set(&v, base_ints[i]));
        efl_model_property_set(child, "test_p_int", &v);
     }

   model = efl_add(EFL_MODEL_COMPOSITE_BOOLEAN_CLASS, NULL,
                  efl_ui_view_model_set(efl_added, base_model),
                  efl_model_composite_boolean_property_add(efl_added, "test_p_true", EINA_TRUE),
                  efl_model_composite_boolean_property_add(efl_added, "test_p_false", EINA_FALSE));
   ck_assert(!!model);

   tdata = calloc(1, sizeof(Test_Data));
   future = efl_model_children_slice_get(model, 0, 0);
   efl_future_then(future, _children_slice_get_then, _future_error_then, NULL, tdata);

   ecore_main_loop_iterate();

   ck_assert(tdata->success_flag);

   ecore_shutdown();
}
END_TEST

START_TEST(efl_test_model_composite_selection)
{
   Efl_Model_Item *base_model, *child;
   int i;
   Eina_Value v;
   Efl_Model_Composite_Selection *model;
   Test_Data *tdata;
   Efl_Future *future;

   fail_if(!ecore_init(), "ERROR: Cannot init Ecore!\n");
   fail_if(!efl_object_init(), "ERROR: Cannot init EO!\n");

   eina_value_setup(&v, EINA_VALUE_TYPE_INT);

   base_model = efl_add(EFL_MODEL_ITEM_CLASS, NULL);
   ck_assert(!!base_model);

   for (i = 0; i < child_number; ++i)
     {
        child = efl_model_child_add(base_model);
        ck_assert(!!child);
        ck_assert(eina_value_set(&v, base_ints[i]));
        efl_model_property_set(child, "test_p_int", &v);
     }

   model = efl_add(EFL_MODEL_COMPOSITE_SELECTION_CLASS, NULL,
                   efl_ui_view_model_set(efl_added, base_model));
   ck_assert(!!model);
   efl_model_composite_selection_select(model, 3);

   tdata = calloc(1, sizeof(Test_Data));
   future = efl_model_children_slice_get(model, 0, 0);
   fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); fflush(stderr);
   efl_future_then(future, _selection_children_slice_get_then, _future_error_then, NULL, tdata);
   fprintf(stderr, "%s %s:%d\n", __func__, __FILE__, __LINE__); fflush(stderr);

   ecore_main_loop_iterate();

   ck_assert(tdata->success_flag);

   ecore_shutdown();
}
END_TEST

void
efl_test_case_model_composite_boolean(TCase *tc)
{
   /* tcase_add_test(tc, efl_test_model_composite_boolean); */
   tcase_add_test(tc, efl_test_model_composite_selection);
}