Fix image-visual natural size issue using atlas
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / utc-Dali-ImageView.cpp
1 /*
2  * Copyright (c) 2017 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-event-thread-callback.h>
22
23 #include <dali-toolkit/dali-toolkit.h>
24 #include <dali/devel-api/scripting/scripting.h>
25 #include <dali-toolkit/devel-api/controls/control-devel.h>
26 #include <dali-toolkit/devel-api/image-loader/texture-manager.h>
27 #include <dali-toolkit/devel-api/visual-factory/visual-base.h>
28 #include <dali-toolkit/devel-api/visuals/image-visual-properties-devel.h>
29 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
30
31 #include <test-native-image.h>
32 #include <sstream>
33 #include <unistd.h>
34
35
36 #include "dummy-control.h"
37
38 using namespace Dali;
39 using namespace Toolkit;
40
41 void utc_dali_toolkit_image_view_startup(void)
42 {
43   test_return_value = TET_UNDEF;
44 }
45
46 void utc_dali_toolkit_image_view_cleanup(void)
47 {
48   test_return_value = TET_PASS;
49 }
50
51 namespace
52 {
53
54 const char* VERTEX_SHADER = DALI_COMPOSE_SHADER(
55   attribute mediump vec2 aPosition;\n
56   varying mediump vec2 vTexCoord;\n
57   uniform mediump mat4 uMvpMatrix;\n
58   uniform mediump vec3 uSize;\n
59   \n
60   void main()\n
61   {\n
62     mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);\n
63     vertexPosition.xyz *= uSize;\n
64     vertexPosition = uMvpMatrix * vertexPosition;\n
65     \n
66     vTexCoord = aPosition + vec2(0.5);\n
67     gl_Position = vertexPosition;\n
68   }\n
69 );
70
71 const char* FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
72   varying mediump vec2 vTexCoord;\n
73   uniform sampler2D sTexture;\n
74   uniform lowp vec4 uColor;\n
75   \n
76   void main()\n
77   {\n
78     gl_FragColor = texture2D( sTexture, vTexCoord ) * uColor;\n
79   }\n
80 );
81
82 const char* TEST_IMAGE_FILE_NAME =  "gallery_image_01.jpg";
83 const char* TEST_IMAGE_FILE_NAME2 =  "gallery_image_02.jpg";
84
85 const char* TEST_IMAGE_1 = TEST_RESOURCE_DIR "/TB-gloss.png";
86 const char* TEST_IMAGE_2 = TEST_RESOURCE_DIR "/tb-norm.png";
87
88 // resolution: 34*34, pixel format: RGBA8888
89 static const char* gImage_34_RGBA = TEST_RESOURCE_DIR "/icon-edit.png";
90 // resolution: 600*600, pixel format: RGB888
91 static const char* gImage_600_RGB = TEST_RESOURCE_DIR "/test-image-600.jpg";
92
93 // resolution: 50*50, frame count: 4, frame delay: 0.2 second for each frame
94 const char* TEST_GIF_FILE_NAME = TEST_RESOURCE_DIR "/anim.gif";
95
96 void TestImage( ImageView imageView, BufferImage image )
97 {
98   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
99
100   Property::Map map;
101   DALI_TEST_CHECK( value.Get( map ) );
102
103   DALI_TEST_CHECK( map.Find( "width" ) );
104   DALI_TEST_CHECK( map.Find( "height" ) );
105   DALI_TEST_CHECK( map.Find( "type" ) );
106
107   int width = 0;
108   DALI_TEST_CHECK( map[ "width" ].Get( width ) );
109   DALI_TEST_EQUALS( (unsigned int)width, image.GetWidth(), TEST_LOCATION );
110
111   int height = 0;
112   DALI_TEST_CHECK( map[ "height" ].Get( height ) );
113   DALI_TEST_EQUALS( (unsigned int)height, image.GetHeight(), TEST_LOCATION );
114
115   std::string type;
116   DALI_TEST_CHECK( map[ "type" ].Get( type ) );
117   DALI_TEST_EQUALS( type, "BufferImage", TEST_LOCATION );
118 }
119
120 void TestImage( ImageView imageView, ResourceImage image )
121 {
122   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
123
124   Property::Map map;
125   DALI_TEST_CHECK( value.Get( map ) );
126
127   if( map.Find( "width" ) )
128   {
129     int width = 0;
130     DALI_TEST_CHECK( map[ "width" ].Get( width ) );
131     DALI_TEST_EQUALS( (unsigned int)width, image.GetWidth(), TEST_LOCATION );
132   }
133
134   if( map.Find( "height" ) )
135   {
136     int height = 0;
137     DALI_TEST_CHECK( map[ "height" ].Get( height ) );
138     DALI_TEST_EQUALS( (unsigned int)height, image.GetHeight(), TEST_LOCATION );
139   }
140
141   DALI_TEST_CHECK( map.Find( "type" ) );
142
143   std::string type;
144   DALI_TEST_CHECK( map[ "type" ].Get( type ) );
145   DALI_TEST_EQUALS( type, "ResourceImage", TEST_LOCATION );
146
147   std::string filename;
148   DALI_TEST_CHECK( map[ "filename" ].Get( filename ) );
149   DALI_TEST_EQUALS( filename, image.GetUrl(), TEST_LOCATION );
150 }
151
152 void TestUrl( ImageView imageView, const std::string url )
153 {
154   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
155
156   std::string urlActual;
157   DALI_TEST_CHECK( value.Get( urlActual ) );
158   DALI_TEST_EQUALS( urlActual, url, TEST_LOCATION );
159 }
160
161 } // namespace
162
163 int UtcDaliImageViewNewP(void)
164 {
165   TestApplication application;
166
167   ImageView imageView = ImageView::New();
168
169   DALI_TEST_CHECK( imageView );
170
171   END_TEST;
172 }
173
174 int UtcDaliImageViewNewImageP(void)
175 {
176   TestApplication application;
177
178   BufferImage image = CreateBufferImage( 100, 200, Vector4( 1.f, 1.f, 1.f, 1.f ) );
179   ImageView imageView = ImageView::New( image );
180
181   DALI_TEST_CHECK( imageView );
182   TestImage( imageView, image );
183
184   END_TEST;
185 }
186
187 int UtcDaliImageViewNewUrlP(void)
188 {
189   TestApplication application;
190
191   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
192   DALI_TEST_CHECK( imageView );
193
194   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
195
196   END_TEST;
197 }
198
199 int UtcDaliImageViewConstructorP(void)
200 {
201   TestApplication application;
202
203   ImageView imageView;
204
205   DALI_TEST_CHECK( !imageView );
206
207   END_TEST;
208 }
209
210 int UtcDaliImageViewCopyConstructorP(void)
211 {
212   TestApplication application;
213
214   // Initialize an object, ref count == 1
215   ImageView imageView = ImageView::New();
216
217   ImageView copy( imageView );
218   DALI_TEST_CHECK( copy );
219
220   END_TEST;
221 }
222
223 int UtcDaliImageViewAssignmentOperatorP(void)
224 {
225   TestApplication application;
226
227   ImageView imageView = ImageView::New();
228
229   ImageView copy( imageView );
230   DALI_TEST_CHECK( copy );
231   DALI_TEST_EQUALS( imageView, copy, TEST_LOCATION );
232
233   END_TEST;
234 }
235
236 int UtcDaliImageViewDownCastP(void)
237 {
238   TestApplication application;
239
240   ImageView imageView = ImageView::New();
241
242   BaseHandle object(imageView);
243
244   ImageView imageView2 = ImageView::DownCast( object );
245   DALI_TEST_CHECK(imageView2);
246
247   ImageView imageView3 = DownCast< ImageView >( object );
248   DALI_TEST_CHECK(imageView3);
249
250   END_TEST;
251 }
252
253 int UtcDaliImageViewDownCastN(void)
254 {
255   TestApplication application;
256
257   BaseHandle unInitializedObject;
258
259   ImageView imageView1 = ImageView::DownCast( unInitializedObject );
260   DALI_TEST_CHECK( !imageView1 );
261
262   ImageView imageView2 = DownCast< ImageView >( unInitializedObject );
263   DALI_TEST_CHECK( !imageView2 );
264
265   END_TEST;
266 }
267
268 int UtcDaliImageViewTypeRegistry(void)
269 {
270   ToolkitTestApplication application;
271
272   TypeRegistry typeRegistry = TypeRegistry::Get();
273   DALI_TEST_CHECK( typeRegistry );
274
275   TypeInfo typeInfo = typeRegistry.GetTypeInfo( "ImageView" );
276   DALI_TEST_CHECK( typeInfo );
277
278   BaseHandle handle = typeInfo.CreateInstance();
279   DALI_TEST_CHECK( handle );
280
281   ImageView imageView = ImageView::DownCast( handle );
282   DALI_TEST_CHECK( imageView );
283
284   END_TEST;
285 }
286
287 int UtcDaliImageViewSetGetProperty01(void)
288 {
289   ToolkitTestApplication application;
290
291   ImageView imageView = ImageView::New();
292
293   Property::Index idx = imageView.GetPropertyIndex( "image" );
294   DALI_TEST_EQUALS( idx, (Property::Index)ImageView::Property::IMAGE, TEST_LOCATION );
295
296   imageView.SetProperty( idx, TEST_IMAGE_FILE_NAME );
297   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
298
299   END_TEST;
300 }
301
302 int UtcDaliImageViewSetGetProperty02(void)
303 {
304   ToolkitTestApplication application;
305
306   Image image = CreateBufferImage( 10, 10, Color::WHITE );
307   ImageView imageView = ImageView::New(image);
308   Vector4 fullImageRect( 0.f, 0.f, 1.f, 1.f );
309
310   Stage::GetCurrent().Add( imageView );
311
312   application.SendNotification();
313   application.Render();
314   TestGlAbstraction& gl = application.GetGlAbstraction();
315
316   Vector4 pixelAreaUniform;
317   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
318   DALI_TEST_EQUALS( pixelAreaUniform, fullImageRect, TEST_LOCATION );
319
320   Property::Value value = imageView.GetProperty( ImageView::Property::PIXEL_AREA );
321   Vector4 pixelAreaValue;
322   DALI_TEST_CHECK( value.Get(pixelAreaValue) );
323   DALI_TEST_EQUALS( pixelAreaValue, fullImageRect, TEST_LOCATION );
324
325   Vector4 pixelAreaSet( 0.2f, 0.2f, 0.3f, 0.3f );
326   imageView.SetProperty( ImageView::Property::PIXEL_AREA, pixelAreaSet);
327
328   application.SendNotification();
329   application.Render();
330
331   value = imageView.GetProperty( ImageView::Property::PIXEL_AREA );
332   value.Get(pixelAreaValue);
333   DALI_TEST_EQUALS( pixelAreaValue, pixelAreaSet, TEST_LOCATION );
334
335   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
336   DALI_TEST_EQUALS( pixelAreaUniform, pixelAreaSet, TEST_LOCATION );
337
338   END_TEST;
339 }
340
341 int UtcDaliImageViewSetGetProperty03(void)
342 {
343   ToolkitTestApplication application;
344
345   Image image = CreateBufferImage( 10, 10, Color::WHITE );
346   ImageView imageView = ImageView::New(image);
347   Stage::GetCurrent().Add( imageView );
348   application.SendNotification();
349   application.Render();
350
351   // conventional alpha blending
352   Renderer renderer = imageView.GetRendererAt( 0 );
353   Property::Value value = renderer.GetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
354   bool enable;
355   DALI_TEST_CHECK( value.Get( enable ) );
356   DALI_TEST_CHECK( !enable );
357
358   // pre-multiplied alpha blending
359   imageView.SetProperty( Toolkit::ImageView::Property::PRE_MULTIPLIED_ALPHA, true );
360   application.SendNotification();
361   application.Render();
362
363   int srcFactorRgb    = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_RGB );
364   int destFactorRgb   = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_RGB );
365   int srcFactorAlpha  = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_SRC_ALPHA );
366   int destFactorAlpha = renderer.GetProperty<int>( Renderer::Property::BLEND_FACTOR_DEST_ALPHA );
367   DALI_TEST_CHECK( srcFactorRgb == BlendFactor::ONE );
368   DALI_TEST_CHECK( destFactorRgb == BlendFactor::ONE_MINUS_SRC_ALPHA );
369   DALI_TEST_CHECK( srcFactorAlpha == BlendFactor::ONE );
370   DALI_TEST_CHECK( destFactorAlpha == BlendFactor::ONE_MINUS_SRC_ALPHA );
371
372   value = renderer.GetProperty( Renderer::Property::BLEND_PRE_MULTIPLIED_ALPHA );
373   DALI_TEST_CHECK( value.Get( enable ) );
374   DALI_TEST_CHECK( enable );
375
376   END_TEST;
377 }
378
379 int UtcDaliImageViewPixelArea(void)
380 {
381   // Test pixel area property
382   ToolkitTestApplication application;
383
384   // Gif image, use AnimatedImageVisual internally
385   // Atlasing is applied to pack multiple frames, use custom wrap mode
386   ImageView gifView = ImageView::New();
387   const Vector4 pixelAreaVisual( 0.f, 0.f, 2.f, 2.f );
388   gifView.SetProperty( ImageView::Property::IMAGE,
389                        Property::Map().Add( ImageVisual::Property::URL, TEST_GIF_FILE_NAME )
390                                       .Add( ImageVisual::Property::PIXEL_AREA, pixelAreaVisual ) );
391
392   // Add to stage
393   Stage stage = Stage::GetCurrent();
394   stage.Add( gifView );
395
396   // loading started
397   application.SendNotification();
398   application.Render(16);
399   DALI_TEST_CHECK( gifView.GetRendererCount() == 1u );
400
401   const Vector4 fullTextureRect( 0.f, 0.f, 1.f, 1.f );
402   // test that the pixel area value defined in the visual property map is registered on renderer
403   Renderer renderer = gifView.GetRendererAt(0);
404   Property::Value pixelAreaValue = renderer.GetProperty( renderer.GetPropertyIndex( "pixelArea" ) );
405   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelAreaVisual, TEST_LOCATION );
406
407   // test that the shader has the default pixel area value registered.
408   Shader shader = renderer.GetShader();
409   pixelAreaValue = shader.GetProperty( shader.GetPropertyIndex( "pixelArea" ) );
410   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), fullTextureRect, TEST_LOCATION );
411
412   // test that the uniform uses the pixelArea property on the renderer.
413   TestGlAbstraction& gl = application.GetGlAbstraction();
414   Vector4 pixelAreaUniform;
415   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
416   DALI_TEST_EQUALS( pixelAreaVisual, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
417
418   // set the pixelArea property on the control
419   const Vector4 pixelAreaControl( -1.f, -1.f, 3.f, 3.f );
420   gifView.SetProperty( ImageView::Property::PIXEL_AREA, pixelAreaControl );
421   application.SendNotification();
422   application.Render(16);
423
424   // check the pixelArea property on the control
425   pixelAreaValue = gifView.GetProperty( gifView.GetPropertyIndex( "pixelArea" ) );
426   DALI_TEST_EQUALS( pixelAreaValue.Get<Vector4>(), pixelAreaControl, TEST_LOCATION );
427   // test that the uniform uses the pixelArea property on the control.
428   DALI_TEST_CHECK( gl.GetUniformValue<Vector4>( "pixelArea", pixelAreaUniform ) );
429   DALI_TEST_EQUALS( pixelAreaControl, pixelAreaUniform, Math::MACHINE_EPSILON_100, TEST_LOCATION );
430
431
432   END_TEST;
433 }
434
435 int UtcDaliImageViewAsyncLoadingWithoutAltasing(void)
436 {
437   ToolkitTestApplication application;
438   TestGlAbstraction& gl = application.GetGlAbstraction();
439   const std::vector<GLuint>& textures = gl.GetBoundTextures();
440   size_t numTextures = textures.size();
441
442   // Async loading, no atlasing for big size image
443   ImageView imageView = ImageView::New( gImage_600_RGB );
444
445   // By default, Aysnc loading is used
446   Stage::GetCurrent().Add( imageView );
447   imageView.SetSize(100, 100);
448   imageView.SetParentOrigin( ParentOrigin::CENTER );
449
450   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
451
452   application.SendNotification();
453   application.Render(16);
454   application.SendNotification();
455
456   const std::vector<GLuint>& textures2 = gl.GetBoundTextures();
457   DALI_TEST_GREATER( textures2.size(), numTextures, TEST_LOCATION );
458
459
460
461   END_TEST;
462 }
463
464 int UtcDaliImageViewAsyncLoadingWithAtlasing(void)
465 {
466   ToolkitTestApplication application;
467
468   //Async loading, automatic atlasing for small size image
469   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
470   callStack.Reset();
471   callStack.Enable(true);
472
473   Property::Map imageMap;
474
475   imageMap[ ImageVisual::Property::URL ] = gImage_34_RGBA;
476   imageMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 34;
477   imageMap[ ImageVisual::Property::DESIRED_WIDTH ] = 34;
478   imageMap[ ImageVisual::Property::ATLASING] = true;
479
480   ImageView imageView = ImageView::New();
481   imageView.SetProperty( ImageView::Property::IMAGE, imageMap );
482   imageView.SetProperty( Toolkit::Control::Property::PADDING, Extents( 10u, 10u, 10u, 10u ) );
483
484   // By default, Aysnc loading is used
485   // loading is not started if the actor is offStage
486
487   Stage::GetCurrent().Add( imageView );
488   application.SendNotification();
489   application.Render(16);
490   application.Render(16);
491   application.SendNotification();
492
493   imageView.SetProperty( Dali::Actor::Property::LAYOUT_DIRECTION,  Dali::LayoutDirection::RIGHT_TO_LEFT );
494   application.SendNotification();
495   application.Render(16);
496   application.Render(16);
497   application.SendNotification();
498
499   // loading started, this waits for the loader thread for max 30 seconds
500   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
501
502   application.SendNotification();
503   application.Render(16);
504
505   callStack.Enable(false);
506
507   TraceCallStack::NamedParams params;
508   params["width"] = ToString(34);
509   params["height"] = ToString(34);
510   DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ), true, TEST_LOCATION );
511
512   END_TEST;
513 }
514
515 int UtcDaliImageViewAsyncLoadingWithAtlasing02(void)
516 {
517   ToolkitTestApplication application;
518
519   //Async loading, automatic atlasing for small size image
520   TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
521   callStack.Reset();
522   callStack.Enable(true);
523
524   Property::Map asyncLoadingMap;
525   asyncLoadingMap[ "url" ] = gImage_34_RGBA;
526   asyncLoadingMap[ "desiredHeight" ] = 34;
527   asyncLoadingMap[ "desiredWidth" ] = 34;
528   asyncLoadingMap[ "synchronousLoading" ] = false;
529   asyncLoadingMap[ "atlasing" ] = true;
530
531   ImageView imageView = ImageView::New();
532   imageView.SetProperty( ImageView::Property::IMAGE, asyncLoadingMap );
533
534   Stage::GetCurrent().Add( imageView );
535   application.SendNotification();
536   application.Render(16);
537   application.Render(16);
538   application.SendNotification();
539
540   // loading started, this waits for the loader thread for max 30 seconds
541   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
542
543   application.SendNotification();
544   application.Render(16);
545
546   callStack.Enable(false);
547
548   TraceCallStack::NamedParams params;
549   params["width"] = ToString(34);
550   params["height"] = ToString(34);
551   DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ), true, TEST_LOCATION );
552
553   END_TEST;
554 }
555
556 int UtcDaliImageViewSyncLoading(void)
557 {
558   ToolkitTestApplication application;
559
560   tet_infoline("ImageView Testing sync loading and size using index key property map");
561
562   Property::Map syncLoadingMap;
563   syncLoadingMap[ ImageVisual::Property::SYNCHRONOUS_LOADING ] = true;
564   syncLoadingMap[ ImageVisual::Property::ATLASING ] = true;
565
566   // Sync loading, no atlasing for big size image
567   {
568     ImageView imageView = ImageView::New();
569
570     // Sync loading is used
571     syncLoadingMap[ ImageVisual::Property::URL ] = gImage_600_RGB;
572     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
573   }
574
575   // Sync loading, automatic atlasing for small size image
576   {
577     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
578     callStack.Reset();
579     callStack.Enable(true);
580
581     ImageView imageView = ImageView::New( );
582
583     // Sync loading is used
584     syncLoadingMap[ ImageVisual::Property::URL ] = gImage_34_RGBA;
585     syncLoadingMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 34;
586     syncLoadingMap[ ImageVisual::Property::DESIRED_WIDTH ] = 34;
587     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
588
589     Stage::GetCurrent().Add( imageView );
590     application.SendNotification();
591     application.Render(16);
592
593     TraceCallStack::NamedParams params;
594     params["width"] = ToString(34);
595     params["height"] = ToString(34);
596     DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ),
597                       true, TEST_LOCATION );
598   }
599   END_TEST;
600 }
601
602 int UtcDaliImageViewSyncLoading02(void)
603 {
604   ToolkitTestApplication application;
605
606   tet_infoline("ImageView Testing sync loading and size using string key property map");
607
608   // Sync loading, automatic atlasing for small size image
609   {
610     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
611     callStack.Reset();
612     callStack.Enable(true);
613
614     ImageView imageView = ImageView::New( );
615
616     // Sync loading is used
617     Property::Map syncLoadingMap;
618     syncLoadingMap[ "url" ] = gImage_34_RGBA;
619     syncLoadingMap[ "desiredHeight" ] = 34;
620     syncLoadingMap[ "desiredWidth" ] = 34;
621     syncLoadingMap[ "synchronousLoading" ] = true;
622     syncLoadingMap[ "atlasing" ] = true;
623     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
624
625     Stage::GetCurrent().Add( imageView );
626     application.SendNotification();
627     application.Render(16);
628
629     TraceCallStack::NamedParams params;
630     params["width"] = ToString(34);
631     params["height"] = ToString(34);
632     DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ),
633                       true, TEST_LOCATION );
634   }
635   END_TEST;
636 }
637
638 int UtcDaliImageViewAddedTexture(void)
639 {
640   ToolkitTestApplication application;
641
642   tet_infoline("ImageView Testing image view with texture provided manager url");
643
644   ImageView imageView = ImageView::New();
645
646   // empty texture is ok, though pointless from app point of view
647   TextureSet  empty;
648   std::string url = TextureManager::AddTexture(empty);
649   DALI_TEST_CHECK(url.size() > 0u);
650
651   Property::Map propertyMap;
652   propertyMap[ImageVisual::Property::URL] = url;
653   imageView.SetProperty(ImageView::Property::IMAGE, propertyMap);
654
655   Stage::GetCurrent().Add( imageView );
656   application.SendNotification();
657   application.Render();
658
659   END_TEST;
660 }
661
662 int UtcDaliImageViewSizeWithBackground(void)
663 {
664   ToolkitTestApplication application;
665
666   int width = 100;
667   int height = 200;
668   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
669   ImageView imageView = ImageView::New();
670   imageView.SetBackgroundImage( image );
671
672   Stage::GetCurrent().Add( imageView );
673   application.SendNotification();
674   application.Render();
675
676   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
677   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
678
679   END_TEST;
680 }
681
682 int UtcDaliImageViewSizeWithBackgroundAndImage(void)
683 {
684   ToolkitTestApplication application;
685
686   int widthBackground = 100;
687   int heightBackground = 200;
688   int width = 300;
689   int height = 400;
690   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
691   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
692
693   ImageView imageView = ImageView::New();
694   imageView.SetBackgroundImage( imageBackground );
695   imageView.SetImage( image );
696
697   Stage::GetCurrent().Add( imageView );
698   application.SendNotification();
699   application.Render();
700
701   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
702   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
703
704   END_TEST;
705 }
706
707 int UtcDaliImageViewHeightForWidthBackground(void)
708 {
709   ToolkitTestApplication application;
710
711   int widthBackground = 100;
712   int heightBackground = 200;
713   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
714
715   ImageView imageView = ImageView::New();
716   imageView.SetBackgroundImage( imageBackground );
717
718   Stage::GetCurrent().Add( imageView );
719   application.SendNotification();
720   application.Render();
721
722   Control control = Control::DownCast( imageView );
723   DALI_TEST_CHECK( control );
724   DALI_TEST_EQUALS( imageView.GetHeightForWidth( 123.f ), control.GetHeightForWidth( 123.f ), TEST_LOCATION );
725   DALI_TEST_EQUALS( imageView.GetWidthForHeight( 321.f ), control.GetWidthForHeight( 321.f ), TEST_LOCATION );
726
727   END_TEST;
728 }
729
730 int UtcDaliImageViewHeightForWidthBackgroundAndImage(void)
731 {
732   ToolkitTestApplication application;
733
734   int widthBackground = 100;
735   int heightBackground = 200;
736   int width = 300;
737   int height = 400;
738   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
739   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
740
741   ImageView imageView = ImageView::New();
742   imageView.SetBackgroundImage( imageBackground );
743   imageView.SetImage( image );
744
745   Stage::GetCurrent().Add( imageView );
746   application.SendNotification();
747   application.Render();
748
749   DALI_TEST_EQUALS( imageView.GetHeightForWidth( width ), (float)height, TEST_LOCATION );
750   DALI_TEST_EQUALS( imageView.GetWidthForHeight( height ), (float)width, TEST_LOCATION );
751
752   END_TEST;
753 }
754
755 int UtcDaliImageViewSetBufferImage(void)
756 {
757   ToolkitTestApplication application;
758
759   int width1 = 300;
760   int height1 = 400;
761   BufferImage image1 = CreateBufferImage( width1, height1, Vector4( 1.f, 1.f, 1.f, 1.f ) );
762   ImageView imageView = ImageView::New();
763   imageView.SetImage( image1 );
764
765   TestImage( imageView, image1 );
766
767   int width2 = 600;
768   int height2 = 500;
769   BufferImage image2 = CreateBufferImage( width2, height2, Vector4( 1.f, 1.f, 1.f, 1.f ) );
770   imageView.SetImage( image2 );
771
772   TestImage( imageView, image2 );
773
774   END_TEST;
775 }
776
777 int UtcDaliImageViewSetImageUrl(void)
778 {
779   ToolkitTestApplication application;
780
781   ImageView imageView = ImageView::New();
782   imageView.SetImage( TEST_IMAGE_FILE_NAME );
783   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
784
785
786   imageView.SetImage( TEST_IMAGE_FILE_NAME2 );
787   TestUrl( imageView, TEST_IMAGE_FILE_NAME2 );
788
789   END_TEST;
790 }
791
792 int UtcDaliImageViewSetImageOnstageP(void)
793 {
794   ToolkitTestApplication application;
795
796   ImageView imageView = ImageView::New();
797
798   Stage::GetCurrent().Add( imageView );
799   application.SendNotification();
800   application.Render();
801
802   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
803   imageView.SetImage( image1 );
804   TestImage( imageView, image1 );
805
806   int width = 300;
807   int height = 400;
808   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
809   imageView.SetImage( image2 );
810   TestImage( imageView, image2 );
811
812   END_TEST;
813 }
814
815 int UtcDaliImageViewSetImageOnstageN(void)
816 {
817   ToolkitTestApplication application;
818
819   ImageView imageView = ImageView::New();
820
821   Stage::GetCurrent().Add( imageView );
822   application.SendNotification();
823   application.Render();
824
825   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
826   imageView.SetImage( image1 );
827   TestImage( imageView, image1 );
828
829   Image image2;
830   imageView.SetImage( image2 );
831
832   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
833
834   //the value should be empty
835   std::string url;
836   DALI_TEST_CHECK( !value.Get( url ) );
837
838   Property::Map map;
839   DALI_TEST_CHECK( !value.Get( map ) );
840
841   END_TEST;
842 }
843
844 int UtcDaliImageViewSetImageOffstageP(void)
845 {
846   ToolkitTestApplication application;
847
848   ImageView imageView = ImageView::New();
849
850   Stage::GetCurrent().Add( imageView );
851   application.SendNotification();
852   application.Render();
853   Stage::GetCurrent().Remove( imageView );
854
855   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
856   imageView.SetImage( image1 );
857   TestImage( imageView, image1 );
858
859   int width = 300;
860   int height = 400;
861   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
862   imageView.SetImage( image2 );
863   TestImage( imageView, image2 );
864
865   END_TEST;
866 }
867
868 bool gResourceReadySignalFired = false;
869 Vector3 gNaturalSize;
870
871 void ResourceReadySignal( Control control )
872 {
873   gResourceReadySignalFired = true;
874 }
875
876 int UtcDaliImageViewCheckResourceReady(void)
877 {
878   ToolkitTestApplication application;
879
880   gResourceReadySignalFired = false;
881
882
883   int width = 100;
884   int height = 200;
885   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
886
887   // Check ImageView with background and main image, to ensure both visuals are marked as loaded
888   ImageView imageView = ImageView::New( TEST_GIF_FILE_NAME );
889
890   imageView.SetBackgroundImage( image );
891
892   DALI_TEST_EQUALS( imageView.IsResourceReady(), false, TEST_LOCATION );
893
894   imageView.ResourceReadySignal().Connect( &ResourceReadySignal);
895
896   Stage::GetCurrent().Add( imageView );
897
898   application.SendNotification();
899   application.Render(16);
900
901
902   DALI_TEST_EQUALS( imageView.IsResourceReady(), true, TEST_LOCATION );
903
904   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
905
906   END_TEST;
907 }
908
909 int UtcDaliImageViewSetImageOffstageN(void)
910 {
911   ToolkitTestApplication application;
912
913   ImageView imageView = ImageView::New();
914
915   Stage::GetCurrent().Add( imageView );
916   application.SendNotification();
917   application.Render();
918   Stage::GetCurrent().Remove( imageView );
919
920   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
921   imageView.SetImage( image1 );
922   TestImage( imageView, image1 );
923
924   Image image2;
925   imageView.SetImage( image2 );
926
927   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
928
929   //the value should be empty
930   std::string url;
931   DALI_TEST_CHECK( !value.Get( url ) );
932
933   Property::Map map;
934   DALI_TEST_CHECK( !value.Get( map ) );
935
936   END_TEST;
937 }
938
939 int UtcDaliImageViewSetImageN(void)
940 {
941   ToolkitTestApplication application;
942
943   Image image1;
944   ImageView imageView = ImageView::New();
945   imageView.SetImage( image1 );
946
947   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
948
949   //the value should be empty
950   std::string url;
951   DALI_TEST_CHECK( !value.Get( url ) );
952
953   Property::Map map;
954   DALI_TEST_CHECK( !value.Get( map ) );
955
956   std::string resource_url;
957   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
958   DALI_TEST_CHECK( !val.Get( resource_url ) );
959
960   END_TEST;
961 }
962
963 int UtcDaliImageViewSetImageTypeChangesP(void)
964 {
965   ToolkitTestApplication application;
966
967   ImageView imageView = ImageView::New();
968   Toolkit::Internal::Control& controlImpl = Toolkit::Internal::GetImplementation( imageView );
969
970   Stage::GetCurrent().Add( imageView );
971
972   std::string url;
973   Property::Map map;
974   Toolkit::Visual::Base visual;
975
976   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
977   visual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
978
979   application.SendNotification();
980   application.Render( 16 );
981
982   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
983   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
984   DALI_TEST_CHECK( ! visual );           // Visual should be invalid
985
986   // Set a URL
987   imageView.SetImage( "TEST_URL" );
988
989   application.SendNotification();
990   application.Render( 16 );
991
992   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
993   visual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
994
995   DALI_TEST_CHECK( value.Get( url ) );   // Value should NOT be empty
996   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
997   DALI_TEST_CHECK( visual );             // Visual should be valid
998
999   // Set an empty Image
1000   imageView.SetImage( Image() );
1001
1002   application.SendNotification();
1003   application.Render( 16 );
1004
1005   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
1006   visual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
1007
1008   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
1009   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
1010   DALI_TEST_CHECK( ! visual );           // Visual should be invalid
1011
1012   // Set an Image
1013   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
1014   imageView.SetImage( image1 );
1015
1016   application.SendNotification();
1017   application.Render( 16 );
1018
1019   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
1020   visual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
1021
1022   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
1023   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
1024   DALI_TEST_CHECK( visual );             // Visual should be valid
1025
1026   // Set an empty URL
1027   imageView.SetImage( "" );
1028
1029   application.SendNotification();
1030   application.Render( 16 );
1031
1032   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
1033   visual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
1034
1035   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
1036   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
1037   DALI_TEST_CHECK( ! visual );           // Visual should be invalid
1038
1039   // Set a URL in property map
1040   Property::Map propertyMap;
1041   propertyMap[ImageVisual::Property::URL] = TEST_IMAGE_FILE_NAME;
1042   imageView.SetProperty( ImageView::Property::IMAGE, propertyMap );
1043
1044   application.SendNotification();
1045   application.Render( 16 );
1046
1047   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
1048   visual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
1049
1050   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
1051   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
1052   DALI_TEST_CHECK( visual );             // Visual should be valid
1053
1054   // Set a URL in property map again
1055   propertyMap[ImageVisual::Property::URL] = gImage_34_RGBA;
1056   imageView.SetProperty( ImageView::Property::IMAGE, propertyMap );
1057
1058   application.SendNotification();
1059   application.Render( 16 );
1060
1061   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
1062   visual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
1063
1064   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
1065   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
1066   DALI_TEST_CHECK( visual );             // Visual should be valid
1067
1068   // Set an empty URL in property map
1069   propertyMap[ImageVisual::Property::URL] = std::string();
1070   imageView.SetProperty( ImageView::Property::IMAGE, propertyMap );
1071
1072   application.SendNotification();
1073   application.Render( 16 );
1074
1075   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
1076   visual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
1077
1078   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
1079   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
1080   DALI_TEST_CHECK( ! visual );           // Visual should be invalid
1081
1082   END_TEST;
1083 }
1084
1085 int UtcDaliImageViewResourceUrlP(void)
1086 {
1087   ToolkitTestApplication application;
1088
1089   ImageView imageView = ImageView::New();
1090   DALI_TEST_CHECK( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >().empty() );
1091
1092   imageView.SetProperty( ImageView::Property::RESOURCE_URL, "TestString" );
1093   DALI_TEST_EQUALS( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >(), "TestString", TEST_LOCATION );
1094
1095   END_TEST;
1096 }
1097
1098 // Scenarios 1: ImageView from regular image
1099 int UtcDaliImageViewSetImageBufferImage(void)
1100 {
1101   ToolkitTestApplication application;
1102
1103   ImageView imageView = ImageView::New();
1104   Stage::GetCurrent().Add( imageView );
1105
1106   TestGlAbstraction& gl = application.GetGlAbstraction();
1107   gl.EnableTextureCallTrace( true );
1108
1109   std::vector< GLuint > ids;
1110   ids.push_back( 23 );
1111   application.GetGlAbstraction().SetNextTextureIds( ids );
1112
1113   int width = 300;
1114   int height = 400;
1115   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1116
1117   imageView.SetImage( image );
1118
1119   application.SendNotification();
1120   application.Render();
1121
1122   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1123
1124   std::stringstream params;
1125   params << GL_TEXTURE_2D << ", " << 23;
1126   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1127
1128   END_TEST;
1129 }
1130
1131 // Scenarios 2: ImageView from Native image
1132 int UtcDaliImageViewSetImageNativeImage(void)
1133 {
1134   ToolkitTestApplication application;
1135
1136   ImageView imageView = ImageView::New();
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   int width = 200;
1147   int height = 500;
1148   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1149   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1150
1151   imageView.SetImage( nativeImage );
1152   application.SendNotification();
1153   application.Render();
1154
1155   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1156
1157   std::stringstream params;
1158   params << GL_TEXTURE_EXTERNAL_OES << ", " << 23;
1159   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1160
1161   END_TEST;
1162 }
1163
1164 // Scenarios 3: ImageView initially from regular image but then SetImage called with Native image
1165 int UtcDaliImageViewSetImageBufferImageToNativeImage(void)
1166 {
1167   ToolkitTestApplication application;
1168
1169   int width = 300;
1170   int height = 400;
1171   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1172
1173   ImageView imageView = ImageView::New( image );
1174   Stage::GetCurrent().Add( imageView );
1175
1176   TestGlAbstraction& gl = application.GetGlAbstraction();
1177   gl.EnableTextureCallTrace( true );
1178
1179   std::vector< GLuint > ids;
1180   ids.push_back( 23 );
1181   application.GetGlAbstraction().SetNextTextureIds( ids );
1182
1183   application.SendNotification();
1184   application.Render();
1185
1186   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1187
1188   std::stringstream params;
1189   params << GL_TEXTURE_2D << ", " << 23;
1190   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1191
1192   width = 200;
1193   height = 500;
1194   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1195   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1196   imageView.SetImage( nativeImage );
1197
1198   ids.clear();
1199   ids.push_back( 24 );
1200   application.GetGlAbstraction().SetNextTextureIds( ids );
1201
1202   application.SendNotification();
1203   application.Render();
1204
1205   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1206
1207   std::stringstream nextTextureParams;
1208   nextTextureParams << GL_TEXTURE_EXTERNAL_OES << ", " << 24;
1209   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
1210
1211   END_TEST;
1212 }
1213
1214 // Scenarios 4: ImageView initially from Native image but then SetImage called with regular image
1215 int UtcDaliImageViewSetImageNativeImageToBufferImage(void)
1216 {
1217   ToolkitTestApplication application;
1218
1219   int width = 300;
1220   int height = 400;
1221   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1222   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1223
1224   ImageView imageView = ImageView::New( nativeImage );
1225   Stage::GetCurrent().Add( imageView );
1226
1227   TestGlAbstraction& gl = application.GetGlAbstraction();
1228   gl.EnableTextureCallTrace( true );
1229
1230   std::vector< GLuint > ids;
1231   ids.push_back( 23 );
1232   application.GetGlAbstraction().SetNextTextureIds( ids );
1233
1234   application.SendNotification();
1235   application.Render();
1236
1237   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1238
1239   std::stringstream params;
1240   params << GL_TEXTURE_EXTERNAL_OES << ", " << 23;
1241   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1242
1243   width = 200;
1244   height = 500;
1245   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1246   imageView.SetImage( image );
1247
1248   ids.clear();
1249   ids.push_back( 24 );
1250   application.GetGlAbstraction().SetNextTextureIds( ids );
1251
1252   application.SendNotification();
1253   application.Render();
1254
1255   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1256
1257   std::stringstream nextTextureParams;
1258   nextTextureParams << GL_TEXTURE_2D << ", " << 24;
1259   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
1260
1261   END_TEST;
1262 }
1263
1264 // Scenarios 5: ImageView from Native image with custom shader
1265 int UtcDaliImageViewSetImageNativeImageWithCustomShader(void)
1266 {
1267   ToolkitTestApplication application;
1268
1269   int width = 300;
1270   int height = 400;
1271
1272   Property::Map customShader;
1273   customShader.Insert( "vertexShader", VERTEX_SHADER );
1274   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
1275
1276   Property::Array shaderHints;
1277   shaderHints.PushBack( "requiresSelfDepthTest" );
1278   shaderHints.PushBack( "outputIsTransparent" );
1279   shaderHints.PushBack( "outputIsOpaque" );
1280   shaderHints.PushBack( "modifiesGeometry" );
1281
1282   customShader.Insert( "hints", shaderHints );
1283
1284   Property::Map map;
1285   map.Insert( "shader", customShader );
1286
1287   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1288   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1289
1290   ImageView imageView = ImageView::New( nativeImage );
1291   imageView.SetProperty( ImageView::Property::IMAGE, map );
1292   Stage::GetCurrent().Add( imageView );
1293
1294   TestGlAbstraction& gl = application.GetGlAbstraction();
1295   gl.EnableTextureCallTrace( true );
1296
1297   std::vector< GLuint > ids;
1298   ids.push_back( 23 );
1299   application.GetGlAbstraction().SetNextTextureIds( ids );
1300
1301   application.SendNotification();
1302   application.Render();
1303
1304   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1305
1306   std::stringstream params;
1307   params << GL_TEXTURE_EXTERNAL_OES << ", " << 23;
1308   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1309
1310   END_TEST;
1311 }
1312
1313 // Scenarios 6: ImageView initially from regular image with custom shader but then SetImage called with Native
1314 int UtcDaliImageViewSetImageBufferImageWithCustomShaderToNativeImage(void)
1315 {
1316   ToolkitTestApplication application;
1317
1318   int width = 300;
1319   int height = 400;
1320
1321   Property::Map customShader;
1322   customShader.Insert( "vertexShader", VERTEX_SHADER );
1323   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
1324
1325   Property::Array shaderHints;
1326   shaderHints.PushBack( "requiresSelfDepthTest" );
1327   shaderHints.PushBack( "outputIsTransparent" );
1328   shaderHints.PushBack( "outputIsOpaque" );
1329   shaderHints.PushBack( "modifiesGeometry" );
1330
1331   customShader.Insert( "hints", shaderHints );
1332
1333   Property::Map map;
1334   map.Insert( "shader", customShader );
1335
1336   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1337
1338   ImageView imageView = ImageView::New( image );
1339   imageView.SetProperty( ImageView::Property::IMAGE, map );
1340   Stage::GetCurrent().Add( imageView );
1341
1342   TestGlAbstraction& gl = application.GetGlAbstraction();
1343   gl.EnableTextureCallTrace( true );
1344
1345   std::vector< GLuint > ids;
1346   ids.push_back( 23 );
1347   application.GetGlAbstraction().SetNextTextureIds( ids );
1348
1349   application.SendNotification();
1350   application.Render();
1351
1352   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1353
1354   std::stringstream params;
1355   params << GL_TEXTURE_2D << ", " << 23;
1356   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1357
1358   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1359   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1360   imageView.SetImage( nativeImage );
1361
1362   ids.clear();
1363   ids.push_back( 24 );
1364   application.GetGlAbstraction().SetNextTextureIds( ids );
1365
1366   application.SendNotification();
1367   application.Render();
1368
1369   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1370
1371   std::stringstream nativeImageParams;
1372   nativeImageParams << GL_TEXTURE_EXTERNAL_OES << ", " << 24;
1373   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nativeImageParams.str()) );
1374
1375
1376   END_TEST;
1377 }
1378
1379 int UtcDaliImageViewGetImageP1(void)
1380 {
1381   ToolkitTestApplication application;
1382
1383   ImageView imageView = ImageView::New();
1384   DALI_TEST_CHECK( ! imageView.GetImage() );
1385
1386   Image image = CreateBufferImage();
1387   imageView.SetImage( image );
1388   DALI_TEST_CHECK( imageView.GetImage() == image );
1389
1390   END_TEST;
1391 }
1392
1393 int UtcDaliImageViewGetImageP2(void)
1394 {
1395   ToolkitTestApplication application;
1396
1397   BufferImage image = CreateBufferImage();
1398   ImageView imageView = ImageView::New( image );
1399   DALI_TEST_CHECK( imageView.GetImage() == image );
1400
1401   END_TEST;
1402 }
1403
1404 int UtcDaliImageViewGetImageN(void)
1405 {
1406   ToolkitTestApplication application;
1407
1408   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
1409   DALI_TEST_CHECK( ! imageView.GetImage() );
1410
1411   Image image = CreateBufferImage();
1412   imageView.SetImage( image );
1413   DALI_TEST_CHECK( imageView.GetImage() == image );
1414
1415   imageView.SetImage( TEST_IMAGE_FILE_NAME );
1416   DALI_TEST_CHECK( ! imageView.GetImage() );
1417
1418   END_TEST;
1419 }
1420
1421
1422 int UtcDaliImageViewReplaceImage(void)
1423 {
1424   ToolkitTestApplication application;
1425
1426   gResourceReadySignalFired = false;
1427
1428   int width = 100;
1429   int height = 200;
1430   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
1431
1432   // Check ImageView with background and main image, to ensure both visuals are marked as loaded
1433   ImageView imageView = ImageView::New( TEST_IMAGE_1 );
1434
1435   DALI_TEST_EQUALS( imageView.IsResourceReady(), false, TEST_LOCATION );
1436
1437   imageView.ResourceReadySignal().Connect( &ResourceReadySignal);
1438
1439   Stage::GetCurrent().Add( imageView );
1440
1441   application.SendNotification();
1442   application.Render(16);
1443
1444   // loading started, this waits for the loader thread for max 30 seconds
1445   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1446
1447   DALI_TEST_EQUALS( imageView.GetRendererCount(), 1u, TEST_LOCATION );
1448
1449   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
1450
1451   gResourceReadySignalFired = false;
1452
1453   imageView.SetImage(TEST_IMAGE_2);
1454
1455   application.SendNotification();
1456   application.Render(16);
1457
1458   // loading started, this waits for the loader thread for max 30 seconds
1459   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1460
1461   DALI_TEST_EQUALS( imageView.GetRendererCount(), 1u, TEST_LOCATION );
1462
1463   DALI_TEST_EQUALS( imageView.IsResourceReady(), true, TEST_LOCATION );
1464
1465   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
1466
1467   END_TEST;
1468 }
1469
1470 void OnRelayoutOverride( Size size )
1471 {
1472   gNaturalSize = size; // Size Relayout is using
1473 }
1474
1475 int UtcDaliImageViewReplaceImageAndGetNaturalSize(void)
1476 {
1477   ToolkitTestApplication application;
1478
1479   // Check ImageView with background and main image, to ensure both visuals are marked as loaded
1480   ImageView imageView = ImageView::New( TEST_IMAGE_1 );
1481   imageView.SetResizePolicy( ResizePolicy::USE_NATURAL_SIZE, Dimension::ALL_DIMENSIONS );
1482
1483   DummyControl dummyControl = DummyControl::New( true );
1484   Impl::DummyControl& dummyImpl = static_cast<Impl::DummyControl&>(dummyControl.GetImplementation());
1485   dummyControl.SetResizePolicy( ResizePolicy::FIT_TO_CHILDREN, Dimension::ALL_DIMENSIONS );
1486
1487   dummyControl.Add( imageView );
1488   dummyImpl.SetRelayoutCallback( &OnRelayoutOverride );
1489   Stage::GetCurrent().Add( dummyControl );
1490
1491   application.SendNotification();
1492   application.Render();
1493
1494   // loading started, this waits for the loader thread for max 30 seconds
1495   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1496
1497   DALI_TEST_EQUALS( gNaturalSize.width, 1024.0f, TEST_LOCATION );
1498   DALI_TEST_EQUALS( gNaturalSize.height, 1024.0f, TEST_LOCATION );
1499
1500   gNaturalSize = Vector3::ZERO;
1501
1502   imageView.SetImage(gImage_600_RGB);
1503
1504   // Waiting for resourceReady so SendNotifcation not called here.
1505
1506   // loading started, this waits for the loader thread for max 30 seconds
1507   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1508
1509   // Trigger a potential relayout
1510   application.SendNotification();
1511   application.Render();
1512
1513   DALI_TEST_EQUALS( gNaturalSize.width, 600.0f, TEST_LOCATION );
1514   DALI_TEST_EQUALS( gNaturalSize.height, 600.0f, TEST_LOCATION );
1515
1516   END_TEST;
1517 }
1518
1519 int UtcDaliImageViewResourceReadySignalWithImmediateLoad(void)
1520 {
1521   tet_infoline("Test Setting Image with IMMEDIATE load and receving ResourceReadySignal before staged.");
1522
1523   ToolkitTestApplication application;
1524
1525   gResourceReadySignalFired = false;
1526
1527   Property::Map imageMap;
1528
1529   imageMap[ ImageVisual::Property::URL ] = gImage_34_RGBA;
1530   imageMap[ ImageVisual::Property::LOAD_POLICY ] =  ImageVisual::LoadPolicy::IMMEDIATE;
1531
1532   tet_infoline("Creating ImageView without URL so image does not start loading");
1533   ImageView imageView = ImageView::New();
1534   tet_infoline("Connect to image loaded signal before setting image");
1535   imageView.ResourceReadySignal().Connect( &ResourceReadySignal);
1536   tet_infoline("Setting Image with IMMEDIATE load, signal already connected so will be triggered.");
1537   imageView.SetProperty( ImageView::Property::IMAGE, imageMap );
1538
1539   // loading started, this waits for the loader thread
1540   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1541
1542   application.SendNotification();
1543   application.Render(16);
1544
1545   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
1546
1547   END_TEST;
1548 }
1549
1550 int UtcDaliImageViewResourceReadySignalWithReusedImage(void)
1551 {
1552   tet_infoline("Test Setting Image that was already loaded by another ImageView and still getting ResourceReadySignal.");
1553
1554   ToolkitTestApplication application;
1555
1556   gResourceReadySignalFired = false;
1557
1558   Property::Map imageMap;
1559
1560   imageMap[ ImageVisual::Property::URL ] = gImage_34_RGBA;
1561   imageMap[ ImageVisual::Property::LOAD_POLICY ] =  ImageVisual::LoadPolicy::IMMEDIATE;
1562
1563   ImageView imageView = ImageView::New();
1564   imageView.ResourceReadySignal().Connect( &ResourceReadySignal);
1565   imageView.SetProperty( ImageView::Property::IMAGE, imageMap );
1566
1567   // loading started, this waits for the loader thread
1568   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1569
1570   application.SendNotification();
1571   application.Render(16);
1572
1573   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
1574   gResourceReadySignalFired = false;
1575
1576   ImageView imageViewWithExistingImage = ImageView::New();
1577   imageViewWithExistingImage.ResourceReadySignal().Connect( &ResourceReadySignal);
1578   imageViewWithExistingImage.SetProperty( ImageView::Property::IMAGE, imageMap );
1579
1580   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
1581
1582   END_TEST;
1583 }
1584
1585 int UtcDaliImageViewResourceReadySignalWithReusedImage02(void)
1586 {
1587   tet_infoline("Test Setting Image that was already loaded by another ImageView and still getting ResourceReadySignal when staged.");
1588
1589   ToolkitTestApplication application;
1590
1591   gResourceReadySignalFired = false;
1592
1593   Property::Map imageImmediateLoadingMap;
1594   imageImmediateLoadingMap[ ImageVisual::Property::URL ] = gImage_34_RGBA;
1595   imageImmediateLoadingMap[ ImageVisual::Property::LOAD_POLICY ] =  ImageVisual::LoadPolicy::IMMEDIATE;
1596
1597   tet_infoline("Immediate load an image");
1598   ImageView imageView = ImageView::New();
1599   imageView.ResourceReadySignal().Connect( &ResourceReadySignal);
1600   imageView.SetProperty( ImageView::Property::IMAGE, imageImmediateLoadingMap );
1601
1602   // loading started, this waits for the loader thread
1603   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
1604
1605   application.SendNotification();
1606   application.Render(16);
1607
1608   tet_infoline("Check image loaded");
1609   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
1610   gResourceReadySignalFired = false;
1611
1612   tet_infoline("Create another ImageView with the same URL");
1613   ImageView imageViewWithExistingImage = ImageView::New( gImage_34_RGBA );
1614   tet_infoline("Connect to ResourceReady signal for second ImageView, it should still fire as resource is ready");
1615   imageViewWithExistingImage.ResourceReadySignal().Connect( &ResourceReadySignal);
1616
1617   Stage::GetCurrent().Add( imageViewWithExistingImage );
1618
1619   DALI_TEST_EQUALS( gResourceReadySignalFired, true, TEST_LOCATION );
1620
1621   END_TEST;
1622 }
1623
1624 int UtcDaliImageViewPaddingProperty(void)
1625 {
1626   ToolkitTestApplication application;
1627
1628   ImageView imageView = ImageView::New();
1629   Property::Map imagePropertyMap;
1630   imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
1631   imagePropertyMap[ Toolkit::ImageVisual::Property::URL ] = TEST_RESOURCE_DIR "/gallery-small-1.jpg" ;
1632   imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = 128;
1633   imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 128;
1634   imageView.SetProperty( Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
1635   imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
1636   imageView.SetParentOrigin( ParentOrigin::TOP_LEFT );
1637   imageView.SetProperty( Control::Property::PADDING, Extents( 15, 10, 5, 10 ) );
1638   Stage::GetCurrent().Add( imageView );
1639
1640   application.SendNotification();
1641   application.Render();
1642
1643   DALI_TEST_EQUALS( imageView.GetProperty<Extents>( Control::Property::PADDING ), Extents( 15, 10, 5, 10 ), TEST_LOCATION );
1644
1645   ImageView childImage = ImageView::New();
1646   childImage.SetBackgroundColor( Color::BLACK );
1647   childImage.SetSize( 10.f, 10.f );
1648   imageView.Add( childImage );
1649
1650   application.SendNotification();
1651   application.Render();
1652
1653   // Child ImageView should be positioned dependinig on Parent ImageView's Padding value
1654   DALI_TEST_EQUALS( childImage.GetProperty<Vector3>( Dali::Actor::Property::POSITION ), Vector3( 15, 5, 0 ), TEST_LOCATION );
1655
1656   // Check whether Image Visual transforms on ImageVieiw::OnRelayout()
1657   Toolkit::Internal::Control& controlImpl = Toolkit::Internal::GetImplementation( imageView );
1658   Toolkit::Visual::Base imageVisual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
1659   Property::Map resultMap;
1660   imageVisual.CreatePropertyMap( resultMap );
1661
1662   Property::Value* transformValue = resultMap.Find( Visual::Property::TRANSFORM );
1663   DALI_TEST_CHECK( transformValue );
1664   Property::Map* retMap = transformValue->GetMap();
1665   DALI_TEST_CHECK( retMap );
1666
1667   // Image Visual should be positioned depending on ImageView's padding
1668   DALI_TEST_EQUALS( retMap->Find( Visual::Transform::Property::OFFSET )->Get< Vector2 >(), Vector2( 15, 5 ), TEST_LOCATION );
1669
1670   END_TEST;
1671 }
1672
1673 int UtcDaliImageViewPaddingProperty02(void)
1674 {
1675   ToolkitTestApplication application;
1676
1677   ImageView imageView = ImageView::New();
1678   Property::Map imagePropertyMap;
1679   imagePropertyMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
1680   imagePropertyMap[ Toolkit::ImageVisual::Property::URL ] = TEST_RESOURCE_DIR "/Kid1.svg" ;
1681   imagePropertyMap[ ImageVisual::Property::DESIRED_WIDTH ] = 128;
1682   imagePropertyMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 128;
1683   imagePropertyMap[ DevelVisual::Property::VISUAL_FITTING_MODE ] = Toolkit::DevelVisual::FIT_KEEP_ASPECT_RATIO;
1684   imageView.SetProperty( Toolkit::ImageView::Property::IMAGE , imagePropertyMap );
1685   imageView.SetAnchorPoint( AnchorPoint::TOP_LEFT );
1686   imageView.SetParentOrigin( ParentOrigin::TOP_LEFT );
1687   imageView.SetProperty( Control::Property::PADDING, Extents( 15, 10, 5, 10 ) );
1688   Stage::GetCurrent().Add( imageView );
1689
1690   application.SendNotification();
1691   application.Render();
1692
1693   DALI_TEST_EQUALS( imageView.GetProperty<Extents>( Control::Property::PADDING ), Extents( 15, 10, 5, 10 ), TEST_LOCATION );
1694
1695   // Check whether Image Visual transforms on ImageVieiw::OnRelayout()
1696   Toolkit::Internal::Control& controlImpl = Toolkit::Internal::GetImplementation( imageView );
1697   Toolkit::Visual::Base imageVisual = DevelControl::GetVisual( controlImpl, ImageView::Property::IMAGE );
1698   Property::Map resultMap;
1699   imageVisual.CreatePropertyMap( resultMap );
1700
1701   Property::Value* transformValue = resultMap.Find( Visual::Property::TRANSFORM );
1702   DALI_TEST_CHECK( transformValue );
1703   Property::Map* retMap = transformValue->GetMap();
1704   DALI_TEST_CHECK( retMap );
1705
1706   // Image Visual should be positioned depending on ImageView's padding
1707   DALI_TEST_EQUALS( retMap->Find( Visual::Transform::Property::OFFSET )->Get< Vector2 >(), Vector2( 15, 5 ), TEST_LOCATION );
1708
1709   END_TEST;
1710 }
1711
1712 int UtcDaliImageViewUsingAtlasAndGetNaturalSize(void)
1713 {
1714   ToolkitTestApplication application;
1715
1716   // Check ImageView with background and main image, to ensure both visuals are marked as loaded
1717   ImageView imageView = ImageView::New();
1718   Property::Map imageMap;
1719   imageMap[ Toolkit::Visual::Property::TYPE ] = Toolkit::Visual::IMAGE;
1720   imageMap[ Toolkit::ImageVisual::Property::URL ] = gImage_34_RGBA;
1721   imageMap[ Toolkit::ImageVisual::Property::ATLASING ] = true;
1722   imageView.SetProperty( Toolkit::ImageView::Property::IMAGE, imageMap );
1723   Stage::GetCurrent().Add( imageView );
1724
1725   // Trigger a potential relayout
1726   application.SendNotification();
1727   application.Render();
1728
1729   Vector3 naturalSize = imageView.GetNaturalSize();
1730
1731   DALI_TEST_EQUALS( naturalSize.width, 34.0f, TEST_LOCATION );
1732   DALI_TEST_EQUALS( naturalSize.height, 34.0f, TEST_LOCATION );
1733
1734   END_TEST;
1735 }
1736