summaryrefslogtreecommitdiff
path: root/src/tests/eio/efl_io_model_test_monitor_add.c
blob: 35e217aa630bd903f5e1b34cfa3e0ce63e3ad794 (plain) (blame)
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
//Compile with:
// gcc -o emodel_test_file emodel_test_file.c `pkg-config --cflags --libs emodel`
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <Eo.h>
#include <Eio.h>
#include <Ecore.h>
#include <Efl.h>
#include <stdio.h>

#include "eio_suite.h"

Eina_Tmpstr* temp_filename = NULL;
const char* tmpdir = NULL;
Eina_Bool children_deleted = EINA_FALSE;

// This will ensure that the child have the time to be automatically destroyed
static Eina_Value
_delayed_quit(Eo *o EINA_UNUSED, void *data EINA_UNUSED, const Eina_Value v)
{
   ecore_main_loop_quit();

   return v;
}

static Eina_Value
_children_got(Eo *o, void *data EINA_UNUSED, const Eina_Value v)
{
   Eo *child = NULL;
   unsigned int i, len;
   Eina_Value r = (Eina_Value) v;

   EINA_VALUE_ARRAY_FOREACH(&v, len, i, child)
     {
        Eina_Value *path;
        char *str;

        path = efl_model_property_get(child, "path");
        fail_if(path == NULL);
        str = eina_value_to_string(path);
        fail_if(str == NULL);

        if (temp_filename && strcmp(str, temp_filename) == 0)
          r = eina_future_as_value(efl_future_then(efl_loop_get(o),
                                                   efl_loop_job(efl_loop_get(o)),
                                                   .success = _delayed_quit));


        free(str);
        eina_value_free(path);
     }

   return r;
}

static Eina_Value
_children_failed(Eo *o EINA_UNUSED, void *data EINA_UNUSED, const Eina_Error err)
{
   ck_abort_msg( "Failed to get the child with '%s'.\n", eina_error_msg_get(err));
   return eina_value_error_init(err);
}

static void
_children_removed_cb(void *data EINA_UNUSED, const Efl_Event* event)
{
   Efl_Model_Children_Event* evt = event->info;
   Eina_Value *path;
   char *str;

   if (!evt->child)
     {
        Eina_Future *f;

        // Last time we can fetch the object
        f = efl_future_then(event->object,
                            efl_model_children_slice_get(event->object, evt->index, 1),
                            .success = _children_got,
                            .error = _children_failed,
                            .success_type = EINA_VALUE_TYPE_ARRAY);
        fail_if(f == NULL);
        return;
     }

   fail_if(evt->child == NULL);
   path = efl_model_property_get(evt->child, "path");
   fail_if(path == NULL);
   str = eina_value_to_string(path);
   fail_if(str == NULL);

   if (temp_filename && strcmp(str, temp_filename) == 0)
     efl_future_then(efl_loop_get(evt->child),
                     efl_loop_job(efl_loop_get(evt->child)),
                     .success = _delayed_quit);

   free(str);
   eina_value_free(path);
}

static Eina_Bool started_up = EINA_FALSE;

static Eina_Value
_children_get(void *data,
              const Eina_Value v,
              const Eina_Future *dead_future EINA_UNUSED)
{
   Efl_Model *filemodel = data;
   Eo *child = NULL;
   unsigned int i, len;

   fail_if(eina_value_type_get(&v) != EINA_VALUE_TYPE_ARRAY);

   EINA_VALUE_ARRAY_FOREACH(&v, len, i, child)
     {
        Eina_Value *path;
        char *str;

        path = efl_model_property_get(child, "path");
        fail_if(path == NULL);
        str = eina_value_to_string(path);
        fail_if(str == NULL);

        if (started_up && strcmp(temp_filename, str) == 0)
          {
             children_deleted = EINA_TRUE;
             efl_model_child_del(filemodel, child);
          }
        free(str);
        eina_value_free(path);
     }

   if (!started_up)
     {
        int fd;

        if ((fd = eina_file_mkstemp("prefixXXXXXX.ext", &temp_filename)) > 0)
          {
             close(fd);
          }
     }
   started_up = EINA_TRUE;

   return v;
}

static void
_children_added_cb(void *d EINA_UNUSED, const Efl_Event* event)
{
   Efl_Model_Children_Event* evt = event->info;
   Eina_Future *future;

   future = efl_model_children_slice_get(event->object, evt->index, 1);
   eina_future_then(future, _children_get, event->object, NULL);
}

EFL_START_TEST(efl_io_model_test_test_monitor_add)
{
   Eo *filemodel = NULL;

   tmpdir = eina_environment_tmp_get();

   filemodel = efl_add(EFL_IO_MODEL_CLASS,
                       efl_main_loop_get(),
                       efl_io_model_path_set(efl_added, tmpdir));
   fail_if(!filemodel, "ERROR: Cannot init model!\n");

   efl_event_callback_add(filemodel, EFL_MODEL_EVENT_CHILD_ADDED, &_children_added_cb, filemodel);
   efl_event_callback_add(filemodel, EFL_MODEL_EVENT_CHILD_REMOVED, &_children_removed_cb, NULL);

   ecore_main_loop_begin();

   efl_del(filemodel);

   fail_if(!children_deleted);
}
EFL_END_TEST

void
efl_io_model_test_monitor_add(TCase *tc)
{
   tcase_add_test(tc, efl_io_model_test_test_monitor_add);
}