tests: fix DSWaylandClient-test
[platform/core/uifw/libds.git] / tests / DSWaylandClient-test.cpp
1 /*
2 * Copyright © 2020 Samsung Electronics co., Ltd. All Rights Reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "libds-tests.h"
25 #include "DSWaylandCompositor.h"
26 #include "DSWaylandClient.h"
27
28 using namespace display_server;
29
30 class DSWaylandClientTest : public ::testing::Test
31 {
32 public:
33         void SetUp(void) override
34         {
35                 char *xdir = getenv("DEFAULT_XDG_RUNTIME_DIR");
36                 setenv("XDG_RUNTIME_DIR", xdir, 1);
37         }
38         void TearDown(void) override
39         {
40                 unsetenv("XDG_RUNTIME_DIR");
41         }
42 };
43
44 /* Test cases */
45 TEST_F(DSWaylandClientTest, NewDSWaylandClient)
46 {
47         DSWaylandCompositor *comp = DSWaylandCompositor::getInstance();
48         EXPECT_TRUE(comp != nullptr);
49
50         if (comp)
51         {
52                 /* client must be created with a valid wl_client ptr later. */
53                 DSWaylandClient *client = new DSWaylandClient(comp, (wl_client *)nullptr);
54                 EXPECT_TRUE(client != nullptr);
55
56                 if (client)
57                         delete client;
58
59                 DSWaylandCompositor::releaseInstance();
60         }
61 }
62
63 TEST_F(DSWaylandClientTest, GetClientFromWlClient)
64 {
65         DSWaylandCompositor *comp = DSWaylandCompositor::getInstance();
66         EXPECT_TRUE(comp != nullptr);
67
68         if (comp)
69         {
70                 /* client must be created with a valid wl_client ptr later. */
71                 auto client = new DSWaylandClient(comp, (wl_client *)nullptr);
72                 EXPECT_TRUE(client != nullptr);
73
74                 if (client)
75                 {
76                         DSWaylandClient *client2 = client->fromWlClient((wl_client *)nullptr);
77                         EXPECT_TRUE(client2 == nullptr);
78                 }
79
80                 DSWaylandCompositor::releaseInstance();
81         }
82 }
83
84 TEST_F(DSWaylandClientTest, GetWlClient)
85 {
86         DSWaylandCompositor *comp = DSWaylandCompositor::getInstance();
87         EXPECT_TRUE(comp != nullptr);
88
89         if (comp)
90         {
91                 /* client must be created with a valid wl_client ptr later. */
92                 auto client = new DSWaylandClient(comp, (wl_client *)nullptr);
93                 EXPECT_TRUE(client != nullptr);
94
95                 if (client)
96                 {
97                         wl_client *wclient  = client->wlClient();
98                         EXPECT_TRUE(wclient == nullptr);
99                 }
100
101                 DSWaylandCompositor::releaseInstance();
102         }
103 }
104
105 TEST_F(DSWaylandClientTest, GetCompositor)
106 {
107         DSWaylandCompositor *comp = DSWaylandCompositor::getInstance();
108         EXPECT_TRUE(comp != nullptr);
109
110         if (comp)
111         {
112                 /* client must be created with a valid wl_client ptr later. */
113                 DSWaylandClient *client = new DSWaylandClient(comp, (wl_client *)nullptr);
114                 EXPECT_TRUE(client != nullptr);
115
116                 if (client)
117                 {
118                         DSWaylandCompositor *comp2 = client->getCompositor();
119                         EXPECT_TRUE(comp2 == comp);
120
121                         delete client;
122                 }
123
124                 DSWaylandCompositor::releaseInstance();
125         }
126 }
127
128 TEST_F(DSWaylandClientTest, GetPidUidGid)
129 {
130         DSWaylandCompositor *comp = DSWaylandCompositor::getInstance();
131         EXPECT_TRUE(comp != nullptr);
132
133         if (comp)
134         {
135                 /* client must be created with a valid wl_client ptr later. */
136                 auto client = new DSWaylandClient(comp, (wl_client *)nullptr);
137                 EXPECT_TRUE(client != nullptr);
138
139                 if (client)
140                 {
141                         pid_t pID;
142                         uid_t uID;
143                         gid_t gID;
144
145                         pID = client->pid();
146                         uID = client->uid();
147                         gID = client->gid();
148
149                         EXPECT_TRUE(pID == 0);
150                         EXPECT_TRUE(uID == 0);
151                         EXPECT_TRUE(gID == 0);
152
153                         delete client;
154                 }
155
156                 DSWaylandCompositor::releaseInstance();
157         }
158 }
159