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