Merge "Update gbs build makefile for node addon" into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageView.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 // Need to override adaptor classes for toolkit test harness, so include
19 // test harness headers before dali headers.
20 #include <dali-toolkit-test-suite-utils.h>
21
22 #include <dali-toolkit/dali-toolkit.h>
23 #include <dali/devel-api/scripting/scripting.h>
24 #include <dali/devel-api/rendering/material.h>
25 #include <dali/devel-api/rendering/renderer.h>
26
27 using namespace Dali;
28 using namespace Toolkit;
29
30 void utc_dali_toolkit_image_view_startup(void)
31 {
32   test_return_value = TET_UNDEF;
33 }
34
35 void utc_dali_toolkit_image_view_cleanup(void)
36 {
37   test_return_value = TET_PASS;
38 }
39
40 namespace
41 {
42 const char* TEST_IMAGE_FILE_NAME =  "gallery_image_01.jpg";
43 const char* TEST_IMAGE_FILE_NAME2 =  "gallery_image_02.jpg";
44
45 void TestImage( ImageView imageView, BufferImage image )
46 {
47   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
48
49   Property::Map map;
50   DALI_TEST_CHECK( value.Get( map ) );
51
52   DALI_TEST_CHECK( map.Find( "width" ) );
53   DALI_TEST_CHECK( map.Find( "height" ) );
54   DALI_TEST_CHECK( map.Find( "type" ) );
55
56   int width = 0;
57   DALI_TEST_CHECK( map[ "width" ].Get( width ) );
58   DALI_TEST_EQUALS( (unsigned int)width, image.GetWidth(), TEST_LOCATION );
59
60   int height = 0;
61   DALI_TEST_CHECK( map[ "height" ].Get( height ) );
62   DALI_TEST_EQUALS( (unsigned int)height, image.GetHeight(), TEST_LOCATION );
63
64   std::string type;
65   DALI_TEST_CHECK( map[ "type" ].Get( type ) );
66   DALI_TEST_EQUALS( type, "BufferImage", TEST_LOCATION );
67 }
68
69 void TestImage( ImageView imageView, ResourceImage image )
70 {
71   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
72
73   Property::Map map;
74   DALI_TEST_CHECK( value.Get( map ) );
75
76   if( map.Find( "width" ) )
77   {
78     int width = 0;
79     DALI_TEST_CHECK( map[ "width" ].Get( width ) );
80     DALI_TEST_EQUALS( (unsigned int)width, image.GetWidth(), TEST_LOCATION );
81   }
82
83   if( map.Find( "height" ) )
84   {
85     int height = 0;
86     DALI_TEST_CHECK( map[ "height" ].Get( height ) );
87     DALI_TEST_EQUALS( (unsigned int)height, image.GetHeight(), TEST_LOCATION );
88   }
89
90   DALI_TEST_CHECK( map.Find( "type" ) );
91
92   std::string type;
93   DALI_TEST_CHECK( map[ "type" ].Get( type ) );
94   DALI_TEST_EQUALS( type, "ResourceImage", TEST_LOCATION );
95
96   std::string filename;
97   DALI_TEST_CHECK( map[ "filename" ].Get( filename ) );
98   DALI_TEST_EQUALS( filename, image.GetUrl(), TEST_LOCATION );
99 }
100
101 void TestUrl( ImageView imageView, const std::string url )
102 {
103   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
104
105   std::string urlActual;
106   DALI_TEST_CHECK( value.Get( urlActual ) );
107   DALI_TEST_EQUALS( urlActual, url, TEST_LOCATION );
108 }
109
110 } // namespace
111
112 int UtcDaliImageViewNewP(void)
113 {
114   TestApplication application;
115
116   ImageView imageView = ImageView::New();
117
118   DALI_TEST_CHECK( imageView );
119
120   END_TEST;
121 }
122
123 int UtcDaliImageViewNewImageP(void)
124 {
125   TestApplication application;
126
127   BufferImage image = CreateBufferImage( 100, 200, Vector4( 1.f, 1.f, 1.f, 1.f ) );
128   ImageView imageView = ImageView::New( image );
129
130   DALI_TEST_CHECK( imageView );
131   TestImage( imageView, image );
132
133   END_TEST;
134 }
135
136 int UtcDaliImageViewNewUrlP(void)
137 {
138   TestApplication application;
139
140   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
141   DALI_TEST_CHECK( imageView );
142
143   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
144
145   END_TEST;
146 }
147
148 int UtcDaliImageViewConstructorP(void)
149 {
150   TestApplication application;
151
152   ImageView imageView;
153
154   DALI_TEST_CHECK( !imageView );
155
156   END_TEST;
157 }
158
159 int UtcDaliImageViewCopyConstructorP(void)
160 {
161   TestApplication application;
162
163   // Initialize an object, ref count == 1
164   ImageView imageView = ImageView::New();
165
166   ImageView copy( imageView );
167   DALI_TEST_CHECK( copy );
168
169   END_TEST;
170 }
171
172 int UtcDaliImageViewAssignmentOperatorP(void)
173 {
174   TestApplication application;
175
176   ImageView imageView = ImageView::New();
177
178   ImageView copy( imageView );
179   DALI_TEST_CHECK( copy );
180   DALI_TEST_EQUALS( imageView, copy, TEST_LOCATION );
181
182   END_TEST;
183 }
184
185 int UtcDaliImageViewDownCastP(void)
186 {
187   TestApplication application;
188
189   ImageView imageView = ImageView::New();
190
191   BaseHandle object(imageView);
192
193   ImageView imageView2 = ImageView::DownCast( object );
194   DALI_TEST_CHECK(imageView2);
195
196   ImageView imageView3 = DownCast< ImageView >( object );
197   DALI_TEST_CHECK(imageView3);
198
199   END_TEST;
200 }
201
202 int UtcDaliImageViewDownCastN(void)
203 {
204   TestApplication application;
205
206   BaseHandle unInitializedObject;
207
208   ImageView imageView1 = ImageView::DownCast( unInitializedObject );
209   DALI_TEST_CHECK( !imageView1 );
210
211   ImageView imageView2 = DownCast< ImageView >( unInitializedObject );
212   DALI_TEST_CHECK( !imageView2 );
213
214   END_TEST;
215 }
216
217 int UtcDaliImageViewTypeRegistry(void)
218 {
219   ToolkitTestApplication application;
220
221   TypeRegistry typeRegistry = TypeRegistry::Get();
222   DALI_TEST_CHECK( typeRegistry );
223
224   TypeInfo typeInfo = typeRegistry.GetTypeInfo( "ImageView" );
225   DALI_TEST_CHECK( typeInfo );
226
227   BaseHandle handle = typeInfo.CreateInstance();
228   DALI_TEST_CHECK( handle );
229
230   ImageView imageView = ImageView::DownCast( handle );
231   DALI_TEST_CHECK( imageView );
232
233   END_TEST;
234 }
235
236 int UtcDaliImageViewSetGetProperty01(void)
237 {
238   ToolkitTestApplication application;
239
240   ImageView imageView = ImageView::New();
241
242   Property::Index idx = imageView.GetPropertyIndex( "image" );
243   DALI_TEST_EQUALS( idx, (Property::Index)ImageView::Property::IMAGE, TEST_LOCATION );
244
245   imageView.SetProperty( idx, TEST_IMAGE_FILE_NAME );
246   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
247
248   END_TEST;
249 }
250
251 int UtcDaliImageViewSetGetProperty02(void)
252 {
253   ToolkitTestApplication application;
254
255   Image image = CreateBufferImage( 10, 10, Color::WHITE );
256   ImageView imageView = ImageView::New(image);
257   Vector4 fullImageRect( 0.f, 0.f, 1.f, 1.f );
258
259   Stage::GetCurrent().Add( imageView );
260
261   application.SendNotification();
262   application.Render();
263   TestGlAbstraction& gl = application.GetGlAbstraction();
264
265   Vector4 pixelAreaUniform;
266   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
267   DALI_TEST_EQUALS( pixelAreaUniform, fullImageRect, TEST_LOCATION );
268
269   Property::Value value = imageView.GetProperty( ImageView::Property::PIXEL_AREA );
270   Vector4 pixelAreaValue;
271   DALI_TEST_CHECK( value.Get(pixelAreaValue) );
272   DALI_TEST_EQUALS( pixelAreaValue, fullImageRect, TEST_LOCATION );
273
274   Vector4 pixelAreaSet( 0.2f, 0.2f, 0.3f, 0.3f );
275   imageView.SetProperty( ImageView::Property::PIXEL_AREA, pixelAreaSet);
276
277   application.SendNotification();
278   application.Render();
279
280   value = imageView.GetProperty( ImageView::Property::PIXEL_AREA );
281   value.Get(pixelAreaValue);
282   DALI_TEST_EQUALS( pixelAreaValue, pixelAreaSet, TEST_LOCATION );
283
284   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
285   DALI_TEST_EQUALS( pixelAreaUniform, pixelAreaSet, TEST_LOCATION );
286
287   END_TEST;
288 }
289
290 int UtcDaliImageViewSetGetProperty03(void)
291 {
292   ToolkitTestApplication application;
293
294   Image image = CreateBufferImage( 10, 10, Color::WHITE );
295   ImageView imageView = ImageView::New(image);
296   Stage::GetCurrent().Add( imageView );
297   application.SendNotification();
298   application.Render();
299
300  // conventional alpha blending
301   Material material = imageView.GetRendererAt( 0 ).GetMaterial();
302   BlendingFactor::Type srcFactorRgb;
303   BlendingFactor::Type destFactorRgb;
304   BlendingFactor::Type srcFactorAlpha;
305   BlendingFactor::Type destFactorAlpha;
306   material.GetBlendFunc(srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha);
307   DALI_TEST_CHECK( srcFactorRgb == BlendingFactor::SRC_ALPHA );
308   DALI_TEST_CHECK( destFactorRgb == BlendingFactor::ONE_MINUS_SRC_ALPHA );
309   DALI_TEST_CHECK( srcFactorAlpha == BlendingFactor::ONE );
310   DALI_TEST_CHECK( destFactorAlpha == BlendingFactor::ONE_MINUS_SRC_ALPHA );
311
312   TestGlAbstraction& gl = application.GetGlAbstraction();
313
314   float alphaBlendingUniform;
315   DALI_TEST_CHECK( gl.GetUniformValue<float>( "uAlphaBlending", alphaBlendingUniform ) );
316   DALI_TEST_EQUALS( alphaBlendingUniform, 1.f, TEST_LOCATION );
317
318   // pre-multiplied alpha blending
319   imageView.SetProperty( Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true );
320   application.SendNotification();
321   application.Render();
322
323   material.GetBlendFunc(srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha);
324   DALI_TEST_CHECK( srcFactorRgb == BlendingFactor::ONE );
325   DALI_TEST_CHECK( destFactorRgb == BlendingFactor::ONE_MINUS_SRC_ALPHA );
326   DALI_TEST_CHECK( srcFactorAlpha == BlendingFactor::ONE );
327   DALI_TEST_CHECK( destFactorAlpha == BlendingFactor::ONE );
328
329   DALI_TEST_CHECK( gl.GetUniformValue<float>( "uAlphaBlending", alphaBlendingUniform ) );
330   DALI_TEST_EQUALS( alphaBlendingUniform, 0.f, TEST_LOCATION );
331
332   END_TEST;
333 }
334
335 int UtcDaliImageViewSizeWithBackground(void)
336 {
337   ToolkitTestApplication application;
338
339   int width = 100;
340   int height = 200;
341   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
342   ImageView imageView = ImageView::New();
343   imageView.SetBackgroundImage( image );
344
345   Stage::GetCurrent().Add( imageView );
346   application.SendNotification();
347   application.Render();
348
349   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
350   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
351
352   END_TEST;
353 }
354
355 int UtcDaliImageViewSizeWithBackgroundAndImage(void)
356 {
357   ToolkitTestApplication application;
358
359   int widthBackground = 100;
360   int heightBackground = 200;
361   int width = 300;
362   int height = 400;
363   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
364   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
365
366   ImageView imageView = ImageView::New();
367   imageView.SetBackgroundImage( imageBackground );
368   imageView.SetImage( image );
369
370   Stage::GetCurrent().Add( imageView );
371   application.SendNotification();
372   application.Render();
373
374   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
375   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
376
377   END_TEST;
378 }
379
380 int UtcDaliImageViewHeightForWidthBackground(void)
381 {
382   ToolkitTestApplication application;
383
384   int widthBackground = 100;
385   int heightBackground = 200;
386   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
387
388   ImageView imageView = ImageView::New();
389   imageView.SetBackgroundImage( imageBackground );
390
391   Stage::GetCurrent().Add( imageView );
392   application.SendNotification();
393   application.Render();
394
395   Control control = Control::DownCast( imageView );
396   DALI_TEST_CHECK( control );
397   DALI_TEST_EQUALS( imageView.GetHeightForWidth( 123.f ), control.GetHeightForWidth( 123.f ), TEST_LOCATION );
398   DALI_TEST_EQUALS( imageView.GetWidthForHeight( 321.f ), control.GetWidthForHeight( 321.f ), TEST_LOCATION );
399
400   END_TEST;
401 }
402
403 int UtcDaliImageViewHeightForWidthBackgroundAndImage(void)
404 {
405   ToolkitTestApplication application;
406
407   int widthBackground = 100;
408   int heightBackground = 200;
409   int width = 300;
410   int height = 400;
411   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
412   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
413
414   ImageView imageView = ImageView::New();
415   imageView.SetBackgroundImage( imageBackground );
416   imageView.SetImage( image );
417
418   Stage::GetCurrent().Add( imageView );
419   application.SendNotification();
420   application.Render();
421
422   DALI_TEST_EQUALS( imageView.GetHeightForWidth( width ), (float)height, TEST_LOCATION );
423   DALI_TEST_EQUALS( imageView.GetWidthForHeight( height ), (float)width, TEST_LOCATION );
424
425   END_TEST;
426 }
427
428 int UtcDaliImageViewSetBufferImage(void)
429 {
430   ToolkitTestApplication application;
431
432   int width1 = 300;
433   int height1 = 400;
434   BufferImage image1 = CreateBufferImage( width1, height1, Vector4( 1.f, 1.f, 1.f, 1.f ) );
435   ImageView imageView = ImageView::New();
436   imageView.SetImage( image1 );
437
438   TestImage( imageView, image1 );
439
440   int width2 = 600;
441   int height2 = 500;
442   BufferImage image2 = CreateBufferImage( width2, height2, Vector4( 1.f, 1.f, 1.f, 1.f ) );
443   imageView.SetImage( image2 );
444
445   TestImage( imageView, image2 );
446
447   END_TEST;
448 }
449
450 int UtcDaliImageViewSetImageUrl(void)
451 {
452   ToolkitTestApplication application;
453
454   ImageView imageView = ImageView::New();
455   imageView.SetImage( TEST_IMAGE_FILE_NAME );
456   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
457
458
459   imageView.SetImage( TEST_IMAGE_FILE_NAME2 );
460   TestUrl( imageView, TEST_IMAGE_FILE_NAME2 );
461
462   END_TEST;
463 }
464
465 int UtcDaliImageViewSetImageOnstageP(void)
466 {
467   ToolkitTestApplication application;
468
469   ImageView imageView = ImageView::New();
470
471   Stage::GetCurrent().Add( imageView );
472   application.SendNotification();
473   application.Render();
474
475   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
476   imageView.SetImage( image1 );
477   TestImage( imageView, image1 );
478
479   int width = 300;
480   int height = 400;
481   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
482   imageView.SetImage( image2 );
483   TestImage( imageView, image2 );
484
485   END_TEST;
486 }
487
488 int UtcDaliImageViewSetImageOnstageN(void)
489 {
490   ToolkitTestApplication application;
491
492   ImageView imageView = ImageView::New();
493
494   Stage::GetCurrent().Add( imageView );
495   application.SendNotification();
496   application.Render();
497
498   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
499   imageView.SetImage( image1 );
500   TestImage( imageView, image1 );
501
502   Image image2;
503   imageView.SetImage( image2 );
504
505   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
506
507   //the value should be empty
508   std::string url;
509   DALI_TEST_CHECK( !value.Get( url ) );
510
511   Property::Map map;
512   DALI_TEST_CHECK( !value.Get( map ) );
513
514   END_TEST;
515 }
516
517 int UtcDaliImageViewSetImageOffstageP(void)
518 {
519   ToolkitTestApplication application;
520
521   ImageView imageView = ImageView::New();
522
523   Stage::GetCurrent().Add( imageView );
524   application.SendNotification();
525   application.Render();
526   Stage::GetCurrent().Remove( imageView );
527
528   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
529   imageView.SetImage( image1 );
530   TestImage( imageView, image1 );
531
532   int width = 300;
533   int height = 400;
534   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
535   imageView.SetImage( image2 );
536   TestImage( imageView, image2 );
537
538   END_TEST;
539 }
540
541 int UtcDaliImageViewSetImageOffstageN(void)
542 {
543   ToolkitTestApplication application;
544
545   ImageView imageView = ImageView::New();
546
547   Stage::GetCurrent().Add( imageView );
548   application.SendNotification();
549   application.Render();
550   Stage::GetCurrent().Remove( imageView );
551
552   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
553   imageView.SetImage( image1 );
554   TestImage( imageView, image1 );
555
556   Image image2;
557   imageView.SetImage( image2 );
558
559   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
560
561   //the value should be empty
562   std::string url;
563   DALI_TEST_CHECK( !value.Get( url ) );
564
565   Property::Map map;
566   DALI_TEST_CHECK( !value.Get( map ) );
567
568   END_TEST;
569 }
570
571 int UtcDaliImageViewSetImageN(void)
572 {
573   ToolkitTestApplication application;
574
575   Image image1;
576   ImageView imageView = ImageView::New();
577   imageView.SetImage( image1 );
578
579   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
580
581   //the value should be empty
582   std::string url;
583   DALI_TEST_CHECK( !value.Get( url ) );
584
585   Property::Map map;
586   DALI_TEST_CHECK( !value.Get( map ) );
587
588   std::string resource_url;
589   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
590   DALI_TEST_CHECK( !val.Get( resource_url ) );
591
592   END_TEST;
593 }
594
595 int UtcDaliImageViewSetImageTypeChangesP(void)
596 {
597   ToolkitTestApplication application;
598
599   ImageView imageView = ImageView::New();
600
601
602   std::string url;
603   Property::Map map;
604
605   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
606   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
607   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
608
609   // Set a URL
610   imageView.SetImage( "TEST_URL" );
611   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
612
613   DALI_TEST_CHECK( value.Get( url ) );   // Value should NOT be empty
614   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
615
616   // Set an empty Image
617   imageView.SetImage( Image() );
618   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
619
620   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
621   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
622
623   // Set an Image
624   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
625   imageView.SetImage( image1 );
626   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
627
628   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
629   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
630
631   // Set an empty URL
632   imageView.SetImage( "" );
633   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
634
635   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
636   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
637
638   END_TEST;
639 }
640
641 int UtcDaliImageViewResourceUrlP(void)
642 {
643   ToolkitTestApplication application;
644
645   ImageView imageView = ImageView::New();
646   DALI_TEST_CHECK( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >().empty() );
647
648   imageView.SetProperty( ImageView::Property::RESOURCE_URL, "TestString" );
649   DALI_TEST_EQUALS( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >(), "TestString", TEST_LOCATION );
650
651   END_TEST;
652 }