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