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