summaryrefslogtreecommitdiff
path: root/src/tests/eldbus_cxx/eldbus_cxx_test_eldbus_client.cc
blob: 95eed8c8c4fed006d58f781694b5e9bdf6b72302 (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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <algorithm>
#include <iostream>

#include <Ecore.hh>
#include <Eldbus.hh>
#include <eldbus_freedesktop.hh>

#include "eldbus_cxx_suite.h"

const char g_bus[] = "org.Enlightenment";
const char g_path[] = "/org/enlightenment";
const char g_interface[] = "org.enlightenment.Test";

static void
on_name_request(std::error_code const& ec, efl::eldbus::const_message
                , efl::eldbus::pending, unsigned int reply)
{
  if(!ec)
    {
       if(reply != ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER)
         {
           printf("error name already in use\n");
           return;
         }
     }
}

START_TEST(eldbus_cxx_client)
{
  namespace edb = efl::eldbus;

  efl::ecore::ecore_init ecore_init;
  edb::eldbus_init init;

  edb::connection c_(edb::session);

  namespace es = edb::service;

  bool expected_bool = true;
  char expected_byte = 0xAA;
  uint32_t expected_uint32 = 0xFFFFFFFF;
  int32_t expected_int32 = -1;
  int16_t expected_int16 = -1;
  double expected_double = 3.1415926;
  std::string expected_string = "expected string";

  edb::service_interface iface = edb::service_interface_register
    (c_, g_path, g_interface
     , es::method("SendBool"
                  , [expected_bool] (edb::const_message, edb::service_interface, bool b)
                  {
                    ck_assert(b == expected_bool);
                    return b;
                  }
                  , es::ins<bool>("bool")
                  , es::outs<bool>("bool")
                  )
     , es::method("SendByte"
                  , [expected_byte] (edb::const_message, edb::service_interface, char c)
                  {
                    ck_assert(c == expected_byte);
                    return c;
                  }
                  , es::ins<char>("byte")
                  , es::outs<char>("byte")
                  )
     , es::method("SendUint32"
                  , [expected_uint32] (edb::const_message, edb::service_interface, uint32_t n)
                  {
                    ck_assert(n == expected_uint32);
                    return n;
                  }
                  , es::ins<uint32_t>("uint32")
                  , es::outs<uint32_t>("uint32")
                  )
     , es::method("SendInt32"
                  , [expected_int32] (edb::const_message, edb::service_interface, int32_t n)
                  {
                    ck_assert(n == expected_int32);
                    return n;
                  }
                  , es::ins<int32_t>("int32")
                  , es::outs<int32_t>("int32")
                  )
     , es::method("SendInt16"
                  , [expected_int16] (edb::const_message, edb::service_interface, int16_t n)
                  {
                    ck_assert(n == expected_int16);
                    return n;
                  }
                  , es::ins<int16_t>("int16")
                  , es::outs<int16_t>("int16")
                  )
     , es::method("SendDouble"
                  , [expected_double] (edb::const_message, edb::service_interface, double n)
                  {
                    ck_assert(n == expected_double);
                    return n;
                  }
                  , es::ins<double>("double")
                  , es::outs<double>("double")
                  )
     , es::method("SendString"
                  , [expected_string] (edb::const_message, edb::service_interface, std::string const& n)
                  {
                    std::cout << "SendString " << n.size() << " " << n << std::endl;
                    ck_assert(n == expected_string);
                    return n;
                  }
                  , es::ins<std::string>("string")
                  , es::outs<std::string>("string")
                  )
     , es::method("GetVoid"
                  , [expected_bool] (edb::const_message, edb::service_interface, bool b)
                  {
                    ck_assert(b == expected_bool);
                  }
                  , es::ins<bool>("string")
                  )
     , es::method("SendStringWithBool"
                  , [expected_string, expected_bool] (edb::const_message, edb::service_interface
                                                      , std::string const& n, bool b)
                  {
                    ck_assert(n == expected_string);
                    ck_assert(b == expected_bool);
                    return n;
                  }
                  , es::ins<std::string, bool>("string", "bool")
                  , es::outs<std::string>("string")
                  )
     , es::method("SendStringAndBool"
                  , [expected_string, expected_bool] (edb::const_message, edb::service_interface
                                                      , std::string const& n, bool b
                                                      , bool* out)
                  {
                    std::cout << "Running SendStringAndBool" << std::endl;
                    ck_assert(n == expected_string);
                    ck_assert(b == expected_bool);
                    *out = b;
                    return n;
                  }
                  , es::ins<std::string, bool>("string", "bool")
                  , es::outs<std::string, bool>("string", "bool")
                  )
     , es::method("SendStringAndBoolWithoutReturn"
                  , [expected_string, expected_bool] (edb::const_message, edb::service_interface
                                                      , std::string const& s, bool b
                                                      , std::string* out_s, bool* out_b)
                  {
                    std::cout << "Running SendStringAndBool" << std::endl;
                    ck_assert(s == expected_string);
                    ck_assert(b == expected_bool);
                    *out_s = s;
                    *out_b = b;
                  }
                  , es::ins<std::string, bool>("string", "bool")
                  , es::outs<std::string, bool>("string", "bool")
                  )
    );
  static_cast<void>(iface);

  using namespace std::placeholders;
  edb::name_request<std::uint32_t>(c_, g_bus, ELDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE
                                   , & ::on_name_request);

  std::cout << "registered" << std::endl;

  edb::object o = c_.get_object(g_bus, g_path);
  edb::proxy p = o.get_proxy(g_interface);

  using namespace std::placeholders;
  p.call<bool>
    ("SendBool"
     , -1
     , std::bind
     ([expected_bool] (std::error_code const& ec, edb::const_message const& /*msg*/, bool b)
      {
        if(!ec)
          {
            std::cout << "bool received " << b << std::endl;
            ck_assert(b == expected_bool);
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname = "", *errmsg = "";
            // eldbus_message_error_get(msg, &errname, &errmsg);
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4)
     , expected_bool
     );

  p.call<char>
    ("SendByte"
     , -1
     , std::bind
     ([expected_byte] (std::error_code const& ec, edb::const_message const& msg, char c)
      {
        if(!ec)
          {
            std::cout << "char received " << c << std::endl;
            ck_assert(c == expected_byte);
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4)
     , expected_byte
     );

  p.call<uint32_t>
    ("SendUint32"
     , -1
     , std::bind
     ([expected_uint32] (std::error_code const& ec, edb::const_message const& msg, uint32_t i)
      {
        if(!ec)
          {
            std::cout << "uint32_t received " << i << std::endl;
            ck_assert(i == expected_uint32);
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4)
     , expected_uint32
     );

  p.call<int32_t>
    ("SendInt32"
     , -1
     , std::bind
     ([expected_int32] (std::error_code const& ec, edb::const_message const& msg, int32_t i)
      {
        if(!ec)
          {
            std::cout << "int32_t received " << i << std::endl;
            ck_assert(i == expected_int32);
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4)
     , expected_int32
     );

  p.call<int16_t>
    ("SendInt16"
     , -1
     , std::bind
     ([expected_int16] (std::error_code const& ec, edb::const_message const& msg, int16_t i)
      {
        if(!ec)
          {
            std::cout << "int16_t received " << i << std::endl;
            ck_assert(i == expected_int16);
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4)
     , expected_int16
     );

  p.call<double>
    ("SendDouble"
     , -1
     , std::bind
     ([expected_double] (std::error_code const& ec, edb::const_message const& msg, double i)
      {
        if(!ec)
          {
            std::cout << "double received " << i << std::endl;
            ck_assert(i == expected_double);
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4)
     , expected_double
     );

  p.call<std::string>
    ("SendString"
     , -1
     , std::bind
     ([expected_string] (std::error_code const& ec, edb::const_message const& msg, std::string i)
      {
        if(!ec)
          {
            std::cout << "string received " << i << std::endl;
            ck_assert(i == expected_string);
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4)
     , expected_string
     );

  p.call<void>
    ("GetVoid"
     , -1
     , std::bind
     ([] (std::error_code const& ec, edb::const_message const& msg)
      {
        if(!ec)
          {
            std::cout << "GetVoid returned succesfully" << std::endl;
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2)
     , expected_bool
     );

  p.call<std::string>
    ("SendStringWithBool"
     , -1
     , std::bind
     ([expected_string] (std::error_code const& ec, edb::const_message const& msg, std::string i)
      {
        if(!ec)
          {
            std::cout << "string received " << i << std::endl;
            ck_assert(i == expected_string);
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4)
     , expected_string
     , expected_bool
     );

  p.call<std::tuple<std::string, bool> >
    ("SendStringAndBool"
     , -1
     , std::bind
     ([expected_string, expected_bool]
      (std::error_code const& ec, edb::const_message const& msg, std::string i, bool b)
      {
        if(!ec)
          {
            std::cout << "string received " << i << std::endl;
            ck_assert(i == expected_string);
            ck_assert(b == expected_bool);
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4, _5)
     , expected_string
     , expected_bool
     );

  p.call<std::tuple<std::string, bool> >
    ("SendStringAndBoolWithoutReturn"
     , -1
     , std::bind
     ([expected_string, expected_bool]
      (std::error_code const& ec, edb::const_message const& msg, std::string i, bool b)
      {
        if(!ec)
          {
            std::cout << "string received " << i << std::endl;
            ck_assert(i == expected_string);
            ck_assert(b == expected_bool);

            ::ecore_main_loop_quit();
          }
        else
          {
            std::cout << "error " << ec.message() << std::endl;
            const char *errname, *errmsg;
            std::tie(errname, errmsg) = msg.error_get();
            std::cout << "error " << errname << " " << errmsg << std::endl;
            std::abort();
          }
      }
      , _1, _2, _4, _5)
     , expected_string
     , expected_bool
     );

  // eldbus_name_owner_changed_callback_add(c.native_handle(), bus, on_name_owner_changed,
  //                                        c.native_handle(), EINA_TRUE);

  ecore_main_loop_begin();

  std::cout << "out of loop" << std::endl;
}
END_TEST

void
eldbus_test_client(TCase* tc)
{
  tcase_add_test(tc, eldbus_cxx_client);
}