Add the logical key to Integration::KeyEvent
[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::Bitmap* bitmap = Integration::Bitmap::New( Integration::Bitmap::BITMAP_2D_PACKED_PIXELS, ResourcePolicy::OWNED_DISCARD );
106   Integration::ResourcePointer resource(bitmap);
107   bitmap->GetPackedPixelsProfile()->ReserveBuffer(Pixel::RGBA8888, width, height, width, height);
108 }
109
110 class SignalHandler : public Dali::ConnectionTracker
111 {
112 public:
113   SignalHandler() :
114     mCalls( 0 )
115   {
116   }
117
118   void Callback( SuperBlurView handle )
119   {
120     mCalls++;
121     tet_infoline( "Signal called" );
122   }
123
124   unsigned int GetCalls() const
125   {
126     return mCalls;
127   }
128
129 private:
130   unsigned int mCalls;  ///< Keeps track of how many times the signal has been called.
131 };
132
133 }//namespace
134
135
136 int UtcDaliSuperBlurViewNew(void)
137 {
138   ToolkitTestApplication application;
139
140   tet_infoline(" UtcDaliSuperBlurViewNew ");
141
142   // Test default constructor.
143   SuperBlurView blurView;
144   DALI_TEST_CHECK( !blurView );
145
146   // Test object creation
147   blurView = SuperBlurView::New( BLUR_LEVELS );
148   DALI_TEST_CHECK( blurView );
149
150   //Additional check to ensure object is created by checking if it's registered
151   ObjectRegistry registry = Stage::GetCurrent().GetObjectRegistry();
152   DALI_TEST_CHECK( registry );
153
154   gObjectCreatedCallBackCalled = false;
155   registry.ObjectCreatedSignal().Connect( &TestCallback );
156   {
157     SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
158   }
159   DALI_TEST_CHECK( gObjectCreatedCallBackCalled );
160
161   // Test copy constructor
162   SuperBlurView blurViewCopy2( blurView );
163   DALI_TEST_CHECK( blurViewCopy2 );
164
165   // Test down cast
166   Actor actorView;
167   actorView = blurView;
168   SuperBlurView downCastView = SuperBlurView::DownCast( actorView );
169   DALI_TEST_CHECK( downCastView );
170   END_TEST;
171 }
172
173 int UtcDaliSuperBlurViewCreate(void)
174 {
175   ToolkitTestApplication application;
176
177   tet_infoline(" UtcDaliSuperBlurViewNew ");
178
179   // Test default constructor.
180   SuperBlurView blurView;
181   DALI_TEST_CHECK( !blurView );
182
183   // Test object creation
184   TypeInfo type = TypeRegistry::Get().GetTypeInfo("SuperBlurView");
185   if( type )
186   {
187     Dali::BaseHandle handle = type.CreateInstance();
188     if( handle )
189     {
190       blurView = Dali::Toolkit::SuperBlurView::DownCast( handle );
191     }
192   }
193
194   DALI_TEST_CHECK( blurView );
195
196   END_TEST;
197 }
198
199
200 int UtcDaliSuperBlurViewSetImage(void)
201 {
202   ToolkitTestApplication application;
203
204   tet_infoline(" UtcDaliSuperBlurViewSetImage ");
205
206   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
207   blurView.SetSize( 100.f, 100.f );
208
209   Image inputImage = CreateSolidColorImage( application, Color::GREEN, 50, 50 );
210   blurView.SetImage( inputImage );
211   // start multiple guassian blur call, each guassian blur creates two render tasks
212   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
213
214   {
215     // create image renderers for the original image and each blurred image
216     Stage::GetCurrent().Add( blurView );
217     Wait(application);
218     DALI_TEST_EQUALS(blurView.GetRendererCount(), BLUR_LEVELS+1, TEST_LOCATION );
219
220     Wait(application);
221     Stage::GetCurrent().Remove( blurView );
222   }
223
224   END_TEST;
225 }
226
227 int UtcDaliSuperBlurViewSetImage2(void)
228 {
229   ToolkitTestApplication application;
230   Stage stage = Stage::GetCurrent();
231
232   tet_infoline(" UtcDaliSuperBlurViewSetImage2 - test setting a second image ");
233
234   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
235   blurView.SetSize( 100.f, 100.f );
236
237   tet_infoline("Call SetImage and add blurview to stage");
238   Image inputImage = CreateSolidColorImage( application, Color::GREEN, 50, 50 );
239   blurView.SetImage( inputImage );
240
241   // start multiple guassian blur call, each guassian blur creates two render tasks
242   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
243   {
244     // create image renderers for the original image and each blurred image
245     stage.Add( blurView );
246     Wait(application);
247     DALI_TEST_EQUALS(blurView.GetRendererCount(), BLUR_LEVELS+1, TEST_LOCATION );
248
249     tet_infoline("Wait for a second to allow blur to finish");
250     Wait(application, 1000);
251
252     tet_infoline("Remove from stage");
253     Stage::GetCurrent().Remove( blurView );
254   }
255
256   tet_infoline("Test that there are no render tasks remaining");
257   DALI_TEST_EQUALS(blurView.GetRendererCount(), 0, TEST_LOCATION );
258
259   tet_infoline("Call SetImage a second time and add blurview back to stage");
260   Image inputImage2 = CreateSolidColorImage( application, Color::CYAN, 50, 50 );
261   blurView.SetImage( inputImage2 );
262   // start multiple guassian blur call, each guassian blur creates two render tasks
263   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
264
265   {
266     // create image renderers for the original image and each blurred image
267     Stage::GetCurrent().Add( blurView );
268     Wait(application);
269     DALI_TEST_EQUALS(blurView.GetRendererCount(), BLUR_LEVELS+1, TEST_LOCATION );
270
271     tet_infoline("Wait for a second to allow blur to finish");
272     Wait(application, 1000);
273
274     tet_infoline("Remove from stage");
275     Stage::GetCurrent().Remove( blurView );
276   }
277
278   tet_infoline("Test that there are no render tasks remaining");
279   DALI_TEST_EQUALS(blurView.GetRendererCount(), 0, TEST_LOCATION );
280
281   END_TEST;
282 }
283
284
285 int UtcDaliSuperBlurViewSetProperty(void)
286 {
287   ToolkitTestApplication application;
288
289   tet_infoline(" UtcDaliSuperBlurViewSetProperty ");
290
291   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
292   // create image renderers for the original image and each blurred image
293   Stage::GetCurrent().Add( blurView );
294   blurView.SetSize( 100.f, 100.f );
295
296   tet_infoline(" Set property map. Set height and width large enough to avoid atlassing");
297   int width(512);
298   int height(513);
299   LoadBitmapResource( application.GetPlatform(), width, height );
300
301   Property::Map propertyMap;
302   propertyMap["filename"] = TEST_IMAGE_FILE_NAME ;
303   propertyMap["width"] = width;
304   propertyMap["height"] = height;
305
306   // Will create ResourceImage
307   blurView.SetProperty(SuperBlurView::Property::IMAGE, propertyMap);
308   Wait(application);
309
310   // start multiple guassian blur call, each guassian blur creates two render tasks
311   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
312
313   Wait(application);
314
315   END_TEST;
316 }
317
318
319 int UtcDaliSuperBlurViewGetProperty(void)
320 {
321   ToolkitTestApplication application;
322
323   tet_infoline(" UtcDaliSuperBlurViewSetProperty ");
324
325   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
326   blurView.SetSize( 100.f, 100.f );
327
328   tet_infoline(" Set property map.");
329   int width(512);
330   int height(513); // Value large enough to avoid future atlassing
331   LoadBitmapResource( application.GetPlatform(), width, height );
332
333   Property::Map propertyMap;
334   propertyMap["filename"] = TEST_IMAGE_FILE_NAME ;
335   propertyMap["width"] = width;
336   propertyMap["height"] = height;
337
338   // Will create ResourceImage
339   blurView.SetProperty(SuperBlurView::Property::IMAGE, propertyMap);
340   Wait(application);
341
342   // create image renderers for the original image and each blurred image
343   Stage::GetCurrent().Add( blurView );
344
345   Property::Value imageProperty = blurView.GetProperty(SuperBlurView::Property::IMAGE);
346   Property::Map* map = imageProperty.GetMap();
347   DALI_TEST_CHECK( map != NULL );
348   if( map )
349   {
350     Property::Map& mapRef = *map;
351     DALI_TEST_EQUALS( mapRef["filename"], TEST_IMAGE_FILE_NAME, TEST_LOCATION );
352   }
353
354   END_TEST;
355 }
356
357
358 int UtcDaliSuperBlurViewSetGetBlurStrength(void)
359 {
360   ToolkitTestApplication application;
361
362   tet_infoline(" UtcDaliSuperBlurViewSetGetBlurStrength ");
363
364   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
365   DALI_TEST_EQUALS(blurView.GetCurrentBlurStrength(), 0.f, TEST_LOCATION );
366
367   blurView.SetBlurStrength( 0.65f );
368   Wait(application);
369   DALI_TEST_EQUALS(blurView.GetCurrentBlurStrength(), 0.65f, TEST_LOCATION );
370   END_TEST;
371 }
372
373 int UtcDaliSuperBlurViewGetBlurStrengthPropertyIndex(void)
374 {
375   ToolkitTestApplication application;
376
377   tet_infoline(" UtcDaliSuperBlurViewGetBlurStrengthPropertyIndex ");
378
379   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
380   Property::Index blurPropertyIdx = blurView.GetBlurStrengthPropertyIndex();
381
382   float blurStrength;
383   (blurView.GetProperty( blurPropertyIdx )).Get(blurStrength);
384   DALI_TEST_EQUALS(blurStrength, 0.f, TEST_LOCATION );
385
386   blurView.SetBlurStrength( 0.65f );
387   Wait(application);
388   (blurView.GetProperty( blurPropertyIdx )).Get(blurStrength);
389   DALI_TEST_EQUALS(blurStrength, 0.65f, TEST_LOCATION );
390   END_TEST;
391 }
392
393 int UtcDaliSuperBlurViewGetBlurredImage(void)
394 {
395   ToolkitTestApplication application;
396
397   tet_infoline( "UtcDaliSuperBlurViewGetBlurredImage" );
398
399   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
400   blurView.SetSize( 100.f,100.f );
401   Image inputImage = CreateSolidColorImage( application, Color::GREEN, 100, 100 );
402   blurView.SetImage( inputImage );
403
404   Wait(application, 200); // Make sure all the gaussian blur finished
405
406   Image image1 = blurView.GetBlurredImage( 1 );
407   DALI_TEST_CHECK( image1 );
408
409   Image image2 = blurView.GetBlurredImage( 2 );
410   DALI_TEST_EQUALS( image2.GetWidth(), 25u, TEST_LOCATION );
411   DALI_TEST_EQUALS( image2.GetHeight(), 25u, TEST_LOCATION );
412
413   Image image3 = blurView.GetBlurredImage( 3 );
414   DALI_TEST_CHECK( FrameBufferImage::DownCast( image3 ) );
415
416   END_TEST;
417 }
418
419 int UtcDaliSuperBlurViewBlurSignal(void)
420 {
421   ToolkitTestApplication application;
422
423   tet_infoline(" UtcDaliSuperBlurViewSignal ");
424
425   SuperBlurView blurView = SuperBlurView::New( BLUR_LEVELS );
426   blurView.SetSize( 100.f, 100.f );
427
428   Image inputImage = CreateSolidColorImage( application, Color::GREEN, 50, 50 );
429   blurView.SetImage( inputImage );
430   // start multiple guassian blur call, each guassian blur creates two render tasks
431   DALI_TEST_CHECK( Stage::GetCurrent().GetRenderTaskList().GetTaskCount() == 1+BLUR_LEVELS*2);
432
433   SignalHandler signalHandler;
434   blurView.BlurFinishedSignal().Connect(&signalHandler, &SignalHandler::Callback);
435
436   // create image renderers for the original image and each blurred image
437   Stage::GetCurrent().Add( blurView );
438   Wait(application, 1000);
439
440   DALI_TEST_EQUALS(blurView.GetRendererCount(), BLUR_LEVELS+1, TEST_LOCATION );
441   //DALI_TEST_EQUALS(signalHandler.GetCalls(), 1, TEST_LOCATION);
442
443   END_TEST;
444 }