Wrap mode support for AnimatedImageVisual
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageView.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 // 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 #include <toolkit-bitmap-loader.h>
22 #include <toolkit-event-thread-callback.h>
23
24 #include <dali-toolkit/dali-toolkit.h>
25 #include <dali/devel-api/scripting/scripting.h>
26 #include <dali/public-api/rendering/renderer.h>
27
28 #include <test-native-image.h>
29 #include <sstream>
30
31 using namespace Dali;
32 using namespace Toolkit;
33
34 void utc_dali_toolkit_image_view_startup(void)
35 {
36   test_return_value = TET_UNDEF;
37 }
38
39 void utc_dali_toolkit_image_view_cleanup(void)
40 {
41   test_return_value = TET_PASS;
42 }
43
44 namespace
45 {
46
47 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
48   attribute mediump vec2 aPosition;\n
49   varying mediump vec2 vTexCoord;\n
50   uniform mediump mat4 uMvpMatrix;\n
51   uniform mediump vec3 uSize;\n
52   \n
53   void main()\n
54   {\n
55     mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
56     vertexPosition.xyz *= uSize;\n
57     vertexPosition = uMvpMatrix * vertexPosition;\n
58     \n
59     vTexCoord = aPosition + vec2(0.5);\n
60     gl_Position = vertexPosition;\n
61   }\n
62 );
63
64 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
65   varying mediump vec2 vTexCoord;\n
66   uniform sampler2D sTexture;\n
67   uniform lowp vec4 uColor;\n
68   \n
69   void main()\n
70   {\n
71     gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
72   }\n
73 );
74
75 const char* TEST_IMAGE_FILE_NAME =  "gallery_image_01.jpg";
76 const char* TEST_IMAGE_FILE_NAME2 =  "gallery_image_02.jpg";
77
78 // resolution: 34*34, pixel format: RGBA8888
79 static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
80 // resolution: 600*600, pixel format: RGB888
81 static const char* gImage_600_RGB = TEST_RESOURCE_DIR "/test-image-600.jpg";
82
83 // resolution: 50*50, frame count: 4, frame delay: 0.2 second for each frame
84 const char* TEST_GIF_FILE_NAME = TEST_RESOURCE_DIR "/anim.gif";
85
86 void TestImage( ImageView imageView, BufferImage image )
87 {
88   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
89
90   Property::Map map;
91   DALI_TEST_CHECK( value.Get( map ) );
92
93   DALI_TEST_CHECK( map.Find( "width" ) );
94   DALI_TEST_CHECK( map.Find( "height" ) );
95   DALI_TEST_CHECK( map.Find( "type" ) );
96
97   int width = 0;
98   DALI_TEST_CHECK( map[ "width" ].Get( width ) );
99   DALI_TEST_EQUALS( (unsigned int)width, image.GetWidth(), TEST_LOCATION );
100
101   int height = 0;
102   DALI_TEST_CHECK( map[ "height" ].Get( height ) );
103   DALI_TEST_EQUALS( (unsigned int)height, image.GetHeight(), TEST_LOCATION );
104
105   std::string type;
106   DALI_TEST_CHECK( map[ "type" ].Get( type ) );
107   DALI_TEST_EQUALS( type, "BufferImage", TEST_LOCATION );
108 }
109
110 void TestImage( ImageView imageView, ResourceImage image )
111 {
112   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
113
114   Property::Map map;
115   DALI_TEST_CHECK( value.Get( map ) );
116
117   if( map.Find( "width" ) )
118   {
119     int width = 0;
120     DALI_TEST_CHECK( map[ "width" ].Get( width ) );
121     DALI_TEST_EQUALS( (unsigned int)width, image.GetWidth(), TEST_LOCATION );
122   }
123
124   if( map.Find( "height" ) )
125   {
126     int height = 0;
127     DALI_TEST_CHECK( map[ "height" ].Get( height ) );
128     DALI_TEST_EQUALS( (unsigned int)height, image.GetHeight(), TEST_LOCATION );
129   }
130
131   DALI_TEST_CHECK( map.Find( "type" ) );
132
133   std::string type;
134   DALI_TEST_CHECK( map[ "type" ].Get( type ) );
135   DALI_TEST_EQUALS( type, "ResourceImage", TEST_LOCATION );
136
137   std::string filename;
138   DALI_TEST_CHECK( map[ "filename" ].Get( filename ) );
139   DALI_TEST_EQUALS( filename, image.GetUrl(), TEST_LOCATION );
140 }
141
142 void TestUrl( ImageView imageView, const std::string url )
143 {
144   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
145
146   std::string urlActual;
147   DALI_TEST_CHECK( value.Get( urlActual ) );
148   DALI_TEST_EQUALS( urlActual, url, TEST_LOCATION );
149 }
150
151 } // namespace
152
153 int UtcDaliImageViewNewP(void)
154 {
155   TestApplication application;
156
157   ImageView imageView = ImageView::New();
158
159   DALI_TEST_CHECK( imageView );
160
161   END_TEST;
162 }
163
164 int UtcDaliImageViewNewImageP(void)
165 {
166   TestApplication application;
167
168   BufferImage image = CreateBufferImage( 100, 200, Vector4( 1.f, 1.f, 1.f, 1.f ) );
169   ImageView imageView = ImageView::New( image );
170
171   DALI_TEST_CHECK( imageView );
172   TestImage( imageView, image );
173
174   END_TEST;
175 }
176
177 int UtcDaliImageViewNewUrlP(void)
178 {
179   TestApplication application;
180
181   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
182   DALI_TEST_CHECK( imageView );
183
184   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
185
186   END_TEST;
187 }
188
189 int UtcDaliImageViewConstructorP(void)
190 {
191   TestApplication application;
192
193   ImageView imageView;
194
195   DALI_TEST_CHECK( !imageView );
196
197   END_TEST;
198 }
199
200 int UtcDaliImageViewCopyConstructorP(void)
201 {
202   TestApplication application;
203
204   // Initialize an object, ref count == 1
205   ImageView imageView = ImageView::New();
206
207   ImageView copy( imageView );
208   DALI_TEST_CHECK( copy );
209
210   END_TEST;
211 }
212
213 int UtcDaliImageViewAssignmentOperatorP(void)
214 {
215   TestApplication application;
216
217   ImageView imageView = ImageView::New();
218
219   ImageView copy( imageView );
220   DALI_TEST_CHECK( copy );
221   DALI_TEST_EQUALS( imageView, copy, TEST_LOCATION );
222
223   END_TEST;
224 }
225
226 int UtcDaliImageViewDownCastP(void)
227 {
228   TestApplication application;
229
230   ImageView imageView = ImageView::New();
231
232   BaseHandle object(imageView);
233
234   ImageView imageView2 = ImageView::DownCast( object );
235   DALI_TEST_CHECK(imageView2);
236
237   ImageView imageView3 = DownCast< ImageView >( object );
238   DALI_TEST_CHECK(imageView3);
239
240   END_TEST;
241 }
242
243 int UtcDaliImageViewDownCastN(void)
244 {
245   TestApplication application;
246
247   BaseHandle unInitializedObject;
248
249   ImageView imageView1 = ImageView::DownCast( unInitializedObject );
250   DALI_TEST_CHECK( !imageView1 );
251
252   ImageView imageView2 = DownCast< ImageView >( unInitializedObject );
253   DALI_TEST_CHECK( !imageView2 );
254
255   END_TEST;
256 }
257
258 int UtcDaliImageViewTypeRegistry(void)
259 {
260   ToolkitTestApplication application;
261
262   TypeRegistry typeRegistry = TypeRegistry::Get();
263   DALI_TEST_CHECK( typeRegistry );
264
265   TypeInfo typeInfo = typeRegistry.GetTypeInfo( "ImageView" );
266   DALI_TEST_CHECK( typeInfo );
267
268   BaseHandle handle = typeInfo.CreateInstance();
269   DALI_TEST_CHECK( handle );
270
271   ImageView imageView = ImageView::DownCast( handle );
272   DALI_TEST_CHECK( imageView );
273
274   END_TEST;
275 }
276
277 int UtcDaliImageViewSetGetProperty01(void)
278 {
279   ToolkitTestApplication application;
280
281   ImageView imageView = ImageView::New();
282
283   Property::Index idx = imageView.GetPropertyIndex( "image" );
284   DALI_TEST_EQUALS( idx, (Property::Index)ImageView::Property::IMAGE, TEST_LOCATION );
285
286   imageView.SetProperty( idx, TEST_IMAGE_FILE_NAME );
287   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
288
289   END_TEST;
290 }
291
292 int UtcDaliImageViewSetGetProperty02(void)
293 {
294   ToolkitTestApplication application;
295
296   Image image = CreateBufferImage( 10, 10, Color::WHITE );
297   ImageView imageView = ImageView::New(image);
298   Vector4 fullImageRect( 0.f, 0.f, 1.f, 1.f );
299
300   Stage::GetCurrent().Add( imageView );
301
302   application.SendNotification();
303   application.Render();
304   TestGlAbstraction& gl = application.GetGlAbstraction();
305
306   Vector4 pixelAreaUniform;
307   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
308   DALI_TEST_EQUALS( pixelAreaUniform, fullImageRect, TEST_LOCATION );
309
310   Property::Value value = imageView.GetProperty( ImageView::Property::PIXEL_AREA );
311   Vector4 pixelAreaValue;
312   DALI_TEST_CHECK( value.Get(pixelAreaValue) );
313   DALI_TEST_EQUALS( pixelAreaValue, fullImageRect, TEST_LOCATION );
314
315   Vector4 pixelAreaSet( 0.2f, 0.2f, 0.3f, 0.3f );
316   imageView.SetProperty( ImageView::Property::PIXEL_AREA, pixelAreaSet);
317
318   application.SendNotification();
319   application.Render();
320
321   value = imageView.GetProperty( ImageView::Property::PIXEL_AREA );
322   value.Get(pixelAreaValue);
323   DALI_TEST_EQUALS( pixelAreaValue, pixelAreaSet, TEST_LOCATION );
324
325   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
326   DALI_TEST_EQUALS( pixelAreaUniform, pixelAreaSet, TEST_LOCATION );
327
328   END_TEST;
329 }
330
331 int UtcDaliImageViewSetGetProperty03(void)
332 {
333   ToolkitTestApplication application;
334
335   Image image = CreateBufferImage( 10, 10, Color::WHITE );
336   ImageView imageView = ImageView::New(image);
337   Stage::GetCurrent().Add( imageView );
338   application.SendNotification();
339   application.Render();
340
341   // conventional alpha blending
342   Renderer renderer = imageView.GetRendererAt( 0 );
343   Property::Value value = renderer.GetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
344   bool enable;
345   DALI_TEST_CHECK( value.Get( enable ) );
346   DALI_TEST_CHECK( !enable );
347
348   // pre-multiplied alpha blending
349   imageView.SetProperty( Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true );
350   application.SendNotification();
351   application.Render();
352
353   int srcFactorRgb    = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_RGB );
354   int destFactorRgb   = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_RGB );
355   int srcFactorAlpha  = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_ALPHA );
356   int destFactorAlpha = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_ALPHA );
357   DALI_TEST_CHECK( srcFactorRgb == BlendFactor::ONE );
358   DALI_TEST_CHECK( destFactorRgb == BlendFactor::ONE_MINUS_SRC_ALPHA );
359   DALI_TEST_CHECK( srcFactorAlpha == BlendFactor::ONE );
360   DALI_TEST_CHECK( destFactorAlpha == BlendFactor::ONE );
361
362   value = renderer.GetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
363   DALI_TEST_CHECK( value.Get( enable ) );
364   DALI_TEST_CHECK( enable );
365
366   END_TEST;
367 }
368
369 int UtcDaliImageViewPixelArea(void)
370 {
371   // Test pixel area property
372   ToolkitTestApplication application;
373
374   // Gif image, use AnimatedImageVisual internally
375   // Atlasing is applied to pack multiple frames, use custom wrap mode
376   ImageView gifView = ImageView::New();
377   const Vector4 pixelAreaVisual( 0.f, 0.f, 2.f, 2.f );
378   gifView.SetProperty( ImageView::Property::IMAGE,
379                        Property::Map().Add( ImageVisual::Property::URL, TEST_GIF_FILE_NAME )
380                                       .Add( ImageVisual::Property::PIXEL_AREA, pixelAreaVisual ) );
381
382   // Add to stage
383   Stage stage = Stage::GetCurrent();
384   stage.Add( gifView );
385
386   // loading started
387   application.SendNotification();
388   application.Render(16);
389   DALI_TEST_CHECK( gifView.GetRendererCount() == 1u );
390
391   const Vector4 fullTextureRect( 0.f, 0.f, 1.f, 1.f );
392   // test that the pixel area value defined in the visual property map is registered on renderer
393   Renderer renderer = gifView.GetRendererAt(0);
394   Property::Value pixelAreaValue = renderer.GetProperty( renderer.GetPropertyIndex( "pixelArea" ) );
395   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelAreaVisual, TEST_LOCATION );
396
397   // test that the shader has the default pixel area value registered.
398   Shader shader = renderer.GetShader();
399   pixelAreaValue = shader.GetProperty( shader.GetPropertyIndex( "pixelArea" ) );
400   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), fullTextureRect, TEST_LOCATION );
401
402   // test that the uniform uses the pixelArea property on the renderer.
403   TestGlAbstraction& gl = application.GetGlAbstraction();
404   Vector4 pixelAreaUniform;
405   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
406   DALI_TEST_EQUALS( pixelAreaVisual, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
407
408   // set the pixelArea property on the control
409   const Vector4 pixelAreaControl( -1.f, -1.f, 3.f, 3.f );
410   gifView.SetProperty( ImageView::Property::PIXEL_AREA, pixelAreaControl );
411   application.SendNotification();
412   application.Render(16);
413
414   // check the pixelArea property on the control
415   pixelAreaValue = gifView.GetProperty( gifView.GetPropertyIndex( "pixelArea" ) );
416   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelAreaControl, TEST_LOCATION );
417   // test that the uniform uses the pixelArea property on the control.
418   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
419   DALI_TEST_EQUALS( pixelAreaControl, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
420
421
422   END_TEST;
423 }
424
425 int UtcDaliImageViewAsyncLoadingWithoutAltasing(void)
426 {
427   ToolkitTestApplication application;
428
429   // Async loading, no atlasing for big size image
430   ImageView imageView = ImageView::New( gImage_600_RGB );
431
432   // By default, Aysnc loading is used
433   Stage::GetCurrent().Add( imageView );
434   application.SendNotification();
435   application.Render(16);
436   application.Render(16);
437   application.SendNotification();
438
439   // BitmapLoader is not used
440   BitmapLoader loader = BitmapLoader::GetLatestCreated();
441   DALI_TEST_CHECK( !loader );
442
443   END_TEST;
444 }
445
446 int UtcDaliImageViewAsyncLoadingWithAtlasing(void)
447 {
448   ToolkitTestApplication application;
449
450   //Async loading, automatic atlasing for small size image
451   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
452   callStack.Reset();
453   callStack.Enable(true);
454
455   ImageView imageView = ImageView::New( gImage_34_RGBA, ImageDimensions( 34, 34 ) );
456
457   // By default, Aysnc loading is used
458   // loading is not started if the actor is offStage
459
460   Stage::GetCurrent().Add( imageView );
461   application.SendNotification();
462   application.Render(16);
463   application.Render(16);
464   application.SendNotification();
465
466   // loading started, this waits for the loader thread for max 30 seconds
467   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
468
469   application.SendNotification();
470   application.Render(16);
471
472   callStack.Enable(false);
473
474   TraceCallStack::NamedParams params;
475   params["width"] = ToString(34);
476   params["height"] = ToString(34);
477   DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ), true, TEST_LOCATION );
478
479   END_TEST;
480 }
481
482 int UtcDaliImageViewAsyncLoadingWithAtlasing02(void)
483 {
484   ToolkitTestApplication application;
485
486   //Async loading, automatic atlasing for small size image
487   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
488   callStack.Reset();
489   callStack.Enable(true);
490
491   Property::Map asyncLoadingMap;
492   asyncLoadingMap[ "url" ] = gImage_34_RGBA;
493   asyncLoadingMap[ "desiredHeight" ] = 34;
494   asyncLoadingMap[ "desiredWidth" ] = 34;
495   asyncLoadingMap[ "synchronousLoading" ] = false;
496
497   ImageView imageView = ImageView::New();
498   imageView.SetProperty( ImageView::Property::IMAGE, asyncLoadingMap );
499
500   Stage::GetCurrent().Add( imageView );
501   application.SendNotification();
502   application.Render(16);
503   application.Render(16);
504   application.SendNotification();
505
506   // loading started, this waits for the loader thread for max 30 seconds
507   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
508
509   application.SendNotification();
510   application.Render(16);
511
512   callStack.Enable(false);
513
514   TraceCallStack::NamedParams params;
515   params["width"] = ToString(34);
516   params["height"] = ToString(34);
517   DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ), true, TEST_LOCATION );
518
519   END_TEST;
520 }
521
522 int UtcDaliImageViewSyncLoading(void)
523 {
524   ToolkitTestApplication application;
525
526   tet_infoline("ImageView Testing sync loading and size using index key property map");
527
528   Property::Map syncLoadingMap;
529   syncLoadingMap[ ImageVisual::Property::SYNCHRONOUS_LOADING ] = true;
530
531   // Sync loading, no atlasing for big size image
532   {
533     ImageView imageView = ImageView::New();
534
535     // Sync loading is used
536     syncLoadingMap[ ImageVisual::Property::URL ] = gImage_600_RGB;
537     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
538
539     // BitmapLoader is used, and the loading is started immediately even the actor is not on stage.
540     BitmapLoader loader = BitmapLoader::GetLatestCreated();
541     DALI_TEST_CHECK( loader );
542   }
543
544   // Sync loading, automatic atlasing for small size image
545   {
546     BitmapLoader::ResetLatestCreated();
547     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
548     callStack.Reset();
549     callStack.Enable(true);
550
551     ImageView imageView = ImageView::New( );
552     // Sync loading is used
553     syncLoadingMap[ ImageVisual::Property::URL ] = gImage_34_RGBA;
554     syncLoadingMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 34;
555     syncLoadingMap[ ImageVisual::Property::DESIRED_WIDTH ] = 34;
556     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
557
558     // loading is started even if the actor is offStage
559     BitmapLoader loader = BitmapLoader::GetLatestCreated();
560     DALI_TEST_CHECK( loader );
561
562     loader.WaitForLoading();
563
564     DALI_TEST_CHECK( loader.IsLoaded() );
565
566     Stage::GetCurrent().Add( imageView );
567     application.SendNotification();
568     application.Render(16);
569
570     TraceCallStack::NamedParams params;
571     params["width"] = ToString(34);
572     params["height"] = ToString(34);
573     DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ),
574                       true, TEST_LOCATION );
575   }
576   END_TEST;
577 }
578
579
580 int UtcDaliImageViewSyncLoading02(void)
581 {
582   ToolkitTestApplication application;
583
584   tet_infoline("ImageView Testing sync loading and size using string key property map");
585
586   // Sync loading, automatic atlasing for small size image
587   {
588     BitmapLoader::ResetLatestCreated();
589     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
590     callStack.Reset();
591     callStack.Enable(true);
592
593     ImageView imageView = ImageView::New( );
594
595     // Sync loading is used
596     Property::Map syncLoadingMap;
597     syncLoadingMap[ "url" ] = gImage_34_RGBA;
598     syncLoadingMap[ "desiredHeight" ] = 34;
599     syncLoadingMap[ "desiredWidth" ] = 34;
600     syncLoadingMap[ "synchronousLoading" ] = true;
601     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
602
603     // loading is started even if the actor is offStage
604     BitmapLoader loader = BitmapLoader::GetLatestCreated();
605     DALI_TEST_CHECK( loader );
606
607     loader.WaitForLoading();
608
609     DALI_TEST_CHECK( loader.IsLoaded() );
610
611     Stage::GetCurrent().Add( imageView );
612     application.SendNotification();
613     application.Render(16);
614
615     TraceCallStack::NamedParams params;
616     params["width"] = ToString(34);
617     params["height"] = ToString(34);
618     DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ),
619                       true, TEST_LOCATION );
620   }
621   END_TEST;
622 }
623
624 int UtcDaliImageViewSizeWithBackground(void)
625 {
626   ToolkitTestApplication application;
627
628   int width = 100;
629   int height = 200;
630   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
631   ImageView imageView = ImageView::New();
632   imageView.SetBackgroundImage( image );
633
634   Stage::GetCurrent().Add( imageView );
635   application.SendNotification();
636   application.Render();
637
638   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
639   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
640
641   END_TEST;
642 }
643
644 int UtcDaliImageViewSizeWithBackgroundAndImage(void)
645 {
646   ToolkitTestApplication application;
647
648   int widthBackground = 100;
649   int heightBackground = 200;
650   int width = 300;
651   int height = 400;
652   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
653   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
654
655   ImageView imageView = ImageView::New();
656   imageView.SetBackgroundImage( imageBackground );
657   imageView.SetImage( image );
658
659   Stage::GetCurrent().Add( imageView );
660   application.SendNotification();
661   application.Render();
662
663   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
664   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
665
666   END_TEST;
667 }
668
669 int UtcDaliImageViewHeightForWidthBackground(void)
670 {
671   ToolkitTestApplication application;
672
673   int widthBackground = 100;
674   int heightBackground = 200;
675   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
676
677   ImageView imageView = ImageView::New();
678   imageView.SetBackgroundImage( imageBackground );
679
680   Stage::GetCurrent().Add( imageView );
681   application.SendNotification();
682   application.Render();
683
684   Control control = Control::DownCast( imageView );
685   DALI_TEST_CHECK( control );
686   DALI_TEST_EQUALS( imageView.GetHeightForWidth( 123.f ), control.GetHeightForWidth( 123.f ), TEST_LOCATION );
687   DALI_TEST_EQUALS( imageView.GetWidthForHeight( 321.f ), control.GetWidthForHeight( 321.f ), TEST_LOCATION );
688
689   END_TEST;
690 }
691
692 int UtcDaliImageViewHeightForWidthBackgroundAndImage(void)
693 {
694   ToolkitTestApplication application;
695
696   int widthBackground = 100;
697   int heightBackground = 200;
698   int width = 300;
699   int height = 400;
700   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
701   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
702
703   ImageView imageView = ImageView::New();
704   imageView.SetBackgroundImage( imageBackground );
705   imageView.SetImage( image );
706
707   Stage::GetCurrent().Add( imageView );
708   application.SendNotification();
709   application.Render();
710
711   DALI_TEST_EQUALS( imageView.GetHeightForWidth( width ), (float)height, TEST_LOCATION );
712   DALI_TEST_EQUALS( imageView.GetWidthForHeight( height ), (float)width, TEST_LOCATION );
713
714   END_TEST;
715 }
716
717 int UtcDaliImageViewSetBufferImage(void)
718 {
719   ToolkitTestApplication application;
720
721   int width1 = 300;
722   int height1 = 400;
723   BufferImage image1 = CreateBufferImage( width1, height1, Vector4( 1.f, 1.f, 1.f, 1.f ) );
724   ImageView imageView = ImageView::New();
725   imageView.SetImage( image1 );
726
727   TestImage( imageView, image1 );
728
729   int width2 = 600;
730   int height2 = 500;
731   BufferImage image2 = CreateBufferImage( width2, height2, Vector4( 1.f, 1.f, 1.f, 1.f ) );
732   imageView.SetImage( image2 );
733
734   TestImage( imageView, image2 );
735
736   END_TEST;
737 }
738
739 int UtcDaliImageViewSetImageUrl(void)
740 {
741   ToolkitTestApplication application;
742
743   ImageView imageView = ImageView::New();
744   imageView.SetImage( TEST_IMAGE_FILE_NAME );
745   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
746
747
748   imageView.SetImage( TEST_IMAGE_FILE_NAME2 );
749   TestUrl( imageView, TEST_IMAGE_FILE_NAME2 );
750
751   END_TEST;
752 }
753
754 int UtcDaliImageViewSetImageOnstageP(void)
755 {
756   ToolkitTestApplication application;
757
758   ImageView imageView = ImageView::New();
759
760   Stage::GetCurrent().Add( imageView );
761   application.SendNotification();
762   application.Render();
763
764   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
765   imageView.SetImage( image1 );
766   TestImage( imageView, image1 );
767
768   int width = 300;
769   int height = 400;
770   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
771   imageView.SetImage( image2 );
772   TestImage( imageView, image2 );
773
774   END_TEST;
775 }
776
777 int UtcDaliImageViewSetImageOnstageN(void)
778 {
779   ToolkitTestApplication application;
780
781   ImageView imageView = ImageView::New();
782
783   Stage::GetCurrent().Add( imageView );
784   application.SendNotification();
785   application.Render();
786
787   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
788   imageView.SetImage( image1 );
789   TestImage( imageView, image1 );
790
791   Image image2;
792   imageView.SetImage( image2 );
793
794   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
795
796   //the value should be empty
797   std::string url;
798   DALI_TEST_CHECK( !value.Get( url ) );
799
800   Property::Map map;
801   DALI_TEST_CHECK( !value.Get( map ) );
802
803   END_TEST;
804 }
805
806 int UtcDaliImageViewSetImageOffstageP(void)
807 {
808   ToolkitTestApplication application;
809
810   ImageView imageView = ImageView::New();
811
812   Stage::GetCurrent().Add( imageView );
813   application.SendNotification();
814   application.Render();
815   Stage::GetCurrent().Remove( imageView );
816
817   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
818   imageView.SetImage( image1 );
819   TestImage( imageView, image1 );
820
821   int width = 300;
822   int height = 400;
823   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
824   imageView.SetImage( image2 );
825   TestImage( imageView, image2 );
826
827   END_TEST;
828 }
829
830 int UtcDaliImageViewSetImageOffstageN(void)
831 {
832   ToolkitTestApplication application;
833
834   ImageView imageView = ImageView::New();
835
836   Stage::GetCurrent().Add( imageView );
837   application.SendNotification();
838   application.Render();
839   Stage::GetCurrent().Remove( imageView );
840
841   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
842   imageView.SetImage( image1 );
843   TestImage( imageView, image1 );
844
845   Image image2;
846   imageView.SetImage( image2 );
847
848   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
849
850   //the value should be empty
851   std::string url;
852   DALI_TEST_CHECK( !value.Get( url ) );
853
854   Property::Map map;
855   DALI_TEST_CHECK( !value.Get( map ) );
856
857   END_TEST;
858 }
859
860 int UtcDaliImageViewSetImageN(void)
861 {
862   ToolkitTestApplication application;
863
864   Image image1;
865   ImageView imageView = ImageView::New();
866   imageView.SetImage( image1 );
867
868   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
869
870   //the value should be empty
871   std::string url;
872   DALI_TEST_CHECK( !value.Get( url ) );
873
874   Property::Map map;
875   DALI_TEST_CHECK( !value.Get( map ) );
876
877   std::string resource_url;
878   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
879   DALI_TEST_CHECK( !val.Get( resource_url ) );
880
881   END_TEST;
882 }
883
884 int UtcDaliImageViewSetImageTypeChangesP(void)
885 {
886   ToolkitTestApplication application;
887
888   ImageView imageView = ImageView::New();
889
890
891   std::string url;
892   Property::Map map;
893
894   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
895   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
896   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
897
898   // Set a URL
899   imageView.SetImage( "TEST_URL" );
900   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
901
902   DALI_TEST_CHECK( value.Get( url ) );   // Value should NOT be empty
903   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
904
905   // Set an empty Image
906   imageView.SetImage( Image() );
907   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
908
909   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
910   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
911
912   // Set an Image
913   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
914   imageView.SetImage( image1 );
915   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
916
917   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
918   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
919
920   // Set an empty URL
921   imageView.SetImage( "" );
922   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
923
924   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
925   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
926
927   END_TEST;
928 }
929
930 int UtcDaliImageViewResourceUrlP(void)
931 {
932   ToolkitTestApplication application;
933
934   ImageView imageView = ImageView::New();
935   DALI_TEST_CHECK( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >().empty() );
936
937   imageView.SetProperty( ImageView::Property::RESOURCE_URL, "TestString" );
938   DALI_TEST_EQUALS( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >(), "TestString", TEST_LOCATION );
939
940   END_TEST;
941 }
942
943 // Scenarios 1: ImageView from regular image
944 int UtcDaliImageViewSetImageBufferImage(void)
945 {
946   ToolkitTestApplication application;
947
948   ImageView imageView = ImageView::New();
949   Stage::GetCurrent().Add( imageView );
950
951   TestGlAbstraction& gl = application.GetGlAbstraction();
952   gl.EnableTextureCallTrace( true );
953
954   std::vector< GLuint > ids;
955   ids.push_back( 23 );
956   application.GetGlAbstraction().SetNextTextureIds( ids );
957
958   int width = 300;
959   int height = 400;
960   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
961
962   imageView.SetImage( image );
963
964   application.SendNotification();
965   application.Render();
966
967   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
968
969   std::stringstream params;
970   params << GL_TEXTURE_2D << ", " << 23;
971   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
972
973   END_TEST;
974 }
975
976 // Scenarios 2: ImageView from Native image
977 int UtcDaliImageViewSetImageNativeImage(void)
978 {
979   ToolkitTestApplication application;
980
981   ImageView imageView = ImageView::New();
982   Stage::GetCurrent().Add( imageView );
983
984   TestGlAbstraction& gl = application.GetGlAbstraction();
985   gl.EnableTextureCallTrace( true );
986
987   std::vector< GLuint > ids;
988   ids.push_back( 23 );
989   application.GetGlAbstraction().SetNextTextureIds( ids );
990
991   int width = 200;
992   int height = 500;
993   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
994   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
995
996   imageView.SetImage( nativeImage );
997   application.SendNotification();
998   application.Render();
999
1000   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1001
1002   std::stringstream params;
1003   params << GL_TEXTURE_2D << ", " << 23;
1004   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1005
1006   END_TEST;
1007 }
1008
1009 // Scenarios 3: ImageView initially from regular image but then SetImage called with Native image
1010 int UtcDaliImageViewSetImageBufferImageToNativeImage(void)
1011 {
1012   ToolkitTestApplication application;
1013
1014   int width = 300;
1015   int height = 400;
1016   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1017
1018   ImageView imageView = ImageView::New( image );
1019   Stage::GetCurrent().Add( imageView );
1020
1021   TestGlAbstraction& gl = application.GetGlAbstraction();
1022   gl.EnableTextureCallTrace( true );
1023
1024   std::vector< GLuint > ids;
1025   ids.push_back( 23 );
1026   application.GetGlAbstraction().SetNextTextureIds( ids );
1027
1028   application.SendNotification();
1029   application.Render();
1030
1031   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1032
1033   std::stringstream params;
1034   params << GL_TEXTURE_2D << ", " << 23;
1035   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1036
1037   width = 200;
1038   height = 500;
1039   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1040   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1041   imageView.SetImage( nativeImage );
1042
1043   ids.clear();
1044   ids.push_back( 24 );
1045   application.GetGlAbstraction().SetNextTextureIds( ids );
1046
1047   application.SendNotification();
1048   application.Render();
1049
1050   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1051
1052   std::stringstream nextTextureParams;
1053   nextTextureParams << GL_TEXTURE_2D << ", " << 24;
1054   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
1055
1056   END_TEST;
1057 }
1058
1059 // Scenarios 4: ImageView initially from Native image but then SetImage called with regular image
1060 int UtcDaliImageViewSetImageNativeImageToBufferImage(void)
1061 {
1062   ToolkitTestApplication application;
1063
1064   int width = 300;
1065   int height = 400;
1066   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1067   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1068
1069   ImageView imageView = ImageView::New( nativeImage );
1070   Stage::GetCurrent().Add( imageView );
1071
1072   TestGlAbstraction& gl = application.GetGlAbstraction();
1073   gl.EnableTextureCallTrace( true );
1074
1075   std::vector< GLuint > ids;
1076   ids.push_back( 23 );
1077   application.GetGlAbstraction().SetNextTextureIds( ids );
1078
1079   application.SendNotification();
1080   application.Render();
1081
1082   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1083
1084   std::stringstream params;
1085   params << GL_TEXTURE_2D << ", " << 23;
1086   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1087
1088   width = 200;
1089   height = 500;
1090   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1091   imageView.SetImage( image );
1092
1093   ids.clear();
1094   ids.push_back( 24 );
1095   application.GetGlAbstraction().SetNextTextureIds( ids );
1096
1097   application.SendNotification();
1098   application.Render();
1099
1100   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1101
1102   std::stringstream nextTextureParams;
1103   nextTextureParams << GL_TEXTURE_2D << ", " << 24;
1104   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
1105
1106   END_TEST;
1107 }
1108
1109 // Scenarios 5: ImageView from Native image with custom shader
1110 int UtcDaliImageViewSetImageNativeImageWithCustomShader(void)
1111 {
1112   ToolkitTestApplication application;
1113
1114   int width = 300;
1115   int height = 400;
1116
1117   Property::Map customShader;
1118   customShader.Insert( "vertexShader", VERTEX_SHADER );
1119   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
1120
1121   Property::Array shaderHints;
1122   shaderHints.PushBack( "requiresSelfDepthTest" );
1123   shaderHints.PushBack( "outputIsTransparent" );
1124   shaderHints.PushBack( "outputIsOpaque" );
1125   shaderHints.PushBack( "modifiesGeometry" );
1126
1127   customShader.Insert( "hints", shaderHints );
1128
1129   Property::Map map;
1130   map.Insert( "shader", customShader );
1131
1132   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1133   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1134
1135   ImageView imageView = ImageView::New( nativeImage );
1136   imageView.SetProperty( ImageView::Property::IMAGE, map );
1137   Stage::GetCurrent().Add( imageView );
1138
1139   TestGlAbstraction& gl = application.GetGlAbstraction();
1140   gl.EnableTextureCallTrace( true );
1141
1142   std::vector< GLuint > ids;
1143   ids.push_back( 23 );
1144   application.GetGlAbstraction().SetNextTextureIds( ids );
1145
1146   application.SendNotification();
1147   application.Render();
1148
1149   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1150
1151   std::stringstream params;
1152   params << GL_TEXTURE_2D << ", " << 23;
1153   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1154
1155   END_TEST;
1156 }
1157
1158 // Scenarios 6: ImageView initially from regular image with custom shader but then SetImage called with Native
1159 int UtcDaliImageViewSetImageBufferImageWithCustomShaderToNativeImage(void)
1160 {
1161   ToolkitTestApplication application;
1162
1163   int width = 300;
1164   int height = 400;
1165
1166   Property::Map customShader;
1167   customShader.Insert( "vertexShader", VERTEX_SHADER );
1168   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
1169
1170   Property::Array shaderHints;
1171   shaderHints.PushBack( "requiresSelfDepthTest" );
1172   shaderHints.PushBack( "outputIsTransparent" );
1173   shaderHints.PushBack( "outputIsOpaque" );
1174   shaderHints.PushBack( "modifiesGeometry" );
1175
1176   customShader.Insert( "hints", shaderHints );
1177
1178   Property::Map map;
1179   map.Insert( "shader", customShader );
1180
1181   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1182
1183   ImageView imageView = ImageView::New( image );
1184   imageView.SetProperty( ImageView::Property::IMAGE, map );
1185   Stage::GetCurrent().Add( imageView );
1186
1187   TestGlAbstraction& gl = application.GetGlAbstraction();
1188   gl.EnableTextureCallTrace( true );
1189
1190   std::vector< GLuint > ids;
1191   ids.push_back( 23 );
1192   application.GetGlAbstraction().SetNextTextureIds( ids );
1193
1194   application.SendNotification();
1195   application.Render();
1196
1197   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1198
1199   std::stringstream params;
1200   params << GL_TEXTURE_2D << ", " << 23;
1201   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1202
1203   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1204   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1205   imageView.SetImage( nativeImage );
1206
1207   ids.clear();
1208   ids.push_back( 24 );
1209   application.GetGlAbstraction().SetNextTextureIds( ids );
1210
1211   application.SendNotification();
1212   application.Render();
1213
1214   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1215
1216   std::stringstream nativeImageParams;
1217   nativeImageParams << GL_TEXTURE_2D << ", " << 24;
1218   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nativeImageParams.str()) );
1219
1220
1221   END_TEST;
1222 }
1223
1224 int UtcDaliImageViewGetImageP1(void)
1225 {
1226   ToolkitTestApplication application;
1227
1228   ImageView imageView = ImageView::New();
1229   DALI_TEST_CHECK( ! imageView.GetImage() );
1230
1231   Image image = CreateBufferImage();
1232   imageView.SetImage( image );
1233   DALI_TEST_CHECK( imageView.GetImage() == image );
1234
1235   END_TEST;
1236 }
1237
1238 int UtcDaliImageViewGetImageP2(void)
1239 {
1240   ToolkitTestApplication application;
1241
1242   BufferImage image = CreateBufferImage();
1243   ImageView imageView = ImageView::New( image );
1244   DALI_TEST_CHECK( imageView.GetImage() == image );
1245
1246   END_TEST;
1247 }
1248
1249 int UtcDaliImageViewGetImageN(void)
1250 {
1251   ToolkitTestApplication application;
1252
1253   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
1254   DALI_TEST_CHECK( ! imageView.GetImage() );
1255
1256   Image image = CreateBufferImage();
1257   imageView.SetImage( image );
1258   DALI_TEST_CHECK( imageView.GetImage() == image );
1259
1260   imageView.SetImage( TEST_IMAGE_FILE_NAME );
1261   DALI_TEST_CHECK( ! imageView.GetImage() );
1262
1263   END_TEST;
1264 }