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