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