Merge "fix issue in negative line spacing with key arrow down" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-BubbleEmitter.cpp
1 /*
2  * Copyright (c) 2022 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 <stdlib.h>
19 #include <iostream>
20
21 // Need to override adaptor classes for toolkit test harness, so include
22 // test harness headers before dali headers.
23 #include <dali-toolkit-test-suite-utils.h>
24
25 #include <dali-toolkit/dali-toolkit.h>
26 #include <dali-toolkit/devel-api/controls/bubble-effect/bubble-emitter.h>
27 #include <dali.h>
28 #include <dali/public-api/rendering/renderer.h>
29
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32
33 void utc_dali_toolkit_bubble_emitter_startup(void)
34 {
35   test_return_value = TET_UNDEF;
36 }
37
38 void utc_dali_toolkit_bubble_emitter_cleanup(void)
39 {
40   test_return_value = TET_PASS;
41 }
42
43 namespace
44 {
45 const int RENDER_FRAME_INTERVAL = 16;
46
47 static bool gObjectCreatedCallBackCalled;
48 static void TestCallback(BaseHandle handle)
49 {
50   gObjectCreatedCallBackCalled = true;
51 }
52
53 /*
54  * Simulate time passed by.
55  *
56  * @note this will always process at least 1 frame (1/60 sec)
57  *
58  * @param application Test application instance
59  * @param duration Time to pass in milliseconds.
60  * @return The actual time passed in milliseconds
61  */
62 static int Wait(ToolkitTestApplication& application, int duration = 0)
63 {
64   int time = 0;
65
66   for(int i = 0; i <= (duration / RENDER_FRAME_INTERVAL); i++)
67   {
68     application.SendNotification();
69     application.Render(RENDER_FRAME_INTERVAL);
70     time += RENDER_FRAME_INTERVAL;
71   }
72
73   return time;
74 }
75
76 static Texture CreateSolidColorTexture(ToolkitTestApplication& application, const Vector4& color, unsigned int width, unsigned int height)
77 {
78   Texture texture = Texture::New(TextureType::TEXTURE_2D, Pixel::RGBA8888, width, height);
79   return texture;
80 }
81 } //namespace
82
83 int UtcDaliBubbleEmitterNew(void)
84 {
85   ToolkitTestApplication application;
86
87   tet_infoline(" UtcDaliBubbleEmitterNew ");
88
89   // Test default constructor
90   BubbleEmitter emitter;
91   DALI_TEST_CHECK(!emitter);
92
93   // Test object creation
94   Texture shapeImage = CreateSolidColorTexture(application, Color::GREEN, 5, 5);
95   emitter            = BubbleEmitter::New(Vector2(50.f, 50.f), shapeImage, 200, Vector2(5.f, 10.f));
96   DALI_TEST_CHECK(emitter);
97
98   // Additional check to ensure object is created by checking if it's registered
99   ObjectRegistry registry = application.GetCore().GetObjectRegistry();
100   DALI_TEST_CHECK(registry);
101   gObjectCreatedCallBackCalled = false;
102   registry.ObjectCreatedSignal().Connect(&TestCallback);
103   {
104     BubbleEmitter emitter = BubbleEmitter::New(Vector2(50.f, 50.f), shapeImage, 200, Vector2(5.f, 10.f));
105   }
106   DALI_TEST_CHECK(gObjectCreatedCallBackCalled);
107
108   // Test copy constructor
109   BubbleEmitter emitterCopy(emitter);
110   DALI_TEST_CHECK(emitterCopy);
111
112   // Test down cast
113   Handle handleEmitter;
114   handleEmitter                 = emitter;
115   BubbleEmitter downCastEmitter = BubbleEmitter::DownCast(handleEmitter);
116   DALI_TEST_CHECK(downCastEmitter);
117   END_TEST;
118 }
119
120 int UtcDaliBubbleEmitterDownCast01(void)
121 {
122   ToolkitTestApplication application;
123
124   tet_infoline(" UtcDaliBubbleEmitterDownCast01 ");
125
126   Texture       shapeImage = CreateSolidColorTexture(application, Color::GREEN, 5, 5);
127   BubbleEmitter emitter    = BubbleEmitter::New(Vector2(50.f, 50.f), shapeImage, 200, Vector2(5.f, 10.f));
128
129   BaseHandle    handle(emitter);
130   BubbleEmitter emitter2 = BubbleEmitter::DownCast(handle);
131   DALI_TEST_EQUALS((bool)emitter2, true, TEST_LOCATION);
132   END_TEST;
133 }
134
135 int UtcDaliBubbleEmitterDownCast02(void)
136 {
137   ToolkitTestApplication application;
138
139   tet_infoline(" UtcDaliBubbleEmitterDownCast02 ");
140
141   Handle        handle  = Handle::New(); // Create a custom object
142   BubbleEmitter emitter = BubbleEmitter::DownCast(handle);
143   DALI_TEST_EQUALS((bool)emitter, false, TEST_LOCATION);
144   END_TEST;
145 }
146
147 int UtcDaliBubbleEmitterGetRootActor(void)
148 {
149   ToolkitTestApplication application;
150   tet_infoline(" UtcDaliBubbleEmitterGetRootActor ");
151
152   Texture       shapeImage = CreateSolidColorTexture(application, Color::GREEN, 5, 5);
153   BubbleEmitter emitter    = BubbleEmitter::New(Vector2(50.f, 50.f), shapeImage, 270, Vector2(5.f, 10.f));
154
155   Actor root = emitter.GetRootActor();
156   DALI_TEST_CHECK(root);
157   DALI_TEST_CHECK(root.GetChildCount() == 0);
158   END_TEST;
159 }
160
161 int UtcDaliBubbleEmitterSetBackground(void)
162 {
163   ToolkitTestApplication application;
164   tet_infoline(" UtcDaliBubbleEmitterSetBackground ");
165
166   Texture       shapeImage = CreateSolidColorTexture(application, Color::GREEN, 5, 5);
167   BubbleEmitter emitter    = BubbleEmitter::New(Vector2(50.f, 50.f), shapeImage, 200, Vector2(5.f, 10.f));
168
169   RenderTaskList taskList  = application.GetScene().GetRenderTaskList();
170   unsigned int   taskCount = taskList.GetTaskCount();
171
172   Texture bgImage = CreateSolidColorTexture(application, Color::RED, 50, 50);
173   emitter.SetBackground(bgImage, Vector3(0.f, 0.f, 0.5f));
174
175   DALI_TEST_CHECK(taskList.GetTaskCount() == taskCount + 1);
176
177   Wait(application, 500);
178   DALI_TEST_CHECK(taskList.GetTaskCount() == taskCount);
179   END_TEST;
180 }
181
182 //TODO: test case for BubbleEmitter::SetShapeImage(Image)
183 // To test that the bubble-shape image is successfully switched in the sampler
184 /*int UtcDaliBubbleEmitterSetShapeImage(void)
185 {
186 }*/
187
188 int UtcDaliBubbleEmitterSetBubbleScale(void)
189 {
190   ToolkitTestApplication application;
191   tet_infoline(" UtcDaliBubbleEmitterSetBubbleScale ");
192
193   static std::vector<UniformData> customUniforms =
194     {
195       UniformData("uDynamicScale", Property::Type::FLOAT),
196     };
197
198   TestGraphicsController& graphics = application.GetGraphicsController();
199   graphics.AddCustomUniforms(customUniforms);
200
201   Texture       shapeImage = CreateSolidColorTexture(application, Color::GREEN, 5, 5);
202   BubbleEmitter emitter    = BubbleEmitter::New(Vector2(50.f, 50.f), shapeImage, 150, Vector2(5.f, 10.f));
203   DALI_TEST_CHECK(emitter);
204   Actor root = emitter.GetRootActor();
205   application.GetScene().Add(root);
206   root.SetProperty(Actor::Property::POSITION, Vector3::ZERO);
207   root.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
208   root.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
209
210   TestGlAbstraction& gl = application.GetGlAbstraction();
211
212   Wait(application);
213
214   float scaleValue;
215   DALI_TEST_CHECK(gl.GetUniformValue<float>("uDynamicScale", scaleValue));
216   DALI_TEST_EQUALS(scaleValue, 1.f, TEST_LOCATION);
217
218   emitter.SetBubbleScale(2.f);
219   Wait(application);
220   DALI_TEST_CHECK(gl.GetUniformValue<float>("uDynamicScale", scaleValue));
221   DALI_TEST_EQUALS(scaleValue, 2.f, TEST_LOCATION);
222
223   emitter.SetBubbleScale(0.5f);
224   Wait(application);
225   DALI_TEST_CHECK(gl.GetUniformValue<float>("uDynamicScale", scaleValue));
226   DALI_TEST_EQUALS(scaleValue, 0.5f, TEST_LOCATION);
227
228   END_TEST;
229 }
230
231 int UtcDaliBubbleEmitterSetBubbleDensity01(void)
232 {
233   ToolkitTestApplication application;
234   tet_infoline(" UtcDaliBubbleEmitterSetBubbleDensity ");
235
236   Texture       shapeImage = CreateSolidColorTexture(application, Color::GREEN, 5, 5);
237   BubbleEmitter emitter    = BubbleEmitter::New(Vector2(50.f, 50.f), shapeImage, 200, Vector2(5.f, 10.f));
238
239   try
240   {
241     emitter.SetBubbleDensity(3.f);
242     DALI_TEST_CHECK(true);
243   }
244   catch(Dali::DaliException& e)
245   {
246     DALI_TEST_PRINT_ASSERT(e);
247     DALI_TEST_ASSERT(e, "density>0 && density<=9", TEST_LOCATION);
248   }
249   END_TEST;
250 }
251
252 int UtcDaliBubbleEmitterSetBubbleDensity02(void)
253 {
254   ToolkitTestApplication application;
255   tet_infoline(" UtcDaliBubbleEmitterSetBubbleDensity ");
256
257   Texture       shapeImage = CreateSolidColorTexture(application, Color::GREEN, 5, 5);
258   BubbleEmitter emitter    = BubbleEmitter::New(Vector2(50.f, 50.f), shapeImage, 200, Vector2(5.f, 10.f));
259
260   try
261   {
262     emitter.SetBubbleDensity(10.f);
263   }
264   catch(Dali::DaliException& e)
265   {
266     DALI_TEST_PRINT_ASSERT(e);
267     DALI_TEST_ASSERT(e, "density > 0 && density <= 9", TEST_LOCATION);
268   }
269   END_TEST;
270 }
271
272 int UtcDaliBubbleEmitterEmitBubble(void)
273 {
274   ToolkitTestApplication application;
275   tet_infoline(" UtcDaliBubbleEmitterEmitBubble ");
276
277   Texture       shapeImage1 = CreateSolidColorTexture(application, Color::GREEN, 5, 5);
278   BubbleEmitter emitter     = BubbleEmitter::New(Vector2(50.f, 50.f), shapeImage1, 200, Vector2(5.f, 10.f));
279
280   Actor    root           = emitter.GetRootActor();
281   Renderer bubbleRenderer = root.GetRendererAt(0);
282   application.GetScene().Add(root);
283   DALI_TEST_CHECK(bubbleRenderer);
284
285   Property::Index propertyIndex0 = bubbleRenderer.GetPropertyIndex("uPercentage[0]");
286   Property::Index propertyIndex1 = bubbleRenderer.GetPropertyIndex("uPercentage[1]");
287   float           value0, value1;
288
289   Animation animation = Animation::New(0.5f);
290   emitter.EmitBubble(animation, Vector2(40.f, 40.f), Vector2(-5.f, -5.f), Vector2(30.f, 30.f));
291   emitter.EmitBubble(animation, Vector2(10.f, 10.f), Vector2(5.f, 5.f), Vector2(30.f, 30.f));
292   (bubbleRenderer.GetProperty(propertyIndex0)).Get(value0);
293   (bubbleRenderer.GetProperty(propertyIndex1)).Get(value1);
294   DALI_TEST_EQUALS(value0, 0.f, TEST_LOCATION);
295   DALI_TEST_EQUALS(value1, 0.f, TEST_LOCATION);
296   (bubbleRenderer.GetCurrentProperty(propertyIndex0)).Get(value0);
297   (bubbleRenderer.GetCurrentProperty(propertyIndex0)).Get(value1);
298   DALI_TEST_EQUALS(value0, 0.f, TEST_LOCATION);
299   DALI_TEST_EQUALS(value1, 0.f, TEST_LOCATION);
300
301   animation.Play();
302
303   Wait(application, 300);
304   propertyIndex0 = bubbleRenderer.GetPropertyIndex("uPercentage[0]");
305   propertyIndex1 = bubbleRenderer.GetPropertyIndex("uPercentage[1]");
306   (bubbleRenderer.GetCurrentProperty(propertyIndex0)).Get(value0);
307   (bubbleRenderer.GetCurrentProperty(propertyIndex0)).Get(value1);
308   DALI_TEST_CHECK(value0 >= 0.6f);
309   DALI_TEST_CHECK(value1 >= 0.6f);
310
311   Wait(application, 500);
312   (bubbleRenderer.GetCurrentProperty(propertyIndex0)).Get(value0);
313   (bubbleRenderer.GetCurrentProperty(propertyIndex0)).Get(value1);
314   DALI_TEST_EQUALS(value0, 1.f, TEST_LOCATION);
315   DALI_TEST_EQUALS(value1, 1.f, TEST_LOCATION);
316   END_TEST;
317 }
318
319 int UtcDaliBubbleEmitterRestore(void)
320 {
321   ToolkitTestApplication application;
322   tet_infoline(" UtcDaliBubbleEmitterRestore ");
323
324   static std::vector<UniformData> customUniforms =
325     {
326       UniformData("uPercentage[90]", Property::Type::FLOAT),
327       UniformData("uStartEndPosition[90]", Property::Type::VECTOR4),
328     };
329
330   TestGraphicsController& graphics = application.GetGraphicsController();
331   graphics.AddCustomUniforms(customUniforms);
332
333   Vector2       movementArea(50.f, 50.f);
334   Texture       shapeImage = CreateSolidColorTexture(application, Color::GREEN, 5, 5);
335   BubbleEmitter emitter    = BubbleEmitter::New(movementArea, shapeImage, 90, Vector2(5.f, 10.f));
336   Actor         root       = emitter.GetRootActor();
337   application.GetScene().Add(root);
338   root.SetProperty(Actor::Property::POSITION, Vector3::ZERO);
339   root.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
340   root.SetProperty(Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
341
342   Renderer renderer = root.GetRendererAt(0);
343   DALI_TEST_CHECK(renderer);
344
345   TestGlAbstraction& gl = application.GetGlAbstraction();
346
347   float   percentageValue;
348   Vector4 startEndPosValue;
349
350   Animation animation = Animation::New(0.5f);
351   emitter.EmitBubble(animation, Vector2(40.f, 40.f), Vector2(-5.f, -5.f), Vector2(30.f, 30.f));
352
353   Wait(application);
354
355   DALI_TEST_CHECK(gl.GetUniformValue<float>("uPercentage[0]", percentageValue));
356   DALI_TEST_EQUALS(percentageValue, 0.f, TEST_LOCATION);
357
358   DALI_TEST_CHECK(gl.GetUniformValue<Vector4>("uStartEndPosition[0]", startEndPosValue));
359   DALI_TEST_EQUALS(startEndPosValue.x, 40.f - movementArea.x * 0.5f, TEST_LOCATION);
360   DALI_TEST_EQUALS(startEndPosValue.y, 40.f - movementArea.x * 0.5f, TEST_LOCATION);
361
362   animation.Play();
363   Wait(application, 200);
364   animation.Clear();
365
366   DALI_TEST_CHECK(gl.GetUniformValue<float>("uPercentage[0]", percentageValue));
367   DALI_TEST_CHECK(percentageValue < 0.5f && percentageValue >= 0.4);
368
369   DALI_TEST_CHECK(gl.GetUniformValue<Vector4>("uStartEndPosition[0]", startEndPosValue));
370   DALI_TEST_EQUALS(startEndPosValue.x, 40.f - movementArea.x * 0.5f, TEST_LOCATION);
371   DALI_TEST_EQUALS(startEndPosValue.y, 40.f - movementArea.x * 0.5f, TEST_LOCATION);
372
373   emitter.Restore();
374   application.SendNotification();
375   application.Render();
376
377   DALI_TEST_CHECK(gl.GetUniformValue<float>("uPercentage[0]", percentageValue));
378   DALI_TEST_EQUALS(percentageValue, 0.f, TEST_LOCATION);
379
380   DALI_TEST_CHECK(gl.GetUniformValue<Vector4>("uStartEndPosition[0]", startEndPosValue));
381   DALI_TEST_EQUALS(startEndPosValue, Vector4::ZERO, TEST_LOCATION);
382
383   END_TEST;
384 }