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