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