(AutomatedTests) Ensure warnings are shown as errors
[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 UtcDaliImageViewSizeWithBackground(void)
250 {
251   ToolkitTestApplication application;
252
253   int width = 100;
254   int height = 200;
255   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
256   ImageView imageView = ImageView::New();
257   imageView.SetBackgroundImage( image );
258
259   Stage::GetCurrent().Add( imageView );
260   application.SendNotification();
261   application.Render();
262
263   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
264   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
265
266   END_TEST;
267 }
268
269 int UtcDaliImageViewSizeWithBackgroundAndImage(void)
270 {
271   ToolkitTestApplication application;
272
273   int widthBackground = 100;
274   int heightBackground = 200;
275   int width = 300;
276   int height = 400;
277   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
278   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
279
280   ImageView imageView = ImageView::New();
281   imageView.SetBackgroundImage( imageBackground );
282   imageView.SetImage( image );
283
284   Stage::GetCurrent().Add( imageView );
285   application.SendNotification();
286   application.Render();
287
288   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
289   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
290
291   END_TEST;
292 }
293
294 int UtcDaliImageViewHeightForWidthBackground(void)
295 {
296   ToolkitTestApplication application;
297
298   int widthBackground = 100;
299   int heightBackground = 200;
300   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
301
302   ImageView imageView = ImageView::New();
303   imageView.SetBackgroundImage( imageBackground );
304
305   Stage::GetCurrent().Add( imageView );
306   application.SendNotification();
307   application.Render();
308
309   Control control = Control::DownCast( imageView );
310   DALI_TEST_CHECK( control );
311   DALI_TEST_EQUALS( imageView.GetHeightForWidth( 123.f ), control.GetHeightForWidth( 123.f ), TEST_LOCATION );
312   DALI_TEST_EQUALS( imageView.GetWidthForHeight( 321.f ), control.GetWidthForHeight( 321.f ), TEST_LOCATION );
313
314   END_TEST;
315 }
316
317 int UtcDaliImageViewHeightForWidthBackgroundAndImage(void)
318 {
319   ToolkitTestApplication application;
320
321   int widthBackground = 100;
322   int heightBackground = 200;
323   int width = 300;
324   int height = 400;
325   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
326   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
327
328   ImageView imageView = ImageView::New();
329   imageView.SetBackgroundImage( imageBackground );
330   imageView.SetImage( image );
331
332   Stage::GetCurrent().Add( imageView );
333   application.SendNotification();
334   application.Render();
335
336   DALI_TEST_EQUALS( imageView.GetHeightForWidth( width ), (float)height, TEST_LOCATION );
337   DALI_TEST_EQUALS( imageView.GetWidthForHeight( height ), (float)width, TEST_LOCATION );
338
339   END_TEST;
340 }
341
342 int UtcDaliImageViewSetBufferImage(void)
343 {
344   ToolkitTestApplication application;
345
346   int width1 = 300;
347   int height1 = 400;
348   BufferImage image1 = CreateBufferImage( width1, height1, Vector4( 1.f, 1.f, 1.f, 1.f ) );
349   ImageView imageView = ImageView::New();
350   imageView.SetImage( image1 );
351
352   TestImage( imageView, image1 );
353
354   int width2 = 600;
355   int height2 = 500;
356   BufferImage image2 = CreateBufferImage( width2, height2, Vector4( 1.f, 1.f, 1.f, 1.f ) );
357   imageView.SetImage( image2 );
358
359   TestImage( imageView, image2 );
360
361   END_TEST;
362 }
363
364 int UtcDaliImageViewSetImageUrl(void)
365 {
366   ToolkitTestApplication application;
367
368   ImageView imageView = ImageView::New();
369   imageView.SetImage( TEST_IMAGE_FILE_NAME );
370   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
371
372
373   imageView.SetImage( TEST_IMAGE_FILE_NAME2 );
374   TestUrl( imageView, TEST_IMAGE_FILE_NAME2 );
375
376   END_TEST;
377 }
378
379 int UtcDaliImageViewSetImageOnstageP(void)
380 {
381   ToolkitTestApplication application;
382
383   ImageView imageView = ImageView::New();
384
385   Stage::GetCurrent().Add( imageView );
386   application.SendNotification();
387   application.Render();
388
389   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
390   imageView.SetImage( image1 );
391   TestImage( imageView, image1 );
392
393   int width = 300;
394   int height = 400;
395   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
396   imageView.SetImage( image2 );
397   TestImage( imageView, image2 );
398
399   END_TEST;
400 }
401
402 int UtcDaliImageViewSetImageOnstageN(void)
403 {
404   ToolkitTestApplication application;
405
406   ImageView imageView = ImageView::New();
407
408   Stage::GetCurrent().Add( imageView );
409   application.SendNotification();
410   application.Render();
411
412   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
413   imageView.SetImage( image1 );
414   TestImage( imageView, image1 );
415
416   Image image2;
417   imageView.SetImage( image2 );
418
419   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
420
421   //the value should be empty
422   std::string url;
423   DALI_TEST_CHECK( !value.Get( url ) );
424
425   Property::Map map;
426   DALI_TEST_CHECK( !value.Get( map ) );
427
428   END_TEST;
429 }
430
431 int UtcDaliImageViewSetImageOffstageP(void)
432 {
433   ToolkitTestApplication application;
434
435   ImageView imageView = ImageView::New();
436
437   Stage::GetCurrent().Add( imageView );
438   application.SendNotification();
439   application.Render();
440   Stage::GetCurrent().Remove( imageView );
441
442   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
443   imageView.SetImage( image1 );
444   TestImage( imageView, image1 );
445
446   int width = 300;
447   int height = 400;
448   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
449   imageView.SetImage( image2 );
450   TestImage( imageView, image2 );
451
452   END_TEST;
453 }
454
455 int UtcDaliImageViewSetImageOffstageN(void)
456 {
457   ToolkitTestApplication application;
458
459   ImageView imageView = ImageView::New();
460
461   Stage::GetCurrent().Add( imageView );
462   application.SendNotification();
463   application.Render();
464   Stage::GetCurrent().Remove( imageView );
465
466   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
467   imageView.SetImage( image1 );
468   TestImage( imageView, image1 );
469
470   Image image2;
471   imageView.SetImage( image2 );
472
473   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
474
475   //the value should be empty
476   std::string url;
477   DALI_TEST_CHECK( !value.Get( url ) );
478
479   Property::Map map;
480   DALI_TEST_CHECK( !value.Get( map ) );
481
482   END_TEST;
483 }
484
485 int UtcDaliImageViewSetImageN(void)
486 {
487   ToolkitTestApplication application;
488
489   Image image1;
490   ImageView imageView = ImageView::New();
491   imageView.SetImage( image1 );
492
493   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
494
495   //the value should be empty
496   std::string url;
497   DALI_TEST_CHECK( !value.Get( url ) );
498
499   Property::Map map;
500   DALI_TEST_CHECK( !value.Get( map ) );
501
502   std::string resource_url;
503   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
504   DALI_TEST_CHECK( !val.Get( resource_url ) );
505
506   END_TEST;
507 }
508
509 int UtcDaliImageViewSetImageTypeChangesP(void)
510 {
511   ToolkitTestApplication application;
512
513   ImageView imageView = ImageView::New();
514
515
516   std::string url;
517   Property::Map map;
518
519   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
520   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
521   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
522
523   // Set a URL
524   imageView.SetImage( "TEST_URL" );
525   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
526
527   DALI_TEST_CHECK( value.Get( url ) );   // Value should NOT be empty
528   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
529
530   // Set an empty Image
531   imageView.SetImage( Image() );
532   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
533
534   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
535   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
536
537   // Set an Image
538   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
539   imageView.SetImage( image1 );
540   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
541
542   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
543   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
544
545   // Set an empty URL
546   imageView.SetImage( "" );
547   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
548
549   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
550   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
551
552   END_TEST;
553 }