Dali 3D User Interface Engine
utc-Dali-BubbleEmitter.cpp
Go to the documentation of this file.
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.
24 
25 #include <dali.h>
29 
30 using namespace Dali;
31 using namespace Dali::Toolkit;
32 
34 {
36 }
37 
39 {
41 }
42 
43 
44 namespace
45 {
46 
47 const int RENDER_FRAME_INTERVAL = 16;
48 
50 static void TestCallback(BaseHandle handle)
51 {
52  gObjectCreatedCallBackCalled = true;
53 }
54 
55 /*
56  * Simulate time passed by.
57  *
58  * @note this will always process at least 1 frame (1/60 sec)
59  *
60  * @param application Test application instance
61  * @param duration Time to pass in milliseconds.
62  * @return The actual time passed in milliseconds
63  */
64 static int Wait(ToolkitTestApplication& application, int duration = 0)
65 {
66  int time = 0;
67 
68  for(int i = 0; i <= ( duration / RENDER_FRAME_INTERVAL); i++)
69  {
70  application.SendNotification();
71  application.Render(RENDER_FRAME_INTERVAL);
72  time += RENDER_FRAME_INTERVAL;
73  }
74 
75  return time;
76 }
77 
78 static Image CreateSolidColorImage( ToolkitTestApplication& application, const Vector4& color, unsigned int width, unsigned int height )
79 {
80  BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
81 
82  // Create the image
83  PixelBuffer* pixbuf = imageData.GetBuffer();
84  unsigned int size = width * height;
85 
86  for( size_t i = 0; i < size; i++ )
87  {
88  pixbuf[i*4+0] = 0xFF * color.r;
89  pixbuf[i*4+1] = 0xFF * color.g;
90  pixbuf[i*4+2] = 0xFF * color.b;
91  pixbuf[i*4+3] = 0xFF * color.a;
92  }
93  imageData.Update();
94 
96  application.SendNotification();
97  application.Render(RENDER_FRAME_INTERVAL);
98  application.Render(RENDER_FRAME_INTERVAL);
99  application.SendNotification();
100 
101  return imageData;
102 }
103 }//namespace
104 
105 
107 {
108  ToolkitTestApplication application;
109 
110  tet_infoline(" UtcDaliBubbleEmitterNew ");
111 
112  // Test default constructor
113  BubbleEmitter emitter;
114  DALI_TEST_CHECK( !emitter );
115 
116  // Test object creation
117  Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
118  emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
119  DALI_TEST_CHECK( emitter );
120 
121  // Additional check to ensure object is created by checking if it's registered
123  DALI_TEST_CHECK( registry );
125  registry.ObjectCreatedSignal().Connect( &TestCallback );
126  {
127  BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
128  }
130 
131  // Test copy constructor
132  BubbleEmitter emitterCopy( emitter );
133  DALI_TEST_CHECK( emitterCopy );
134 
135  // Test down cast
136  Handle handleEmitter;
137  handleEmitter = emitter;
138  BubbleEmitter downCastEmitter = BubbleEmitter::DownCast( handleEmitter );
139  DALI_TEST_CHECK( downCastEmitter );
140  END_TEST;
141 }
142 
144 {
145  ToolkitTestApplication application;
146 
147  tet_infoline(" UtcDaliBubbleEmitterDownCast01 ");
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  BaseHandle handle(emitter);
153  BubbleEmitter emitter2 = BubbleEmitter::DownCast(handle);
154  DALI_TEST_EQUALS( (bool)emitter2, true, TEST_LOCATION );
155  END_TEST;
156 }
157 
159 {
160  ToolkitTestApplication application;
161 
162  tet_infoline(" UtcDaliBubbleEmitterDownCast02 ");
163 
164  Handle handle = Handle::New(); // Create a custom object
165  BubbleEmitter emitter = BubbleEmitter::DownCast(handle);
166  DALI_TEST_EQUALS( (bool)emitter, false, TEST_LOCATION );
167  END_TEST;
168 }
169 
171 {
172  ToolkitTestApplication application;
173  tet_infoline( " UtcDaliBubbleEmitterGetRootActor " );
174 
175  Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
176  BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 270, Vector2( 5.f, 10.f ));
177 
178  Actor root = emitter.GetRootActor();
179  DALI_TEST_CHECK( root );
180  DALI_TEST_CHECK( root.GetChildCount() == 3 );
181  END_TEST;
182 }
183 
185 {
186  ToolkitTestApplication application;
187  tet_infoline( " UtcDaliBubbleEmitterSetBackground " );
188 
189  Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
190  BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
191 
193  unsigned int taskCount = taskList.GetTaskCount();
194 
195  Image bgImage = CreateSolidColorImage( application, Color::RED, 50, 50 );
196  emitter.SetBackground( bgImage, Vector3(0.f, 0.f, 0.5f) );
197 
198  DALI_TEST_CHECK( taskList.GetTaskCount() == taskCount+1 );
199 
200  Wait(application, 500);
201  DALI_TEST_CHECK( taskList.GetTaskCount() == taskCount );
202  END_TEST;
203 }
204 
205 //TODO: test case for BubbleEmitter::SetShapeImage(Image)
206 // To test that the bubble-shape image is successfully switched in the sampler
207 /*int UtcDaliBubbleEmitterSetShapeImage(void)
208 {
209 }*/
210 
212 {
213  ToolkitTestApplication application;
214  tet_infoline( " UtcDaliBubbleEmitterSetBubbleScale " );
215 
216  Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
217  BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 150, Vector2( 5.f, 10.f ));
218  DALI_TEST_CHECK(emitter);
219  Actor root = emitter.GetRootActor();
220  Stage::GetCurrent().Add( root );
221  root.SetPosition( Vector3::ZERO );
224 
225  TestGlAbstraction& gl = application.GetGlAbstraction();
226 
227  Wait(application);
228 
229  float scaleValue;
230  DALI_TEST_CHECK( gl.GetUniformValue<float>( "uDynamicScale", scaleValue ) );
231  DALI_TEST_EQUALS( scaleValue, 1.f, TEST_LOCATION );
232 
233  emitter.SetBubbleScale( 2.f );
234  Wait(application);
235  DALI_TEST_CHECK( gl.GetUniformValue<float>( "uDynamicScale", scaleValue ) );
236  DALI_TEST_EQUALS( scaleValue, 2.f, TEST_LOCATION );
237 
238  emitter.SetBubbleScale( 0.5f );
239  Wait(application);
240  DALI_TEST_CHECK( gl.GetUniformValue<float>( "uDynamicScale", scaleValue ) );
241  DALI_TEST_EQUALS( scaleValue, 0.5f, TEST_LOCATION );
242 
243  END_TEST;
244 }
245 
247 {
248  ToolkitTestApplication application;
249  tet_infoline( " UtcDaliBubbleEmitterSetBubbleDensity " );
250 
251  Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
252  BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
253 
254  try
255  {
256  emitter.SetBubbleDensity( 3.f );
257  DALI_TEST_CHECK(true);
258  }
259  catch(Dali::DaliException& e)
260  {
262  DALI_TEST_ASSERT(e, "density>0 && density<=9", TEST_LOCATION );
263  }
264  END_TEST;
265 }
266 
268 {
269  ToolkitTestApplication application;
270  tet_infoline( " UtcDaliBubbleEmitterSetBubbleDensity " );
271 
272  Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
273  BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 200, Vector2( 5.f, 10.f ));
274 
275  try
276  {
277  emitter.SetBubbleDensity( 10.f );
278  }
279  catch(Dali::DaliException& e)
280  {
282  DALI_TEST_ASSERT(e, "density>0 && density<=9", TEST_LOCATION );
283  }
284  END_TEST;
285 }
286 
288 {
289  ToolkitTestApplication application;
290  tet_infoline( " UtcDaliBubbleEmitterEmitBubble " );
291 
292  Image shapeImage1 = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
293  BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage1, 200, Vector2( 5.f, 10.f ));
294 
295  Actor root = emitter.GetRootActor();
296  Actor bubbleMesh = root.GetChildAt( 0 );
297  Stage::GetCurrent().Add( root );
298  DALI_TEST_CHECK( bubbleMesh );
299 
300  Property::Index propertyIndex0 = bubbleMesh.GetPropertyIndex( "uPercentage[0]" );
301  Property::Index propertyIndex1 = bubbleMesh.GetPropertyIndex( "uPercentage[1]" );
302  float value0, value1;
303 
304  Animation animation = Animation::New( 0.5f );
305  emitter.EmitBubble( animation, Vector2(40.f,40.f), Vector2(-5.f,-5.f), Vector2(30.f,30.f) );
306  emitter.EmitBubble( animation, Vector2(10.f,10.f), Vector2(5.f,5.f), Vector2(30.f,30.f) );
307  (bubbleMesh.GetProperty(propertyIndex0)).Get( value0 );
308  (bubbleMesh.GetProperty(propertyIndex1)).Get( value1 );
309  DALI_TEST_EQUALS(value0, 0.f, TEST_LOCATION );
310  DALI_TEST_EQUALS(value1, 0.f, TEST_LOCATION );
311 
312  animation.Play();
313 
314  Wait(application, 300);
315  propertyIndex0 = bubbleMesh.GetPropertyIndex( "uPercentage[0]" );
316  propertyIndex1 = bubbleMesh.GetPropertyIndex( "uPercentage[1]" );
317  (bubbleMesh.GetProperty(propertyIndex0)).Get( value0 );
318  (bubbleMesh.GetProperty(propertyIndex1)).Get( value1 );
319  DALI_TEST_CHECK( value0 >= 0.6f );
320  DALI_TEST_CHECK( value1 >= 0.6f );
321 
322  Wait(application,500);
323  (bubbleMesh.GetProperty(propertyIndex0)).Get( value0 );
324  (bubbleMesh.GetProperty(propertyIndex1)).Get( value1 );
325  DALI_TEST_EQUALS(value0, 1.f, TEST_LOCATION );
326  DALI_TEST_EQUALS(value1, 1.f, TEST_LOCATION );
327  END_TEST;
328 }
329 
331 {
332  ToolkitTestApplication application;
333  tet_infoline( " UtcDaliBubbleEmitterRestore " );
334 
335  Image shapeImage = CreateSolidColorImage( application, Color::GREEN, 5, 5 );
336  BubbleEmitter emitter = BubbleEmitter::New( Vector2(50.f,50.f),shapeImage, 90, Vector2( 5.f, 10.f ));
337  Actor root = emitter.GetRootActor();
338  Stage::GetCurrent().Add( root );
339  root.SetPosition( Vector3::ZERO );
342 
343  Actor bubbleMesh = root.GetChildAt( 0 );
344  Renderer renderer = bubbleMesh.GetRendererAt( 0 );
345  DALI_TEST_CHECK( renderer );
346 
347  TestGlAbstraction& gl = application.GetGlAbstraction();
348 
349  float percentageValue;
350  Vector4 startEndPosValue;
351 
352  Animation animation = Animation::New( 0.5f );
353  emitter.EmitBubble( animation, Vector2(40.f,40.f), Vector2(-5.f,-5.f), Vector2(30.f,30.f) );
354 
355  Wait(application);
356 
357  DALI_TEST_CHECK( gl.GetUniformValue<float>( "uPercentage[0]", percentageValue ) );
358  DALI_TEST_EQUALS( percentageValue, 0.f, TEST_LOCATION );
359 
360  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uStartEndPosition[0]", startEndPosValue ) );
361  DALI_TEST_EQUALS( startEndPosValue.x, 40.f, TEST_LOCATION );
362  DALI_TEST_EQUALS( startEndPosValue.y, 40.f, TEST_LOCATION );
363 
364  animation.Play();
365  Wait(application, 200);
366  animation.Clear();
367 
368  DALI_TEST_CHECK( gl.GetUniformValue<float>( "uPercentage[0]", percentageValue ) );
369  DALI_TEST_CHECK( percentageValue < 0.5f && percentageValue >= 0.4);
370 
371  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uStartEndPosition[0]", startEndPosValue ) );
372  DALI_TEST_EQUALS( startEndPosValue.x, 40.f, TEST_LOCATION );
373  DALI_TEST_EQUALS( startEndPosValue.y, 40.f, TEST_LOCATION );
374 
375  emitter.Restore();
376  application.SendNotification();
377  application.Render();
378 
379  DALI_TEST_CHECK( gl.GetUniformValue<float>( "uPercentage[0]", percentageValue ) );
380  DALI_TEST_EQUALS( percentageValue, 0.f, TEST_LOCATION );
381 
382  DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "uStartEndPosition[0]", startEndPosValue ) );
383  DALI_TEST_EQUALS( startEndPosValue, Vector4::ZERO, TEST_LOCATION );
384 
385  END_TEST;
386 }
Dali Docs Home
Read more about Dali