Simple implementation of ImageView.
[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
24 using namespace Dali;
25 using namespace Toolkit;
26
27 void utc_dali_toolkit_image_view_startup(void)
28 {
29   test_return_value = TET_UNDEF;
30 }
31
32 void utc_dali_toolkit_image_view_cleanup(void)
33 {
34   test_return_value = TET_PASS;
35 }
36
37 namespace
38 {
39 const char* TEST_IMAGE_FILE_NAME =  "gallery_image_01.jpg";
40 } // namespace
41
42 int UtcDaliImageViewNewP(void)
43 {
44   TestApplication application;
45
46   ImageView imageView = ImageView::New();
47
48   DALI_TEST_CHECK( imageView );
49
50   END_TEST;
51 }
52
53 int UtcDaliImageViewNewImageP(void)
54 {
55   TestApplication application;
56
57   Image image = CreateBufferImage( 100, 200, Vector4( 1.f, 1.f, 1.f, 1.f ) );
58   ImageView imageView = ImageView::New( image );
59
60   DALI_TEST_CHECK( imageView );
61   DALI_TEST_EQUALS( image, imageView.GetImage(), TEST_LOCATION );
62
63   END_TEST;
64 }
65
66 int UtcDaliImageViewNewUrlP(void)
67 {
68   TestApplication application;
69
70   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
71   DALI_TEST_CHECK( imageView );
72   DALI_TEST_CHECK( imageView.GetImage() );
73
74   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "resource-url" ) );
75   std::string resource_url;
76   DALI_TEST_CHECK( val.Get( resource_url ) );
77   DALI_TEST_EQUALS( resource_url, TEST_IMAGE_FILE_NAME, TEST_LOCATION );
78
79   Image image = imageView.GetImage();
80   DALI_TEST_CHECK( image );
81
82   ResourceImage resourceImage = ResourceImage::DownCast( image );
83   DALI_TEST_CHECK( resourceImage );
84   DALI_TEST_EQUALS( resourceImage.GetUrl(), TEST_IMAGE_FILE_NAME, TEST_LOCATION );
85
86   END_TEST;
87 }
88
89 int UtcDaliImageViewConstructorP(void)
90 {
91   TestApplication application;
92
93   ImageView imageView;
94
95   DALI_TEST_CHECK( !imageView );
96
97   END_TEST;
98 }
99
100 int UtcDaliImageViewCopyConstructorP(void)
101 {
102   TestApplication application;
103
104   // Initialize an object, ref count == 1
105   ImageView imageView = ImageView::New();
106
107   ImageView copy( imageView );
108   DALI_TEST_CHECK( copy );
109
110   END_TEST;
111 }
112
113 int UtcDaliImageViewAssignmentOperatorP(void)
114 {
115   TestApplication application;
116
117   ImageView imageView = ImageView::New();
118
119   ImageView copy( imageView );
120   DALI_TEST_CHECK( copy );
121   DALI_TEST_EQUALS( imageView, copy, TEST_LOCATION );
122
123   END_TEST;
124 }
125
126 int UtcDaliImageViewDownCastP(void)
127 {
128   TestApplication application;
129
130   ImageView imageView = ImageView::New();
131
132   BaseHandle object(imageView);
133
134   ImageView imageView2 = ImageView::DownCast( object );
135   DALI_TEST_CHECK(imageView2);
136
137   ImageView imageView3 = DownCast< ImageView >( object );
138   DALI_TEST_CHECK(imageView3);
139
140   END_TEST;
141 }
142
143 int UtcDaliImageViewDownCastN(void)
144 {
145   TestApplication application;
146
147   BaseHandle unInitializedObject;
148
149   ImageView imageView1 = ImageView::DownCast( unInitializedObject );
150   DALI_TEST_CHECK( !imageView1 );
151
152   ImageView imageView2 = DownCast< ImageView >( unInitializedObject );
153   DALI_TEST_CHECK( !imageView2 );
154
155   END_TEST;
156 }
157
158 int UtcDaliImageViewTypeRegistry(void)
159 {
160   ToolkitTestApplication application;
161
162   TypeRegistry typeRegistry = TypeRegistry::Get();
163   DALI_TEST_CHECK( typeRegistry );
164
165   TypeInfo typeInfo = typeRegistry.GetTypeInfo( "ImageView" );
166   DALI_TEST_CHECK( typeInfo );
167
168   BaseHandle handle = typeInfo.CreateInstance();
169   DALI_TEST_CHECK( handle );
170
171   ImageView imageView = ImageView::DownCast( handle );
172   DALI_TEST_CHECK( imageView );
173
174   END_TEST;
175 }
176
177 int UtcDaliImageViewSetGetProperty(void)
178 {
179   ToolkitTestApplication application;
180
181   ImageView imageView = ImageView::New();
182
183   Property::Index idx = imageView.GetPropertyIndex( "resource-url" );
184   DALI_TEST_EQUALS( idx, ImageView::Property::RESOURCE_URL, TEST_LOCATION );
185
186   imageView.SetProperty( idx, TEST_IMAGE_FILE_NAME );
187   Property::Value val = imageView.GetProperty( idx );
188   std::string resource_url;
189   DALI_TEST_CHECK( val.Get( resource_url ) );
190   DALI_TEST_EQUALS( resource_url, TEST_IMAGE_FILE_NAME, TEST_LOCATION );
191
192   Image image = imageView.GetImage();
193   DALI_TEST_CHECK( image );
194
195   ResourceImage resourceImage = ResourceImage::DownCast( image );
196   DALI_TEST_CHECK( resourceImage );
197   DALI_TEST_EQUALS( resourceImage.GetUrl(), TEST_IMAGE_FILE_NAME, TEST_LOCATION );
198
199   END_TEST;
200 }
201
202 int UtcDaliImageViewSizeWithBackground(void)
203 {
204   ToolkitTestApplication application;
205
206   int width = 100;
207   int height = 200;
208   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
209   ImageView imageView = ImageView::New();
210   imageView.SetBackgroundImage( image );
211
212   Stage::GetCurrent().Add( imageView );
213   application.SendNotification();
214   application.Render();
215
216   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, width, TEST_LOCATION );
217   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, height, TEST_LOCATION );
218
219   END_TEST;
220 }
221
222 int UtcDaliImageViewSizeWithBackgroundAndImage(void)
223 {
224   ToolkitTestApplication application;
225
226   int widthBackground = 100;
227   int heightBackground = 200;
228   int width = 300;
229   int height = 400;
230   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
231   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
232
233   ImageView imageView = ImageView::New();
234   imageView.SetBackgroundImage( imageBackground );
235   imageView.SetImage( image );
236
237   Stage::GetCurrent().Add( imageView );
238   application.SendNotification();
239   application.Render();
240
241   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, width, TEST_LOCATION );
242   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, height, TEST_LOCATION );
243
244   END_TEST;
245 }
246
247 int UtcDaliImageViewHeightForWidthBackground(void)
248 {
249   ToolkitTestApplication application;
250
251   int widthBackground = 100;
252   int heightBackground = 200;
253   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
254
255   ImageView imageView = ImageView::New();
256   imageView.SetBackgroundImage( imageBackground );
257
258   Stage::GetCurrent().Add( imageView );
259   application.SendNotification();
260   application.Render();
261
262   Control control = Control::DownCast( imageView );
263   DALI_TEST_CHECK( control );
264   DALI_TEST_EQUALS( imageView.GetHeightForWidth( 123.f ), control.GetHeightForWidth( 123.f ), TEST_LOCATION );
265   DALI_TEST_EQUALS( imageView.GetWidthForHeight( 321.f ), control.GetWidthForHeight( 321.f ), TEST_LOCATION );
266
267   END_TEST;
268 }
269
270 int UtcDaliImageViewHeightForWidthBackgroundAndImage(void)
271 {
272   ToolkitTestApplication application;
273
274   int widthBackground = 100;
275   int heightBackground = 200;
276   int width = 300;
277   int height = 400;
278   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
279   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
280
281   ImageView imageView = ImageView::New();
282   imageView.SetBackgroundImage( imageBackground );
283   imageView.SetImage( image );
284
285   Stage::GetCurrent().Add( imageView );
286   application.SendNotification();
287   application.Render();
288
289   DALI_TEST_EQUALS( imageView.GetHeightForWidth( width ), height, TEST_LOCATION );
290   DALI_TEST_EQUALS( imageView.GetWidthForHeight( height ), width, TEST_LOCATION );
291
292   END_TEST;
293 }
294
295 int UtcDaliImageViewSetBufferImage(void)
296 {
297   ToolkitTestApplication application;
298
299   int width = 300;
300   int height = 400;
301   Image image = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
302   ImageView imageView = ImageView::New();
303   imageView.SetImage( image );
304
305   std::string resource_url;
306   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "resource-url" ) );
307   DALI_TEST_CHECK( val.Get( resource_url ) );
308   DALI_TEST_CHECK( resource_url.empty() );
309
310   END_TEST;
311 }
312
313 int UtcDaliImageViewSetResourceImage(void)
314 {
315   ToolkitTestApplication application;
316
317   Image image = ResourceImage::New( TEST_IMAGE_FILE_NAME );
318   ImageView imageView = ImageView::New();
319   imageView.SetImage( image );
320
321   std::string resource_url;
322   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "resource-url" ) );
323   DALI_TEST_CHECK( val.Get( resource_url ) );
324   DALI_TEST_EQUALS( resource_url, TEST_IMAGE_FILE_NAME, TEST_LOCATION );
325
326   END_TEST;
327 }
328
329 int UtcDaliImageViewSetImageOnstageP(void)
330 {
331   ToolkitTestApplication application;
332
333   ImageView imageView = ImageView::New();
334
335   Stage::GetCurrent().Add( imageView );
336   application.SendNotification();
337   application.Render();
338
339   Image image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
340   imageView.SetImage( image1 );
341
342   Image image2 = imageView.GetImage();
343   DALI_TEST_EQUALS( image1, image2, TEST_LOCATION );
344
345   int width = 300;
346   int height = 400;
347   Image image3 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
348   imageView.SetImage( image3 );
349
350   Image image4 = imageView.GetImage();
351   DALI_TEST_EQUALS( image3, image4, TEST_LOCATION );
352
353   END_TEST;
354 }
355
356 int UtcDaliImageViewSetImageOnstageN(void)
357 {
358   ToolkitTestApplication application;
359
360   ImageView imageView = ImageView::New();
361
362   Stage::GetCurrent().Add( imageView );
363   application.SendNotification();
364   application.Render();
365
366   Image image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
367   imageView.SetImage( image1 );
368
369   Image image2 = imageView.GetImage();
370   DALI_TEST_EQUALS( image1, image2, TEST_LOCATION );
371
372   Image image3;
373   imageView.SetImage( image3 );
374
375   Image image4 = imageView.GetImage();
376   DALI_TEST_CHECK( !image4 );
377
378   END_TEST;
379 }
380
381 int UtcDaliImageViewSetImageOffstageP(void)
382 {
383   ToolkitTestApplication application;
384
385   ImageView imageView = ImageView::New();
386
387   Stage::GetCurrent().Add( imageView );
388   application.SendNotification();
389   application.Render();
390   Stage::GetCurrent().Remove( imageView );
391
392   Image image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
393   imageView.SetImage( image1 );
394
395   Image image2 = imageView.GetImage();
396   DALI_TEST_EQUALS( image1, image2, TEST_LOCATION );
397
398   int width = 300;
399   int height = 400;
400   Image image3 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
401   imageView.SetImage( image3 );
402
403   Image image4 = imageView.GetImage();
404   DALI_TEST_EQUALS( image3, image4, TEST_LOCATION );
405
406   END_TEST;
407 }
408
409 int UtcDaliImageViewSetImageOffstageN(void)
410 {
411   ToolkitTestApplication application;
412
413   ImageView imageView = ImageView::New();
414
415   Stage::GetCurrent().Add( imageView );
416   application.SendNotification();
417   application.Render();
418   Stage::GetCurrent().Remove( imageView );
419
420   Image image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
421   imageView.SetImage( image1 );
422
423   Image image2 = imageView.GetImage();
424   DALI_TEST_EQUALS( image1, image2, TEST_LOCATION );
425
426   Image image3;
427   imageView.SetImage( image3 );
428
429   Image image4 = imageView.GetImage();
430   DALI_TEST_CHECK( !image4 );
431
432   END_TEST;
433 }
434
435 int UtcDaliImageViewSetImageN(void)
436 {
437   ToolkitTestApplication application;
438
439   Image image1;
440   ImageView imageView = ImageView::New();
441   imageView.SetImage( image1 );
442
443   Image image2 = imageView.GetImage();
444   DALI_TEST_CHECK( !image2 );
445
446   std::string resource_url;
447   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "resource-url" ) );
448   DALI_TEST_CHECK( val.Get( resource_url ) );
449   DALI_TEST_CHECK( resource_url.empty() );
450
451   END_TEST;
452 }