virtual: client get voutput's output by tdm_client_get_output
[platform/core/uifw/libtdm.git] / haltests / src / tc_tdm_client.cpp
index 10e122d..01668cb 100644 (file)
@@ -50,6 +50,15 @@ enum {
        TDM_UT_PIPE_MSG_TERMINATE_SERVER,
 };
 
+#define TDM_UT_WAIT(fmt, ...) \
+       do { \
+               char ch; \
+               do { \
+                       printf(fmt" [n]):next ", ##__VA_ARGS__); \
+                       ch = tc_tdm_getchar(); \
+               } while(ch != 'n'); \
+       } while (0)
+
 static int _tc_tdm_pipe_read_msg(int fd);
 static bool _tc_tdm_pipe_write_msg(int fd, int reply_fd, int msg);
 static pid_t _tc_tdm_client_server_fork(int *pipe_to_parent, int *pipe_to_child);
@@ -66,6 +75,7 @@ public:
        tdm_client *client;
        tdm_client_output *output;
        tdm_client_vblank *vblank;
+       tdm_client_voutput *voutput;
 
        double vrefresh_interval, start, end;
 
@@ -199,6 +209,23 @@ bool TDMClient::PrepareVblank(void)
        return true;
 }
 
+char
+tc_tdm_getchar(void)
+{
+       int c = getchar();
+       int ch = c;
+
+       if (ch == '\n' || ch == '\r')
+               ch = 'y';
+       else if (ch < 'a')
+               ch += ('a' - 'A');
+
+       while (c != '\n' && c != EOF)
+               c = getchar();
+
+       return ch;
+}
+
 static int
 _tc_tdm_pipe_read_msg(int fd)
 {
@@ -1358,6 +1385,306 @@ TEST_P(TDMClient, ClientVblankIsWaitingNullObject)
        ASSERT_EQ(waiting, 0);
 }
 
+TEST_P(TDMClient, ClientCreateVOutput)
+{
+       tdm_error ret;
+       const char name[TDM_NAME_LEN] = "Virtual Output";
+
+       ASSERT_EQ(PrepareClient(), true);
+       
+       voutput = tdm_client_create_voutput(client, name, &ret);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+       ASSERT_NE(voutput, NULL);
+
+       tdm_client_voutput_destroy(voutput);
+}
+
+class TDMVirtualOutput : public ::testing::Test
+{
+public:
+       TDMVirtualOutput() {};
+       ~TDMVirtualOutput() {};
+
+       static void SetUpTestCase();
+       static void TearDownTestCase();
+       static bool PrepareVOutput(void);
+
+protected:
+       static tdm_client *client;
+       static tdm_client_voutput *voutput;
+       const int MODE_COUNT = 2;
+
+private:
+       static pid_t server_pid;
+
+       /* 0: read, 1: write */
+       static int pipe_parent[2];
+       static int pipe_child[2];
+
+       static void ServerFork(void);
+       static void ServerKill(void);
+};
+
+pid_t TDMVirtualOutput::server_pid = -1;
+int TDMVirtualOutput::pipe_parent[2] = {-1, -1};
+int TDMVirtualOutput::pipe_child[2] = {-1, -1};
+tdm_client* TDMVirtualOutput::client = nullptr;
+tdm_client_voutput* TDMVirtualOutput::voutput = nullptr;
+
+void TDMVirtualOutput::ServerKill(void)
+{
+       if (pipe_child[0] >= 0)
+               close(pipe_child[0]);
+       if (pipe_child[1] >= 0) {
+               if (server_pid > 0) {
+                       bool ret = _tc_tdm_pipe_write_msg(pipe_child[1], pipe_parent[0], TDM_UT_PIPE_MSG_TERMINATE_SERVER);
+                       if (ret) {
+                               if (waitpid(server_pid, NULL, 0) == server_pid)
+                                       TDM_INFO("*** server terminated ***");
+                               else
+                                       TDM_ERR("*** failed to terminate server ***");
+                       } else {
+                               if (kill(server_pid, 9) < 0)
+                                       TDM_ERR("*** failed to kill server ***");
+                       }
+               }
+               close(pipe_child[1]);
+       }
+
+       if (pipe_parent[0] >= 0)
+               close(pipe_parent[0]);
+       if (pipe_parent[1] >= 0)
+               close(pipe_parent[1]);
+
+       server_pid = -1;
+       pipe_parent[0] = pipe_parent[1] = -1;
+       pipe_child[0] = pipe_child[1] = -1;
+}
+
+void TDMVirtualOutput::ServerFork(void)
+{
+       if (server_pid > 0)
+               return;
+
+       server_pid = _tc_tdm_client_server_fork(pipe_parent, pipe_child);
+       ASSERT_GT(server_pid, 0);
+}
+
+void TDMVirtualOutput::SetUpTestCase(void)
+{
+       setenv("XDG_RUNTIME_DIR", "/run", 1);
+       setenv("TBM_DISPLAY_SERVER", "1", 1);
+
+       if (server_pid == -1)
+               ServerFork();
+
+       ASSERT_EQ(PrepareVOutput(), true);
+}
+
+void TDMVirtualOutput::TearDownTestCase(void)
+{
+//     TDM_UT_WAIT("check & press");
+
+       if (voutput)
+               tdm_client_voutput_destroy(voutput);
+
+       if (client)
+               tdm_client_destroy(client);
+
+       ServerKill();
+
+       unsetenv("XDG_RUNTIME_DIR");
+       unsetenv("TBM_DISPLAY_SERVER");
+}
+
+bool TDMVirtualOutput::PrepareVOutput(void)
+{
+       tdm_error ret;
+       const char name[TDM_NAME_LEN] = "Virtual Output";
+
+       client = tdm_client_create(&ret);
+       TDM_UT_RETURN_FALSE_IF_FAIL(ret == TDM_ERROR_NONE);
+       TDM_UT_RETURN_FALSE_IF_FAIL(client != NULL);
+
+
+       voutput = tdm_client_create_voutput(client, name, &ret);
+       TDM_UT_RETURN_FALSE_IF_FAIL(ret == TDM_ERROR_NONE);
+       TDM_UT_RETURN_FALSE_IF_FAIL(voutput != NULL);
+
+//     TDM_UT_WAIT("check & press");
+
+       return true;
+}
+
+static void
+_tc_tdm_client_virutual_make_available_mode(tdm_client_output_mode *modes, int count)
+{
+       int i;
+
+       for (i = 0; i < count; i++) {
+               modes[i].clock = 25200;
+               modes[i].hdisplay = 640;
+               modes[i].hsync_start = 656;
+               modes[i].hsync_end = 752;
+               modes[i].htotal = 800;
+               modes[i].hskew = 0;
+               modes[i].vdisplay = 480;
+               modes[i].vsync_start = 490;
+               modes[i].vsync_end = 492;
+               modes[i].vtotal = 525;
+               modes[i].vscan = 0;
+               modes[i].vrefresh = 30;
+               modes[i].flags = 0;
+               modes[i].type = 0;
+               snprintf(modes[i].name, TDM_NAME_LEN, "%dx%d_%d", modes[i].hdisplay, modes[i].vdisplay, i);
+       }
+}
+
+TEST_F(TDMVirtualOutput, SetAvailableModes)
+{
+       tdm_error ret;
+       tdm_client_output_mode modes[this->MODE_COUNT];
+       int count = this->MODE_COUNT;
+
+       _tc_tdm_client_virutual_make_available_mode(modes, count);
+
+       ret = tdm_client_voutput_set_available_modes(this->voutput, modes, count);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+}
+
+TEST_F(TDMVirtualOutput, FailTestSetAvailableModes)
+{
+       tdm_error ret;
+       tdm_client_output_mode modes[this->MODE_COUNT];
+       int count = this->MODE_COUNT;
+
+       ret = tdm_client_voutput_set_available_modes(NULL, modes, count);
+       ASSERT_EQ(ret, TDM_ERROR_INVALID_PARAMETER);
+
+       ret = tdm_client_voutput_set_available_modes(this->voutput, NULL, count);
+       ASSERT_EQ(ret, TDM_ERROR_INVALID_PARAMETER);
+}
+
+TEST_F(TDMVirtualOutput, SetPhysicalSize)
+{
+       tdm_error ret;
+       unsigned int mmWidth = 1234, mmHeight = 1234;
+
+       ret = tdm_client_voutput_set_physical_size(this->voutput, mmWidth, mmHeight);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+}
+
+TEST_F(TDMVirtualOutput, FailTestSetPhysicalSize)
+{
+       tdm_error ret;
+       unsigned int invalid_mmWidth = 0, invalid_mmHeight = 0;
+
+       ret = tdm_client_voutput_set_physical_size(this->voutput, invalid_mmWidth, invalid_mmHeight);
+       ASSERT_EQ(ret, TDM_ERROR_INVALID_PARAMETER);
+}
+
+static void
+_tc_tdm_client_voutput_commit_handler(tdm_client_voutput *voutput, tbm_surface_h buffer, void *user_data)
+{
+       int *flag;
+       flag = (int *)user_data;
+       *flag = 1;
+}
+
+TEST_F(TDMVirtualOutput, AddCommitHandler)
+{
+       tdm_error ret;
+       int flag_callback_called = 0;
+
+       ret = tdm_client_voutput_add_commit_handler(this->voutput,
+                                                                                               _tc_tdm_client_voutput_commit_handler,
+                                                                                               &flag_callback_called);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+//     ASSERT_EQ(flag_callback_called, 1);
+
+       tdm_client_voutput_remove_commit_handler(this->voutput,
+                                                                                        _tc_tdm_client_voutput_commit_handler,
+                                                                                        &flag_callback_called);
+}
+
+TEST_F(TDMVirtualOutput, CommitDone)
+{
+       tdm_error ret;
+
+       ret = tdm_client_voutput_commit_done(this->voutput);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+}
+
+TEST_F(TDMVirtualOutput, GetClientOutput)
+{
+       tdm_error ret;
+       tdm_client_output *output;
+
+       output = tdm_client_voutput_get_client_output(this->voutput, &ret);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+       ASSERT_NE(output, NULL);
+}
+
+TEST_F(TDMVirtualOutput, Connect)
+{
+       tdm_error ret;
+       tdm_client_output *output;
+       unsigned int mmWidth = 300, mmHeight = 150;
+       tdm_client_output_mode modes[this->MODE_COUNT];
+       int count = this->MODE_COUNT;
+
+       output = tdm_client_voutput_get_client_output(this->voutput, &ret);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+       ASSERT_NE(output, NULL);
+
+       ret = tdm_client_voutput_set_physical_size(this->voutput, mmWidth, mmHeight);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+
+       _tc_tdm_client_virutual_make_available_mode(modes, count);
+       ret = tdm_client_voutput_set_available_modes(this->voutput, modes, count);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+
+       ret = tdm_client_output_connect(output);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+
+       tdm_client_handle_events_timeout(this->client, 0);
+}
+
+TEST_F(TDMVirtualOutput, Disconnect)
+{
+       tdm_error ret;
+       tdm_client_output *output;
+
+//     TDM_UT_WAIT("check & press");
+
+       output = tdm_client_voutput_get_client_output(this->voutput, &ret);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+       ASSERT_NE(output, NULL);
+
+       ret = tdm_client_output_disconnect(output);
+       ASSERT_EQ(ret, TDM_ERROR_NONE);
+
+       tdm_client_handle_events_timeout(this->client, 0);
+}
+
+#if 0
+TEST_F(TDMVirtualOutput, FailTestGetClientOutput)
+{
+       tdm_error ret;
+}
+
+TEST_F(TDMVirtualOutput, SetBufferQueue)
+{
+       tdm_error ret;
+}
+
+TEST_F(TDMVirtualOutput, FailTestSetBufferQueue)
+{
+       tdm_error ret;
+}
+
+#endif
+
 #ifdef TDM_UT_TEST_WITH_PARAMS
 INSTANTIATE_TEST_CASE_P(TDMClientParams,
                                                TDMClient,
@@ -1368,4 +1695,4 @@ INSTANTIATE_TEST_CASE_P(TDMClientParams,
                                                Values(TDM_DEFAULT_MODULE));
 #endif
 
-/* LCOV_EXCL_END */
\ No newline at end of file
+/* LCOV_EXCL_END */