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