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