Cleanup of Visual::Base::SetSize
[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 UtcDaliImageViewAsyncLoadingWithAltasing(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   application.GetPlatform().SetClosestImageSize(Vector2(34, 34));
397   BitmapLoader::ResetLatestCreated();
398   ImageView imageView = ImageView::New( gImage_34_RGBA, ImageDimensions( 34, 34 ) );
399
400   // By default, Aysnc loading is used
401   // loading is not started if the actor is offStage
402   BitmapLoader loader = BitmapLoader::GetLatestCreated();
403   DALI_TEST_CHECK( !loader );
404
405   Stage::GetCurrent().Add( imageView );
406   application.SendNotification();
407   application.Render(16);
408   application.Render(16);
409   application.SendNotification();
410
411   // loading started
412   loader = BitmapLoader::GetLatestCreated();
413   DALI_TEST_CHECK( loader );
414
415   loader.WaitForLoading();// waiting until the image to be loaded
416   DALI_TEST_CHECK( loader.IsLoaded() );
417
418   DALI_TEST_EQUALS( Test::WaitForEventThreadTrigger( 1 ), true, TEST_LOCATION );
419
420   application.SendNotification();
421   application.Render(16);
422
423   callStack.Enable(false);
424
425   TraceCallStack::NamedParams params;
426   params["width"] = ToString(34);
427   params["height"] = ToString(34);
428   DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ), true, TEST_LOCATION );
429
430   END_TEST;
431 }
432
433 int UtcDaliImageViewSyncLoading(void)
434 {
435   ToolkitTestApplication application;
436
437   Property::Map syncLoadingMap;
438   syncLoadingMap[ "synchronousLoading" ] = true;
439
440   // Sync loading, no atlasing for big size image
441   {
442     ImageView imageView = ImageView::New();
443
444     // Sync loading is used
445     syncLoadingMap[ ImageVisual::Property::URL ] = gImage_600_RGB;
446     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
447
448     // BitmapLoader is used, and the loading is started immediately even the actor is not on stage.
449     BitmapLoader loader = BitmapLoader::GetLatestCreated();
450     DALI_TEST_CHECK( loader );
451   }
452
453   // Sync loading, automatic atlasing for small size image
454   {
455     BitmapLoader::ResetLatestCreated();
456     TraceCallStack& callStack = application.GetGlAbstraction().GetTextureTrace();
457     callStack.Reset();
458     callStack.Enable(true);
459
460     ImageView imageView = ImageView::New( );
461     // Sync loading is used
462     syncLoadingMap[ ImageVisual::Property::URL ] = gImage_34_RGBA;
463     syncLoadingMap[ ImageVisual::Property::DESIRED_HEIGHT ] = 34;
464     syncLoadingMap[ ImageVisual::Property::DESIRED_WIDTH ] = 34;
465     imageView.SetProperty( ImageView::Property::IMAGE, syncLoadingMap );
466
467     // loading is started even if the actor is offStage
468     BitmapLoader loader = BitmapLoader::GetLatestCreated();
469     DALI_TEST_CHECK( loader );
470
471     loader.WaitForLoading();
472
473     DALI_TEST_CHECK( loader.IsLoaded() );
474
475     Stage::GetCurrent().Add( imageView );
476     application.SendNotification();
477     application.Render(16);
478
479     TraceCallStack::NamedParams params;
480     params["width"] = ToString(34);
481     params["height"] = ToString(34);
482     DALI_TEST_EQUALS( callStack.FindMethodAndParams( "TexSubImage2D", params ),
483                       true, TEST_LOCATION );
484   }
485   END_TEST;
486 }
487
488 int UtcDaliImageViewSizeWithBackground(void)
489 {
490   ToolkitTestApplication application;
491
492   int width = 100;
493   int height = 200;
494   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
495   ImageView imageView = ImageView::New();
496   imageView.SetBackgroundImage( image );
497
498   Stage::GetCurrent().Add( imageView );
499   application.SendNotification();
500   application.Render();
501
502   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
503   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
504
505   END_TEST;
506 }
507
508 int UtcDaliImageViewSizeWithBackgroundAndImage(void)
509 {
510   ToolkitTestApplication application;
511
512   int widthBackground = 100;
513   int heightBackground = 200;
514   int width = 300;
515   int height = 400;
516   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
517   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
518
519   ImageView imageView = ImageView::New();
520   imageView.SetBackgroundImage( imageBackground );
521   imageView.SetImage( image );
522
523   Stage::GetCurrent().Add( imageView );
524   application.SendNotification();
525   application.Render();
526
527   DALI_TEST_EQUALS( imageView.GetCurrentSize().width, (float)width, TEST_LOCATION );
528   DALI_TEST_EQUALS( imageView.GetCurrentSize().height, (float)height, TEST_LOCATION );
529
530   END_TEST;
531 }
532
533 int UtcDaliImageViewHeightForWidthBackground(void)
534 {
535   ToolkitTestApplication application;
536
537   int widthBackground = 100;
538   int heightBackground = 200;
539   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
540
541   ImageView imageView = ImageView::New();
542   imageView.SetBackgroundImage( imageBackground );
543
544   Stage::GetCurrent().Add( imageView );
545   application.SendNotification();
546   application.Render();
547
548   Control control = Control::DownCast( imageView );
549   DALI_TEST_CHECK( control );
550   DALI_TEST_EQUALS( imageView.GetHeightForWidth( 123.f ), control.GetHeightForWidth( 123.f ), TEST_LOCATION );
551   DALI_TEST_EQUALS( imageView.GetWidthForHeight( 321.f ), control.GetWidthForHeight( 321.f ), TEST_LOCATION );
552
553   END_TEST;
554 }
555
556 int UtcDaliImageViewHeightForWidthBackgroundAndImage(void)
557 {
558   ToolkitTestApplication application;
559
560   int widthBackground = 100;
561   int heightBackground = 200;
562   int width = 300;
563   int height = 400;
564   Image imageBackground = CreateBufferImage( widthBackground, heightBackground, Vector4(1.f, 1.f, 1.f, 1.f) );
565   Image image = CreateBufferImage( width, height, Vector4(1.f, 1.f, 1.f, 1.f) );
566
567   ImageView imageView = ImageView::New();
568   imageView.SetBackgroundImage( imageBackground );
569   imageView.SetImage( image );
570
571   Stage::GetCurrent().Add( imageView );
572   application.SendNotification();
573   application.Render();
574
575   DALI_TEST_EQUALS( imageView.GetHeightForWidth( width ), (float)height, TEST_LOCATION );
576   DALI_TEST_EQUALS( imageView.GetWidthForHeight( height ), (float)width, TEST_LOCATION );
577
578   END_TEST;
579 }
580
581 int UtcDaliImageViewSetBufferImage(void)
582 {
583   ToolkitTestApplication application;
584
585   int width1 = 300;
586   int height1 = 400;
587   BufferImage image1 = CreateBufferImage( width1, height1, Vector4( 1.f, 1.f, 1.f, 1.f ) );
588   ImageView imageView = ImageView::New();
589   imageView.SetImage( image1 );
590
591   TestImage( imageView, image1 );
592
593   int width2 = 600;
594   int height2 = 500;
595   BufferImage image2 = CreateBufferImage( width2, height2, Vector4( 1.f, 1.f, 1.f, 1.f ) );
596   imageView.SetImage( image2 );
597
598   TestImage( imageView, image2 );
599
600   END_TEST;
601 }
602
603 int UtcDaliImageViewSetImageUrl(void)
604 {
605   ToolkitTestApplication application;
606
607   ImageView imageView = ImageView::New();
608   imageView.SetImage( TEST_IMAGE_FILE_NAME );
609   TestUrl( imageView, TEST_IMAGE_FILE_NAME );
610
611
612   imageView.SetImage( TEST_IMAGE_FILE_NAME2 );
613   TestUrl( imageView, TEST_IMAGE_FILE_NAME2 );
614
615   END_TEST;
616 }
617
618 int UtcDaliImageViewSetImageOnstageP(void)
619 {
620   ToolkitTestApplication application;
621
622   ImageView imageView = ImageView::New();
623
624   Stage::GetCurrent().Add( imageView );
625   application.SendNotification();
626   application.Render();
627
628   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
629   imageView.SetImage( image1 );
630   TestImage( imageView, image1 );
631
632   int width = 300;
633   int height = 400;
634   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
635   imageView.SetImage( image2 );
636   TestImage( imageView, image2 );
637
638   END_TEST;
639 }
640
641 int UtcDaliImageViewSetImageOnstageN(void)
642 {
643   ToolkitTestApplication application;
644
645   ImageView imageView = ImageView::New();
646
647   Stage::GetCurrent().Add( imageView );
648   application.SendNotification();
649   application.Render();
650
651   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
652   imageView.SetImage( image1 );
653   TestImage( imageView, image1 );
654
655   Image image2;
656   imageView.SetImage( image2 );
657
658   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
659
660   //the value should be empty
661   std::string url;
662   DALI_TEST_CHECK( !value.Get( url ) );
663
664   Property::Map map;
665   DALI_TEST_CHECK( !value.Get( map ) );
666
667   END_TEST;
668 }
669
670 int UtcDaliImageViewSetImageOffstageP(void)
671 {
672   ToolkitTestApplication application;
673
674   ImageView imageView = ImageView::New();
675
676   Stage::GetCurrent().Add( imageView );
677   application.SendNotification();
678   application.Render();
679   Stage::GetCurrent().Remove( imageView );
680
681   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
682   imageView.SetImage( image1 );
683   TestImage( imageView, image1 );
684
685   int width = 300;
686   int height = 400;
687   BufferImage image2 = CreateBufferImage( width, height, Vector4( 1.f, 1.f, 1.f, 1.f ) );
688   imageView.SetImage( image2 );
689   TestImage( imageView, image2 );
690
691   END_TEST;
692 }
693
694 int UtcDaliImageViewSetImageOffstageN(void)
695 {
696   ToolkitTestApplication application;
697
698   ImageView imageView = ImageView::New();
699
700   Stage::GetCurrent().Add( imageView );
701   application.SendNotification();
702   application.Render();
703   Stage::GetCurrent().Remove( imageView );
704
705   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
706   imageView.SetImage( image1 );
707   TestImage( imageView, image1 );
708
709   Image image2;
710   imageView.SetImage( image2 );
711
712   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
713
714   //the value should be empty
715   std::string url;
716   DALI_TEST_CHECK( !value.Get( url ) );
717
718   Property::Map map;
719   DALI_TEST_CHECK( !value.Get( map ) );
720
721   END_TEST;
722 }
723
724 int UtcDaliImageViewSetImageN(void)
725 {
726   ToolkitTestApplication application;
727
728   Image image1;
729   ImageView imageView = ImageView::New();
730   imageView.SetImage( image1 );
731
732   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
733
734   //the value should be empty
735   std::string url;
736   DALI_TEST_CHECK( !value.Get( url ) );
737
738   Property::Map map;
739   DALI_TEST_CHECK( !value.Get( map ) );
740
741   std::string resource_url;
742   Property::Value val = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
743   DALI_TEST_CHECK( !val.Get( resource_url ) );
744
745   END_TEST;
746 }
747
748 int UtcDaliImageViewSetImageTypeChangesP(void)
749 {
750   ToolkitTestApplication application;
751
752   ImageView imageView = ImageView::New();
753
754
755   std::string url;
756   Property::Map map;
757
758   Property::Value value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
759   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
760   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
761
762   // Set a URL
763   imageView.SetImage( "TEST_URL" );
764   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
765
766   DALI_TEST_CHECK( value.Get( url ) );   // Value should NOT be empty
767   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
768
769   // Set an empty Image
770   imageView.SetImage( Image() );
771   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
772
773   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
774   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
775
776   // Set an Image
777   ResourceImage image1 = ResourceImage::New( TEST_IMAGE_FILE_NAME );
778   imageView.SetImage( image1 );
779   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
780
781   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
782   DALI_TEST_CHECK( value.Get( map ) );   // Value should NOT be empty
783
784   // Set an empty URL
785   imageView.SetImage( "" );
786   value = imageView.GetProperty( imageView.GetPropertyIndex( "image" ) );
787
788   DALI_TEST_CHECK( ! value.Get( url ) ); // Value should be empty
789   DALI_TEST_CHECK( ! value.Get( map ) ); // Value should be empty
790
791   END_TEST;
792 }
793
794 int UtcDaliImageViewResourceUrlP(void)
795 {
796   ToolkitTestApplication application;
797
798   ImageView imageView = ImageView::New();
799   DALI_TEST_CHECK( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >().empty() );
800
801   imageView.SetProperty( ImageView::Property::RESOURCE_URL, "TestString" );
802   DALI_TEST_EQUALS( imageView.GetProperty( ImageView::Property::RESOURCE_URL ).Get< std::string >(), "TestString", TEST_LOCATION );
803
804   END_TEST;
805 }
806
807 // Scenarios 1: ImageView from regular image
808 int UtcDaliImageViewSetImageBufferImage(void)
809 {
810   ToolkitTestApplication application;
811
812   ImageView imageView = ImageView::New();
813   Stage::GetCurrent().Add( imageView );
814
815   TestGlAbstraction& gl = application.GetGlAbstraction();
816   gl.EnableTextureCallTrace( true );
817
818   std::vector< GLuint > ids;
819   ids.push_back( 23 );
820   application.GetGlAbstraction().SetNextTextureIds( ids );
821
822   int width = 300;
823   int height = 400;
824   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
825
826   imageView.SetImage( image );
827
828   application.SendNotification();
829   application.Render();
830
831   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
832
833   std::stringstream params;
834   params << GL_TEXTURE_2D << ", " << 23;
835   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
836
837   END_TEST;
838 }
839
840 // Scenarios 2: ImageView from Native image
841 int UtcDaliImageViewSetImageNativeImage(void)
842 {
843   ToolkitTestApplication application;
844
845   ImageView imageView = ImageView::New();
846   Stage::GetCurrent().Add( imageView );
847
848   TestGlAbstraction& gl = application.GetGlAbstraction();
849   gl.EnableTextureCallTrace( true );
850
851   std::vector< GLuint > ids;
852   ids.push_back( 23 );
853   application.GetGlAbstraction().SetNextTextureIds( ids );
854
855   int width = 200;
856   int height = 500;
857   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
858   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
859
860   imageView.SetImage( nativeImage );
861   application.SendNotification();
862   application.Render();
863
864   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
865
866   std::stringstream params;
867   params << GL_TEXTURE_2D << ", " << 23;
868   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
869
870   END_TEST;
871 }
872
873 // Scenarios 3: ImageView initially from regular image but then SetImage called with Native image
874 int UtcDaliImageViewSetImageBufferImageToNativeImage(void)
875 {
876   ToolkitTestApplication application;
877
878   int width = 300;
879   int height = 400;
880   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
881
882   ImageView imageView = ImageView::New( image );
883   Stage::GetCurrent().Add( imageView );
884
885   TestGlAbstraction& gl = application.GetGlAbstraction();
886   gl.EnableTextureCallTrace( true );
887
888   std::vector< GLuint > ids;
889   ids.push_back( 23 );
890   application.GetGlAbstraction().SetNextTextureIds( ids );
891
892   application.SendNotification();
893   application.Render();
894
895   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
896
897   std::stringstream params;
898   params << GL_TEXTURE_2D << ", " << 23;
899   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
900
901   width = 200;
902   height = 500;
903   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
904   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
905   imageView.SetImage( nativeImage );
906
907   ids.clear();
908   ids.push_back( 24 );
909   application.GetGlAbstraction().SetNextTextureIds( ids );
910
911   application.SendNotification();
912   application.Render();
913
914   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
915
916   std::stringstream nextTextureParams;
917   nextTextureParams << GL_TEXTURE_2D << ", " << 24;
918   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
919
920   END_TEST;
921 }
922
923 // Scenarios 4: ImageView initially from Native image but then SetImage called with regular image
924 int UtcDaliImageViewSetImageNativeImageToBufferImage(void)
925 {
926   ToolkitTestApplication application;
927
928   int width = 300;
929   int height = 400;
930   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
931   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
932
933   ImageView imageView = ImageView::New( nativeImage );
934   Stage::GetCurrent().Add( imageView );
935
936   TestGlAbstraction& gl = application.GetGlAbstraction();
937   gl.EnableTextureCallTrace( true );
938
939   std::vector< GLuint > ids;
940   ids.push_back( 23 );
941   application.GetGlAbstraction().SetNextTextureIds( ids );
942
943   application.SendNotification();
944   application.Render();
945
946   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
947
948   std::stringstream params;
949   params << GL_TEXTURE_2D << ", " << 23;
950   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
951
952   width = 200;
953   height = 500;
954   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
955   imageView.SetImage( image );
956
957   ids.clear();
958   ids.push_back( 24 );
959   application.GetGlAbstraction().SetNextTextureIds( ids );
960
961   application.SendNotification();
962   application.Render();
963
964   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
965
966   std::stringstream nextTextureParams;
967   nextTextureParams << GL_TEXTURE_2D << ", " << 24;
968   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nextTextureParams.str()) );
969
970   END_TEST;
971 }
972
973 // Scenarios 5: ImageView from Native image with custom shader
974 int UtcDaliImageViewSetImageNativeImageWithCustomShader(void)
975 {
976   ToolkitTestApplication application;
977
978   int width = 300;
979   int height = 400;
980
981   Property::Map customShader;
982   customShader.Insert( "vertexShader", VERTEX_SHADER );
983   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
984
985   Property::Array shaderHints;
986   shaderHints.PushBack( "requiresSelfDepthTest" );
987   shaderHints.PushBack( "outputIsTransparent" );
988   shaderHints.PushBack( "outputIsOpaque" );
989   shaderHints.PushBack( "modifiesGeometry" );
990
991   customShader.Insert( "hints", shaderHints );
992
993   Property::Map map;
994   map.Insert( "shader", customShader );
995
996   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
997   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
998
999   ImageView imageView = ImageView::New( nativeImage );
1000   imageView.SetProperty( ImageView::Property::IMAGE, map );
1001   Stage::GetCurrent().Add( imageView );
1002
1003   TestGlAbstraction& gl = application.GetGlAbstraction();
1004   gl.EnableTextureCallTrace( true );
1005
1006   std::vector< GLuint > ids;
1007   ids.push_back( 23 );
1008   application.GetGlAbstraction().SetNextTextureIds( ids );
1009
1010   application.SendNotification();
1011   application.Render();
1012
1013   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1014
1015   std::stringstream params;
1016   params << GL_TEXTURE_2D << ", " << 23;
1017   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1018
1019   END_TEST;
1020 }
1021
1022 // Scenarios 6: ImageView initially from regular image with custom shader but then SetImage called with Native
1023 int UtcDaliImageViewSetImageBufferImageWithCustomShaderToNativeImage(void)
1024 {
1025   ToolkitTestApplication application;
1026
1027   int width = 300;
1028   int height = 400;
1029
1030   Property::Map customShader;
1031   customShader.Insert( "vertexShader", VERTEX_SHADER );
1032   customShader.Insert( "fragmentShader", FRAGMENT_SHADER );
1033
1034   Property::Array shaderHints;
1035   shaderHints.PushBack( "requiresSelfDepthTest" );
1036   shaderHints.PushBack( "outputIsTransparent" );
1037   shaderHints.PushBack( "outputIsOpaque" );
1038   shaderHints.PushBack( "modifiesGeometry" );
1039
1040   customShader.Insert( "hints", shaderHints );
1041
1042   Property::Map map;
1043   map.Insert( "shader", customShader );
1044
1045   BufferImage image = CreateBufferImage( width, height, Color::WHITE );
1046
1047   ImageView imageView = ImageView::New( image );
1048   imageView.SetProperty( ImageView::Property::IMAGE, map );
1049   Stage::GetCurrent().Add( imageView );
1050
1051   TestGlAbstraction& gl = application.GetGlAbstraction();
1052   gl.EnableTextureCallTrace( true );
1053
1054   std::vector< GLuint > ids;
1055   ids.push_back( 23 );
1056   application.GetGlAbstraction().SetNextTextureIds( ids );
1057
1058   application.SendNotification();
1059   application.Render();
1060
1061   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1062
1063   std::stringstream params;
1064   params << GL_TEXTURE_2D << ", " << 23;
1065   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", params.str()) );
1066
1067   TestNativeImagePointer nativeImageInterface = TestNativeImage::New( width, height );
1068   NativeImage nativeImage = NativeImage::New( *(nativeImageInterface.Get()) );
1069   imageView.SetImage( nativeImage );
1070
1071   ids.clear();
1072   ids.push_back( 24 );
1073   application.GetGlAbstraction().SetNextTextureIds( ids );
1074
1075   application.SendNotification();
1076   application.Render();
1077
1078   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethod("BindTexture") );
1079
1080   std::stringstream nativeImageParams;
1081   nativeImageParams << GL_TEXTURE_2D << ", " << 24;
1082   DALI_TEST_CHECK( gl.GetTextureTrace().FindMethodAndParams("BindTexture", nativeImageParams.str()) );
1083
1084
1085   END_TEST;
1086 }
1087
1088 int UtcDaliImageViewGetImageP1(void)
1089 {
1090   ToolkitTestApplication application;
1091
1092   ImageView imageView = ImageView::New();
1093   DALI_TEST_CHECK( ! imageView.GetImage() );
1094
1095   Image image = CreateBufferImage();
1096   imageView.SetImage( image );
1097   DALI_TEST_CHECK( imageView.GetImage() == image );
1098
1099   END_TEST;
1100 }
1101
1102 int UtcDaliImageViewGetImageP2(void)
1103 {
1104   ToolkitTestApplication application;
1105
1106   BufferImage image = CreateBufferImage();
1107   ImageView imageView = ImageView::New( image );
1108   DALI_TEST_CHECK( imageView.GetImage() == image );
1109
1110   END_TEST;
1111 }
1112
1113 int UtcDaliImageViewGetImageN(void)
1114 {
1115   ToolkitTestApplication application;
1116
1117   ImageView imageView = ImageView::New( TEST_IMAGE_FILE_NAME );
1118   DALI_TEST_CHECK( ! imageView.GetImage() );
1119
1120   Image image = CreateBufferImage();
1121   imageView.SetImage( image );
1122   DALI_TEST_CHECK( imageView.GetImage() == image );
1123
1124   imageView.SetImage( TEST_IMAGE_FILE_NAME );
1125   DALI_TEST_CHECK( ! imageView.GetImage() );
1126
1127   END_TEST;
1128 }