Builder templated constant expansion
[platform/core/uifw/dali-toolkit.git] / automated-tests / TET / dali-test-suite / bubble-emitter / utc-Dali-BubbleEmitter.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 #include <iostream>
18
19 #include <stdlib.h>
20 #include <tet_api.h>
21
22 #include <dali/public-api/dali-core.h>
23 #include <dali-toolkit/dali-toolkit.h>
24
25 #include <dali-toolkit-test-suite-utils.h>
26
27 using namespace Dali;
28 using namespace Dali::Toolkit;
29
30 namespace
31 {
32 const int RENDER_FRAME_INTERVAL = 16;
33
34 static bool gObjectCreatedCallBackCalled;
35 static void TestCallback(BaseHandle handle)
36 {
37   gObjectCreatedCallBackCalled = true;
38 }
39
40 /*
41  * Simulate time passed by.
42  *
43  * @note this will always process at least 1 frame (1/60 sec)
44  *
45  * @param application Test application instance
46  * @param duration Time to pass in milliseconds.
47  * @return The actual time passed in milliseconds
48  */
49 int Wait(ToolkitTestApplication& application, int duration = 0)
50 {
51   int time = 0;
52
53   for(int i = 0; i <= ( duration / RENDER_FRAME_INTERVAL); i++)
54   {
55     application.SendNotification();
56     application.Render(RENDER_FRAME_INTERVAL);
57     time += RENDER_FRAME_INTERVAL;
58   }
59
60   return time;
61 }
62
63 Image CreateSolidColorImage( ToolkitTestApplication& application, const Vector4& color, unsigned int width, unsigned int height )
64 {
65   BitmapImage imageData = BitmapImage::New( width, height, Pixel::RGBA8888 );
66
67   // Create the image
68   PixelBuffer* pixbuf = imageData.GetBuffer();
69   unsigned int size = width * height;
70
71   for( size_t i = 0; i < size; i++ )
72     {
73       pixbuf[i*4+0] = 0xFF * color.r;
74       pixbuf[i*4+1] = 0xFF * color.g;
75       pixbuf[i*4+2] = 0xFF * color.b;
76       pixbuf[i*4+3] = 0xFF * color.a;
77     }
78   imageData.Update();
79
80   application.GetGlAbstraction().SetCheckFramebufferStatusResult(GL_FRAMEBUFFER_COMPLETE );
81   application.SendNotification();
82   application.Render(RENDER_FRAME_INTERVAL);
83   application.Render(RENDER_FRAME_INTERVAL);
84   application.SendNotification();
85
86   return imageData;
87 }
88 }//namespace
89
90 static void Startup();
91 static void Cleanup();
92
93 extern "C" {
94   void (*tet_startup)() = Startup;
95   void (*tet_cleanup)() = Cleanup;
96 }
97
98 enum {
99   POSITIVE_TC_IDX = 0x01,
100   NEGATIVE_TC_IDX,
101 };
102
103 #define MAX_NUMBER_OF_TESTS 10000
104 extern "C" {
105   struct tet_testlist tet_testlist[MAX_NUMBER_OF_TESTS];
106 }
107
108 // Add test functionality for all APIs in the class (Positive and Negative)
109 TEST_FUNCTION( UtcDaliBubbleEmitterNew, POSITIVE_TC_IDX );
110 TEST_FUNCTION( UtcDaliBubbleEmitterGetRootActor, POSITIVE_TC_IDX );
111 TEST_FUNCTION( UtcDaliBubbleEmitterSetBackground, POSITIVE_TC_IDX );
112 TEST_FUNCTION( UtcDaliBubbleEmitterSetShapeImage, POSITIVE_TC_IDX );
113 TEST_FUNCTION( UtcDaliBubbleEmitterSetBubbleScale, POSITIVE_TC_IDX );
114 TEST_FUNCTION( UtcDaliBubbleEmitterSetBubbleDensity01, POSITIVE_TC_IDX );
115 TEST_FUNCTION( UtcDaliBubbleEmitterSetBubbleDensity02, POSITIVE_TC_IDX );
116 TEST_FUNCTION( UtcDaliBubbleEmitterSetBlendMode, POSITIVE_TC_IDX );
117 TEST_FUNCTION( UtcDaliBubbleEmitterEmitBubble, POSITIVE_TC_IDX );
118 TEST_FUNCTION( UtcDaliBubbleEmitterStartExplosion, POSITIVE_TC_IDX );
119 TEST_FUNCTION( UtcDaliBubbleEmitterRestore, POSITIVE_TC_IDX );
120
121 // Called only once before first test is run.
122 static void Startup()
123 {
124 }
125
126 // Called only once after last test is run
127 static void Cleanup()
128 {
129 }
130
131 static void UtcDaliBubbleEmitterNew()
132 {
133   ToolkitTestApplication application;
134
135   tet_infoline(" UtcDaliBubbleEmitterNew ");
136
137   // Test default constructor
138   BubbleEmitter emitter;
139   DALI_TEST_CHECK( !emitter );
140
141   // Test object creation
142   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
143   emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
144   DALI_TEST_CHECK( emitter );
145
146   // Additional check to ensure object is created by checking if it's registered
147   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
148   DALI_TEST_CHECK( registry );
149   gObjectCreatedCallBackCalled = false;
150   registry.ObjectCreatedSignal().Connect( &TestCallback );
151   {
152     BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
153   }
154   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
155
156   // Test copy constructor
157   BubbleEmitter emitterCopy( emitter );
158   DALI_TEST_CHECK( emitterCopy );
159
160   // Test down cast
161   Handle handleEmitter;
162   handleEmitter = emitter;
163   BubbleEmitter downCastEmitter = BubbleEmitter::DownCast( handleEmitter );
164   DALI_TEST_CHECK( downCastEmitter );
165 }
166
167 static void UtcDaliBubbleEmitterGetRootActor()
168 {
169   ToolkitTestApplication application;
170   tet_infoline( " UtcDaliBubbleEmitterGetRootActor " );
171
172   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
173   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
174
175   Actor root = emitter.GetRootActor();
176   DALI_TEST_CHECK( root );
177   DALI_TEST_CHECK( root.GetChildCount() == 3 );
178 }
179
180 static void UtcDaliBubbleEmitterSetBackground()
181 {
182   ToolkitTestApplication application;
183   tet_infoline( " UtcDaliBubbleEmitterSetBackground " );
184
185   Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
186   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
187
188   RenderTaskList taskList = Stage::GetCurrent().GetRenderTaskList();
189   unsigned int taskCount = taskList.GetTaskCount();
190
191   Image bgImage = CreateSolidColorImage( application, Color::RED, 50, 50 );
192   emitter.SetBackground( bgImage, Vector3(0.f, 0.f, 0.5f) );
193
194   DALI_TEST_CHECK( taskList.GetTaskCount() == taskCount+1 );
195
196   Wait(application, 500);
197   DALI_TEST_CHECK( taskList.GetTaskCount() == taskCount );
198 }
199
200 static void UtcDaliBubbleEmitterSetShapeImage()
201 {
202   ToolkitTestApplication application;
203   tet_infoline( " UtcDaliBubbleEmitterSetShapeImage " );
204
205   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
206   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
207
208   Actor root = emitter.GetRootActor();
209   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
210   Material material = bubbleMesh.GetMaterial();
211
212   DALI_TEST_CHECK( material.GetDiffuseTexture() == shapeImage1 );
213
214   Image shapeImage2 = CreateSolidColorImage( application, Color::RED, 8, 8 );
215   emitter.SetShapeImage( shapeImage2 );
216
217   DALI_TEST_CHECK( material.GetDiffuseTexture() == shapeImage2 );
218 }
219
220 static void UtcDaliBubbleEmitterSetBubbleScale()
221 {
222   ToolkitTestApplication application;
223   tet_infoline( " UtcDaliBubbleEmitterSetBubbleScale " );
224
225   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
226   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
227
228   Actor root = emitter.GetRootActor();
229   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
230   ShaderEffect effect = bubbleMesh.GetShaderEffect();
231   DALI_TEST_CHECK( effect );
232
233   Property::Index scalePropertyIndex = effect.GetPropertyIndex( "uDynamicScale" );
234   float scaleValue;
235   (effect.GetProperty(scalePropertyIndex)).Get( scaleValue );
236   DALI_TEST_EQUALS(scaleValue, 1.f, TEST_LOCATION );
237
238   emitter.SetBubbleScale( 2.f );
239   application.SendNotification();
240   application.Render();
241   (effect.GetProperty(scalePropertyIndex)).Get( scaleValue );
242   DALI_TEST_EQUALS(scaleValue, 2.f, TEST_LOCATION );
243
244   emitter.SetBubbleScale( 0.5f );
245   application.SendNotification();
246   application.Render();
247   (effect.GetProperty(scalePropertyIndex)).Get( scaleValue );
248   DALI_TEST_EQUALS(scaleValue, 0.5f, TEST_LOCATION );
249 }
250
251 static void UtcDaliBubbleEmitterSetBubbleDensity01()
252 {
253   ToolkitTestApplication application;
254   tet_infoline( " UtcDaliBubbleEmitterSetBubbleDensity " );
255
256   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
257   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
258
259   try
260   {
261     emitter.SetBubbleDensity( 3.f );
262     DALI_TEST_CHECK(true);
263   }
264   catch(Dali::DaliException& e)
265   {
266     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
267     DALI_TEST_ASSERT(e, "density>0 && density<=9", TEST_LOCATION );
268   }
269 }
270
271 static void UtcDaliBubbleEmitterSetBubbleDensity02()
272 {
273   ToolkitTestApplication application;
274   tet_infoline( " UtcDaliBubbleEmitterSetBubbleDensity " );
275
276   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
277   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
278
279   try
280   {
281     emitter.SetBubbleDensity( 10.f );
282   }
283   catch(Dali::DaliException& e)
284   {
285     tet_printf("Assertion %s failed at %s\n", e.mCondition.c_str(), e.mLocation.c_str());
286     DALI_TEST_ASSERT(e, "density>0 && density<=9", TEST_LOCATION );
287   }
288 }
289
290 static void UtcDaliBubbleEmitterSetBlendMode()
291 {
292   ToolkitTestApplication application;
293   tet_infoline( " UtcDaliBubbleEmitterSetBlendMode " );
294
295   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
296   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
297
298   Actor root = emitter.GetRootActor();
299   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
300
301   BlendingFactor::Type srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha;
302
303   emitter.SetBlendMode( true );
304   bubbleMesh.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
305   DALI_TEST_CHECK( srcFactorRgb == BlendingFactor::SRC_ALPHA );
306   DALI_TEST_CHECK( destFactorRgb == BlendingFactor::ONE );
307   DALI_TEST_CHECK( srcFactorAlpha == BlendingFactor::ZERO );
308   DALI_TEST_CHECK( destFactorAlpha == BlendingFactor::ONE );
309
310   emitter.SetBlendMode( false );
311   bubbleMesh.GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
312   DALI_TEST_CHECK( srcFactorRgb == BlendingFactor::SRC_ALPHA );
313   DALI_TEST_CHECK( destFactorRgb == BlendingFactor::ONE_MINUS_SRC_ALPHA );
314   DALI_TEST_CHECK( srcFactorAlpha == BlendingFactor::ONE );
315   DALI_TEST_CHECK( destFactorAlpha == BlendingFactor::ONE_MINUS_SRC_ALPHA );
316 }
317
318 static void UtcDaliBubbleEmitterEmitBubble()
319 {
320   ToolkitTestApplication application;
321   tet_infoline( " UtcDaliBubbleEmitterEmitBubble " );
322
323   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
324   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
325
326   Actor root = emitter.GetRootActor();
327   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
328   ShaderEffect effect = bubbleMesh.GetShaderEffect();
329   DALI_TEST_CHECK( effect );
330
331   Property::Index propertyIndex0 = effect.GetPropertyIndex( "uPercentage[0]" );
332   Property::Index propertyIndex1 = effect.GetPropertyIndex( "uPercentage[1]" );
333   float value0, value1;
334
335   Animation animation = Animation::New( 0.5f );
336   emitter.EmitBubble( animation, Vector2(40.f,40.f), Vector2(-5.f,-5.f), Vector2(30.f,30.f) );
337   emitter.EmitBubble( animation, Vector2(10.f,10.f), Vector2(5.f,5.f), Vector2(30.f,30.f) );
338   (effect.GetProperty(propertyIndex0)).Get( value0 );
339   (effect.GetProperty(propertyIndex1)).Get( value1 );
340   DALI_TEST_EQUALS(value0, 0.f, TEST_LOCATION );
341   DALI_TEST_EQUALS(value1, 0.f, TEST_LOCATION );
342
343   animation.Play();
344
345   Wait(application, 300);
346   (effect.GetProperty(propertyIndex0)).Get( value0 );
347   (effect.GetProperty(propertyIndex1)).Get( value1 );
348   DALI_TEST_CHECK( value0 >= 0.6f );
349   DALI_TEST_CHECK( value1 >= 0.6f );
350
351   Wait(application, 600);
352   (effect.GetProperty(propertyIndex0)).Get( value0 );
353   (effect.GetProperty(propertyIndex1)).Get( value1 );
354   DALI_TEST_EQUALS(value0, 1.f, TEST_LOCATION );
355   DALI_TEST_EQUALS(value1, 1.f, TEST_LOCATION );
356 }
357
358 static void UtcDaliBubbleEmitterStartExplosion()
359 {
360   ToolkitTestApplication application;
361   tet_infoline( " UtcDaliBubbleEmitterStartExplosion " );
362
363   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
364   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
365   Actor root = emitter.GetRootActor();
366   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
367   ShaderEffect effect = bubbleMesh.GetShaderEffect();
368   DALI_TEST_CHECK( effect );
369
370   Property::Index propertyIndex = effect.GetPropertyIndex( "uMagnification" );
371   float value;
372   (effect.GetProperty(propertyIndex)).Get( value );
373   DALI_TEST_EQUALS(value, 1.f, TEST_LOCATION );
374
375   emitter.StartExplosion( 0.4, 4.f );
376
377   Wait(application, 200); // 0.2s
378   (effect.GetProperty(propertyIndex)).Get( value );
379   DALI_TEST_CHECK( value >= 2.f );
380
381   Wait(application, 100); // 0.3s
382   (effect.GetProperty(propertyIndex)).Get( value );
383   DALI_TEST_CHECK( value >= 3.f );
384
385   Wait(application, 100); // 0.4s
386   (effect.GetProperty(propertyIndex)).Get( value );
387   DALI_TEST_EQUALS(value, 1.f, TEST_LOCATION );
388 }
389
390 static void UtcDaliBubbleEmitterRestore()
391 {
392   ToolkitTestApplication application;
393   tet_infoline( " UtcDaliBubbleEmitterRestore " );
394
395   Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
396   BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
397   Actor root = emitter.GetRootActor();
398   MeshActor bubbleMesh = MeshActor::DownCast( root.GetChildAt( 0 ) );
399   ShaderEffect effect = bubbleMesh.GetShaderEffect();
400   DALI_TEST_CHECK( effect );
401
402   Property::Index percentagePropertyIndex = effect.GetPropertyIndex( "uPercentage[0]" );
403   float percentage;
404
405   Animation animation = Animation::New( 0.5f );
406   emitter.EmitBubble( animation, Vector2(40.f,40.f), Vector2(-5.f,-5.f), Vector2(30.f,30.f) );
407   (effect.GetProperty(percentagePropertyIndex)).Get( percentage );
408   DALI_TEST_EQUALS(percentage, 0.f, TEST_LOCATION );
409
410   animation.Play();
411   Wait(application, 200);
412   animation.Clear();
413
414   (effect.GetProperty(percentagePropertyIndex)).Get( percentage );
415   DALI_TEST_CHECK( percentage < 0.5f && percentage >= 0.4);
416
417   emitter.Restore();
418   application.SendNotification();
419   application.Render();
420
421   (effect.GetProperty(percentagePropertyIndex)).Get( percentage );
422   DALI_TEST_EQUALS(percentage, 1.f, TEST_LOCATION );
423 }