[dali_1.2.14] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-SuperBlurView.cpp
1 /*
2  * Copyright (c) 2016 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-toolkit/devel-api/controls/super-blur-view/super-blur-view.h>
28
29 using namespace Dali;
30 using namespace Dali::Toolkit;
31
32 void utc_dali_toolkit_super_blur_view_startup(void)
33 {
34   test_return_value = TET_UNDEF;
35 }
36
37 void utc_dali_toolkit_super_blur_view_cleanup(void)
38 {
39   test_return_value = TET_PASS;
40 }
41
42
43 namespace
44 {
45 const int BLUR_LEVELS = 3;
46 const int RENDER_FRAME_INTERVAL = 16;
47 const char* TEST_IMAGE_FILE_NAME("image.png");
48 static bool gObjectCreatedCallBackCalled;
49 static void TestCallback(BaseHandle handle)
50 {
51   gObjectCreatedCallBackCalled = true;
52 }
53
54 /*
55  * Simulate time passed by.
56  *
57  * @note this will always process at least 1 frame (1/60 sec)
58  *
59  * @param application Test application instance
60  * @param duration Time to pass in milliseconds.
61  * @return The actual time passed in milliseconds
62  */
63 int Wait(ToolkitTestApplication& application, int duration = 0)
64 {
65   int time = 0;
66
67   for(int i = 0; i <= ( duration / RENDER_FRAME_INTERVAL); i++)
68   {
69     application.SendNotification();
70     application.Render(RENDER_FRAME_INTERVAL);
71     time += RENDER_FRAME_INTERVAL;
72   }
73
74   return time;
75 }
76
77 Image CreateSolidColorImage( ToolkitTestApplication& application, const Vector4& color, unsigned int width, unsigned int height )
78 {
79   BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
80
81   // Create the image
82   PixelBuffer* pixbuf = imageData.GetBuffer();
83   unsigned int size = width * height;
84
85   for( size_t i = 0; i < size; i++ )
86     {
87       pixbuf[i*4+0] = 0xFF * color.r;
88       pixbuf[i*4+1] = 0xFF * color.g;
89       pixbuf[i*4+2] = 0xFF * color.b;
90       pixbuf[i*4+3] = 0xFF * color.a;
91     }
92   imageData.Update();
93
94   application.GetGlAbstraction().SetCheckFramebufferStatusResult(GL_FRAMEBUFFER_COMPLETE );
95   application.SendNotification();
96   application.Render(RENDER_FRAME_INTERVAL);
97   application.Render(RENDER_FRAME_INTERVAL);
98   application.SendNotification();
99
100   return imageData;
101 }
102
103 void LoadBitmapResource(TestPlatformAbstraction& platform, int width, int height)
104 {
105   Integration::ResourceRequest* request = platform.GetRequest();
106   Integration::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_DISCARD );
107   Integration::ResourcePointer resource(bitmap);
108   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, width, height, width, height);
109
110   if(request)
111   {
112     platform.SetResourceLoaded(request->GetId(), request->GetType()->id, resource);
113   }
114 }
115
116 class SignalHandler : public Dali::ConnectionTracker
117 {
118 public:
119   SignalHandler() :
120     mCalls( 0 )
121   {
122   }
123
124   void Callback( SuperBlurView handle )
125   {
126     mCalls++;
127     tet_infoline( "Signal called" );
128   }
129
130   unsigned int GetCalls() const
131   {
132     return mCalls;
133   }
134
135 private:
136   unsigned int mCalls;  ///< Keeps track of how many times the signal has been called.
137 };
138
139 }//namespace
140
141
142 int UtcDaliSuperBlurViewNew(void)
143 {
144   ToolkitTestApplication application;
145
146   tet_infoline(" UtcDaliSuperBlurViewNew ");
147
148   // Test default constructor.
149   SuperBlurView blurView;
150   DALI_TEST_CHECK( !blurView );
151
152   // Test object creation
153   blurView = SuperBlurView::New( BLUR_LEVELS );
154   DALI_TEST_CHECK( blurView );
155
156   //Additional check to ensure object is created by checking if it's registered
157   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
158   DALI_TEST_CHECK( registry );
159
160   gObjectCreatedCallBackCalled = false;
161   registry.ObjectCreatedSignal().Connect( &TestCallback );
162   {
163     SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
164   }
165   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
166
167   // Test copy constructor
168   SuperBlurView blurViewCopy2( blurView );
169   DALI_TEST_CHECK( blurViewCopy2 );
170
171   // Test down cast
172   Actor actorView;
173   actorView = blurView;
174   SuperBlurView downCastView = SuperBlurView::DownCast( actorView );
175   DALI_TEST_CHECK( downCastView );
176   END_TEST;
177 }
178
179 int UtcDaliSuperBlurViewCreate(void)
180 {
181   ToolkitTestApplication application;
182
183   tet_infoline(" UtcDaliSuperBlurViewNew ");
184
185   // Test default constructor.
186   SuperBlurView blurView;
187   DALI_TEST_CHECK( !blurView );
188
189   // Test object creation
190   TypeInfo type = TypeRegistry::Get().GetTypeInfo("SuperBlurView");
191   if( type )
192   {
193     Dali::BaseHandle handle = type.CreateInstance();
194     if( handle )
195     {
196       blurView = Dali::Toolkit::SuperBlurView::DownCast( handle );
197     }
198   }
199
200   DALI_TEST_CHECK( blurView );
201
202   END_TEST;
203 }
204
205
206 int UtcDaliSuperBlurViewSetImage(void)
207 {
208   ToolkitTestApplication application;
209
210   tet_infoline(" UtcDaliSuperBlurViewSetImage ");
211
212   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
213   blurView.SetSize( 100.f, 100.f );
214
215   Image inputImage = CreateSolidColorImage( application, Color::GREEN, 50, 50 );
216   blurView.SetImage( inputImage );
217   // start multiple guassian blur call, each guassian blur creates two render tasks
218   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
219
220   {
221     // create image renderers for the original image and each blurred image
222     Stage::GetCurrent().Add( blurView );
223     Wait(application);
224     DALI_TEST_EQUALS(blurView.GetRendererCount(), BLUR_LEVELS+1, TEST_LOCATION );
225
226     Wait(application);
227     Stage::GetCurrent().Remove( blurView );
228   }
229
230   END_TEST;
231 }
232
233 int UtcDaliSuperBlurViewSetImage2(void)
234 {
235   ToolkitTestApplication application;
236   Stage stage = Stage::GetCurrent();
237
238   tet_infoline(" UtcDaliSuperBlurViewSetImage2 - test setting a second image ");
239
240   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
241   blurView.SetSize( 100.f, 100.f );
242
243   tet_infoline("Call SetImage and add blurview to stage");
244   Image inputImage = CreateSolidColorImage( application, Color::GREEN, 50, 50 );
245   blurView.SetImage( inputImage );
246
247   // start multiple guassian blur call, each guassian blur creates two render tasks
248   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
249   {
250     // create image renderers for the original image and each blurred image
251     stage.Add( blurView );
252     Wait(application);
253     DALI_TEST_EQUALS(blurView.GetRendererCount(), BLUR_LEVELS+1, TEST_LOCATION );
254
255     tet_infoline("Wait for a second to allow blur to finish");
256     Wait(application, 1000);
257
258     tet_infoline("Remove from stage");
259     Stage::GetCurrent().Remove( blurView );
260   }
261
262   tet_infoline("Test that there are no render tasks remaining");
263   DALI_TEST_EQUALS(blurView.GetRendererCount(), 0, TEST_LOCATION );
264
265   tet_infoline("Call SetImage a second time and add blurview back to stage");
266   Image inputImage2 = CreateSolidColorImage( application, Color::CYAN, 50, 50 );
267   blurView.SetImage( inputImage2 );
268   // start multiple guassian blur call, each guassian blur creates two render tasks
269   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
270
271   {
272     // create image renderers for the original image and each blurred image
273     Stage::GetCurrent().Add( blurView );
274     Wait(application);
275     DALI_TEST_EQUALS(blurView.GetRendererCount(), BLUR_LEVELS+1, TEST_LOCATION );
276
277     tet_infoline("Wait for a second to allow blur to finish");
278     Wait(application, 1000);
279
280     tet_infoline("Remove from stage");
281     Stage::GetCurrent().Remove( blurView );
282   }
283
284   tet_infoline("Test that there are no render tasks remaining");
285   DALI_TEST_EQUALS(blurView.GetRendererCount(), 0, TEST_LOCATION );
286
287   END_TEST;
288 }
289
290
291 int UtcDaliSuperBlurViewSetProperty(void)
292 {
293   ToolkitTestApplication application;
294
295   tet_infoline(" UtcDaliSuperBlurViewSetProperty ");
296
297   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
298   // create image renderers for the original image and each blurred image
299   Stage::GetCurrent().Add( blurView );
300   blurView.SetSize( 100.f, 100.f );
301
302   tet_infoline(" Set property map. Set height and width large enough to avoid atlassing");
303   int width(512);
304   int height(513);
305   LoadBitmapResource( application.GetPlatform(), width, height );
306
307   Property::Map propertyMap;
308   propertyMap["filename"] = TEST_IMAGE_FILE_NAME ;
309   propertyMap["width"] = width;
310   propertyMap["height"] = height;
311
312   // Will create ResourceImage
313   blurView.SetProperty(SuperBlurView::Property::IMAGE, propertyMap);
314   Wait(application);
315
316   // start multiple guassian blur call, each guassian blur creates two render tasks
317   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
318
319   Wait(application);
320
321   END_TEST;
322 }
323
324
325 int UtcDaliSuperBlurViewGetProperty(void)
326 {
327   ToolkitTestApplication application;
328
329   tet_infoline(" UtcDaliSuperBlurViewSetProperty ");
330
331   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
332   blurView.SetSize( 100.f, 100.f );
333
334   tet_infoline(" Set property map.");
335   int width(512);
336   int height(513); // Value large enough to avoid future atlassing
337   LoadBitmapResource( application.GetPlatform(), width, height );
338
339   Property::Map propertyMap;
340   propertyMap["filename"] = TEST_IMAGE_FILE_NAME ;
341   propertyMap["width"] = width;
342   propertyMap["height"] = height;
343
344   // Will create ResourceImage
345   blurView.SetProperty(SuperBlurView::Property::IMAGE, propertyMap);
346   Wait(application);
347
348   // create image renderers for the original image and each blurred image
349   Stage::GetCurrent().Add( blurView );
350
351   Property::Value imageProperty = blurView.GetProperty(SuperBlurView::Property::IMAGE);
352   Property::Map* map = imageProperty.GetMap();
353   DALI_TEST_CHECK( map != NULL );
354   if( map )
355   {
356     Property::Map& mapRef = *map;
357     DALI_TEST_EQUALS( mapRef["filename"], TEST_IMAGE_FILE_NAME, TEST_LOCATION );
358   }
359
360   END_TEST;
361 }
362
363
364 int UtcDaliSuperBlurViewSetGetBlurStrength(void)
365 {
366   ToolkitTestApplication application;
367
368   tet_infoline(" UtcDaliSuperBlurViewSetGetBlurStrength ");
369
370   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
371   DALI_TEST_EQUALS(blurView.GetCurrentBlurStrength(), 0.f, TEST_LOCATION );
372
373   blurView.SetBlurStrength( 0.65f );
374   Wait(application);
375   DALI_TEST_EQUALS(blurView.GetCurrentBlurStrength(), 0.65f, TEST_LOCATION );
376   END_TEST;
377 }
378
379 int UtcDaliSuperBlurViewGetBlurStrengthPropertyIndex(void)
380 {
381   ToolkitTestApplication application;
382
383   tet_infoline(" UtcDaliSuperBlurViewGetBlurStrengthPropertyIndex ");
384
385   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
386   Property::Index blurPropertyIdx = blurView.GetBlurStrengthPropertyIndex();
387
388   float blurStrength;
389   (blurView.GetProperty( blurPropertyIdx )).Get(blurStrength);
390   DALI_TEST_EQUALS(blurStrength, 0.f, TEST_LOCATION );
391
392   blurView.SetBlurStrength( 0.65f );
393   Wait(application);
394   (blurView.GetProperty( blurPropertyIdx )).Get(blurStrength);
395   DALI_TEST_EQUALS(blurStrength, 0.65f, TEST_LOCATION );
396   END_TEST;
397 }
398
399 int UtcDaliSuperBlurViewGetBlurredImage(void)
400 {
401   ToolkitTestApplication application;
402
403   tet_infoline( "UtcDaliSuperBlurViewGetBlurredImage" );
404
405   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
406   blurView.SetSize( 100.f,100.f );
407   Image inputImage = CreateSolidColorImage( application, Color::GREEN, 100, 100 );
408   blurView.SetImage( inputImage );
409
410   Wait(application, 200); // Make sure all the gaussian blur finished
411
412   Image image1 = blurView.GetBlurredImage( 1 );
413   DALI_TEST_CHECK( image1 );
414
415   Image image2 = blurView.GetBlurredImage( 2 );
416   DALI_TEST_EQUALS( image2.GetWidth(), 25u, TEST_LOCATION );
417   DALI_TEST_EQUALS( image2.GetHeight(), 25u, TEST_LOCATION );
418
419   Image image3 = blurView.GetBlurredImage( 3 );
420   DALI_TEST_CHECK( FrameBufferImage::DownCast( image3 ) );
421
422   END_TEST;
423 }
424
425 int UtcDaliSuperBlurViewBlurSignal(void)
426 {
427   ToolkitTestApplication application;
428
429   tet_infoline(" UtcDaliSuperBlurViewSignal ");
430
431   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
432   blurView.SetSize( 100.f, 100.f );
433
434   Image inputImage = CreateSolidColorImage( application, Color::GREEN, 50, 50 );
435   blurView.SetImage( inputImage );
436   // start multiple guassian blur call, each guassian blur creates two render tasks
437   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
438
439   SignalHandler signalHandler;
440   blurView.BlurFinishedSignal().Connect(&signalHandler, &SignalHandler::Callback);
441
442   // create image renderers for the original image and each blurred image
443   Stage::GetCurrent().Add( blurView );
444   Wait(application, 1000);
445
446   DALI_TEST_EQUALS(blurView.GetRendererCount(), BLUR_LEVELS+1, TEST_LOCATION );
447   //DALI_TEST_EQUALS(signalHandler.GetCalls(), 1, TEST_LOCATION);
448
449   END_TEST;
450 }