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