Package version up to 2.7.1
[platform/core/uifw/libtdm.git] / haltests / src / tc_tdm_client.cpp
1 /**************************************************************************
2  *
3  * Copyright 2017 Samsung Electronics co., Ltd. All Rights Reserved.
4  *
5  * Contact: Konstantin Drabeniuk <k.drabeniuk@samsung.com>
6  * Contact: Andrii Sokolenko <a.sokolenko@samsung.com>
7  * Contact: Roman Marchenko <r.marchenko@samsung.com>
8  * Contact: Sergey Sizonov <s.sizonov@samsung.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the
12  * "Software"), to deal in the Software without restriction, including
13  * without limitation the rights to use, copy, modify, merge, publish,
14  * distribute, sub license, and/or sell copies of the Software, and to
15  * permit persons to whom the Software is furnished to do so, subject to
16  * the following conditions:
17  *
18  * The above copyright notice and this permission notice (including the
19  * next paragraph) shall be included in all copies or substantial portions
20  * of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29  *
30 **************************************************************************/
31
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <sys/signalfd.h>
35 #include <poll.h>
36 #include <sys/prctl.h>
37
38 #include "tc_tdm.h"
39 #include "tdm_client.h"
40
41 /* LCOV_EXCL_START */
42
43 enum {
44         TDM_UT_PIPE_MSG_NONE,
45         TDM_UT_PIPE_MSG_REPLY,
46         TDM_UT_PIPE_MSG_SERVER_READY,
47         TDM_UT_PIPE_MSG_SERVER_FAILED,
48         TDM_UT_PIPE_MSG_DPMS_ON,
49         TDM_UT_PIPE_MSG_DPMS_OFF,
50         TDM_UT_PIPE_MSG_TERMINATE_SERVER,
51 };
52
53 #define TDM_UT_WAIT(fmt, ...) \
54         do { \
55                 char ch; \
56                 do { \
57                         printf(fmt" [n]):next ", ##__VA_ARGS__); \
58                         ch = tc_tdm_getchar(); \
59                 } while (ch != 'n'); \
60         } while (0)
61
62 static int _tc_tdm_pipe_read_msg(int fd);
63 static bool _tc_tdm_pipe_write_msg(int fd, int reply_fd, int msg);
64 static pid_t _tc_tdm_client_server_fork(int *pipe_to_parent, int *pipe_to_child);
65
66 class TDMClient : public TDMEnv
67 {
68 public:
69         static pid_t server_pid;
70
71         /* 0: read, 1: write */
72         static int pipe_parent[2];
73         static int pipe_child[2];
74
75         tdm_client *client;
76         tdm_client_output *output;
77         tdm_client_vblank *vblank;
78         tdm_client_voutput *voutput;
79
80         double vrefresh_interval, start, end;
81
82         TDMClient();
83
84         void SetUp(void);
85         void TearDown(void);
86         bool PrepareClient(void);
87         bool PrepareOutput(void);
88         bool PrepareVblank(void);
89
90         static void TearDownTestCase(void);
91         static void ServerFork(void);
92         static void ServerKill(void);
93 };
94
95 pid_t TDMClient::server_pid = -1;
96 int TDMClient::pipe_parent[2] = {-1, -1};
97 int TDMClient::pipe_child[2] = {-1, -1};
98
99 void TDMClient::TearDownTestCase(void)
100 {
101         ServerKill();
102 }
103
104 void TDMClient::ServerFork(void)
105 {
106         if (server_pid > 0)
107                 return;
108
109         server_pid = _tc_tdm_client_server_fork(pipe_parent, pipe_child);
110         ASSERT_GT(server_pid, 0);
111 }
112
113 void TDMClient::ServerKill(void)
114 {
115         if (pipe_child[0] >= 0)
116                 close(pipe_child[0]);
117         if (pipe_child[1] >= 0) {
118                 if (server_pid > 0) {
119                         bool ret = _tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_TERMINATE_SERVER);
120                         if (ret) {
121                                 if (waitpid(server_pid, NULL, 0) == server_pid)
122                                         TDM_INFO("*** server terminated ***");
123                                 else
124                                         TDM_ERR("*** failed to terminate server ***");
125                         } else {
126                                 if (kill(server_pid, 9) < 0)
127                                         TDM_ERR("*** failed to kill server ***");
128                         }
129                 }
130                 close(pipe_child[1]);
131         }
132
133         if (pipe_parent[0] >= 0)
134                 close(pipe_parent[0]);
135         if (pipe_parent[1] >= 0)
136                 close(pipe_parent[1]);
137
138         server_pid = -1;
139         pipe_parent[0] = pipe_parent[1] = -1;
140         pipe_child[0] = pipe_child[1] = -1;
141 }
142
143 TDMClient::TDMClient()
144 {
145         client = NULL;
146         output = NULL;
147         vblank = NULL;
148         voutput = NULL;
149         vrefresh_interval = start = end = 0.0;
150 }
151
152 void TDMClient::SetUp(void)
153 {
154         TDMEnv::SetUp();
155
156         if (server_pid == -1)
157                 ServerFork();
158 }
159
160 void TDMClient::TearDown(void)
161 {
162         if (vblank)
163                 tdm_client_vblank_destroy(vblank);
164         if (client)
165                 tdm_client_destroy(client);
166
167         TDMEnv::TearDown();
168 }
169
170 bool TDMClient::PrepareClient(void)
171 {
172         tdm_error ret;
173         client = tdm_client_create(&ret);
174         TDM_UT_RETURN_FALSE_IF_FAIL(ret == TDM_ERROR_NONE);
175         TDM_UT_RETURN_FALSE_IF_FAIL(client != NULL);
176
177         return true;
178 }
179
180 bool TDMClient::PrepareOutput(void)
181 {
182         tdm_error ret;
183
184         TDM_UT_RETURN_FALSE_IF_FAIL(client != NULL);
185
186         output = tdm_client_get_output(client, NULL, &ret);
187         TDM_UT_RETURN_FALSE_IF_FAIL(ret == TDM_ERROR_NONE);
188         TDM_UT_RETURN_FALSE_IF_FAIL(output != NULL);
189
190         return true;
191 }
192
193 bool TDMClient::PrepareVblank(void)
194 {
195         tdm_error ret;
196         unsigned int refresh;
197
198         TDM_UT_RETURN_FALSE_IF_FAIL(output != NULL);
199
200         vblank = tdm_client_output_create_vblank(output, &ret);
201         TDM_UT_RETURN_FALSE_IF_FAIL(ret == TDM_ERROR_NONE);
202         TDM_UT_RETURN_FALSE_IF_FAIL(vblank != NULL);
203
204         TDM_UT_RETURN_FALSE_IF_FAIL(tdm_client_output_get_refresh_rate(output, &refresh) == TDM_ERROR_NONE);
205         TDM_UT_RETURN_FALSE_IF_FAIL(refresh > 0);
206
207         vrefresh_interval = 1.0 / (double)refresh;
208         TDM_UT_RETURN_FALSE_IF_FAIL(vrefresh_interval > 0);
209
210         return true;
211 }
212
213 char
214 tc_tdm_getchar(void)
215 {
216         int c = getchar();
217         int ch = c;
218
219         if (ch == '\n' || ch == '\r')
220                 ch = 'y';
221         else if (ch < 'a')
222                 ch += ('a' - 'A');
223
224         while (c != '\n' && c != EOF)
225                 c = getchar();
226
227         return ch;
228 }
229
230 static int
231 _tc_tdm_pipe_read_msg(int fd)
232 {
233         ssize_t len;
234         int msg;
235
236         do {
237                 len = read(fd, &msg, sizeof msg);
238         } while (len < 0 && errno == EINTR);
239
240         if (len <= 0)
241                 msg = TDM_UT_PIPE_MSG_NONE;
242
243         return msg;
244 }
245
246 static bool
247 _tc_tdm_pipe_write_msg(int fd, int reply_fd, int msg)
248 {
249         ssize_t len = write(fd, &msg, sizeof msg);
250         TDM_UT_RETURN_FALSE_IF_FAIL(len == sizeof msg);
251
252         if (reply_fd >= 0) {
253                 int reply = _tc_tdm_pipe_read_msg(reply_fd);
254                 TDM_UT_RETURN_FALSE_IF_FAIL(reply == TDM_UT_PIPE_MSG_REPLY);
255         }
256
257         return true;
258 }
259
260 static bool
261 _tc_tdm_server_set_output_dpms(tdm_display *dpy, int msg)
262 {
263         tdm_error ret;
264         tdm_output *output;
265         tdm_output_dpms dpms;
266
267         output = tdm_display_find_output(dpy, "primary", &ret);
268         TDM_UT_RETURN_FALSE_IF_FAIL(ret == TDM_ERROR_NONE);
269         TDM_UT_RETURN_FALSE_IF_FAIL(output != NULL);
270
271         TDM_UT_RETURN_FALSE_IF_FAIL(tdm_output_get_dpms(output, &dpms) == TDM_ERROR_NONE);
272
273         switch (msg) {
274         case TDM_UT_PIPE_MSG_DPMS_ON:
275                 if (dpms != TDM_OUTPUT_DPMS_ON)
276                         TDM_UT_RETURN_FALSE_IF_FAIL(tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_ON) == TDM_ERROR_NONE);
277                 break;
278         case TDM_UT_PIPE_MSG_DPMS_OFF:
279                 if (dpms != TDM_OUTPUT_DPMS_OFF)
280                         TDM_UT_RETURN_FALSE_IF_FAIL(tdm_output_set_dpms(output, TDM_OUTPUT_DPMS_OFF) == TDM_ERROR_NONE);
281                 break;
282         default:
283                 break;
284         }
285
286         return true;
287 }
288
289 static void
290 _tc_tdm_server_run(int *pipe_parent, int *pipe_child)
291 {
292         tdm_display *dpy = NULL;
293         tdm_error ret;
294         struct pollfd fds[2];
295         int tdm_fd, err;
296         int output_count = 0;
297
298         dpy = tdm_display_init(&ret);
299         TDM_UT_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
300         TDM_UT_GOTO_IF_FAIL(dpy != NULL, failed);
301
302         TDM_UT_GOTO_IF_FAIL(tdm_display_get_output_count(dpy, &output_count) == TDM_ERROR_NONE, failed);
303
304         for (int o = 0; o < output_count; o++) {
305                 tdm_output *output = tdm_display_get_output(dpy, o, &ret);
306                 TDM_UT_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, failed);
307                 TDM_UT_GOTO_IF_FAIL(output != NULL, failed);
308
309                 if (!tc_tdm_output_is_connected(output))
310                         continue;
311
312                 TDM_UT_GOTO_IF_FAIL(tc_tdm_output_prepare(dpy, output, true) == true, failed);
313         }
314
315         TDM_UT_GOTO_IF_FAIL(_tc_tdm_pipe_write_msg(pipe_parent[1], -1, TDM_UT_PIPE_MSG_SERVER_READY) == true, done);
316
317         TDM_INFO("*** server ready ***");
318
319         ret = tdm_display_get_fd(dpy, &tdm_fd);
320         TDM_UT_GOTO_IF_FAIL(ret == TDM_ERROR_NONE, done);
321
322         fds[0].events = POLLIN;
323         fds[0].fd = tdm_fd;
324         fds[0].revents = 0;
325
326         fds[1].events = POLLIN;
327         fds[1].fd = pipe_child[0];
328         fds[1].revents = 0;
329
330         while (1) {
331                 /* make sure all events are flushed to clients before falling in sleep */
332                 tdm_display_flush(dpy);
333
334                 err = poll(fds, 2, -1);
335                 if (err < 0) {
336                         if (errno == EINTR || errno == EAGAIN) {
337                                 continue;
338                         } else {
339                                 TDM_ERR("server-process: poll failed: %m\n");
340                                 goto done;
341                         }
342                 }
343
344                 if (fds[0].revents & POLLIN)
345                         ret = tc_tdm_display_handle_events(dpy);
346
347                 if (fds[1].revents & POLLIN) {
348                         int msg = _tc_tdm_pipe_read_msg(pipe_child[0]);
349                         _tc_tdm_pipe_write_msg(pipe_parent[1], -1, TDM_UT_PIPE_MSG_REPLY);
350
351                         switch (msg) {
352                         case TDM_UT_PIPE_MSG_DPMS_ON:
353                         case TDM_UT_PIPE_MSG_DPMS_OFF:
354                                 _tc_tdm_server_set_output_dpms(dpy, msg);
355                                 break;
356                         case TDM_UT_PIPE_MSG_TERMINATE_SERVER:
357                                 goto done;
358                         default:
359                                 break;
360                         }
361                 }
362         }
363
364 done:
365         if (dpy)
366                 tdm_display_deinit(dpy);
367         return;
368
369 failed:
370         TDM_UT_GOTO_IF_FAIL(_tc_tdm_pipe_write_msg(pipe_parent[1], -1, TDM_UT_PIPE_MSG_SERVER_FAILED) == true, done);
371         TDM_INFO("*** server failed ***");
372
373         if (dpy)
374                 tdm_display_deinit(dpy);
375         return;
376
377 }
378
379 static void _tc_tdm_client_sig_handler(int sig)
380 {
381         TDM_UT_ERR("got signal: %d", sig);
382         kill(TDMClient::server_pid, 9);
383         abort();
384 }
385
386 static pid_t
387 _tc_tdm_client_server_fork(int *pipe_parent, int *pipe_child)
388 {
389         pid_t pid;
390         int msg;
391
392         TDM_UT_GOTO_IF_FAIL(pipe(pipe_parent) == 0, failed);
393         TDM_UT_GOTO_IF_FAIL(pipe(pipe_child) == 0, failed);
394
395         signal(SIGCHLD, SIG_IGN);
396         signal(SIGSEGV, _tc_tdm_client_sig_handler);
397
398         prctl(PR_SET_PDEATHSIG, SIGHUP);
399
400         pid = fork();
401         TDM_UT_GOTO_IF_FAIL(pid >= 0, failed);
402
403         if (pid == 0) {
404                 _tc_tdm_server_run(pipe_parent, pipe_child);
405                 close(pipe_child[0]);
406                 close(pipe_child[1]);
407                 close(pipe_parent[0]);
408                 close(pipe_parent[1]);
409
410 #ifdef TIZEN_TEST_GCOV
411                 __gcov_flush();
412 #endif
413
414                 exit(0);
415         }
416
417         msg = _tc_tdm_pipe_read_msg(pipe_parent[0]);
418         TDM_UT_GOTO_IF_FAIL(msg == TDM_UT_PIPE_MSG_SERVER_READY, failed);
419
420         TDM_INFO("*** server fork done ***");
421
422         return pid;
423
424 failed:
425         return -1;
426 }
427
428 TEST_P(TDMClient, ClientCreate)
429 {
430         tdm_error ret;
431
432         client = tdm_client_create(&ret);
433         ASSERT_EQ(ret, TDM_ERROR_NONE);
434         ASSERT_NE(client, NULL);
435 }
436
437 TEST_P(TDMClient, ClientCreateNullOther)
438 {
439         client = tdm_client_create(NULL);
440         ASSERT_NE(client, NULL);
441 }
442
443 TEST_P(TDMClient, ClientDestroy)
444 {
445         tdm_error ret;
446
447         client = tdm_client_create(&ret);
448         ASSERT_EQ(ret, TDM_ERROR_NONE);
449         ASSERT_NE(client, NULL);
450
451         tdm_client_destroy(client);
452         client = NULL;
453 }
454
455 TEST_P(TDMClient, ClientNullObject)
456 {
457         tdm_client_destroy(NULL);
458 }
459
460 /* tdm_client_get_fd */
461 TEST_P(TDMClient, ClientGetFd)
462 {
463         int fd = TDM_UT_INVALID_VALUE;
464
465         ASSERT_EQ(PrepareClient(), true);
466
467         ASSERT_EQ(tdm_client_get_fd(client, &fd), TDM_ERROR_NONE);
468         ASSERT_GE(fd, 0);
469 }
470
471 TEST_P(TDMClient, ClientGetFdNullObject)
472 {
473         int fd = TDM_UT_INVALID_VALUE;
474         ASSERT_EQ(tdm_client_get_fd(NULL, &fd), TDM_ERROR_INVALID_PARAMETER);
475         ASSERT_EQ(fd, TDM_UT_INVALID_VALUE);
476 }
477
478 TEST_P(TDMClient, ClientGetFdNullOther)
479 {
480         ASSERT_EQ(PrepareClient(), true);
481
482         ASSERT_EQ(tdm_client_get_fd(client, NULL), TDM_ERROR_INVALID_PARAMETER);
483 }
484
485 static void
486 _tc_tdm_client_vblank_cb(unsigned int sequence, unsigned int tv_sec, unsigned int tv_usec, void *user_data)
487 {
488         bool *done = (bool *)user_data;
489         if (done)
490                 *done = true;
491 }
492
493 /* tdm_client_handle_events_timeout */
494 TEST_P(TDMClient, ClientHandleEvent)
495 {
496         bool done = false;
497
498         ASSERT_EQ(PrepareClient(), true);
499
500         ASSERT_EQ(tdm_client_wait_vblank(client, NULL, 1, 1, 0, _tc_tdm_client_vblank_cb, &done), TDM_ERROR_NONE);
501         ASSERT_EQ(done, false);
502
503         while (!done)
504                 ASSERT_EQ(tdm_client_handle_events(client), TDM_ERROR_NONE);
505 }
506
507 TEST_P(TDMClient, ClientHandleEventNullObject)
508 {
509         ASSERT_EQ(tdm_client_handle_events(NULL), TDM_ERROR_INVALID_PARAMETER);
510 }
511
512 /* tdm_client_wait_vblank, deprecated */
513 TEST_P(TDMClient, ClientWaitVblank)
514 {
515         bool done = false;
516
517         ASSERT_EQ(PrepareClient(), true);
518
519         ASSERT_EQ(tdm_client_wait_vblank(client, NULL, 1, 1, 0, _tc_tdm_client_vblank_cb, &done), TDM_ERROR_NONE);
520         ASSERT_EQ(done, false);
521
522         while (!done)
523                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
524 }
525
526 /* tdm_client_get_output */
527 TEST_P(TDMClient, ClientGetOutput)
528 {
529         tdm_error ret;
530
531         ASSERT_EQ(PrepareClient(), true);
532
533         output = tdm_client_get_output(client, NULL, &ret);
534         ASSERT_EQ(ret, TDM_ERROR_NONE);
535         ASSERT_NE(output, NULL);
536 }
537
538 TEST_P(TDMClient, ClientGetOutputPrimary)
539 {
540         tdm_error ret;
541
542         ASSERT_EQ(PrepareClient(), true);
543
544         output = tdm_client_get_output(client, (char*)"primary", &ret);
545         ASSERT_EQ(ret, TDM_ERROR_NONE);
546         ASSERT_NE(output, NULL);
547 }
548
549 TEST_P(TDMClient, ClientGetOutputDefault)
550 {
551         tdm_error ret;
552
553         ASSERT_EQ(PrepareClient(), true);
554
555         output = tdm_client_get_output(client, (char*)"default", &ret);
556         ASSERT_EQ(ret, TDM_ERROR_NONE);
557         ASSERT_NE(output, NULL);
558 }
559
560 TEST_P(TDMClient, ClientGetOutputInvalidName)
561 {
562         tdm_error ret;
563
564         ASSERT_EQ(PrepareClient(), true);
565
566         output = tdm_client_get_output(client, (char*)"invalid", &ret);
567         ASSERT_EQ(ret, TDM_ERROR_INVALID_PARAMETER);
568         ASSERT_EQ(output, NULL);
569 }
570
571 TEST_P(TDMClient, ClientGetOutputNullObject)
572 {
573         tdm_error ret;
574
575         output = tdm_client_get_output(NULL, NULL, &ret);
576         ASSERT_EQ(ret, TDM_ERROR_INVALID_PARAMETER);
577         ASSERT_EQ(output, NULL);
578 }
579
580 TEST_P(TDMClient, ClientGetOutputNullOther)
581 {
582         ASSERT_EQ(PrepareClient(), true);
583
584         output = tdm_client_get_output(client, NULL, NULL);
585         ASSERT_NE(output, NULL);
586 }
587
588 static void
589 _tc_tdm_client_output_change_dpms_cb(tdm_client_output *output,
590                                                                          tdm_output_change_type type,
591                                                                          tdm_value value,
592                                                                          void *user_data)
593 {
594         bool *done = (bool *)user_data;
595
596         switch (type) {
597         case TDM_OUTPUT_CHANGE_DPMS:
598                 if (done)
599                         *done = true;
600                 break;
601         default:
602                 break;
603         }
604 }
605
606 /* tdm_client_output_add_change_handler */
607 TEST_P(TDMClient, ClientOutputAddChangeHandler)
608 {
609         bool done = false;
610         tdm_output_dpms dpms;
611
612         ASSERT_EQ(PrepareClient(), true);
613         ASSERT_EQ(PrepareOutput(), true);
614
615         ASSERT_EQ(tdm_client_output_add_change_handler(output, _tc_tdm_client_output_change_dpms_cb, &done), TDM_ERROR_NONE);
616         ASSERT_EQ(_tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_DPMS_OFF), true);
617
618         while (!done)
619                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
620
621         ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
622         ASSERT_EQ(dpms, TDM_OUTPUT_DPMS_OFF);
623
624         ASSERT_EQ(_tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_DPMS_ON), true);
625         while (dpms != TDM_OUTPUT_DPMS_ON) {
626                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
627                 ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
628         }
629 }
630
631 TEST_P(TDMClient, ClientOutputAddChangeHandlerTwice)
632 {
633         ASSERT_EQ(PrepareClient(), true);
634         ASSERT_EQ(PrepareOutput(), true);
635
636         ASSERT_EQ(tdm_client_output_add_change_handler(output, _tc_tdm_client_output_change_dpms_cb, NULL), TDM_ERROR_NONE);
637         ASSERT_EQ(tdm_client_output_add_change_handler(output, _tc_tdm_client_output_change_dpms_cb, NULL), TDM_ERROR_BAD_REQUEST);
638 }
639
640 TEST_P(TDMClient, ClientOutputAddChangeHandlerNullObject)
641 {
642         ASSERT_EQ(tdm_client_output_add_change_handler(NULL, _tc_tdm_client_output_change_dpms_cb, NULL), TDM_ERROR_INVALID_PARAMETER);
643 }
644
645 TEST_P(TDMClient, ClientOutputAddChangeHandlerNullOther)
646 {
647         ASSERT_EQ(PrepareClient(), true);
648         ASSERT_EQ(PrepareOutput(), true);
649
650         ASSERT_EQ(tdm_client_output_add_change_handler(output, NULL, NULL), TDM_ERROR_INVALID_PARAMETER);
651 }
652
653 /* tdm_client_output_remove_change_handler */
654 TEST_P(TDMClient, ClientOutputRemoveChangeHandler)
655 {
656         ASSERT_EQ(PrepareClient(), true);
657         ASSERT_EQ(PrepareOutput(), true);
658
659         ASSERT_EQ(tdm_client_output_add_change_handler(output, _tc_tdm_client_output_change_dpms_cb, NULL), TDM_ERROR_NONE);
660         tdm_client_output_remove_change_handler(output, _tc_tdm_client_output_change_dpms_cb, NULL);
661 }
662
663 TEST_P(TDMClient, ClientOutputRemoveChangeHandlerDifferentData)
664 {
665         bool done = (bool)TDM_UT_INVALID_VALUE;
666
667         ASSERT_EQ(PrepareClient(), true);
668         ASSERT_EQ(PrepareOutput(), true);
669
670         ASSERT_EQ(tdm_client_output_add_change_handler(output, _tc_tdm_client_output_change_dpms_cb, &done), TDM_ERROR_NONE);
671         tdm_client_output_remove_change_handler(output, _tc_tdm_client_output_change_dpms_cb, NULL);
672 }
673
674 static void
675 _tc_tdm_client_output_change_dpms_cb2(tdm_client_output *output,
676                                                                           tdm_output_change_type type,
677                                                                           tdm_value value,
678                                                                           void *user_data)
679 {
680         switch (type) {
681         case TDM_OUTPUT_CHANGE_DPMS:
682                 tdm_client_output_remove_change_handler(output, _tc_tdm_client_output_change_dpms_cb2, user_data);
683                 break;
684         default:
685                 break;
686         }
687 }
688
689 TEST_P(TDMClient, ClientOutputRemoveChangeHandlerInHandler)
690 {
691         tdm_output_dpms dpms = TDM_OUTPUT_DPMS_ON;
692
693         ASSERT_EQ(PrepareClient(), true);
694         ASSERT_EQ(PrepareOutput(), true);
695
696         ASSERT_EQ(tdm_client_output_add_change_handler(output, _tc_tdm_client_output_change_dpms_cb2, NULL), TDM_ERROR_NONE);
697         ASSERT_EQ(_tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_DPMS_OFF), true);
698         ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
699         while (dpms != TDM_OUTPUT_DPMS_OFF) {
700                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
701                 ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
702         }
703
704         ASSERT_EQ(_tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_DPMS_ON), true);
705         while (dpms != TDM_OUTPUT_DPMS_ON)
706                 ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
707 }
708
709 TEST_P(TDMClient, ClientOutputRemoveChangeHandlerNullObject)
710 {
711         tdm_client_output_remove_change_handler(NULL, _tc_tdm_client_output_change_dpms_cb, NULL);
712 }
713
714 TEST_P(TDMClient, ClientOutputRemoveChangeHandlerNullOther)
715 {
716         ASSERT_EQ(PrepareClient(), true);
717         ASSERT_EQ(PrepareOutput(), true);
718
719         tdm_client_output_remove_change_handler(output, NULL, NULL);
720 }
721
722 /* tdm_client_output_get_refresh_rate */
723 TEST_P(TDMClient, ClientOutputGetRefreshRate)
724 {
725         unsigned int refresh = 0;
726
727         ASSERT_EQ(PrepareClient(), true);
728         ASSERT_EQ(PrepareOutput(), true);
729
730         ASSERT_EQ(tdm_client_output_get_refresh_rate(output, &refresh), TDM_ERROR_NONE);
731         ASSERT_GT(refresh, 0);
732 }
733
734 TEST_P(TDMClient, ClientOutputGetRefreshRateNullObject)
735 {
736         unsigned int refresh = (unsigned int)TDM_UT_INVALID_VALUE;
737
738         ASSERT_EQ(tdm_client_output_get_refresh_rate(NULL, &refresh), TDM_ERROR_INVALID_PARAMETER);
739         ASSERT_EQ(refresh, (unsigned int)TDM_UT_INVALID_VALUE);
740 }
741
742 TEST_P(TDMClient, ClientOutputGetRefreshRateNullOther)
743 {
744         ASSERT_EQ(PrepareClient(), true);
745         ASSERT_EQ(PrepareOutput(), true);
746
747         ASSERT_EQ(tdm_client_output_get_refresh_rate(output, NULL), TDM_ERROR_INVALID_PARAMETER);
748 }
749
750 /* tdm_client_output_get_mode */
751 TEST_P(TDMClient, ClientOutputGetMode)
752 {
753         unsigned int width = 0, height = 0;
754
755         ASSERT_EQ(PrepareClient(), true);
756         ASSERT_EQ(PrepareOutput(), true);
757
758         ASSERT_EQ(tdm_client_output_get_mode(output, &width, &height), TDM_ERROR_NONE);
759         ASSERT_GT(width, 0);
760         ASSERT_GT(height, 0);
761 }
762
763 TEST_P(TDMClient, ClientOutputGetModeNullObject)
764 {
765         unsigned int width = (unsigned int)TDM_UT_INVALID_VALUE;
766         unsigned int height = (unsigned int)TDM_UT_INVALID_VALUE;
767
768         ASSERT_EQ(tdm_client_output_get_mode(NULL, &width, &height), TDM_ERROR_INVALID_PARAMETER);
769         ASSERT_EQ(width, (unsigned int)TDM_UT_INVALID_VALUE);
770         ASSERT_EQ(height, (unsigned int)TDM_UT_INVALID_VALUE);
771 }
772
773 TEST_P(TDMClient, ClientOutputGetModeNullOther)
774 {
775         ASSERT_EQ(PrepareClient(), true);
776         ASSERT_EQ(PrepareOutput(), true);
777
778         ASSERT_EQ(tdm_client_output_get_mode(output, NULL, NULL), TDM_ERROR_INVALID_PARAMETER);
779 }
780
781 /* tdm_client_output_get_conn_status */
782 TEST_P(TDMClient, ClientOutputGetConnStatus)
783 {
784         tdm_output_conn_status status = (tdm_output_conn_status)TDM_UT_INVALID_VALUE;
785
786         ASSERT_EQ(PrepareClient(), true);
787         ASSERT_EQ(PrepareOutput(), true);
788
789         ASSERT_EQ(tdm_client_output_get_conn_status(output, &status), TDM_ERROR_NONE);
790         ASSERT_NE(status, (tdm_output_conn_status)TDM_UT_INVALID_VALUE);
791 }
792
793 TEST_P(TDMClient, ClientOutputGetConnStatusNullObject)
794 {
795         tdm_output_conn_status status = (tdm_output_conn_status)TDM_UT_INVALID_VALUE;
796
797         ASSERT_EQ(tdm_client_output_get_conn_status(NULL, &status), TDM_ERROR_INVALID_PARAMETER);
798         ASSERT_EQ(status, (tdm_output_conn_status)TDM_UT_INVALID_VALUE);
799 }
800
801 TEST_P(TDMClient, ClientOutputGetConnStatusNullOther)
802 {
803         ASSERT_EQ(PrepareClient(), true);
804         ASSERT_EQ(PrepareOutput(), true);
805
806         ASSERT_EQ(tdm_client_output_get_conn_status(output, NULL), TDM_ERROR_INVALID_PARAMETER);
807 }
808
809 /* tdm_client_output_get_dpms */
810 TEST_P(TDMClient, ClientOutputGetDpms)
811 {
812         tdm_output_dpms dpms = (tdm_output_dpms)TDM_UT_INVALID_VALUE;
813
814         ASSERT_EQ(PrepareClient(), true);
815         ASSERT_EQ(PrepareOutput(), true);
816
817         ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
818         ASSERT_NE(dpms, (tdm_output_dpms)TDM_UT_INVALID_VALUE);
819 }
820
821 TEST_P(TDMClient, ClientOutputGetDpmsNullObject)
822 {
823         tdm_output_dpms dpms = (tdm_output_dpms)TDM_UT_INVALID_VALUE;
824
825         ASSERT_EQ(tdm_client_output_get_dpms(NULL, &dpms), TDM_ERROR_INVALID_PARAMETER);
826         ASSERT_EQ(dpms, (tdm_output_dpms)TDM_UT_INVALID_VALUE);
827 }
828
829 TEST_P(TDMClient, ClientOutputGetDpmsNullOther)
830 {
831         ASSERT_EQ(PrepareClient(), true);
832         ASSERT_EQ(PrepareOutput(), true);
833
834         ASSERT_EQ(tdm_client_output_get_dpms(output, NULL), TDM_ERROR_INVALID_PARAMETER);
835 }
836
837 /* tdm_client_output_create_vblank */
838 TEST_P(TDMClient, ClientOutputCreateVblank)
839 {
840         tdm_error ret;
841
842         ASSERT_EQ(PrepareClient(), true);
843         ASSERT_EQ(PrepareOutput(), true);
844
845         vblank = tdm_client_output_create_vblank(output, &ret);
846         ASSERT_EQ(ret, TDM_ERROR_NONE);
847         ASSERT_NE(vblank, NULL);
848 }
849
850 TEST_P(TDMClient, ClientOutputCreateVblankNullObject)
851 {
852         tdm_error ret;
853
854         vblank = tdm_client_output_create_vblank(NULL, &ret);
855         ASSERT_EQ(ret, TDM_ERROR_INVALID_PARAMETER);
856         ASSERT_EQ(vblank, NULL);
857 }
858
859 TEST_P(TDMClient, ClientOutputCreateVblankNullOther)
860 {
861         ASSERT_EQ(PrepareClient(), true);
862         ASSERT_EQ(PrepareOutput(), true);
863
864         vblank = tdm_client_output_create_vblank(output, NULL);
865         ASSERT_NE(vblank, NULL);
866 }
867
868 /* tdm_client_vblank_destroy */
869 TEST_P(TDMClient, ClientVblankDestroy)
870 {
871         tdm_error ret;
872
873         ASSERT_EQ(PrepareClient(), true);
874         ASSERT_EQ(PrepareOutput(), true);
875
876         vblank = tdm_client_output_create_vblank(output, &ret);
877         ASSERT_EQ(ret, TDM_ERROR_NONE);
878         ASSERT_NE(vblank, NULL);
879
880         tdm_client_vblank_destroy(vblank);
881         vblank = NULL;
882 }
883
884 TEST_P(TDMClient, ClientVblankDestroyNullObject)
885 {
886         tdm_client_vblank_destroy(NULL);
887 }
888
889 /* tdm_client_vblank_set_name */
890 TEST_P(TDMClient, ClientVblankSetName)
891 {
892         ASSERT_EQ(PrepareClient(), true);
893         ASSERT_EQ(PrepareOutput(), true);
894         ASSERT_EQ(PrepareVblank(), true);
895
896         ASSERT_EQ(tdm_client_vblank_set_name(vblank, TDM_UT_VBLANK_NAME), TDM_ERROR_NONE);
897 }
898
899 TEST_P(TDMClient, ClientVblankSetNameTwice)
900 {
901         ASSERT_EQ(PrepareClient(), true);
902         ASSERT_EQ(PrepareOutput(), true);
903         ASSERT_EQ(PrepareVblank(), true);
904
905         ASSERT_EQ(tdm_client_vblank_set_name(vblank, TDM_UT_VBLANK_NAME), TDM_ERROR_NONE);
906         ASSERT_EQ(tdm_client_vblank_set_name(vblank, TDM_UT_VBLANK_NAME), TDM_ERROR_NONE);
907 }
908
909 TEST_P(TDMClient, ClientVblankSetNameNullObject)
910 {
911         ASSERT_EQ(tdm_client_vblank_set_name(NULL, TDM_UT_VBLANK_NAME), TDM_ERROR_INVALID_PARAMETER);
912 }
913
914 /* tdm_client_vblank_set_sync */
915 TEST_P(TDMClient, ClientVblankSetSync)
916 {
917         ASSERT_EQ(PrepareClient(), true);
918         ASSERT_EQ(PrepareOutput(), true);
919         ASSERT_EQ(PrepareVblank(), true);
920
921         ASSERT_EQ(tdm_client_vblank_set_sync(vblank, 1), TDM_ERROR_NONE);
922 }
923
924 TEST_P(TDMClient, ClientVblankSetSyncTwice)
925 {
926         ASSERT_EQ(PrepareClient(), true);
927         ASSERT_EQ(PrepareOutput(), true);
928         ASSERT_EQ(PrepareVblank(), true);
929
930         ASSERT_EQ(tdm_client_vblank_set_sync(vblank, 1), TDM_ERROR_NONE);
931         ASSERT_EQ(tdm_client_vblank_set_sync(vblank, 1), TDM_ERROR_NONE);
932 }
933
934 TEST_P(TDMClient, ClientVblankSetSyncNullObject)
935 {
936         ASSERT_EQ(tdm_client_vblank_set_sync(NULL, 1), TDM_ERROR_INVALID_PARAMETER);
937 }
938
939 /* tdm_client_vblank_set_fps */
940 TEST_P(TDMClient, ClientVblankSetFps)
941 {
942         ASSERT_EQ(PrepareClient(), true);
943         ASSERT_EQ(PrepareOutput(), true);
944         ASSERT_EQ(PrepareVblank(), true);
945
946         ASSERT_EQ(tdm_client_vblank_set_fps(vblank, 30), TDM_ERROR_NONE);
947 }
948
949 TEST_P(TDMClient, ClientVblankSetFpsTwice)
950 {
951         ASSERT_EQ(PrepareClient(), true);
952         ASSERT_EQ(PrepareOutput(), true);
953         ASSERT_EQ(PrepareVblank(), true);
954
955         ASSERT_EQ(tdm_client_vblank_set_fps(vblank, 30), TDM_ERROR_NONE);
956         ASSERT_EQ(tdm_client_vblank_set_fps(vblank, 30), TDM_ERROR_NONE);
957 }
958
959 TEST_P(TDMClient, ClientVblankSetFpsNullObject)
960 {
961         ASSERT_EQ(tdm_client_vblank_set_fps(NULL, 30), TDM_ERROR_INVALID_PARAMETER);
962 }
963
964 /* tdm_client_vblank_set_offset */
965 TEST_P(TDMClient, ClientVblankSetOffset)
966 {
967         ASSERT_EQ(PrepareClient(), true);
968         ASSERT_EQ(PrepareOutput(), true);
969         ASSERT_EQ(PrepareVblank(), true);
970
971         ASSERT_EQ(tdm_client_vblank_set_offset(vblank, 10), TDM_ERROR_NONE);
972 }
973
974 TEST_P(TDMClient, ClientVblankSetOffsetTwice)
975 {
976         ASSERT_EQ(PrepareClient(), true);
977         ASSERT_EQ(PrepareOutput(), true);
978         ASSERT_EQ(PrepareVblank(), true);
979
980         ASSERT_EQ(tdm_client_vblank_set_offset(vblank, 10), TDM_ERROR_NONE);
981         ASSERT_EQ(tdm_client_vblank_set_offset(vblank, 10), TDM_ERROR_NONE);
982 }
983
984 TEST_P(TDMClient, ClientVblankSetOffsetNullObject)
985 {
986         ASSERT_EQ(tdm_client_vblank_set_offset(NULL, 10), TDM_ERROR_INVALID_PARAMETER);
987 }
988
989 /* tdm_client_vblank_set_enable_fake */
990 TEST_P(TDMClient, ClientVblankSetEnableFake)
991 {
992         ASSERT_EQ(PrepareClient(), true);
993         ASSERT_EQ(PrepareOutput(), true);
994         ASSERT_EQ(PrepareVblank(), true);
995
996         ASSERT_EQ(tdm_client_vblank_set_enable_fake(vblank, 1), TDM_ERROR_NONE);
997 }
998
999 TEST_P(TDMClient, ClientVblankSetEnableFakeTwice)
1000 {
1001         ASSERT_EQ(PrepareClient(), true);
1002         ASSERT_EQ(PrepareOutput(), true);
1003         ASSERT_EQ(PrepareVblank(), true);
1004
1005         ASSERT_EQ(tdm_client_vblank_set_enable_fake(vblank, 1), TDM_ERROR_NONE);
1006         ASSERT_EQ(tdm_client_vblank_set_enable_fake(vblank, 1), TDM_ERROR_NONE);
1007 }
1008
1009 TEST_P(TDMClient, ClientVblankSetEnableFakeNullObject)
1010 {
1011         ASSERT_EQ(tdm_client_vblank_set_enable_fake(NULL, 1), TDM_ERROR_INVALID_PARAMETER);
1012 }
1013
1014 static void
1015 _tc_tdm_client_vblank_cb2(tdm_client_vblank *vblank,
1016                                                   tdm_error error,
1017                                                   unsigned int sequence,
1018                                                   unsigned int tv_sec,
1019                                                   unsigned int tv_usec,
1020                                                   void *user_data)
1021 {
1022         bool *done = (bool *)user_data;
1023         if (done)
1024                 *done = true;
1025 }
1026
1027 /* tdm_client_vblank_wait */
1028 TEST_P(TDMClient, ClientVblankWait)
1029 {
1030         bool done;
1031
1032         ASSERT_EQ(PrepareClient(), true);
1033         ASSERT_EQ(PrepareOutput(), true);
1034         ASSERT_EQ(PrepareVblank(), true);
1035
1036         done = false;
1037         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb2, &done), TDM_ERROR_NONE);
1038
1039         start = tdm_helper_get_time();
1040         while (!done)
1041                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1042         end = tdm_helper_get_time();
1043
1044         /* "+ vrefresh_interval" consider the delay of socket communication between kernel and platform */
1045         ASSERT_LT((end - start), (vrefresh_interval + vrefresh_interval));
1046 }
1047
1048 TEST_P(TDMClient, ClientVblankWaitFewTime)
1049 {
1050         bool done1, done2, done3;
1051
1052         ASSERT_EQ(PrepareClient(), true);
1053         ASSERT_EQ(PrepareOutput(), true);
1054         ASSERT_EQ(PrepareVblank(), true);
1055
1056         done1 = done2 = done3 = false;
1057         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb2, &done1), TDM_ERROR_NONE);
1058         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb2, &done2), TDM_ERROR_NONE);
1059         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb2, &done3), TDM_ERROR_NONE);
1060
1061         start = tdm_helper_get_time();
1062         while (!done1 || !done2 || !done3)
1063                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1064         end = tdm_helper_get_time();
1065
1066         /* "+ vrefresh_interval" consider the delay of socket communication between kernel and platform */
1067         ASSERT_LT((end - start), (vrefresh_interval + vrefresh_interval));
1068
1069 }
1070
1071 TEST_P(TDMClient, ClientVblankWaitInterval0)
1072 {
1073         ASSERT_EQ(PrepareClient(), true);
1074         ASSERT_EQ(PrepareOutput(), true);
1075         ASSERT_EQ(PrepareVblank(), true);
1076
1077         ASSERT_EQ(tdm_client_vblank_wait(vblank, 0, _tc_tdm_client_vblank_cb2, NULL), TDM_ERROR_INVALID_PARAMETER);
1078 }
1079
1080 TEST_P(TDMClient, ClientVblankWaitInterval)
1081 {
1082         bool done;
1083
1084         ASSERT_EQ(PrepareClient(), true);
1085         ASSERT_EQ(PrepareOutput(), true);
1086         ASSERT_EQ(PrepareVblank(), true);
1087
1088         /* start from 1 */
1089         for (int t = 1; t < 10; t++) {
1090                 done = false;
1091                 ASSERT_EQ(tdm_client_vblank_wait(vblank, t, _tc_tdm_client_vblank_cb2, &done), TDM_ERROR_NONE);
1092
1093                 start = tdm_helper_get_time();
1094                 while (!done)
1095                         ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1096                 end = tdm_helper_get_time();
1097
1098                 /* "+ vrefresh_interval" consider the delay of socket communication between kernel and platform */
1099                 ASSERT_GT((end - start), (vrefresh_interval * (t - 1)));
1100                 ASSERT_LT((end - start), (vrefresh_interval * t + vrefresh_interval));
1101         }
1102 }
1103
1104 static void
1105 _tc_tdm_client_vblank_cb3(tdm_client_vblank *vblank,
1106                                                   tdm_error error,
1107                                                   unsigned int sequence,
1108                                                   unsigned int tv_sec,
1109                                                   unsigned int tv_usec,
1110                                                   void *user_data)
1111 {
1112         unsigned int *cur_seq = (unsigned int *)user_data;
1113         if (cur_seq)
1114                 *cur_seq = sequence;
1115 }
1116
1117 TEST_P(TDMClient, ClientVblankWaitSeq)
1118 {
1119         ASSERT_EQ(PrepareClient(), true);
1120         ASSERT_EQ(PrepareOutput(), true);
1121         ASSERT_EQ(PrepareVblank(), true);
1122
1123         for (int t = 0; t < 10; t++) {
1124                 unsigned int cur_seq = 0, temp = 0;
1125
1126                 ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb3, &cur_seq), TDM_ERROR_NONE);
1127                 while (cur_seq == 0)
1128                         ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1129
1130                 start = tdm_helper_get_time();
1131                 ASSERT_EQ(tdm_client_vblank_wait_seq(vblank, cur_seq + 1, _tc_tdm_client_vblank_cb3, &temp), TDM_ERROR_NONE);
1132                 while (temp == 0)
1133                         ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1134                 end = tdm_helper_get_time();
1135
1136                 /* "+ vrefresh_interval" consider the delay of socket communication between kernel and platform */
1137                 ASSERT_LT((end - start), (vrefresh_interval + vrefresh_interval));
1138         }
1139 }
1140
1141 TEST_P(TDMClient, ClientVblankWaitSeqInterval)
1142 {
1143         ASSERT_EQ(PrepareClient(), true);
1144         ASSERT_EQ(PrepareOutput(), true);
1145         ASSERT_EQ(PrepareVblank(), true);
1146
1147         /* start from 1 */
1148         for (int t = 1; t < 10; t++) {
1149                 unsigned int cur_seq = 0, temp = 0;
1150
1151                 ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb3, &cur_seq), TDM_ERROR_NONE);
1152                 while (cur_seq == 0)
1153                         ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1154
1155                 start = tdm_helper_get_time();
1156                 ASSERT_EQ(tdm_client_vblank_wait_seq(vblank, cur_seq + t, _tc_tdm_client_vblank_cb3, &temp), TDM_ERROR_NONE);
1157                 while (temp == 0)
1158                         ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1159                 end = tdm_helper_get_time();
1160
1161                 /* "+ vrefresh_interval" consider the delay of socket communication between kernel and platform */
1162                 ASSERT_GT((end - start), (vrefresh_interval * (t - 1)));
1163                 ASSERT_LT((end - start), (vrefresh_interval * t + vrefresh_interval));
1164         }
1165 }
1166
1167 TEST_P(TDMClient, ClientVblankWaitSetOffset)
1168 {
1169         bool done;
1170
1171         ASSERT_EQ(PrepareClient(), true);
1172         ASSERT_EQ(PrepareOutput(), true);
1173         ASSERT_EQ(PrepareVblank(), true);
1174
1175         ASSERT_EQ(tdm_client_vblank_set_offset(vblank, 100), TDM_ERROR_NONE);
1176
1177         done = false;
1178         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb2, &done), TDM_ERROR_NONE);
1179
1180         start = tdm_helper_get_time();
1181         while (!done)
1182                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1183         end = tdm_helper_get_time();
1184
1185         /* "+ vrefresh_interval" consider the delay of socket communication between kernel and platform */
1186         ASSERT_GT((end - start), (0.1));
1187         ASSERT_LT((end - start), (vrefresh_interval + vrefresh_interval + 0.1));
1188 }
1189
1190 TEST_P(TDMClient, ClientVblankWaitSetFps)
1191 {
1192         bool done;
1193         double interval;
1194         unsigned int fps = 10;
1195
1196         ASSERT_EQ(PrepareClient(), true);
1197         ASSERT_EQ(PrepareOutput(), true);
1198         ASSERT_EQ(PrepareVblank(), true);
1199
1200         ASSERT_EQ(tdm_client_vblank_set_fps(vblank, fps), TDM_ERROR_NONE);
1201         interval = 1.0 / (double)fps;
1202
1203         done = false;
1204         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb2, &done), TDM_ERROR_NONE);
1205
1206         start = tdm_helper_get_time();
1207         while (!done)
1208                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1209         end = tdm_helper_get_time();
1210
1211         /* "+ vrefresh_interval" consider the delay of socket communication between kernel and platform */
1212         ASSERT_GT((end - start), (interval - vrefresh_interval * 2));
1213         ASSERT_LT((end - start), (interval + vrefresh_interval));
1214 }
1215
1216 #if 0
1217
1218 TEST_P(TDMVblank, VblankWaitEnableDisableGlobalFps)
1219 {
1220         TDM_UT_SKIP_FLAG(has_outputs);
1221
1222         unsigned int fps = (unsigned int)TDM_UT_INVALID_VALUE;
1223         double vrefresh_interval;
1224         unsigned int cur_seq[3];
1225         unsigned int global_fps = 5;
1226         double start, end, interval;
1227
1228         ASSERT_EQ(TestPrepareOutput(), true);
1229         ASSERT_EQ(TestCreateVblanks3(), true);
1230         ASSERT_EQ(vblank_count, 3);
1231
1232         ASSERT_EQ(tdm_vblank_get_fps(vblanks[0], &fps), TDM_ERROR_NONE);
1233         ASSERT_TRUE(fps >= 30 && fps != (unsigned int)TDM_UT_INVALID_VALUE);
1234         vrefresh_interval = 1.0 / (double)fps;
1235
1236         for (int v = 0; v < 3; v++)
1237                 ASSERT_EQ(tdm_vblank_set_fixed_fps(vblanks[v], 10 * (v + 1)), TDM_ERROR_NONE);
1238
1239         /* enable test */
1240         tdm_vblank_enable_global_fps(1, global_fps);
1241         interval = 1.0 / (double)global_fps;
1242
1243         for (int v = 0; v < 3; v++) {
1244                 cur_seq[v] = 0;
1245                 ASSERT_EQ(tdm_vblank_wait(vblanks[v], 0, 0, 1, _tc_tdm_vblank_cb, &cur_seq[v]), TDM_ERROR_NONE);
1246         }
1247
1248         start = tdm_helper_get_time();
1249         while (cur_seq[0] == 0)
1250                 ASSERT_EQ(tc_tdm_display_handle_events(dpy), TDM_ERROR_NONE);
1251         end = tdm_helper_get_time();
1252
1253         ASSERT_NE(cur_seq[1], 0);
1254         ASSERT_NE(cur_seq[2], 0);
1255
1256         /* "+- vrefresh_interval" consider the delay of socket communication between kernel and platform */
1257         ASSERT_GT((end - start), (interval - vrefresh_interval));
1258         ASSERT_LT((end - start), (interval + vrefresh_interval));
1259
1260         /* disable test */
1261         tdm_vblank_enable_global_fps(0, 0);
1262
1263         for (int v = 0; v < 3; v++) {
1264                 cur_seq[v] = 0;
1265                 ASSERT_EQ(tdm_vblank_wait(vblanks[v], 0, 0, 1, _tc_tdm_vblank_cb, &cur_seq[v]), TDM_ERROR_NONE);
1266         }
1267
1268         while (cur_seq[0] == 0)
1269                 ASSERT_EQ(tc_tdm_display_handle_events(dpy), TDM_ERROR_NONE);
1270         ASSERT_EQ(cur_seq[1], 0);
1271         ASSERT_EQ(cur_seq[2], 0);
1272
1273         while (cur_seq[1] == 0)
1274                 ASSERT_EQ(tc_tdm_display_handle_events(dpy), TDM_ERROR_NONE);
1275         ASSERT_EQ(cur_seq[2], 0);
1276 }
1277
1278 TEST_P(TDMVblank, VblankWaitIgnoreGlobalFps)
1279 {
1280         TDM_UT_SKIP_FLAG(has_outputs);
1281
1282         unsigned int fps = (unsigned int)TDM_UT_INVALID_VALUE;
1283         unsigned int cur_seq[3];
1284         unsigned int global_fps = 5;
1285         double start, end, interval;
1286
1287         ASSERT_EQ(TestPrepareOutput(), true);
1288         ASSERT_EQ(TestCreateVblanks3(), true);
1289         ASSERT_EQ(vblank_count, 3);
1290
1291         ASSERT_EQ(tdm_vblank_get_fps(vblanks[0], &fps), TDM_ERROR_NONE);
1292         ASSERT_TRUE(fps >= 30 && fps != (unsigned int)TDM_UT_INVALID_VALUE);
1293         interval = 1.0 / (double)fps;
1294
1295         /* 2nd vblank will ignore the global fps. */
1296         ASSERT_EQ(tdm_vblank_ignore_global_fps(vblanks[1], 1), TDM_ERROR_NONE);
1297
1298         tdm_vblank_enable_global_fps(1, global_fps);
1299
1300         for (int v = 0; v < 3; v++) {
1301                 cur_seq[v] = 0;
1302                 ASSERT_EQ(tdm_vblank_wait(vblanks[v], 0, 0, 1, _tc_tdm_vblank_cb, &cur_seq[v]), TDM_ERROR_NONE);
1303         }
1304
1305         start = tdm_helper_get_time();
1306         while (cur_seq[1] == 0)
1307                 ASSERT_EQ(tc_tdm_display_handle_events(dpy), TDM_ERROR_NONE);
1308         end = tdm_helper_get_time();
1309
1310         ASSERT_EQ(cur_seq[0], 0);
1311         ASSERT_EQ(cur_seq[2], 0);
1312
1313         /* "+ vrefresh_interval" consider the delay of socket communication between kernel and platform */
1314         ASSERT_LT((end - start), (interval + interval));
1315
1316         while (cur_seq[0] == 0)
1317                 ASSERT_EQ(tc_tdm_display_handle_events(dpy), TDM_ERROR_NONE);
1318         ASSERT_NE(cur_seq[2], 0);
1319 }
1320
1321 #endif
1322
1323 TEST_P(TDMClient, ClientVblankWaitNullObject)
1324 {
1325         unsigned int cur_seq = 0;
1326
1327         ASSERT_EQ(tdm_client_vblank_wait(NULL, 1, _tc_tdm_client_vblank_cb3, &cur_seq), TDM_ERROR_INVALID_PARAMETER);
1328 }
1329
1330 TEST_P(TDMClient, ClientVblankWaitNullOther)
1331 {
1332         ASSERT_EQ(PrepareClient(), true);
1333         ASSERT_EQ(PrepareOutput(), true);
1334         ASSERT_EQ(PrepareVblank(), true);
1335
1336         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, NULL, NULL), TDM_ERROR_INVALID_PARAMETER);
1337 }
1338
1339 TEST_P(TDMClient, ClientVblankWaitDpmsOff)
1340 {
1341         tdm_output_dpms dpms = (tdm_output_dpms)TDM_UT_INVALID_VALUE;
1342
1343         ASSERT_EQ(PrepareClient(), true);
1344         ASSERT_EQ(PrepareOutput(), true);
1345         ASSERT_EQ(PrepareVblank(), true);
1346
1347         ASSERT_EQ(_tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_DPMS_OFF), true);
1348         while (dpms != TDM_OUTPUT_DPMS_OFF)
1349                 ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
1350         ASSERT_EQ(dpms, TDM_OUTPUT_DPMS_OFF);
1351
1352         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb2, NULL), TDM_ERROR_DPMS_OFF);
1353
1354         ASSERT_EQ(_tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_DPMS_ON), true);
1355         while (dpms != TDM_OUTPUT_DPMS_ON)
1356                 ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
1357 }
1358
1359 TEST_P(TDMClient, ClientVblankWaitSetEnableFakeDpmsOff)
1360 {
1361         tdm_output_dpms dpms = (tdm_output_dpms)TDM_UT_INVALID_VALUE;
1362         bool done;
1363
1364         ASSERT_EQ(PrepareClient(), true);
1365         ASSERT_EQ(PrepareOutput(), true);
1366         ASSERT_EQ(PrepareVblank(), true);
1367
1368         ASSERT_EQ(_tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_DPMS_OFF), true);
1369         while (dpms != TDM_OUTPUT_DPMS_OFF)
1370                 ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
1371
1372         ASSERT_EQ(tdm_client_vblank_set_enable_fake(vblank, 1), TDM_ERROR_NONE);
1373
1374         done = false;
1375         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb2, &done), TDM_ERROR_NONE);
1376
1377         while (!done)
1378                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1379
1380         ASSERT_EQ(_tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_DPMS_ON), true);
1381         while (dpms != TDM_OUTPUT_DPMS_ON)
1382                 ASSERT_EQ(tdm_client_output_get_dpms(output, &dpms), TDM_ERROR_NONE);
1383 }
1384
1385 /* tdm_client_vblank_wait */
1386 TEST_P(TDMClient, ClientVblankIsWaiting)
1387 {
1388         bool done;
1389         unsigned int waiting;
1390
1391         ASSERT_EQ(PrepareClient(), true);
1392         ASSERT_EQ(PrepareOutput(), true);
1393         ASSERT_EQ(PrepareVblank(), true);
1394
1395         done = false;
1396         ASSERT_EQ(tdm_client_vblank_wait(vblank, 1, _tc_tdm_client_vblank_cb2, &done), TDM_ERROR_NONE);
1397
1398         waiting = tdm_client_vblank_is_waiting(vblank);
1399         ASSERT_EQ(waiting, 1);
1400
1401         start = tdm_helper_get_time();
1402         while (!done)
1403                 ASSERT_EQ(tdm_client_handle_events_timeout(client, 3000), TDM_ERROR_NONE);
1404         end = tdm_helper_get_time();
1405
1406         /* "+ vrefresh_interval" consider the delay of socket communication between kernel and platform */
1407         ASSERT_LT((end - start), (vrefresh_interval + vrefresh_interval));
1408
1409         waiting = tdm_client_vblank_is_waiting(vblank);
1410         ASSERT_EQ(waiting, 0);
1411 }
1412
1413 /* tdm_client_vblank_wait */
1414 TEST_P(TDMClient, ClientVblankIsWaitingNullObject)
1415 {
1416         unsigned int waiting = tdm_client_vblank_is_waiting(NULL);
1417         ASSERT_EQ(waiting, 0);
1418 }
1419
1420 TEST_P(TDMClient, ClientCreateVOutput)
1421 {
1422         tdm_error ret;
1423         int virtual_conf = 0;
1424         const char name[TDM_NAME_LEN] = "Virtual Output";
1425
1426         virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1427         if (virtual_conf == 0) {
1428                 ASSERT_EQ(virtual_conf, 0);
1429                 return;
1430         }
1431
1432         ASSERT_EQ(PrepareClient(), true);
1433
1434         voutput = tdm_client_create_voutput(client, name, &ret);
1435         ASSERT_EQ(ret, TDM_ERROR_NONE);
1436         ASSERT_NE(voutput, NULL);
1437
1438         tdm_client_voutput_destroy(voutput);
1439 }
1440
1441 class TDMVirtualOutput : public ::testing::Test
1442 {
1443 public:
1444         TDMVirtualOutput() {};
1445         ~TDMVirtualOutput() {};
1446
1447         static void SetUpTestCase();
1448         static void TearDownTestCase();
1449         static bool PrepareVOutput(void);
1450
1451 protected:
1452         static tdm_client *client;
1453         static tdm_client_voutput *voutput;
1454         const int MODE_COUNT = 2;
1455
1456 private:
1457         static pid_t server_pid;
1458
1459         /* 0: read, 1: write */
1460         static int pipe_parent[2];
1461         static int pipe_child[2];
1462
1463         static void ServerFork(void);
1464         static void ServerKill(void);
1465 };
1466
1467 pid_t TDMVirtualOutput::server_pid = -1;
1468 int TDMVirtualOutput::pipe_parent[2] = {-1, -1};
1469 int TDMVirtualOutput::pipe_child[2] = {-1, -1};
1470 tdm_client* TDMVirtualOutput::client = nullptr;
1471 tdm_client_voutput* TDMVirtualOutput::voutput = nullptr;
1472
1473 void TDMVirtualOutput::ServerKill(void)
1474 {
1475         if (pipe_child[0] >= 0)
1476                 close(pipe_child[0]);
1477         if (pipe_child[1] >= 0) {
1478                 if (server_pid > 0) {
1479                         bool ret = _tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_TERMINATE_SERVER);
1480                         if (ret) {
1481                                 if (waitpid(server_pid, NULL, 0) == server_pid)
1482                                         TDM_INFO("*** server terminated ***");
1483                                 else
1484                                         TDM_ERR("*** failed to terminate server ***");
1485                         } else {
1486                                 if (kill(server_pid, 9) < 0)
1487                                         TDM_ERR("*** failed to kill server ***");
1488                         }
1489                 }
1490                 close(pipe_child[1]);
1491         }
1492
1493         if (pipe_parent[0] >= 0)
1494                 close(pipe_parent[0]);
1495         if (pipe_parent[1] >= 0)
1496                 close(pipe_parent[1]);
1497
1498         server_pid = -1;
1499         pipe_parent[0] = pipe_parent[1] = -1;
1500         pipe_child[0] = pipe_child[1] = -1;
1501 }
1502
1503 void TDMVirtualOutput::ServerFork(void)
1504 {
1505         if (server_pid > 0)
1506                 return;
1507
1508         server_pid = _tc_tdm_client_server_fork(pipe_parent, pipe_child);
1509         ASSERT_GT(server_pid, 0);
1510 }
1511
1512 void TDMVirtualOutput::SetUpTestCase(void)
1513 {
1514         setenv("XDG_RUNTIME_DIR", "/run", 1);
1515         setenv("TBM_DISPLAY_SERVER", "1", 1);
1516
1517         if (server_pid == -1)
1518                 ServerFork();
1519
1520         ASSERT_EQ(PrepareVOutput(), true);
1521 }
1522
1523 void TDMVirtualOutput::TearDownTestCase(void)
1524 {
1525 //      TDM_UT_WAIT("check & press");
1526
1527         if (voutput)
1528                 tdm_client_voutput_destroy(voutput);
1529
1530         if (client)
1531                 tdm_client_destroy(client);
1532
1533         ServerKill();
1534
1535         unsetenv("XDG_RUNTIME_DIR");
1536         unsetenv("TBM_DISPLAY_SERVER");
1537 }
1538
1539 bool TDMVirtualOutput::PrepareVOutput(void)
1540 {
1541         tdm_error ret;
1542         int virtual_conf = 0;
1543         const char name[TDM_NAME_LEN] = "Virtual Output";
1544
1545         virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1546         if (virtual_conf == 0) return true;
1547
1548         client = tdm_client_create(&ret);
1549         TDM_UT_RETURN_FALSE_IF_FAIL(ret == TDM_ERROR_NONE);
1550         TDM_UT_RETURN_FALSE_IF_FAIL(client != NULL);
1551
1552         voutput = tdm_client_create_voutput(client, name, &ret);
1553         TDM_UT_RETURN_FALSE_IF_FAIL(ret == TDM_ERROR_NONE);
1554         TDM_UT_RETURN_FALSE_IF_FAIL(voutput != NULL);
1555
1556 //      TDM_UT_WAIT("check & press");
1557
1558         return true;
1559 }
1560
1561 static void
1562 _tc_tdm_client_virutual_make_available_mode(tdm_client_output_mode *modes, int count)
1563 {
1564         int i;
1565
1566         for (i = 0; i < count; i++) {
1567                 modes[i].clock = 25200;
1568                 modes[i].hdisplay = 640;
1569                 modes[i].hsync_start = 656;
1570                 modes[i].hsync_end = 752;
1571                 modes[i].htotal = 800;
1572                 modes[i].hskew = 0;
1573                 modes[i].vdisplay = 480;
1574                 modes[i].vsync_start = 490;
1575                 modes[i].vsync_end = 492;
1576                 modes[i].vtotal = 525;
1577                 modes[i].vscan = 0;
1578                 modes[i].vrefresh = 30;
1579                 modes[i].flags = 0;
1580                 modes[i].type = 0;
1581                 snprintf(modes[i].name, TDM_NAME_LEN, "%dx%d_%d", modes[i].hdisplay, modes[i].vdisplay, i);
1582         }
1583 }
1584
1585 TEST_F(TDMVirtualOutput, SetAvailableModes)
1586 {
1587         tdm_error ret;
1588         tdm_client_output_mode modes[this->MODE_COUNT];
1589         int count = this->MODE_COUNT;
1590         int virtual_conf;
1591
1592         if (this->voutput == NULL) {
1593                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1594                 ASSERT_EQ(virtual_conf, 0);
1595                 return;
1596         }
1597         _tc_tdm_client_virutual_make_available_mode(modes, count);
1598
1599         ret = tdm_client_voutput_set_available_modes(this->voutput, modes, count);
1600         ASSERT_EQ(ret, TDM_ERROR_NONE);
1601 }
1602
1603 TEST_F(TDMVirtualOutput, FailTestSetAvailableModes)
1604 {
1605         tdm_error ret;
1606         tdm_client_output_mode modes[this->MODE_COUNT];
1607         int count = this->MODE_COUNT;
1608         int virtual_conf;
1609
1610         if (this->voutput == NULL) {
1611                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1612                 ASSERT_EQ(virtual_conf, 0);
1613                 return;
1614         }
1615
1616         ret = tdm_client_voutput_set_available_modes(NULL, modes, count);
1617         ASSERT_EQ(ret, TDM_ERROR_INVALID_PARAMETER);
1618
1619         ret = tdm_client_voutput_set_available_modes(this->voutput, NULL, count);
1620         ASSERT_EQ(ret, TDM_ERROR_INVALID_PARAMETER);
1621 }
1622
1623 TEST_F(TDMVirtualOutput, SetPhysicalSize)
1624 {
1625         tdm_error ret;
1626         unsigned int mmWidth = 1234, mmHeight = 1234;
1627         int virtual_conf;
1628
1629         if (this->voutput == NULL) {
1630                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1631                 ASSERT_EQ(virtual_conf, 0);
1632                 return;
1633         }
1634
1635         ret = tdm_client_voutput_set_physical_size(this->voutput, mmWidth, mmHeight);
1636         ASSERT_EQ(ret, TDM_ERROR_NONE);
1637 }
1638
1639 TEST_F(TDMVirtualOutput, FailTestSetPhysicalSize)
1640 {
1641         tdm_error ret;
1642         unsigned int invalid_mmWidth = 0, invalid_mmHeight = 0;
1643         int virtual_conf;
1644
1645         if (this->voutput == NULL) {
1646                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1647                 ASSERT_EQ(virtual_conf, 0);
1648                 return;
1649         }
1650
1651         ret = tdm_client_voutput_set_physical_size(this->voutput, invalid_mmWidth, invalid_mmHeight);
1652         ASSERT_EQ(ret, TDM_ERROR_INVALID_PARAMETER);
1653 }
1654
1655 static void
1656 _tc_tdm_client_voutput_commit_handler(tdm_client_voutput *voutput, tbm_surface_h buffer, void *user_data)
1657 {
1658         int *flag;
1659         flag = (int *)user_data;
1660         *flag = 1;
1661 }
1662
1663 TEST_F(TDMVirtualOutput, AddCommitHandler)
1664 {
1665         tdm_error ret;
1666         int flag_callback_called = 0;
1667         int virtual_conf;
1668
1669         if (this->voutput == NULL) {
1670                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1671                 ASSERT_EQ(virtual_conf, 0);
1672                 return;
1673         }
1674
1675         ret = tdm_client_voutput_add_commit_handler(this->voutput,
1676                                                                                                 _tc_tdm_client_voutput_commit_handler,
1677                                                                                                 &flag_callback_called);
1678         ASSERT_EQ(ret, TDM_ERROR_NONE);
1679 //      ASSERT_EQ(flag_callback_called, 1);
1680
1681         tdm_client_voutput_remove_commit_handler(this->voutput,
1682                                                                                          _tc_tdm_client_voutput_commit_handler,
1683                                                                                          &flag_callback_called);
1684 }
1685
1686 TEST_F(TDMVirtualOutput, CommitDone)
1687 {
1688         tdm_error ret;
1689         int virtual_conf;
1690
1691         if (this->voutput == NULL) {
1692                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1693                 ASSERT_EQ(virtual_conf, 0);
1694                 return;
1695         }
1696
1697         ret = tdm_client_voutput_commit_done(this->voutput);
1698         ASSERT_EQ(ret, TDM_ERROR_NONE);
1699 }
1700
1701 TEST_F(TDMVirtualOutput, GetClientOutput)
1702 {
1703         tdm_error ret;
1704         tdm_client_output *output;
1705         int virtual_conf;
1706
1707         if (this->voutput == NULL) {
1708                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1709                 ASSERT_EQ(virtual_conf, 0);
1710                 return;
1711         }
1712
1713         output = tdm_client_voutput_get_client_output(this->voutput, &ret);
1714         ASSERT_EQ(ret, TDM_ERROR_NONE);
1715         ASSERT_NE(output, NULL);
1716 }
1717
1718 TEST_F(TDMVirtualOutput, Connect)
1719 {
1720         tdm_error ret;
1721         unsigned int mmWidth = 300, mmHeight = 150;
1722         tdm_client_output_mode modes[this->MODE_COUNT];
1723         int count = this->MODE_COUNT;
1724         int virtual_conf;
1725
1726         if (this->voutput == NULL) {
1727                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1728                 ASSERT_EQ(virtual_conf, 0);
1729                 return;
1730         }
1731
1732         ret = tdm_client_voutput_set_physical_size(this->voutput, mmWidth, mmHeight);
1733         ASSERT_EQ(ret, TDM_ERROR_NONE);
1734
1735         _tc_tdm_client_virutual_make_available_mode(modes, count);
1736         ret = tdm_client_voutput_set_available_modes(this->voutput, modes, count);
1737         ASSERT_EQ(ret, TDM_ERROR_NONE);
1738
1739         ret = tdm_client_voutput_connect(this->voutput);
1740         ASSERT_EQ(ret, TDM_ERROR_NONE);
1741
1742         tdm_client_handle_events_timeout(this->client, 0);
1743 }
1744
1745 TEST_F(TDMVirtualOutput, Disconnect)
1746 {
1747         tdm_error ret;
1748         int virtual_conf;
1749
1750         if (this->voutput == NULL) {
1751                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1752                 ASSERT_EQ(virtual_conf, 0);
1753                 return;
1754         }
1755
1756 //      TDM_UT_WAIT("check & press");
1757
1758         ret = tdm_client_voutput_disconnect(this->voutput);
1759         ASSERT_EQ(ret, TDM_ERROR_NONE);
1760
1761         tdm_client_handle_events_timeout(this->client, 0);
1762 }
1763
1764
1765 TEST_F(TDMVirtualOutput, SetMode)
1766 {
1767         tdm_error ret;
1768         unsigned int mmWidth = 300, mmHeight = 150;
1769         tdm_client_output_mode modes[this->MODE_COUNT];
1770         int count = this->MODE_COUNT;
1771         int virtual_conf;
1772
1773         if (this->voutput == NULL) {
1774                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1775                 ASSERT_EQ(virtual_conf, 0);
1776                 return;
1777         }
1778
1779         ret = tdm_client_voutput_set_physical_size(this->voutput, mmWidth, mmHeight);
1780         ASSERT_EQ(ret, TDM_ERROR_NONE);
1781
1782         _tc_tdm_client_virutual_make_available_mode(modes, count);
1783         ret = tdm_client_voutput_set_available_modes(this->voutput, modes, count);
1784         ASSERT_EQ(ret, TDM_ERROR_NONE);
1785
1786         ret = tdm_client_voutput_connect(this->voutput);
1787         ASSERT_EQ(ret, TDM_ERROR_NONE);
1788
1789         tdm_client_handle_events_timeout(this->client, 100);
1790
1791         ASSERT_EQ(tdm_client_voutput_set_mode(this->voutput, count - 1), TDM_ERROR_NONE);
1792
1793         tdm_client_handle_events_timeout(this->client, 100);
1794
1795         ret = tdm_client_voutput_disconnect(this->voutput);
1796         ASSERT_EQ(ret, TDM_ERROR_NONE);
1797
1798         tdm_client_handle_events_timeout(this->client, 0);
1799 }
1800
1801 TEST_F(TDMVirtualOutput, SetModeNullObject)
1802 {
1803         tdm_error ret;
1804         unsigned int mmWidth = 300, mmHeight = 150;
1805         tdm_client_output_mode modes[this->MODE_COUNT];
1806         int count = this->MODE_COUNT;
1807         int virtual_conf;
1808
1809         if (this->voutput == NULL) {
1810                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1811                 ASSERT_EQ(virtual_conf, 0);
1812                 return;
1813         }
1814
1815         ret = tdm_client_voutput_set_physical_size(this->voutput, mmWidth, mmHeight);
1816         ASSERT_EQ(ret, TDM_ERROR_NONE);
1817
1818         _tc_tdm_client_virutual_make_available_mode(modes, count);
1819         ret = tdm_client_voutput_set_available_modes(this->voutput, modes, count);
1820         ASSERT_EQ(ret, TDM_ERROR_NONE);
1821
1822         ret = tdm_client_voutput_connect(this->voutput);
1823         ASSERT_EQ(ret, TDM_ERROR_NONE);
1824
1825         tdm_client_handle_events_timeout(this->client, 100);
1826
1827         ASSERT_EQ(tdm_client_voutput_set_mode(NULL, 0), TDM_ERROR_INVALID_PARAMETER);
1828
1829         ret = tdm_client_voutput_disconnect(this->voutput);
1830         ASSERT_EQ(ret, TDM_ERROR_NONE);
1831
1832         tdm_client_handle_events_timeout(this->client, 0);
1833 }
1834
1835 TEST_F(TDMVirtualOutput, SetModeInvalidIndex)
1836 {
1837         tdm_error ret;
1838         unsigned int mmWidth = 300, mmHeight = 150;
1839         tdm_client_output_mode modes[this->MODE_COUNT];
1840         int count = this->MODE_COUNT;
1841         int virtual_conf;
1842
1843         if (this->voutput == NULL) {
1844                 virtual_conf = tdm_config_get_int(TDM_CONFIG_KEY_GENERAL_VIRTUAL_OUTPUT, 0);
1845                 ASSERT_EQ(virtual_conf, 0);
1846                 return;
1847         }
1848
1849         ret = tdm_client_voutput_set_physical_size(this->voutput, mmWidth, mmHeight);
1850         ASSERT_EQ(ret, TDM_ERROR_NONE);
1851
1852         _tc_tdm_client_virutual_make_available_mode(modes, count);
1853         ret = tdm_client_voutput_set_available_modes(this->voutput, modes, count);
1854         ASSERT_EQ(ret, TDM_ERROR_NONE);
1855
1856         ret = tdm_client_voutput_connect(this->voutput);
1857         ASSERT_EQ(ret, TDM_ERROR_NONE);
1858
1859         tdm_client_handle_events_timeout(this->client, 100);
1860
1861         ASSERT_EQ(tdm_client_voutput_set_mode(this->voutput, -1), TDM_ERROR_INVALID_PARAMETER);
1862
1863         ret = tdm_client_voutput_disconnect(this->voutput);
1864         ASSERT_EQ(ret, TDM_ERROR_NONE);
1865
1866         tdm_client_handle_events_timeout(this->client, 0);
1867 }
1868
1869 #ifdef TDM_UT_TEST_WITH_PARAMS
1870 INSTANTIATE_TEST_CASE_P(TDMClientParams,
1871                                                 TDMClient,
1872                                                 Combine(Bool(), Bool(), Values(TDM_DEFAULT_MODULE)));
1873 #else
1874 INSTANTIATE_TEST_CASE_P(TDMClientParams,
1875                                                 TDMClient,
1876                                                 Values(TDM_DEFAULT_MODULE));
1877 #endif
1878
1879 /* LCOV_EXCL_END */