mmifw-main-test : mmi_event_add_listener should be called before it's event called...
[platform/core/uifw/mmi-framework.git] / tests / mmifw-main-test.cpp
1 /*
2 * Copyright © 2021 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 "mmifw.h"
25 #include "mmifw-tests.h"
26 #include "mmifw-ipc.h"
27
28 #include <Ecore.h>
29 #include <rpc-port-internal.h>
30
31 class MMIFWMainTest : public ::testing::Test
32 {
33 public:
34         void SetUp(void) override
35         {
36         }
37
38         void TearDown(void) override
39         {
40         }
41 };
42
43 void wait_for_state_change(mmi_handle h, mmi_state state)
44 {
45         int timer = 0;
46         while (state != mmi_state_get_current_state(h) && timer < MAX_WAIT_TIME)
47         {
48                 ecore_main_loop_iterate();
49                 timer++;
50         }
51 }
52
53 TEST_F(MMIFWMainTest, MMIFWMainInit)
54 {
55         int res = mmi_init();
56
57         EXPECT_EQ(res, 1);
58
59         if(res)
60                 mmi_shutdown();
61 }
62
63 TEST_F(MMIFWMainTest, MMIFWMainInstanceCreateDestroySuccess)
64 {
65         int res = mmi_init();
66         const char *app_id = "org.tizen.mmi-system-ux-test";
67         mmi_handle h = mmi_instance_create(app_id);
68
69         EXPECT_NE(h, nullptr);
70
71         if(h){
72                 mmi_instance_destroy(&h);
73                 EXPECT_EQ(h, nullptr);
74         }
75
76         if(res)
77                 mmi_shutdown();
78 }
79
80 TEST_F(MMIFWMainTest, MMIFWMainInstanceCreateAndConnectSuccess)
81 {
82         int res = mmi_init();
83         const char *app_id = "org.tizen.mmi-system-ux-test";
84         mmi_handle h = mmi_instance_create(app_id);
85
86         EXPECT_NE(h, nullptr);
87
88         wait_for_connect();
89
90         EXPECT_TRUE(mmi_ipc_is_connected());
91
92         if(h){
93                 mmi_instance_destroy(&h);
94                 EXPECT_EQ(h, nullptr);
95         }
96
97         if(res)
98                 mmi_shutdown();
99 }
100
101 TEST_F(MMIFWMainTest, MMIFWMainInstanceCreateFail)
102 {
103         int res = mmi_init();
104         const char *app_id = NULL;
105         mmi_handle h = mmi_instance_create(app_id);
106
107         EXPECT_EQ(h, nullptr);
108
109         if(h){
110                 mmi_instance_destroy(&h);
111                 EXPECT_EQ(h, nullptr);
112         }
113
114         if(res)
115                 mmi_shutdown();
116 }
117
118 static Eina_Bool
119 _cb_focus(void *data, int type, void *event)
120 {
121         if (!event)
122                 return ECORE_CALLBACK_PASS_ON;
123
124         EXPECT_TRUE(true);
125         return ECORE_CALLBACK_PASS_ON;
126 }
127
128 static Eina_Bool
129 _cb_connection(void *data, int type, void *event)
130 {
131         if (!event)
132                 return ECORE_CALLBACK_PASS_ON;
133
134         EXPECT_TRUE(true);
135         return ECORE_CALLBACK_PASS_ON;
136 }
137
138 TEST_F(MMIFWMainTest, MMIFWMainEventAddSingleListener)
139 {
140         int res = mmi_init();
141         const char *app_id = "org.tizen.mmi-system-ux-test";
142         mmi_handle h = mmi_instance_create(app_id);
143         mmi_event_listener * handler = NULL;
144
145         EXPECT_NE(h, nullptr);
146
147         if(h){
148                 handler = mmi_event_add_listener(h, MMI_EVENT_CONNECTION, _cb_connection, h);
149         }
150
151         wait_for_connect();
152
153         EXPECT_TRUE(mmi_ipc_is_connected());
154
155         EXPECT_NE(handler, nullptr);
156
157         if(handler)
158                 mmi_event_remove_listener(h, handler);
159
160         if(h){
161                 mmi_instance_destroy(&h);
162                 EXPECT_EQ(h, nullptr);
163         }
164
165         if(res)
166                 mmi_shutdown();
167 }
168
169 TEST_F(MMIFWMainTest, MMIFWMainEventAddSingleListenerFail)
170 {
171         int res = mmi_init();
172         const char *app_id = "org.tizen.mmi-system-ux-test";
173         mmi_handle h = mmi_instance_create(app_id);
174         mmi_event_listener * handler =NULL;
175
176         EXPECT_NE(h, nullptr);
177
178         if(h){
179                 handler = mmi_event_add_listener(h, MMI_EVENT_CONNECTION, NULL, NULL);
180         }
181
182         wait_for_connect();
183
184         EXPECT_TRUE(mmi_ipc_is_connected());
185
186         EXPECT_EQ(handler, nullptr);
187
188         if(handler)
189                 mmi_event_remove_listener(h, handler);
190
191         if(h){
192                 mmi_instance_destroy(&h);
193                 EXPECT_EQ(h, nullptr);
194         }
195
196         if(res)
197                 mmi_shutdown();
198 }
199
200 TEST_F(MMIFWMainTest, MMIFWMainEventAddSingleListenerSecondFail)
201 {
202         int res = mmi_init();
203         const char *app_id = "org.tizen.mmi-system-ux-test";
204         mmi_handle h = mmi_instance_create(app_id);
205         mmi_event_listener * handler =NULL;
206
207         EXPECT_NE(h, nullptr);
208
209         if(h){
210                 handler = mmi_event_add_listener(h, 100, _cb_connection, NULL);
211         }
212
213         wait_for_connect();
214
215         EXPECT_TRUE(mmi_ipc_is_connected());
216
217         EXPECT_EQ(handler, nullptr);
218
219         if(handler)
220                 mmi_event_remove_listener(h, handler);
221
222         if(h){
223                 mmi_instance_destroy(&h);
224                 EXPECT_EQ(h, nullptr);
225         }
226
227         if(res)
228                 mmi_shutdown();
229 }
230
231 TEST_F(MMIFWMainTest, MMIFWMainRequestSendGetFocus)
232 {
233         int res = mmi_init();
234         const char *app_id = "org.tizen.mmi-system-ux-test";
235         mmi_handle h = mmi_instance_create(app_id);
236
237         EXPECT_NE(h, nullptr);
238
239         wait_for_connect();
240
241         EXPECT_TRUE(mmi_ipc_is_connected());
242
243         if(h){
244                 mmi_result mmi_res = mmi_request_send_get_focus(h);
245                 EXPECT_EQ(mmi_res, MMI_RESULT_SUCCESS);
246         }
247
248         if(h){
249                 mmi_instance_destroy(&h);
250                 EXPECT_EQ(h, nullptr);
251         }
252
253         if(res)
254                 mmi_shutdown();
255 }
256
257 TEST_F(MMIFWMainTest, MMIFWMainRequestSendSetGetState)
258 {
259         int res = mmi_init();
260         const char *app_id = "org.tizen.mmi-system-ux-test";
261         mmi_handle h = mmi_instance_create(app_id);
262
263         EXPECT_NE(h, nullptr);
264
265         wait_for_connect();
266
267         EXPECT_TRUE(mmi_ipc_is_connected());
268
269         if(h){
270                 mmi_result mmi_res = mmi_request_send_get_focus(h);
271                 EXPECT_EQ(mmi_res, MMI_RESULT_SUCCESS);
272
273                 mmi_state state = MMI_STATE_INITIATION;
274                 mmi_res  = mmi_request_send_set_state(h, state);
275                 EXPECT_EQ(mmi_res, MMI_RESULT_SUCCESS);
276
277                 wait_for_state_change(h, MMI_STATE_INITIATION);
278
279                 state = mmi_state_get_current_state(h);
280                 EXPECT_EQ(state, MMI_STATE_INITIATION);
281         }
282
283         if(h){
284                 mmi_instance_destroy(&h);
285                 EXPECT_EQ(h, nullptr);
286         }
287
288         if(res)
289                 mmi_shutdown();
290 }
291
292 TEST_F(MMIFWMainTest, MMIFWMainEventRemoveAllListener)
293 {
294         int res = mmi_init();
295         const char *app_id = "org.tizen.mmi-system-ux-test";
296         mmi_handle h = mmi_instance_create(app_id);
297         mmi_event_listener * handler = NULL;
298         mmi_event_listener * handler2 = NULL;
299
300         EXPECT_NE(h, nullptr);
301         EXPECT_EQ(handler, nullptr);
302
303         wait_for_connect();
304
305         EXPECT_TRUE(mmi_ipc_is_connected());
306
307         if(h){
308                 handler = mmi_event_add_listener(h, MMI_EVENT_CONNECTION, _cb_connection, h);
309                 handler2 = mmi_event_add_listener(h, MMI_EVENT_FOCUS, _cb_focus, h);
310         }
311
312         EXPECT_NE(handler, nullptr);
313         EXPECT_NE(handler2, nullptr);
314
315         if(handler) {
316                 mmi_event_remove_all_listeners(h);
317         }
318
319         if(h){
320                 mmi_instance_destroy(&h);
321                 EXPECT_EQ(h, nullptr);
322         }
323
324         if(res)
325                 mmi_shutdown();
326 }