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