[dali_1.9.26] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / automated-tests / src / dali / utc-Dali-ObjectRegistry.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <iostream>
19
20 #include <stdlib.h>
21 #include <dali-test-suite-utils.h>
22 #include <dali/public-api/dali-core.h>
23
24 using namespace Dali;
25
26 namespace
27 {
28
29 // Functors to test whether Object created/destroyed signal is emitted for different types of Objects
30
31 struct TestObjectDestroyedCallback
32 {
33   TestObjectDestroyedCallback(bool& signalReceived, Dali::RefObject*& objectPointer)
34   : mSignalVerified(signalReceived),
35     mObjectPointer(objectPointer)
36   {
37   }
38
39   void operator()(const Dali::RefObject* objectPointer)
40   {
41     tet_infoline("Verifying TestObjectDestroyedCallback()");
42
43     if(objectPointer == mObjectPointer)
44     {
45       mSignalVerified = true;
46     }
47   }
48
49   bool& mSignalVerified;
50   Dali::RefObject*& mObjectPointer;
51 };
52
53 struct TestActorCallback
54 {
55   TestActorCallback(bool& signalReceived)
56   : mSignalVerified(signalReceived)
57   {
58   }
59
60   void operator()(BaseHandle object)
61   {
62     tet_infoline("Verifying TestActorCallback()");
63     Actor actor = Actor::DownCast(object);
64     if(actor)
65     {
66       mSignalVerified = true;
67     }
68   }
69
70   bool& mSignalVerified;
71 };
72
73 struct TestCameraActorCallback
74 {
75   TestCameraActorCallback(bool& signalReceived)
76   : mSignalVerified(signalReceived)
77   {
78   }
79   void operator()(BaseHandle object)
80   {
81     tet_infoline("Verifying TestCameraActorCallback()");
82     CameraActor actor = CameraActor::DownCast(object);
83     if(actor)
84     {
85       mSignalVerified = true;
86     }
87   }
88   bool& mSignalVerified;
89 };
90
91 struct TestLayerCallback
92 {
93   TestLayerCallback(bool& signalReceived)
94   : mSignalVerified(signalReceived)
95   {
96   }
97   void operator()(BaseHandle object)
98   {
99     tet_infoline("Verifying TestLayerCallback()");
100     Layer actor = Layer::DownCast(object);
101     if(actor)
102     {
103       mSignalVerified = true;
104     }
105   }
106   bool& mSignalVerified;
107 };
108
109 struct TestAnimationCallback
110 {
111   TestAnimationCallback(bool& signalReceived)
112   : mSignalVerified(signalReceived)
113   {
114   }
115   void operator()(BaseHandle object)
116   {
117     tet_infoline("Verifying TestAnimationCallback()");
118     Animation actor = Animation::DownCast(object);
119     if(actor)
120     {
121       mSignalVerified = true;
122     }
123   }
124   bool& mSignalVerified;
125 };
126
127 } // anonymous namespace
128
129 int UtcDaliObjectRegistryGet(void)
130 {
131   TestApplication application;
132
133   ObjectRegistry registry; //  like this for ctor code coverage
134   registry= application.GetCore().GetObjectRegistry();
135
136   DALI_TEST_CHECK( registry );
137   END_TEST;
138 }
139
140 int UtcDaliObjectRegistryCopyConstructor(void)
141 {
142   TestApplication application;
143
144   ObjectRegistry myRegistry;
145   ObjectRegistry anotherRegistry( myRegistry );
146
147   DALI_TEST_EQUALS( myRegistry, anotherRegistry, TEST_LOCATION );
148   END_TEST;
149 }
150
151 int UtcDaliObjectRegistryMoveConstructor(void)
152 {
153   TestApplication application;
154
155   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
156   DALI_TEST_CHECK( registry );
157   DALI_TEST_EQUALS( 2, registry.GetBaseObject().ReferenceCount(), TEST_LOCATION );
158
159   ObjectRegistry move = std::move( registry );
160   DALI_TEST_CHECK( move );
161
162   // Check that object is moved (not copied, so ref count keeps the same)
163   DALI_TEST_EQUALS( 2, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
164   DALI_TEST_CHECK( !registry );
165
166   END_TEST;
167 }
168
169 int UtcDaliObjectRegistryMoveAssignment(void)
170 {
171   TestApplication application;
172
173   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
174   DALI_TEST_CHECK( registry );
175   DALI_TEST_EQUALS( 2, registry.GetBaseObject().ReferenceCount(), TEST_LOCATION );
176
177   ObjectRegistry move;
178   move = std::move( registry );
179   DALI_TEST_CHECK( move );
180
181   // Check that object is moved (not copied, so ref count keeps the same)
182   DALI_TEST_EQUALS( 2, move.GetBaseObject().ReferenceCount(), TEST_LOCATION );
183   DALI_TEST_CHECK( !registry );
184
185   END_TEST;
186 }
187
188 int UtcDaliObjectRegistrySignalActorCreated(void)
189 {
190   tet_infoline("Testing GetObjectRegistry()");
191   TestApplication application;
192   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
193   DALI_TEST_CHECK( registry );
194
195   bool verified = false;
196   TestActorCallback test(verified);
197
198   Dali::RefObject* objectPointer = NULL;
199   TestObjectDestroyedCallback test2(verified, objectPointer);
200
201   registry.ObjectCreatedSignal().Connect(&application, test);
202   registry.ObjectDestroyedSignal().Connect(&application, test2);
203
204   {
205     Actor actor = Actor::New();
206     DALI_TEST_CHECK( test.mSignalVerified );
207
208     verified = false;
209     objectPointer = actor.GetObjectPtr();
210   }
211   DALI_TEST_CHECK( test.mSignalVerified );
212   END_TEST;
213 }
214
215 int UtcDaliObjectRegistrySignalCameraCreated(void)
216 {
217   TestApplication application;
218
219   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
220
221   bool verified = false;
222   TestCameraActorCallback test(verified);
223
224   Dali::RefObject* objectPointer = NULL;
225   TestObjectDestroyedCallback test2(verified, objectPointer);
226
227   registry.ObjectCreatedSignal().Connect(&application, test);
228   registry.ObjectDestroyedSignal().Connect(&application, test2);
229
230   {
231     CameraActor actor = CameraActor::New();
232     DALI_TEST_CHECK( test.mSignalVerified );
233
234     verified = false;
235     objectPointer = actor.GetObjectPtr();
236   }
237   DALI_TEST_CHECK( test.mSignalVerified );
238   END_TEST;
239 }
240
241 int UtcDaliObjectRegistrySignalLayerCreated(void)
242 {
243   TestApplication application;
244   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
245
246   bool verified = false;
247   TestLayerCallback test(verified);
248
249   Dali::RefObject* objectPointer = NULL;
250   TestObjectDestroyedCallback test2(verified, objectPointer);
251
252   registry.ObjectCreatedSignal().Connect(&application, test);
253   registry.ObjectDestroyedSignal().Connect(&application, test2);
254
255   {
256     Layer actor = Layer::New();
257     DALI_TEST_CHECK( test.mSignalVerified );
258
259     verified = false;
260     objectPointer = actor.GetObjectPtr();
261   }
262   DALI_TEST_CHECK( test.mSignalVerified );
263   END_TEST;
264 }
265
266 int UtcDaliObjectRegistrySignalAnimationCreated(void)
267 {
268   TestApplication application;
269   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
270
271   bool verified = false;
272   TestAnimationCallback test(verified);
273
274   Dali::RefObject* objectPointer = NULL;
275   TestObjectDestroyedCallback test2(verified, objectPointer);
276
277   registry.ObjectCreatedSignal().Connect(&application, test);
278   registry.ObjectDestroyedSignal().Connect(&application, test2);
279
280   {
281     Animation animation = Animation::New(1.0f);
282     DALI_TEST_CHECK( test.mSignalVerified );
283
284     verified = false;
285     objectPointer = animation.GetObjectPtr();
286   }
287   DALI_TEST_CHECK( test.mSignalVerified );
288   END_TEST;
289 }
290
291 int UtcDaliObjectRegistryObjectCreatedSignalNegative(void)
292 {
293   TestApplication application;
294   Dali::ObjectRegistry instance;
295   try
296   {
297     instance.ObjectCreatedSignal();
298     DALI_TEST_CHECK(false); // Should not get here
299   }
300   catch(...)
301   {
302     DALI_TEST_CHECK(true); // We expect an assert
303   }
304   END_TEST;
305 }
306
307 int UtcDaliObjectRegistryObjectDestroyedSignalNegative(void)
308 {
309   TestApplication application;
310   Dali::ObjectRegistry instance;
311   try
312   {
313     instance.ObjectDestroyedSignal();
314     DALI_TEST_CHECK(false); // Should not get here
315   }
316   catch(...)
317   {
318     DALI_TEST_CHECK(true); // We expect an assert
319   }
320   END_TEST;
321 }