Merge remote-tracking branch 'origin/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/devel-api/actors/mesh-actor.h>
28 #include <dali/devel-api/modeling/material.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 Image CreateSolidColorImage( ToolkitTestApplication& application, const Vector4& color, unsigned int width, unsigned int height )
79 {
80   BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
81
82   // Create the image
83   PixelBuffer* pixbuf = imageData.GetBuffer();
84   unsigned int size = width * height;
85
86   for( size_t i = 0; i < size; i++ )
87     {
88       pixbuf[i*4+0] = 0xFF * color.r;
89       pixbuf[i*4+1] = 0xFF * color.g;
90       pixbuf[i*4+2] = 0xFF * color.b;
91       pixbuf[i*4+3] = 0xFF * color.a;
92     }
93   imageData.Update();
94
95   application.GetGlAbstraction().SetCheckFramebufferStatusResult(GL_FRAMEBUFFER_COMPLETE );
96   application.SendNotification();
97   application.Render(RENDER_FRAME_INTERVAL);
98   application.Render(RENDER_FRAME_INTERVAL);
99   application.SendNotification();
100
101   return imageData;
102 }
103 }//namespace
104
105
106 int UtcDaliBubbleEmitterNew(void)
107 {
108   ToolkitTestApplication application;
109
110   tet_infoline(" UtcDaliBubbleEmitterNew ");
111
112   // Test default constructor
113   BubbleEmitter emitter;
114   DALI_TEST_CHECK( !emitter );
115
116   // Test object creation
117   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
118   emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
119   DALI_TEST_CHECK( emitter );
120
121   // Additional check to ensure object is created by checking if it's registered
122   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
123   DALI_TEST_CHECK( registry );
124   gObjectCreatedCallBackCalled = false;
125   registry.ObjectCreatedSignal().Connect( &TestCallback );
126   {
127     BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
128   }
129   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
130
131   // Test copy constructor
132   BubbleEmitter emitterCopy( emitter );
133   DALI_TEST_CHECK( emitterCopy );
134
135   // Test down cast
136   Handle handleEmitter;
137   handleEmitter = emitter;
138   BubbleEmitter downCastEmitter = BubbleEmitter::DownCast( handleEmitter );
139   DALI_TEST_CHECK( downCastEmitter );
140   END_TEST;
141 }
142
143 int UtcDaliBubbleEmitterGetRootActor(void)
144 {
145   ToolkitTestApplication application;
146   tet_infoline( " UtcDaliBubbleEmitterGetRootActor " );
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   Actor root = emitter.GetRootActor();
152   DALI_TEST_CHECK( root );
153   DALI_TEST_CHECK( root.GetChildCount() == 3 );
154   END_TEST;
155 }
156
157 int UtcDaliBubbleEmitterSetBackground(void)
158 {
159   ToolkitTestApplication application;
160   tet_infoline( " UtcDaliBubbleEmitterSetBackground " );
161
162   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
163   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
164
165   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
166   unsigned int taskCount = taskList.GetTaskCount();
167
168   Image bgImage = CreateSolidColorImage( application, Color::RED, 50, 50 );
169   emitter.SetBackground( bgImage, Vector3(0.f, 0.f, 0.5f) );
170
171   DALI_TEST_CHECK( taskList.GetTaskCount() == taskCount+1 );
172
173   Wait(application, 500);
174   DALI_TEST_CHECK( taskList.GetTaskCount() == taskCount );
175   END_TEST;
176 }
177
178 int UtcDaliBubbleEmitterSetShapeImage(void)
179 {
180   ToolkitTestApplication application;
181   tet_infoline( " UtcDaliBubbleEmitterSetShapeImage " );
182
183   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
184   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
185
186   Actor root = emitter.GetRootActor();
187   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
188   Material material = bubbleMesh.GetMaterial();
189
190   DALI_TEST_CHECK( material.GetDiffuseTexture() == shapeImage1 );
191
192   Image shapeImage2 = CreateSolidColorImage( application, Color::RED, 8, 8 );
193   emitter.SetShapeImage( shapeImage2 );
194
195   DALI_TEST_CHECK( material.GetDiffuseTexture() == shapeImage2 );
196   END_TEST;
197 }
198
199 int UtcDaliBubbleEmitterSetBubbleScale(void)
200 {
201   ToolkitTestApplication application;
202   tet_infoline( " UtcDaliBubbleEmitterSetBubbleScale " );
203
204   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
205   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
206
207   Actor root = emitter.GetRootActor();
208   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
209   ShaderEffect effect = bubbleMesh.GetShaderEffect();
210   DALI_TEST_CHECK( effect );
211
212   Property::Index scalePropertyIndex = effect.GetPropertyIndex( "uDynamicScale" );
213   float scaleValue;
214   (effect.GetProperty(scalePropertyIndex)).Get( scaleValue );
215   DALI_TEST_EQUALS(scaleValue, 1.f, TEST_LOCATION );
216
217   emitter.SetBubbleScale( 2.f );
218   application.SendNotification();
219   application.Render();
220   (effect.GetProperty(scalePropertyIndex)).Get( scaleValue );
221   DALI_TEST_EQUALS(scaleValue, 2.f, TEST_LOCATION );
222
223   emitter.SetBubbleScale( 0.5f );
224   application.SendNotification();
225   application.Render();
226   (effect.GetProperty(scalePropertyIndex)).Get( scaleValue );
227   DALI_TEST_EQUALS(scaleValue, 0.5f, TEST_LOCATION );
228   END_TEST;
229 }
230
231 int UtcDaliBubbleEmitterSetBubbleDensity01(void)
232 {
233   ToolkitTestApplication application;
234   tet_infoline( " UtcDaliBubbleEmitterSetBubbleDensity " );
235
236   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
237   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 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   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
258   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 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 UtcDaliBubbleEmitterSetBlendMode(void)
273 {
274   ToolkitTestApplication application;
275   tet_infoline( " UtcDaliBubbleEmitterSetBlendMode " );
276
277   Image shapeImage1 = CreateSolidColorImage( 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   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
282
283   BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
284
285   emitter.SetBlendMode( true );
286   bubbleMesh.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
287   DALI_TEST_CHECK( srcFactorRgb == BlendingFactor::SRC_ALPHA );
288   DALI_TEST_CHECK( destFactorRgb == BlendingFactor::ONE );
289   DALI_TEST_CHECK( srcFactorAlpha == BlendingFactor::ZERO );
290   DALI_TEST_CHECK( destFactorAlpha == BlendingFactor::ONE );
291
292   emitter.SetBlendMode( false );
293   bubbleMesh.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
294   DALI_TEST_CHECK( srcFactorRgb == BlendingFactor::SRC_ALPHA );
295   DALI_TEST_CHECK( destFactorRgb == BlendingFactor::ONE_MINUS_SRC_ALPHA );
296   DALI_TEST_CHECK( srcFactorAlpha == BlendingFactor::ONE );
297   DALI_TEST_CHECK( destFactorAlpha == BlendingFactor::ONE_MINUS_SRC_ALPHA );
298   END_TEST;
299 }
300
301 int UtcDaliBubbleEmitterEmitBubble(void)
302 {
303   ToolkitTestApplication application;
304   tet_infoline( " UtcDaliBubbleEmitterEmitBubble " );
305
306   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
307   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
308
309   Actor root = emitter.GetRootActor();
310   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
311   ShaderEffect effect = bubbleMesh.GetShaderEffect();
312   DALI_TEST_CHECK( effect );
313
314   Property::Index propertyIndex0 = effect.GetPropertyIndex( "uPercentage[0]" );
315   Property::Index propertyIndex1 = effect.GetPropertyIndex( "uPercentage[1]" );
316   float value0, value1;
317
318   Animation animation = Animation::New( 0.5f );
319   emitter.EmitBubble( animation, Vector2(40.f,40.f), Vector2(-5.f,-5.f), Vector2(30.f,30.f) );
320   emitter.EmitBubble( animation, Vector2(10.f,10.f), Vector2(5.f,5.f), Vector2(30.f,30.f) );
321   (effect.GetProperty(propertyIndex0)).Get( value0 );
322   (effect.GetProperty(propertyIndex1)).Get( value1 );
323   DALI_TEST_EQUALS(value0, 0.f, TEST_LOCATION );
324   DALI_TEST_EQUALS(value1, 0.f, TEST_LOCATION );
325
326   animation.Play();
327
328   Wait(application, 300);
329   (effect.GetProperty(propertyIndex0)).Get( value0 );
330   (effect.GetProperty(propertyIndex1)).Get( value1 );
331   DALI_TEST_CHECK( value0 >= 0.6f );
332   DALI_TEST_CHECK( value1 >= 0.6f );
333
334   Wait(application, 600);
335   (effect.GetProperty(propertyIndex0)).Get( value0 );
336   (effect.GetProperty(propertyIndex1)).Get( value1 );
337   DALI_TEST_EQUALS(value0, 1.f, TEST_LOCATION );
338   DALI_TEST_EQUALS(value1, 1.f, TEST_LOCATION );
339   END_TEST;
340 }
341
342 int UtcDaliBubbleEmitterStartExplosion(void)
343 {
344   ToolkitTestApplication application;
345   tet_infoline( " UtcDaliBubbleEmitterStartExplosion " );
346
347   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
348   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
349   Actor root = emitter.GetRootActor();
350   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
351   ShaderEffect effect = bubbleMesh.GetShaderEffect();
352   DALI_TEST_CHECK( effect );
353
354   Property::Index propertyIndex = effect.GetPropertyIndex( "uMagnification" );
355   float value;
356   (effect.GetProperty(propertyIndex)).Get( value );
357   DALI_TEST_EQUALS(value, 1.f, TEST_LOCATION );
358
359   emitter.StartExplosion( 0.4, 4.f );
360
361   Wait(application, 200); // 0.2s
362   (effect.GetProperty(propertyIndex)).Get( value );
363   DALI_TEST_CHECK( value >= 2.f );
364
365   Wait(application, 100); // 0.3s
366   (effect.GetProperty(propertyIndex)).Get( value );
367   DALI_TEST_CHECK( value >= 3.f );
368
369   Wait(application, 100); // 0.4s
370   (effect.GetProperty(propertyIndex)).Get( value );
371   DALI_TEST_EQUALS(value, 1.f, TEST_LOCATION );
372   END_TEST;
373 }
374
375 int UtcDaliBubbleEmitterRestore(void)
376 {
377   ToolkitTestApplication application;
378   tet_infoline( " UtcDaliBubbleEmitterRestore " );
379
380   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
381   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
382   Actor root = emitter.GetRootActor();
383   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
384   ShaderEffect effect = bubbleMesh.GetShaderEffect();
385   DALI_TEST_CHECK( effect );
386
387   Property::Index percentagePropertyIndex = effect.GetPropertyIndex( "uPercentage[0]" );
388   float percentage;
389
390   Animation animation = Animation::New( 0.5f );
391   emitter.EmitBubble( animation, Vector2(40.f,40.f), Vector2(-5.f,-5.f), Vector2(30.f,30.f) );
392   (effect.GetProperty(percentagePropertyIndex)).Get( percentage );
393   DALI_TEST_EQUALS(percentage, 0.f, TEST_LOCATION );
394
395   animation.Play();
396   Wait(application, 200);
397   animation.Clear();
398
399   (effect.GetProperty(percentagePropertyIndex)).Get( percentage );
400   DALI_TEST_CHECK( percentage < 0.5f && percentage >= 0.4);
401
402   emitter.Restore();
403   application.SendNotification();
404   application.Render();
405
406   (effect.GetProperty(percentagePropertyIndex)).Get( percentage );
407   DALI_TEST_EQUALS(percentage, 1.f, TEST_LOCATION );
408   END_TEST;
409 }