summaryrefslogtreecommitdiff
path: root/src/tests/eio/eio_model_test_monitor_add.c
blob: fe3cdc2ba17eaf333d36c7e70c64d53024d3292f (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
//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;

struct _pair
{
  Eo *parent, *child;
};

void _children_removed_get_value_cb(void* data EINA_UNUSED, Efl_Event const* event)
{
  Efl_Future_Event_Success* info = event->info;
  Eina_Value* value = info->value;
  char* filename = eina_value_to_string(value);
  if(temp_filename && strcmp(filename, temp_filename) == 0)
    ecore_main_loop_quit();
  free(filename);

  ecore_main_loop_quit();
}

static void
_children_removed_cb(void *data EINA_UNUSED, const Efl_Event* event)
{
   if(children_deleted)
     {
        Efl_Model_Children_Event* evt = event->info;
        Efl_Future* future;

        future = efl_model_property_get(evt->child, "path");

        efl_future_then(future, _children_removed_get_value_cb, NULL, NULL, NULL);
     }
}

struct _added_get_value_data
{
  Eo *object, *child;
};
typedef struct _added_get_value_data added_get_value_data;

void _children_added_get_value_cb(void* d, Efl_Event const* event)
{
  Efl_Future_Event_Success* info = event->info;
  Eina_Value* value = info->value;
  added_get_value_data* data = d;

   char* filename = eina_value_to_string(value);

   if(temp_filename && strcmp(temp_filename, filename) == 0)
     {
        children_deleted = EINA_TRUE;
        efl_model_child_del(data->object, data->child);
     }
   free(filename);
}

static void
_children_added_cb(void *d EINA_UNUSED, const Efl_Event* event)
{
   Efl_Model_Children_Event* evt = event->info;
   Efl_Future* future;
   
   future = efl_model_property_get(evt->child, "path");

   added_get_value_data* data = malloc(sizeof(added_get_value_data));
   data->object = event->object;
   data->child = evt->child;
   efl_future_then(future, _children_added_get_value_cb, NULL, NULL, data);
}

static void
_create_file(void *data EINA_UNUSED, Efl_Event const* event EINA_UNUSED)
{
   int fd;
   if((fd = eina_file_mkstemp("prefixXXXXXX.ext", &temp_filename)) > 0)
     {
        close(fd);
     }
}


static void
_create_file_error(void *data EINA_UNUSED, Efl_Event const* event EINA_UNUSED)
{
   ck_abort_msg(0, "Error Promise cb called in Create file");
   ecore_main_loop_quit();
}

EFL_START_TEST(eio_model_test_test_monitor_add)
{
   Eo *filemodel = NULL;

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

   tmpdir = eina_environment_tmp_get();

   filemodel = efl_add(EIO_MODEL_CLASS, efl_main_loop_get(), eio_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);

   Efl_Future* future;
   future = efl_model_children_slice_get(filemodel, 0, 0);

   efl_future_then(future, &_create_file, &_create_file_error, NULL, NULL);

   ecore_main_loop_begin();

   efl_unref(filemodel);

   eio_shutdown();
   ecore_shutdown();
   eina_shutdown();

   fail_if(!children_deleted);
}
EFL_END_TEST

void
eio_model_test_monitor_add(TCase *tc)
{
   tcase_add_test(tc, eio_model_test_test_monitor_add);
}