Merge "update bouncing-effect-actor to use new mesh" into devel/new_mesh
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-BubbleEmitter.cpp
1 /*
2  * Copyright (c) 2014 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-toolkit/dali-toolkit.h>
27
28 using namespace Dali;
29 using namespace Dali::Toolkit;
30
31 void utc_dali_toolkit_bubble_emitter_startup(void)
32 {
33   test_return_value = TET_UNDEF;
34 }
35
36 void utc_dali_toolkit_bubble_emitter_cleanup(void)
37 {
38   test_return_value = TET_PASS;
39 }
40
41
42 namespace
43 {
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 Image CreateSolidColorImage( ToolkitTestApplication& application, const Vector4& color, unsigned int width, unsigned int height )
77 {
78   BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
79
80   // Create the image
81   PixelBuffer* pixbuf = imageData.GetBuffer();
82   unsigned int size = width * height;
83
84   for( size_t i = 0; i < size; i++ )
85     {
86       pixbuf[i*4+0] = 0xFF * color.r;
87       pixbuf[i*4+1] = 0xFF * color.g;
88       pixbuf[i*4+2] = 0xFF * color.b;
89       pixbuf[i*4+3] = 0xFF * color.a;
90     }
91   imageData.Update();
92
93   application.GetGlAbstraction().SetCheckFramebufferStatusResult(GL_FRAMEBUFFER_COMPLETE );
94   application.SendNotification();
95   application.Render(RENDER_FRAME_INTERVAL);
96   application.Render(RENDER_FRAME_INTERVAL);
97   application.SendNotification();
98
99   return imageData;
100 }
101 }//namespace
102
103
104 int UtcDaliBubbleEmitterNew(void)
105 {
106   ToolkitTestApplication application;
107
108   tet_infoline(" UtcDaliBubbleEmitterNew ");
109
110   // Test default constructor
111   BubbleEmitter emitter;
112   DALI_TEST_CHECK( !emitter );
113
114   // Test object creation
115   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
116   emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
117   DALI_TEST_CHECK( emitter );
118
119   // Additional check to ensure object is created by checking if it's registered
120   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
121   DALI_TEST_CHECK( registry );
122   gObjectCreatedCallBackCalled = false;
123   registry.ObjectCreatedSignal().Connect( &TestCallback );
124   {
125     BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
126   }
127   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
128
129   // Test copy constructor
130   BubbleEmitter emitterCopy( emitter );
131   DALI_TEST_CHECK( emitterCopy );
132
133   // Test down cast
134   Handle handleEmitter;
135   handleEmitter = emitter;
136   BubbleEmitter downCastEmitter = BubbleEmitter::DownCast( handleEmitter );
137   DALI_TEST_CHECK( downCastEmitter );
138   END_TEST;
139 }
140
141 int UtcDaliBubbleEmitterDownCast01(void)
142 {
143   ToolkitTestApplication application;
144
145   tet_infoline(" UtcDaliBubbleEmitterDownCast01 ");
146
147   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
148   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
149
150   BaseHandle handle(emitter);
151   BubbleEmitter emitter2 = BubbleEmitter::DownCast(handle);
152   DALI_TEST_EQUALS( (bool)emitter2, true, TEST_LOCATION );
153   END_TEST;
154 }
155
156 int UtcDaliBubbleEmitterDownCast02(void)
157 {
158   ToolkitTestApplication application;
159
160   tet_infoline(" UtcDaliBubbleEmitterDownCast02 ");
161
162   Handle handle = Handle::New(); // Create a custom object
163   BubbleEmitter emitter = BubbleEmitter::DownCast(handle);
164   DALI_TEST_EQUALS( (bool)emitter, false, TEST_LOCATION );
165   END_TEST;
166 }
167
168 int UtcDaliBubbleEmitterGetRootActor(void)
169 {
170   ToolkitTestApplication application;
171   tet_infoline( " UtcDaliBubbleEmitterGetRootActor " );
172
173   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
174   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 270, Vector2( 5.f, 10.f ));
175
176   Actor root = emitter.GetRootActor();
177   DALI_TEST_CHECK( root );
178   DALI_TEST_CHECK( root.GetChildCount() == 3 );
179   END_TEST;
180 }
181
182 int UtcDaliBubbleEmitterSetBackground(void)
183 {
184   ToolkitTestApplication application;
185   tet_infoline( " UtcDaliBubbleEmitterSetBackground " );
186
187   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
188   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
189
190   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
191   unsigned int taskCount = taskList.GetTaskCount();
192
193   Image bgImage = CreateSolidColorImage( application, Color::RED, 50, 50 );
194   emitter.SetBackground( bgImage, Vector3(0.f, 0.f, 0.5f) );
195
196   DALI_TEST_CHECK( taskList.GetTaskCount() == taskCount+1 );
197
198   Wait(application, 500);
199   DALI_TEST_CHECK( taskList.GetTaskCount() == taskCount );
200   END_TEST;
201 }
202
203 //TODO: test case for BubbleEmitter::SetShapeImage(Image)
204 // To test that the bubble-shape image is successfully switched in the sampler
205 /*int UtcDaliBubbleEmitterSetShapeImage(void)
206 {
207 }*/
208
209 int UtcDaliBubbleEmitterSetBubbleScale(void)
210 {
211   ToolkitTestApplication application;
212   tet_infoline( " UtcDaliBubbleEmitterSetBubbleScale " );
213
214   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
215   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 150, Vector2( 5.f, 10.f ));
216   Actor root = emitter.GetRootActor();
217   Stage::GetCurrent().Add( root );
218
219   TestGlAbstraction& gl = application.GetGlAbstraction();
220
221   Wait(application);
222
223   float scaleValue;
224   DALI_TEST_CHECK( gl.GetUniformValue<float>( "uDynamicScale", scaleValue ) );
225   DALI_TEST_EQUALS( scaleValue, 1.f, TEST_LOCATION );
226
227   emitter.SetBubbleScale( 2.f );
228   Wait(application);
229   DALI_TEST_CHECK( gl.GetUniformValue<float>( "uDynamicScale", scaleValue ) );
230   DALI_TEST_EQUALS( scaleValue, 2.f, TEST_LOCATION );
231
232   emitter.SetBubbleScale( 0.5f );
233   Wait(application);
234   DALI_TEST_CHECK( gl.GetUniformValue<float>( "uDynamicScale", scaleValue ) );
235   DALI_TEST_EQUALS( scaleValue, 0.5f, TEST_LOCATION );
236   END_TEST;
237 }
238
239 int UtcDaliBubbleEmitterSetBubbleDensity01(void)
240 {
241   ToolkitTestApplication application;
242   tet_infoline( " UtcDaliBubbleEmitterSetBubbleDensity " );
243
244   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
245   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
246
247   try
248   {
249     emitter.SetBubbleDensity( 3.f );
250     DALI_TEST_CHECK(true);
251   }
252   catch(Dali::DaliException& e)
253   {
254     DALI_TEST_PRINT_ASSERT( e );
255     DALI_TEST_ASSERT(e, "density>0 && density<=9", TEST_LOCATION );
256   }
257   END_TEST;
258 }
259
260 int UtcDaliBubbleEmitterSetBubbleDensity02(void)
261 {
262   ToolkitTestApplication application;
263   tet_infoline( " UtcDaliBubbleEmitterSetBubbleDensity " );
264
265   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
266   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
267
268   try
269   {
270     emitter.SetBubbleDensity( 10.f );
271   }
272   catch(Dali::DaliException& e)
273   {
274     DALI_TEST_PRINT_ASSERT( e );
275     DALI_TEST_ASSERT(e, "density>0 && density<=9", TEST_LOCATION );
276   }
277   END_TEST;
278 }
279
280 int UtcDaliBubbleEmitterSetBlendMode(void)
281 {
282   ToolkitTestApplication application;
283   tet_infoline( " UtcDaliBubbleEmitterSetBlendMode " );
284
285   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
286   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 150, Vector2( 5.f, 10.f ));
287   Actor root = emitter.GetRootActor();
288   Stage::GetCurrent().Add( root );
289
290   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
291   Wait(application);
292   DALI_TEST_EQUALS( (GLenum)GL_SRC_ALPHA, glAbstraction.GetLastBlendFuncSrcRgb(), TEST_LOCATION );
293   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_ALPHA, glAbstraction.GetLastBlendFuncDstRgb(), TEST_LOCATION );
294   DALI_TEST_EQUALS( (GLenum)GL_ONE, glAbstraction.GetLastBlendFuncSrcAlpha(), TEST_LOCATION );
295   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_ALPHA, glAbstraction.GetLastBlendFuncDstAlpha(), TEST_LOCATION );
296
297   emitter.SetBlendMode( true );
298   Wait(application);
299   DALI_TEST_EQUALS( (GLenum)GL_SRC_ALPHA, glAbstraction.GetLastBlendFuncSrcRgb(), TEST_LOCATION );
300   DALI_TEST_EQUALS( (GLenum)GL_ONE, glAbstraction.GetLastBlendFuncDstRgb(), TEST_LOCATION );
301   DALI_TEST_EQUALS( (GLenum)GL_ZERO, glAbstraction.GetLastBlendFuncSrcAlpha(), TEST_LOCATION );
302   DALI_TEST_EQUALS( (GLenum)GL_ONE, glAbstraction.GetLastBlendFuncDstAlpha(), TEST_LOCATION );
303
304   emitter.SetBlendMode( false );
305   Wait(application);
306   DALI_TEST_EQUALS( (GLenum)GL_SRC_ALPHA, glAbstraction.GetLastBlendFuncSrcRgb(), TEST_LOCATION );
307   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_ALPHA, glAbstraction.GetLastBlendFuncDstRgb(), TEST_LOCATION );
308   DALI_TEST_EQUALS( (GLenum)GL_ONE, glAbstraction.GetLastBlendFuncSrcAlpha(), TEST_LOCATION );
309   DALI_TEST_EQUALS( (GLenum)GL_ONE_MINUS_SRC_ALPHA, glAbstraction.GetLastBlendFuncDstAlpha(), TEST_LOCATION );
310
311   END_TEST;
312 }
313
314 int UtcDaliBubbleEmitterEmitBubble(void)
315 {
316   ToolkitTestApplication application;
317   tet_infoline( " UtcDaliBubbleEmitterEmitBubble " );
318
319   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
320   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
321
322   Actor root = emitter.GetRootActor();
323   Actor bubbleMesh = root.GetChildAt( 0 );
324   Stage::GetCurrent().Add( root );
325   DALI_TEST_CHECK( bubbleMesh );
326
327   Property::Index propertyIndex0 = bubbleMesh.GetPropertyIndex( "percentage-0" );
328   Property::Index propertyIndex1 = bubbleMesh.GetPropertyIndex( "percentage-1" );
329   float value0, value1;
330
331   Animation animation = Animation::New( 0.5f );
332   emitter.EmitBubble( animation, Vector2(40.f,40.f), Vector2(-5.f,-5.f), Vector2(30.f,30.f) );
333   emitter.EmitBubble( animation, Vector2(10.f,10.f), Vector2(5.f,5.f), Vector2(30.f,30.f) );
334   (bubbleMesh.GetProperty(propertyIndex0)).Get( value0 );
335   (bubbleMesh.GetProperty(propertyIndex1)).Get( value1 );
336   DALI_TEST_EQUALS(value0, 0.f, TEST_LOCATION );
337   DALI_TEST_EQUALS(value1, 0.f, TEST_LOCATION );
338
339   animation.Play();
340
341   Wait(application, 300);
342   propertyIndex0 = bubbleMesh.GetPropertyIndex( "percentage-0" );
343   propertyIndex1 = bubbleMesh.GetPropertyIndex( "percentage-1" );
344   (bubbleMesh.GetProperty(propertyIndex0)).Get( value0 );
345   (bubbleMesh.GetProperty(propertyIndex1)).Get( value1 );
346   DALI_TEST_CHECK( value0 >= 0.6f );
347   DALI_TEST_CHECK( value1 >= 0.6f );
348
349   Wait(application,500);
350   (bubbleMesh.GetProperty(propertyIndex0)).Get( value0 );
351   (bubbleMesh.GetProperty(propertyIndex1)).Get( value1 );
352   DALI_TEST_EQUALS(value0, 1.f, TEST_LOCATION );
353   DALI_TEST_EQUALS(value1, 1.f, TEST_LOCATION );
354   END_TEST;
355 }
356
357 int UtcDaliBubbleEmitterRestore(void)
358 {
359   ToolkitTestApplication application;
360   tet_infoline( " UtcDaliBubbleEmitterRestore " );
361
362   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
363   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 90, Vector2( 5.f, 10.f ));
364   Actor root = emitter.GetRootActor();
365   Stage::GetCurrent().Add( root );
366
367   Actor bubbleMesh = root.GetChildAt( 0 );
368   Renderer renderer = bubbleMesh.GetRendererAt( 0 );
369   DALI_TEST_CHECK( renderer );
370
371   TestGlAbstraction& gl = application.GetGlAbstraction();
372
373   float percentageValue;
374   Vector4 startEndPosValue;
375
376   Animation animation = Animation::New( 0.5f );
377   emitter.EmitBubble( animation, Vector2(40.f,40.f), Vector2(-5.f,-5.f), Vector2(30.f,30.f) );
378
379   Wait(application);
380
381   DALI_TEST_CHECK( gl.GetUniformValue<float>( "uPercentage[0]", percentageValue ) );
382   DALI_TEST_EQUALS( percentageValue, 0.f, TEST_LOCATION );
383
384   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uStartAndEndPos[0]", startEndPosValue ) );
385   DALI_TEST_EQUALS( startEndPosValue.x, 40.f, TEST_LOCATION );
386   DALI_TEST_EQUALS( startEndPosValue.y, 40.f, TEST_LOCATION );
387
388   animation.Play();
389   Wait(application, 200);
390   animation.Clear();
391
392   DALI_TEST_CHECK( gl.GetUniformValue<float>( "uPercentage[0]", percentageValue ) );
393   DALI_TEST_CHECK( percentageValue < 0.5f && percentageValue >= 0.4);
394
395   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uStartAndEndPos[0]", startEndPosValue ) );
396   DALI_TEST_EQUALS( startEndPosValue.x, 40.f, TEST_LOCATION );
397   DALI_TEST_EQUALS( startEndPosValue.y, 40.f, TEST_LOCATION );
398
399   emitter.Restore();
400   application.SendNotification();
401   application.Render();
402
403   DALI_TEST_CHECK( gl.GetUniformValue<float>( "uPercentage[0]", percentageValue ) );
404   DALI_TEST_EQUALS( percentageValue, 0.f, TEST_LOCATION );
405
406   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uStartAndEndPos[0]", startEndPosValue ) );
407   DALI_TEST_EQUALS( startEndPosValue,  Vector4::ZERO, TEST_LOCATION );
408   END_TEST;
409 }